class History { constructor(maxStates = 50) { this.states = []; this.currentIndex = -1; this.maxStates = maxStates; } push(state) { // Remove any states after current index this.states = this.states.slice(0, this.currentIndex + 1); // Add new state this.states.push(state); // Remove oldest state if exceeding maxStates if (this.states.length > this.maxStates) { this.states.shift(); } this.currentIndex = this.states.length - 1; } undo() { if (this.currentIndex > 0) { this.currentIndex--; return this.states[this.currentIndex]; } return null; } redo() { if (this.currentIndex < this.states.length - 1) { this.currentIndex++; return this.states[this.currentIndex]; } return null; } canUndo() { return this.currentIndex > 0; } canRedo() { return this.currentIndex < this.states.length - 1; } }