undo.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * undo redo
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. /**
  7. * 撤销上一次执行的命令
  8. * @command undo
  9. * @method execCommand
  10. * @param { String } cmd 命令字符串
  11. * @example
  12. * ```javascript
  13. * editor.execCommand( 'undo' );
  14. * ```
  15. */
  16. /**
  17. * 重做上一次执行的命令
  18. * @command redo
  19. * @method execCommand
  20. * @param { String } cmd 命令字符串
  21. * @example
  22. * ```javascript
  23. * editor.execCommand( 'redo' );
  24. * ```
  25. */
  26. UE.plugins['undo'] = function () {
  27. var saveSceneTimer;
  28. var me = this,
  29. maxUndoCount = me.options.maxUndoCount || 20,
  30. maxInputCount = me.options.maxInputCount || 20,
  31. fillchar = new RegExp(domUtils.fillChar + '|<\/hr>', 'gi');// ie会产生多余的</hr>
  32. var noNeedFillCharTags = {
  33. ol:1,ul:1,table:1,tbody:1,tr:1,body:1
  34. };
  35. var orgState = me.options.autoClearEmptyNode;
  36. function compareAddr(indexA, indexB) {
  37. if (indexA.length != indexB.length)
  38. return 0;
  39. for (var i = 0, l = indexA.length; i < l; i++) {
  40. if (indexA[i] != indexB[i])
  41. return 0
  42. }
  43. return 1;
  44. }
  45. function compareRangeAddress(rngAddrA, rngAddrB) {
  46. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  47. return 0;
  48. }
  49. if (!compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) || !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)) {
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. function UndoManager() {
  55. this.list = [];
  56. this.index = 0;
  57. this.hasUndo = false;
  58. this.hasRedo = false;
  59. this.undo = function () {
  60. if (this.hasUndo) {
  61. if (!this.list[this.index - 1] && this.list.length == 1) {
  62. this.reset();
  63. return;
  64. }
  65. while (this.list[this.index].content == this.list[this.index - 1].content) {
  66. this.index--;
  67. if (this.index == 0) {
  68. return this.restore(0);
  69. }
  70. }
  71. this.restore(--this.index);
  72. }
  73. };
  74. this.redo = function () {
  75. if (this.hasRedo) {
  76. while (this.list[this.index].content == this.list[this.index + 1].content) {
  77. this.index++;
  78. if (this.index == this.list.length - 1) {
  79. return this.restore(this.index);
  80. }
  81. }
  82. this.restore(++this.index);
  83. }
  84. };
  85. this.restore = function () {
  86. var me = this.editor;
  87. var scene = this.list[this.index];
  88. var root = UE.htmlparser(scene.content.replace(fillchar, ''));
  89. me.options.autoClearEmptyNode = false;
  90. me.filterInputRule(root);
  91. me.options.autoClearEmptyNode = orgState;
  92. //trace:873
  93. //去掉展位符
  94. me.document.body.innerHTML = root.toHtml();
  95. me.fireEvent('afterscencerestore');
  96. //处理undo后空格不展位的问题
  97. if (browser.ie) {
  98. utils.each(domUtils.getElementsByTagName(me.document,'td th caption p'),function(node){
  99. if(domUtils.isEmptyNode(node)){
  100. domUtils.fillNode(me.document, node);
  101. }
  102. })
  103. }
  104. try{
  105. var rng = new dom.Range(me.document).moveToAddress(scene.address);
  106. rng.select(noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]);
  107. }catch(e){}
  108. this.update();
  109. this.clearKey();
  110. //不能把自己reset了
  111. me.fireEvent('reset', true);
  112. };
  113. this.getScene = function () {
  114. var me = this.editor;
  115. var rng = me.selection.getRange(),
  116. rngAddress = rng.createAddress(false,true);
  117. me.fireEvent('beforegetscene');
  118. var root = UE.htmlparser(me.body.innerHTML);
  119. me.options.autoClearEmptyNode = false;
  120. me.filterOutputRule(root);
  121. me.options.autoClearEmptyNode = orgState;
  122. var cont = root.toHtml();
  123. //trace:3461
  124. //这个会引起回退时导致空格丢失的情况
  125. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  126. me.fireEvent('aftergetscene');
  127. return {
  128. address:rngAddress,
  129. content:cont
  130. }
  131. };
  132. this.save = function (notCompareRange,notSetCursor) {
  133. clearTimeout(saveSceneTimer);
  134. var currentScene = this.getScene(notSetCursor),
  135. lastScene = this.list[this.index];
  136. if(lastScene && lastScene.content != currentScene.content){
  137. me.trigger('contentchange')
  138. }
  139. //内容相同位置相同不存
  140. if (lastScene && lastScene.content == currentScene.content &&
  141. ( notCompareRange ? 1 : compareRangeAddress(lastScene.address, currentScene.address) )
  142. ) {
  143. return;
  144. }
  145. this.list = this.list.slice(0, this.index + 1);
  146. this.list.push(currentScene);
  147. //如果大于最大数量了,就把最前的剔除
  148. if (this.list.length > maxUndoCount) {
  149. this.list.shift();
  150. }
  151. this.index = this.list.length - 1;
  152. this.clearKey();
  153. //跟新undo/redo状态
  154. this.update();
  155. };
  156. this.update = function () {
  157. this.hasRedo = !!this.list[this.index + 1];
  158. this.hasUndo = !!this.list[this.index - 1];
  159. };
  160. this.reset = function () {
  161. this.list = [];
  162. this.index = 0;
  163. this.hasUndo = false;
  164. this.hasRedo = false;
  165. this.clearKey();
  166. };
  167. this.clearKey = function () {
  168. keycont = 0;
  169. lastKeyCode = null;
  170. };
  171. }
  172. me.undoManger = new UndoManager();
  173. me.undoManger.editor = me;
  174. function saveScene() {
  175. this.undoManger.save();
  176. }
  177. me.addListener('saveScene', function () {
  178. var args = Array.prototype.splice.call(arguments,1);
  179. this.undoManger.save.apply(this.undoManger,args);
  180. });
  181. // me.addListener('beforeexeccommand', saveScene);
  182. // me.addListener('afterexeccommand', saveScene);
  183. me.addListener('reset', function (type, exclude) {
  184. if (!exclude) {
  185. this.undoManger.reset();
  186. }
  187. });
  188. me.commands['redo'] = me.commands['undo'] = {
  189. execCommand:function (cmdName) {
  190. this.undoManger[cmdName]();
  191. },
  192. queryCommandState:function (cmdName) {
  193. return this.undoManger['has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')] ? 0 : -1;
  194. },
  195. notNeedUndo:1
  196. };
  197. var keys = {
  198. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  199. /*Shift*/ 16:1, /*Ctrl*/ 17:1, /*Alt*/ 18:1,
  200. 37:1, 38:1, 39:1, 40:1
  201. },
  202. keycont = 0,
  203. lastKeyCode;
  204. //输入法状态下不计算字符数
  205. var inputType = false;
  206. me.addListener('ready', function () {
  207. domUtils.on(this.body, 'compositionstart', function () {
  208. inputType = true;
  209. });
  210. domUtils.on(this.body, 'compositionend', function () {
  211. inputType = false;
  212. })
  213. });
  214. //快捷键
  215. me.addshortcutkey({
  216. "Undo":"ctrl+90", //undo
  217. "Redo":"ctrl+89" //redo
  218. });
  219. var isCollapsed = true;
  220. me.addListener('keydown', function (type, evt) {
  221. var me = this;
  222. var keyCode = evt.keyCode || evt.which;
  223. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  224. if (inputType)
  225. return;
  226. if(!me.selection.getRange().collapsed){
  227. me.undoManger.save(false,true);
  228. isCollapsed = false;
  229. return;
  230. }
  231. if (me.undoManger.list.length == 0) {
  232. me.undoManger.save(true);
  233. }
  234. clearTimeout(saveSceneTimer);
  235. function save(cont){
  236. cont.undoManger.save(false,true);
  237. cont.fireEvent('selectionchange');
  238. }
  239. saveSceneTimer = setTimeout(function(){
  240. if(inputType){
  241. var interalTimer = setInterval(function(){
  242. if(!inputType){
  243. save(me);
  244. clearInterval(interalTimer)
  245. }
  246. },300)
  247. return;
  248. }
  249. save(me);
  250. },200);
  251. lastKeyCode = keyCode;
  252. keycont++;
  253. if (keycont >= maxInputCount ) {
  254. save(me)
  255. }
  256. }
  257. });
  258. me.addListener('keyup', function (type, evt) {
  259. var keyCode = evt.keyCode || evt.which;
  260. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  261. if (inputType)
  262. return;
  263. if(!isCollapsed){
  264. this.undoManger.save(false,true);
  265. isCollapsed = true;
  266. }
  267. }
  268. });
  269. //扩展实例,添加关闭和开启命令undo
  270. me.stopCmdUndo = function(){
  271. me.__hasEnterExecCommand = true;
  272. };
  273. me.startCmdUndo = function(){
  274. me.__hasEnterExecCommand = false;
  275. }
  276. };