undo.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. ///import core
  2. ///commands 撤销和重做
  3. ///commandsName Undo,Redo
  4. ///commandsTitle 撤销,重做
  5. /**
  6. * @description 回退
  7. * @author zhanyi
  8. */
  9. UE.plugins['undo'] = function () {
  10. var me = this,
  11. maxUndoCount = me.options.maxUndoCount || 20,
  12. maxInputCount = me.options.maxInputCount || 20,
  13. fillchar = new RegExp(domUtils.fillChar + '|<\/hr>', 'gi');// ie会产生多余的</hr>
  14. function compareAddr(indexA, indexB) {
  15. if (indexA.length != indexB.length)
  16. return 0;
  17. for (var i = 0, l = indexA.length; i < l; i++) {
  18. if (indexA[i] != indexB[i])
  19. return 0
  20. }
  21. return 1;
  22. }
  23. function compareRangeAddress(rngAddrA, rngAddrB) {
  24. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  25. return 0;
  26. }
  27. if (!compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) || !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)) {
  28. return 0;
  29. }
  30. return 1;
  31. }
  32. function adjustContent(cont) {
  33. var specialAttr = /\b(?:href|src|name)="[^"]*?"/gi;
  34. return cont.replace(specialAttr, '')
  35. .replace(/([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi, function (a, b, c) {
  36. return b.toLowerCase() + '=' + c.replace(/['"]/g, '').toLowerCase()
  37. })
  38. .replace(/(<[\w\-]+)|([\w\-]+>)/gi, function (a, b, c) {
  39. return (b || c).toLowerCase()
  40. });
  41. }
  42. function UndoManager() {
  43. this.list = [];
  44. this.index = 0;
  45. this.hasUndo = false;
  46. this.hasRedo = false;
  47. this.undo = function () {
  48. if (this.hasUndo) {
  49. var currentScene = this.getScene(),
  50. lastScene = this.list[this.index],
  51. lastContent = adjustContent(lastScene.content),
  52. currentContent = adjustContent(currentScene.content);
  53. if (lastContent != currentContent) {
  54. this.save();
  55. }
  56. if (!this.list[this.index - 1] && this.list.length == 1) {
  57. this.reset();
  58. return;
  59. }
  60. while (this.list[this.index].content == this.list[this.index - 1].content) {
  61. this.index--;
  62. if (this.index == 0) {
  63. return this.restore(0);
  64. }
  65. }
  66. this.restore(--this.index);
  67. }
  68. };
  69. this.redo = function () {
  70. if (this.hasRedo) {
  71. while (this.list[this.index].content == this.list[this.index + 1].content) {
  72. this.index++;
  73. if (this.index == this.list.length - 1) {
  74. return this.restore(this.index);
  75. }
  76. }
  77. this.restore(++this.index);
  78. }
  79. };
  80. this.restore = function () {
  81. var scene = this.list[this.index];
  82. //trace:873
  83. //去掉展位符
  84. me.document.body.innerHTML = scene.content.replace(fillchar, '');
  85. //处理undo后空格不展位的问题
  86. if (browser.ie) {
  87. utils.each(domUtils.getElementsByTagName(me.document,'td th caption p'),function(node){
  88. if(domUtils.isEmptyNode(node)){
  89. domUtils.fillNode(me.document, node);
  90. }
  91. })
  92. }
  93. new dom.Range(me.document).moveToAddress(scene.address).select();
  94. this.update();
  95. this.clearKey();
  96. //不能把自己reset了
  97. me.fireEvent('reset', true);
  98. };
  99. this.getScene = function (notSetCursor) {
  100. var rng = me.selection.getRange(),
  101. restoreAddress = rng.createAddress(),
  102. rngAddress = rng.createAddress(false,true);
  103. me.fireEvent('beforegetscene');
  104. var cont = me.body.innerHTML.replace(fillchar, '');
  105. browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  106. me.fireEvent('aftergetscene');
  107. try{
  108. !notSetCursor && rng.moveToAddress(restoreAddress).select(true);
  109. }catch(e){}
  110. return {
  111. address:rngAddress,
  112. content:cont
  113. }
  114. };
  115. this.save = function (notCompareRange,notSetCursor) {
  116. var currentScene = this.getScene(notSetCursor),
  117. lastScene = this.list[this.index];
  118. //内容相同位置相同不存
  119. if (lastScene && lastScene.content == currentScene.content &&
  120. ( notCompareRange ? 1 : compareRangeAddress(lastScene.address, currentScene.address) )
  121. ) {
  122. return;
  123. }
  124. this.list = this.list.slice(0, this.index + 1);
  125. this.list.push(currentScene);
  126. //如果大于最大数量了,就把最前的剔除
  127. if (this.list.length > maxUndoCount) {
  128. this.list.shift();
  129. }
  130. this.index = this.list.length - 1;
  131. this.clearKey();
  132. //跟新undo/redo状态
  133. this.update();
  134. };
  135. this.update = function () {
  136. this.hasRedo = !!this.list[this.index + 1];
  137. this.hasUndo = !!this.list[this.index - 1];
  138. };
  139. this.reset = function () {
  140. this.list = [];
  141. this.index = 0;
  142. this.hasUndo = false;
  143. this.hasRedo = false;
  144. this.clearKey();
  145. };
  146. this.clearKey = function () {
  147. keycont = 0;
  148. lastKeyCode = null;
  149. };
  150. }
  151. me.undoManger = new UndoManager();
  152. function saveScene() {
  153. this.undoManger.save();
  154. }
  155. me.addListener('saveScene', function () {
  156. me.undoManger.save();
  157. });
  158. me.addListener('beforeexeccommand', saveScene);
  159. me.addListener('afterexeccommand', saveScene);
  160. me.addListener('reset', function (type, exclude) {
  161. if (!exclude) {
  162. me.undoManger.reset();
  163. }
  164. });
  165. me.commands['redo'] = me.commands['undo'] = {
  166. execCommand:function (cmdName) {
  167. me.undoManger[cmdName]();
  168. },
  169. queryCommandState:function (cmdName) {
  170. return me.undoManger['has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')] ? 0 : -1;
  171. },
  172. notNeedUndo:1
  173. };
  174. var keys = {
  175. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  176. /*Shift*/ 16:1, /*Ctrl*/ 17:1, /*Alt*/ 18:1,
  177. 37:1, 38:1, 39:1, 40:1,
  178. 13:1 /*enter*/
  179. },
  180. keycont = 0,
  181. lastKeyCode;
  182. //输入法状态下不计算字符数
  183. var inputType = false;
  184. me.addListener('ready', function () {
  185. domUtils.on(me.body, 'compositionstart', function () {
  186. inputType = true;
  187. });
  188. domUtils.on(me.body, 'compositionend', function () {
  189. inputType = false;
  190. })
  191. });
  192. //快捷键
  193. me.addshortcutkey({
  194. "Undo":"ctrl+90", //undo
  195. "Redo":"ctrl+89" //redo
  196. });
  197. me.addListener('keydown', function (type, evt) {
  198. var keyCode = evt.keyCode || evt.which;
  199. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  200. if (inputType)
  201. return;
  202. if (me.undoManger.list.length == 0 || ((keyCode == 8 || keyCode == 46) && lastKeyCode != keyCode)) {
  203. me.fireEvent('contentchange');
  204. me.undoManger.save(true,true);
  205. lastKeyCode = keyCode;
  206. return;
  207. }
  208. //trace:856
  209. //修正第一次输入后,回退,再输入要到keycont>maxInputCount才能在回退的问题
  210. if (me.undoManger.list.length == 2 && me.undoManger.index == 0 && keycont == 0) {
  211. me.undoManger.list.splice(1, 1);
  212. me.undoManger.update();
  213. }
  214. lastKeyCode = keyCode;
  215. keycont++;
  216. if (keycont >= maxInputCount || me.undoManger.mousedown) {
  217. if (me.selection.getRange().collapsed)
  218. me.fireEvent('contentchange');
  219. me.undoManger.save(false,true);
  220. me.undoManger.mousedown = false;
  221. }
  222. }
  223. });
  224. me.addListener('mousedown',function(){
  225. me.undoManger.mousedown = true;
  226. })
  227. };