From 64c562ed20840145de3093d7ec44a5e08a8fec3e Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Sun, 12 Apr 2026 04:45:43 +0100 Subject: [PATCH] feat: implement basic 2-stack undo-redo this is quite basic, uses a snapshot style history instead of a command-style storage, where we could store forward/reverse callbacks. as a result, this can presently only be used for storing and loading full snapshots. you cannot undo favourites/ratings with this model. to improve this, i would probably update this down the line to make each item of the undostack store a: {forwardOp, reverseOp} and the queue snapshotting logic can be augmented to simply be: {() => {return newState;}, () => {return oldState;}} Signed-off-by: oppiliappan --- src/js/history.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/js/history.js 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(); -- 2.51.2