| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,148 @@ |
| 1 |
+/** |
|
| 2 |
+ * $Id$ |
|
| 3 |
+ * |
|
| 4 |
+ * @author Moxiecode |
|
| 5 |
+ * @copyright Copyright � 2004-2007, Moxiecode Systems AB, All rights reserved. |
|
| 6 |
+ */ |
|
| 7 |
+ |
|
| 8 |
+/** |
|
| 9 |
+ * Constructs a undo redo instance, this instance handles the custom undo/redo handeling in TinyMCE. |
|
| 10 |
+ * |
|
| 11 |
+ * @param {TinyMCE_Control} inst TinyMCE editor control instance.
|
|
| 12 |
+ * @constructor |
|
| 13 |
+ * @member TinyMCE_UndoRedo |
|
| 14 |
+ */ |
|
| 15 |
+function TinyMCE_UndoRedo(inst) {
|
|
| 16 |
+ this.instance = inst; |
|
| 17 |
+ this.undoLevels = []; |
|
| 18 |
+ this.undoIndex = 0; |
|
| 19 |
+ this.typingUndoIndex = -1; |
|
| 20 |
+ this.undoRedo = true; |
|
| 21 |
+}; |
|
| 22 |
+ |
|
| 23 |
+/**#@+ |
|
| 24 |
+ * @member TinyMCE_UndoRedo |
|
| 25 |
+ */ |
|
| 26 |
+TinyMCE_UndoRedo.prototype = {
|
|
| 27 |
+ /**#@+ |
|
| 28 |
+ * @method |
|
| 29 |
+ */ |
|
| 30 |
+ |
|
| 31 |
+ /** |
|
| 32 |
+ * Adds a new undo level, this will take a snapshot of the current instance HTML or use the specified level. |
|
| 33 |
+ * |
|
| 34 |
+ * @param {TinyMCE_UndoRedoLevel} l Optional undo/redo level to add.
|
|
| 35 |
+ * @return true/false on success or failure. |
|
| 36 |
+ * @type boolean |
|
| 37 |
+ */ |
|
| 38 |
+ add : function(l) {
|
|
| 39 |
+ var b, customUndoLevels, newHTML, inst = this.instance, i, ul, ur; |
|
| 40 |
+ |
|
| 41 |
+ if (l) {
|
|
| 42 |
+ this.undoLevels[this.undoLevels.length] = l; |
|
| 43 |
+ return true; |
|
| 44 |
+ } |
|
| 45 |
+ |
|
| 46 |
+ if (this.typingUndoIndex != -1) {
|
|
| 47 |
+ this.undoIndex = this.typingUndoIndex; |
|
| 48 |
+ |
|
| 49 |
+ if (tinyMCE.typingUndoIndex != -1) |
|
| 50 |
+ tinyMCE.undoIndex = tinyMCE.typingUndoIndex; |
|
| 51 |
+ } |
|
| 52 |
+ |
|
| 53 |
+ newHTML = tinyMCE.trim(inst.getBody().innerHTML); |
|
| 54 |
+ if (this.undoLevels[this.undoIndex] && newHTML != this.undoLevels[this.undoIndex].content) {
|
|
| 55 |
+ //tinyMCE.debug(newHTML, this.undoLevels[this.undoIndex].content); |
|
| 56 |
+ |
|
| 57 |
+ // Is dirty again |
|
| 58 |
+ inst.isNotDirty = false; |
|
| 59 |
+ |
|
| 60 |
+ tinyMCE.dispatchCallback(inst, 'onchange_callback', 'onChange', inst); |
|
| 61 |
+ |
|
| 62 |
+ // Time to compress |
|
| 63 |
+ customUndoLevels = tinyMCE.settings.custom_undo_redo_levels; |
|
| 64 |
+ if (customUndoLevels != -1 && this.undoLevels.length > customUndoLevels) {
|
|
| 65 |
+ for (i=0; i<this.undoLevels.length-1; i++) |
|
| 66 |
+ this.undoLevels[i] = this.undoLevels[i+1]; |
|
| 67 |
+ |
|
| 68 |
+ this.undoLevels.length--; |
|
| 69 |
+ this.undoIndex--; |
|
| 70 |
+ |
|
| 71 |
+ // Todo: Implement global undo/redo logic here |
|
| 72 |
+ } |
|
| 73 |
+ |
|
| 74 |
+ b = inst.undoBookmark; |
|
| 75 |
+ |
|
| 76 |
+ if (!b) |
|
| 77 |
+ b = inst.selection.getBookmark(); |
|
| 78 |
+ |
|
| 79 |
+ this.undoIndex++; |
|
| 80 |
+ this.undoLevels[this.undoIndex] = {
|
|
| 81 |
+ content : newHTML, |
|
| 82 |
+ bookmark : b |
|
| 83 |
+ }; |
|
| 84 |
+ |
|
| 85 |
+ // Remove all above from global undo/redo |
|
| 86 |
+ ul = tinyMCE.undoLevels; |
|
| 87 |
+ for (i=tinyMCE.undoIndex + 1; i<ul.length; i++) {
|
|
| 88 |
+ ur = ul[i].undoRedo; |
|
| 89 |
+ |
|
| 90 |
+ if (ur.undoIndex == ur.undoLevels.length -1) |
|
| 91 |
+ ur.undoIndex--; |
|
| 92 |
+ |
|
| 93 |
+ ur.undoLevels.length--; |
|
| 94 |
+ } |
|
| 95 |
+ |
|
| 96 |
+ // Add global undo level |
|
| 97 |
+ tinyMCE.undoLevels[tinyMCE.undoIndex++] = inst; |
|
| 98 |
+ tinyMCE.undoLevels.length = tinyMCE.undoIndex; |
|
| 99 |
+ |
|
| 100 |
+ this.undoLevels.length = this.undoIndex + 1; |
|
| 101 |
+ |
|
| 102 |
+ return true; |
|
| 103 |
+ } |
|
| 104 |
+ |
|
| 105 |
+ return false; |
|
| 106 |
+ }, |
|
| 107 |
+ |
|
| 108 |
+ /** |
|
| 109 |
+ * Performes a undo action, this will restore the HTML contents of the editor to a former state. |
|
| 110 |
+ */ |
|
| 111 |
+ undo : function() {
|
|
| 112 |
+ var inst = this.instance; |
|
| 113 |
+ |
|
| 114 |
+ // Do undo |
|
| 115 |
+ if (this.undoIndex > 0) {
|
|
| 116 |
+ this.undoIndex--; |
|
| 117 |
+ |
|
| 118 |
+ tinyMCE.setInnerHTML(inst.getBody(), this.undoLevels[this.undoIndex].content); |
|
| 119 |
+ inst.repaint(); |
|
| 120 |
+ |
|
| 121 |
+ if (inst.settings.custom_undo_redo_restore_selection) |
|
| 122 |
+ inst.selection.moveToBookmark(this.undoLevels[this.undoIndex].bookmark); |
|
| 123 |
+ } |
|
| 124 |
+ }, |
|
| 125 |
+ |
|
| 126 |
+ /** |
|
| 127 |
+ * Performes a undo action, this will restore the HTML contents of the editor to a former undoed state. |
|
| 128 |
+ */ |
|
| 129 |
+ redo : function() {
|
|
| 130 |
+ var inst = this.instance; |
|
| 131 |
+ |
|
| 132 |
+ tinyMCE.execCommand("mceEndTyping");
|
|
| 133 |
+ |
|
| 134 |
+ if (this.undoIndex < (this.undoLevels.length-1)) {
|
|
| 135 |
+ this.undoIndex++; |
|
| 136 |
+ |
|
| 137 |
+ tinyMCE.setInnerHTML(inst.getBody(), this.undoLevels[this.undoIndex].content); |
|
| 138 |
+ inst.repaint(); |
|
| 139 |
+ |
|
| 140 |
+ if (inst.settings.custom_undo_redo_restore_selection) |
|
| 141 |
+ inst.selection.moveToBookmark(this.undoLevels[this.undoIndex].bookmark); |
|
| 142 |
+ } |
|
| 143 |
+ |
|
| 144 |
+ tinyMCE.triggerNodeChange(); |
|
| 145 |
+ } |
|
| 146 |
+ |
|
| 147 |
+ /**#@-*/ |
|
| 148 |
+}; |