-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjs-undo-manager.min.js
10 lines (10 loc) · 4.88 KB
/
js-undo-manager.min.js
1
2
3
4
5
6
7
8
9
10
!function(t,e){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var i=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}();/*!
* JavaScript Undo Manager 1.0.0
* Simple JavaScript undo/redo command manager supporting transactions with no dependencies.
*
* Copyright: Alexey Grinko, 2017
* Git repository: https://github.com/agrinko/js-undo-manager.git
*
* @license MIT - https://opensource.org/licenses/MIT
*/
!function(t){var e={limit:100,debug:!1,bindHotKeys:!1,useTransactions:!0},s=function(){function t(i){n(this,t),i=o({},e,i),this.limit=i.limit,this.options=i,this.reset(),i.useTransactions&&(this.transaction=new r(this)),i.bindHotKeys&&this.bindHotKeys(),this.log("Initialized with stack limit of "+this.limit+" commands")}return i(t,[{key:"bindHotKeys",value:function(){var t=this;return this.log("Bound 'undo' and 'redo' actions to 'Ctrl+Z', 'Ctrl+Y' & 'Ctrl+Shift+Z' hot keys"),document.addEventListener("keydown",function(e){90===e.keyCode&&e.ctrlKey&&!e.shiftKey?t.undo():(90===e.keyCode&&e.ctrlKey&&e.shiftKey||89===e.keyCode&&e.ctrlKey)&&t.redo()}),this}},{key:"record",value:function(t){if(t&&"function"==typeof t.redo&&"function"==typeof t.undo)this._record(t);else{if("function"!=typeof arguments[0]||"function"!=typeof arguments[1])throw new TypeError("JSUndoManager.record(): unexpected arguments");this._record({redo:arguments[0],undo:arguments[1]})}return this}},{key:"execute",value:function(t){var e=t.redo||t;return this.record.apply(this,arguments),this.log("Executing function..."),e(),this}},{key:"_record",value:function(t){if(this.transaction.isInProgress())return this.transaction._record(t);this.log("Recording command",t),this._rebase(),this.stack.push(t),this.sp++,this._keepLimit()}},{key:"_rebase",value:function(){this.canRedo()&&(this.stack.length=this.sp+1)}},{key:"_keepLimit",value:function(){if(!(this.stack.length<=this.limit)){var t=this.stack.length-this.limit;this.log("Stack size reached its limit: ${this.limit} commands. Cutting off most old commands..."),1===t?this.stack.shift():this.stack.splice(0,t),this.sp-=t}}},{key:"undo",value:function(){if(!this.canUndo())return this;var t=this.stack[this.sp];return this.log("undo"),this.sp--,t.undo(),this}},{key:"canUndo",value:function(){return this.sp>=0}},{key:"redo",value:function(){if(!this.canRedo())return this;var t=this.stack[this.sp+1];return this.log("redo"),this.sp++,t.redo(),this}},{key:"canRedo",value:function(){return this.sp<this.stack.length-1}},{key:"setLimit",value:function(t){var e=this.stack.length-this.sp-1;if(t<1||"number"!=typeof t)throw new TypeError("JSUndoManager.setLimit(): unexpected argument limit="+t+". Should be a positive number");return t<e?console.warn("JSUndoManager.setLimit(): cannot set stack limit ("+t+") less than the number of 'redoable' commands ("+e+")"):(this.limit=Math.floor(t),this._keepLimit()),this}},{key:"reset",value:function(){return this.log("reset"),this.stack=[],this.sp=-1,this}},{key:"isEmpty",value:function(){return!this.stack.length}},{key:"isFull",value:function(){return this.stack.length===this.limit}},{key:"getSize",value:function(){return this.stack.length}},{key:"log",value:function(t){for(var e,n=arguments.length,i=Array(n>1?n-1:0),s=1;s<n;s++)i[s-1]=arguments[s];this.options.debug&&(e=console).log.apply(e,["Command Manager: "+t].concat(i))}}]),t}(),r=function(){function t(e){n(this,t),this.tracker=e,this._reset(),e.log("TransactionManager is initialized")}return i(t,null,[{key:"_execForward",value:function(t){for(var e=0;e<t.length;e++)t[e].redo()}},{key:"_execBack",value:function(t){for(var e=t.length-1;e>=0;e--)t[e].undo()}}]),i(t,[{key:"begin",value:function(){this.state=t.IN_PROGRESS,this.tracker.log("Begin transaction")}},{key:"end",value:function(){var e=this.sequence;this._reset(),e.length>0&&this.tracker.record({redo:t._execForward.bind(null,e),undo:t._execBack.bind(null,e)}),this.tracker.log("End transaction")}},{key:"cancel",value:function(){t._execBack(this.sequence),this._reset(),this.tracker.log("Cancel transaction")}},{key:"isInProgress",value:function(){return this.state===t.IN_PROGRESS}},{key:"isPending",value:function(){return this.state===t.PENDING}},{key:"_record",value:function(t){this.sequence.push(t),this.tracker.log("Recording command in transaction...",t)}},{key:"_reset",value:function(){this.state=t.PENDING,this.sequence=[]}}]),t}();r.PENDING=0,r.IN_PROGRESS=1;var o=Object.assign||function(t){for(var e=1;e<arguments.length;e++)for(var n in arguments[e])arguments[e].hasOwnProperty(n)&&(t[n]=arguments[e][n]);return t};"function"==typeof define&&define.amd?define(function(){return s}):"undefined"!=typeof module&&module.exports?module.exports=s:t.JSUndoManager=s}(window),e.true=t}({},function(){return this}());