KISSY.Editor.add("undo/support",function(){function h(a){var b=a._getRawData(),c;if(b)c=a.getSelection();this.contents=b;this.bookmarks=c&&c.createBookmarks2(true)}function i(a){this.history=[];this.index=-1;this.editor=a;this.bufferRunner=e.Utils.buffer(this.save,this,500);this._init()}var f=KISSY,e=f.Editor,j=e.Utils.arrayCompare,k=f.UA,l=f.Event;f.augment(h,{equals:function(a){if(this.contents!=a.contents)return false;var b=this.bookmarks;a=a.bookmarks;if(b||a){if(!b||!a||b.length!=a.length)return false;
for(var c=0;c<b.length;c++){var d=b[c],g=a[c];if(d.startOffset!=g.startOffset||d.endOffset!=g.endOffset||!j(d.start,g.start)||!j(d.end,g.end))return false}}return true}});var m={16:1,17:1,18:1},n={37:1,38:1,39:1,40:1,33:1,34:1};f.augment(i,{_keyMonitor:function(){var a=this.editor;l.on([a.document,a.textarea],"keydown",function(b){var c=b.keyCode;if(!(c in n||c in m))if(c===90&&(b.ctrlKey||b.metaKey)){a.fire("restore",{d:-1});b.halt()}else if(c===89&&(b.ctrlKey||b.metaKey)){a.fire("restore",{d:1});
b.halt()}else a.fire("save",{buffer:1})})},_init:function(){var a=this,b=a.editor;b.on("save",function(c){if(b.getMode()==e.WYSIWYG_MODE)c.buffer?a.bufferRunner():a.save()});b.on("restore",function(c){b.getMode()==e.WYSIWYG_MODE&&a.restore(c)});a._keyMonitor();setTimeout(function(){a.save()},0)},save:function(){var a=this.history,b=this.index;a.length>b+1&&a.splice(b+1,a.length-b-1);var c=this.editor;b=a[a.length-1];var d=new h(c);if(!b||!b.equals(d)){a.length===30&&a.shift();a.push(d);this.index=
b=a.length-1;c.fire("afterSave",{history:a,index:b})}},restore:function(a){a=a.d;var b=this.history,c=this.editor,d=b[this.index+a];if(d){c._setRawData(d.contents);if(d.bookmarks)c.getSelection().selectBookmarks(d.bookmarks);else if(k.ie){d=c.document.body.createTextRange();d.collapse(true);d.select()}(d=c.getSelection())&&d.scrollIntoView();this.index+=a;c.fire("afterRestore",{history:b,index:this.index});c.notifySelectionChange()}}});e.UndoManager=i},{attach:false});
