From a8fec45b10a4989a960d20e97761af742bc208da Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 5 Jul 2018 17:24:19 -0300 Subject: Stub undo manager, remove --- undo-manager.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 undo-manager.js (limited to 'undo-manager.js') diff --git a/undo-manager.js b/undo-manager.js new file mode 100644 index 0000000..4a042ad --- /dev/null +++ b/undo-manager.js @@ -0,0 +1,42 @@ +class UndoManager { + constructor() { + this.actionStack = [] + this.undoneStack = [] + } + + pushAction(action) { + this.undoneStack = [] + this.actionStack.push(action) + action.activate() + } + + undoLastAction() { + if (this.actionStack.length === 0) { + return + } + + const action = this.actionStack.pop() + this.undoneStack.push(action) + + action.undo() + } + + redoLastUndoneAction() { + if (this.undoneStack.length === 0) { + return + } + + const action = this.undoneStack.pop() + this.actionStack.push(action) + action.activate() + } + + get safeToPushAction() { + // Is it safe to push a new action? That is, since pushing a new action + // clears the undone actions stack, will any undone actions be lost? + + return this.undoStack.length === 0 + } +} + +module.exports = UndoManager -- cgit 1.3.0-6-gf8a5