diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-11-17 15:46:39 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-11-17 15:46:39 -0500 |
| commit | 5fa235a556d384117840b7088575012dcd1787dd (patch) | |
| tree | 2e819d13093cd446bdb32a2885c068da4ab94cf0 /assets/javascripts/util/undostack.js | |
| parent | 548ef92e8157f1ae0b594d0fd2c609438d748222 (diff) | |
polygonal lasso
Diffstat (limited to 'assets/javascripts/util/undostack.js')
| -rw-r--r-- | assets/javascripts/util/undostack.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/assets/javascripts/util/undostack.js b/assets/javascripts/util/undostack.js new file mode 100644 index 0000000..040a4eb --- /dev/null +++ b/assets/javascripts/util/undostack.js @@ -0,0 +1,54 @@ +(function(){ + + var UndoStack = function(){ + this.debug = true + this.stack = [] + this.types = {} + this.pointer = -1 + } + UndoStack.prototype.push = function(action){ + this.pointer++ + this.stack[this.pointer] = action + this.purge() + this.debug && console.log("push", action.type) + } + UndoStack.prototype.purge = function(){ + if (this.stack.length-1 == this.pointer) return + this.stack.length = this.pointer+1 + } + UndoStack.prototype.undo = function(){ + if (this.pointer == -1) return false + var action = this.stack[this.pointer] + this.debug && console.log("undo", action.type) + this.types[ action.type ].undo(action.undo) + this.pointer-- + return this.pointer > -1 + } + UndoStack.prototype.redo = function(){ + if (this.pointer == this.stack.length-1) return false + this.pointer++ + var action = this.stack[this.pointer] + this.debug && console.log("redo", action.type) + this.types[ action.type ].redo(action.redo) + return this.pointer < this.stack.length-1 + } + UndoStack.prototype.register = function(actions){ + if (actions.length) { + actions.forEach(this.registerOne.bind(this)) + } + else { + this.registerOne(actions) + } + } + UndoStack.prototype.registerOne = function(action){ + if (! action.redo) { action.redo = action.undo } + this.types[ action.type ] = action + } + if ('window' in this) { + window.UndoStack = new UndoStack + } + else { + module.exports = new UndoStack + } + +})() |
