|
| 1 | +/* |
| 2 | +Simple JavaScript undo and redo. |
| 3 | +https://github.com/ArthurClemens/JavaScript-Undo-Manager |
| 4 | +*/ |
| 5 | + |
| 6 | +(function () { |
| 7 | + 'use strict'; |
| 8 | + |
| 9 | + function removeFromTo(array, from, to) { |
| 10 | + array.splice( |
| 11 | + from, |
| 12 | + !to || |
| 13 | + 1 + |
| 14 | + to - |
| 15 | + from + |
| 16 | + (!((to < 0) ^ (from >= 0)) && (to < 0 || -1) * array.length), |
| 17 | + ); |
| 18 | + return array.length; |
| 19 | + } |
| 20 | + |
| 21 | + let UndoManager = function () { |
| 22 | + let commands = [], |
| 23 | + index = -1, |
| 24 | + limit = 0, |
| 25 | + isExecuting = false, |
| 26 | + callback, |
| 27 | + // functions |
| 28 | + execute; |
| 29 | + |
| 30 | + execute = function (command, action) { |
| 31 | + if (!command || typeof command[action] !== 'function') { |
| 32 | + return this; |
| 33 | + } |
| 34 | + isExecuting = true; |
| 35 | + |
| 36 | + command[action](); |
| 37 | + |
| 38 | + isExecuting = false; |
| 39 | + return this; |
| 40 | + }; |
| 41 | + |
| 42 | + return { |
| 43 | + /* |
| 44 | + Add a command to the queue. |
| 45 | + */ |
| 46 | + add: function (command) { |
| 47 | + if (isExecuting) { |
| 48 | + return this; |
| 49 | + } |
| 50 | + // if we are here after having called undo, |
| 51 | + // invalidate items higher on the stack |
| 52 | + commands.splice(index + 1, commands.length - index); |
| 53 | + |
| 54 | + commands.push(command); |
| 55 | + |
| 56 | + // if limit is set, remove items from the start |
| 57 | + if (limit && commands.length > limit) { |
| 58 | + removeFromTo(commands, 0, -(limit + 1)); |
| 59 | + } |
| 60 | + |
| 61 | + // set the current index to the end |
| 62 | + index = commands.length - 1; |
| 63 | + if (callback) { |
| 64 | + callback(); |
| 65 | + } |
| 66 | + return this; |
| 67 | + }, |
| 68 | + |
| 69 | + /* |
| 70 | + Pass a function to be called on undo and redo actions. |
| 71 | + */ |
| 72 | + setCallback: function (callbackFunc) { |
| 73 | + callback = callbackFunc; |
| 74 | + }, |
| 75 | + |
| 76 | + /* |
| 77 | + Perform undo: call the undo function at the current index and decrease the index by 1. |
| 78 | + */ |
| 79 | + undo: function () { |
| 80 | + let command = commands[index]; |
| 81 | + if (!command) { |
| 82 | + return this; |
| 83 | + } |
| 84 | + execute(command, 'undo'); |
| 85 | + index -= 1; |
| 86 | + if (callback) { |
| 87 | + callback(); |
| 88 | + } |
| 89 | + return this; |
| 90 | + }, |
| 91 | + |
| 92 | + /* |
| 93 | + Perform redo: call the redo function at the next index and increase the index by 1. |
| 94 | + */ |
| 95 | + redo: function () { |
| 96 | + let command = commands[index + 1]; |
| 97 | + if (!command) { |
| 98 | + return this; |
| 99 | + } |
| 100 | + execute(command, 'redo'); |
| 101 | + index += 1; |
| 102 | + if (callback) { |
| 103 | + callback(); |
| 104 | + } |
| 105 | + return this; |
| 106 | + }, |
| 107 | + |
| 108 | + /* |
| 109 | + Clear the memory, losing all stored states. Reset the index. |
| 110 | + */ |
| 111 | + clear: function () { |
| 112 | + let prev_size = commands.length; |
| 113 | + |
| 114 | + commands = []; |
| 115 | + index = -1; |
| 116 | + |
| 117 | + if (callback && prev_size > 0) { |
| 118 | + callback(); |
| 119 | + } |
| 120 | + }, |
| 121 | + |
| 122 | + hasUndo: function () { |
| 123 | + return index !== -1; |
| 124 | + }, |
| 125 | + |
| 126 | + hasRedo: function () { |
| 127 | + return index < commands.length - 1; |
| 128 | + }, |
| 129 | + |
| 130 | + getCommands: function () { |
| 131 | + return commands; |
| 132 | + }, |
| 133 | + |
| 134 | + getIndex: function () { |
| 135 | + return index; |
| 136 | + }, |
| 137 | + |
| 138 | + setLimit: function (l) { |
| 139 | + limit = l; |
| 140 | + }, |
| 141 | + }; |
| 142 | + }; |
| 143 | + |
| 144 | + if ( |
| 145 | + typeof define === 'function' && |
| 146 | + typeof define.amd === 'object' && |
| 147 | + define.amd |
| 148 | + ) { |
| 149 | + // AMD. Register as an anonymous module. |
| 150 | + define(function () { |
| 151 | + return UndoManager; |
| 152 | + }); |
| 153 | + } else if (typeof module !== 'undefined' && module.exports) { |
| 154 | + module.exports = UndoManager; |
| 155 | + } else { |
| 156 | + window.UndoManager = UndoManager; |
| 157 | + } |
| 158 | +})(); |
0 commit comments