diff --git a/src/js/history.js b/src/js/history.js new file mode 100644 index 0000000..0251535 --- /dev/null +++ b/src/js/history.js @@ -0,0 +1,35 @@ +class History { + constructor() { + this.undoStack = new BoundedStack(32); + this.redoStack = new BoundedStack(32); + } + + saveState(snapshot) { + this.undoStack.push(snapshot); + this.redoStack.clear(); + } + + undo(currentSnapshot) { + if (this.undoStack.isEmpty()) return null; + + // save current state to redo stack before undoing + if (currentSnapshot) { + this.redoStack.push(currentSnapshot); + } + + return this.undoStack.pop(); + } + + redo(currentSnapshot) { + if (this.redoStack.isEmpty()) return null; + + // save current state to undo stack before redoing + if (currentSnapshot) { + this.undoStack.push(currentSnapshot); + } + + return this.redoStack.pop(); + } +} + +const history = new History();