mirror of https://github.com/luileito/jsketch.git
1 line
1.9 KiB
JavaScript
1 line
1.9 KiB
JavaScript
!function(a){function b(a){function b(b,c){a.handler(function(a,d){d.sketch.clear(),d.sketch.context.drawImage(b,0,0),d.strokes=c.strokes.slice(),d.sketch.callStack=c.callStack.slice()})}function c(b){a.handler(function(a,c){b&&b.identifier>0?e[f].strokes=c.strokes.slice():(e.push({image:a.toDataURL(),strokes:c.strokes.slice(),callStack:c.sketch.callStack.slice()}),f++)})}function d(a){if(a.ctrlKey)switch(a.which){case 26:a.shiftKey?g.redo():g.undo();break;case 25:g.redo()}}var e=[],f=-1,g=this;this.undo=function(){return f>0&&(f--,this.restore(),a.trigger("mementoundo")),this},this.redo=function(){return f<e.length-1&&(f++,this.restore(),a.trigger("mementoredo")),this},this.reset=function(){return e=[],f=-1,c(),this},this.save=function(b){return c(b),a.trigger("mementosave"),this},this.state=function(){return JSON.parse(JSON.stringify(e[f]))},this.restore=function(c){c||(c=e[f]);var d=new Image;return d.src=c.image,d.onload=function(){b(this,c),a.trigger("mementochange")},this},this.init=function(){return Event.remove(document,"keypress",d),Event.add(document,"keypress",d),a.trigger("mementoinit"),c(),this},this.destroy=function(){return Event.remove(document,"keypress",d),a.trigger("mementodestroy"),this.reset()}}var c="sketchable";Sketchable.prototype.plugins.memento=function(a){for(var d={clear:function(a,b){b.memento.reset()},mouseup:function(a,b,c){b.memento.save(c)},destroy:function(a,b){b.memento.destroy()}},e="mouseup clear destroy".split(" "),f=0;f<e.length;f++){var g=e[f];a.decorate(g,d[g],"memento")}deepExtend(a,{memento:{undo:function(){var b=dataBind(a.elem)[c];return b.memento.undo(),a},redo:function(){var b=dataBind(a.elem)[c];return b.memento.redo(),a},save:function(){var b=dataBind(a.elem)[c];return b.memento.save(),a},state:function(){var b=dataBind(a.elem)[c];return b.memento.state()},restore:function(b){var d=dataBind(a.elem)[c];return d.memento.restore(b),a}}});var h=dataBind(a.elem)[c];h.memento=new b(a),h.memento.init()}}(this); |