forked from tiff/wysihtml5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundo_manager.js
254 lines (210 loc) · 7.79 KB
/
undo_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Undo Manager for wysihtml5
* slightly inspired by http://rniwa.com/editing/undomanager.html#the-undomanager-interface
*/
(function(wysihtml5) {
var Z_KEY = 90,
Y_KEY = 89,
BACKSPACE_KEY = 8,
DELETE_KEY = 46,
MAX_HISTORY_ENTRIES = 25,
DATA_ATTR_NODE = "data-wysihtml5-selection-node",
DATA_ATTR_OFFSET = "data-wysihtml5-selection-offset",
UNDO_HTML = '<span id="_wysihtml5-undo" class="_wysihtml5-temp">' + wysihtml5.INVISIBLE_SPACE + '</span>',
REDO_HTML = '<span id="_wysihtml5-redo" class="_wysihtml5-temp">' + wysihtml5.INVISIBLE_SPACE + '</span>',
dom = wysihtml5.dom;
function cleanTempElements(doc) {
var tempElement;
while (tempElement = doc.querySelector("._wysihtml5-temp")) {
tempElement.parentNode.removeChild(tempElement);
}
}
wysihtml5.UndoManager = wysihtml5.lang.Dispatcher.extend(
/** @scope wysihtml5.UndoManager.prototype */ {
constructor: function(editor) {
this.editor = editor;
this.composer = editor.composer;
this.element = this.composer.element;
this.position = 0;
this.historyStr = [];
this.historyDom = [];
this.transact();
this._observe();
},
_observe: function() {
var that = this,
doc = this.composer.sandbox.getDocument(),
lastKey;
// Catch CTRL+Z and CTRL+Y
dom.observe(this.element, "keydown", function(event) {
if (event.altKey || (!event.ctrlKey && !event.metaKey)) {
return;
}
var keyCode = event.keyCode,
isUndo = keyCode === Z_KEY && !event.shiftKey,
isRedo = (keyCode === Z_KEY && event.shiftKey) || (keyCode === Y_KEY);
if (isUndo) {
that.undo();
event.preventDefault();
} else if (isRedo) {
that.redo();
event.preventDefault();
}
});
// Catch delete and backspace
dom.observe(this.element, "keydown", function(event) {
var keyCode = event.keyCode;
if (keyCode === lastKey) {
return;
}
lastKey = keyCode;
if (keyCode === BACKSPACE_KEY || keyCode === DELETE_KEY) {
that.transact();
}
});
// Now this is very hacky:
// These days browsers don't offer a undo/redo event which we could hook into
// to be notified when the user hits undo/redo in the contextmenu.
// Therefore we simply insert two elements as soon as the contextmenu gets opened.
// The last element being inserted will be immediately be removed again by a exexCommand("undo")
// => When the second element appears in the dom tree then we know the user clicked "redo" in the context menu
// => When the first element disappears from the dom tree then we know the user clicked "undo" in the context menu
if (wysihtml5.browser.hasUndoInContextMenu()) {
var interval, observed, cleanUp = function() {
cleanTempElements(doc);
clearInterval(interval);
};
dom.observe(this.element, "contextmenu", function() {
cleanUp();
that.composer.selection.executeAndRestoreSimple(function() {
if (that.element.lastChild) {
that.composer.selection.setAfter(that.element.lastChild);
}
// enable undo button in context menu
doc.execCommand("insertHTML", false, UNDO_HTML);
// enable redo button in context menu
doc.execCommand("insertHTML", false, REDO_HTML);
doc.execCommand("undo", false, null);
});
interval = setInterval(function() {
if (doc.getElementById("_wysihtml5-redo")) {
cleanUp();
that.redo();
} else if (!doc.getElementById("_wysihtml5-undo")) {
cleanUp();
that.undo();
}
}, 400);
if (!observed) {
observed = true;
dom.observe(document, "mousedown", cleanUp);
dom.observe(doc, ["mousedown", "paste", "cut", "copy"], cleanUp);
}
});
}
this.editor
.on("newword:composer", function() {
that.transact();
})
.on("beforecommand:composer", function() {
that.transact();
});
},
transact: function() {
var previousHtml = this.historyStr[this.position - 1],
currentHtml = this.composer.getValue();
if (currentHtml === previousHtml) {
return;
}
var length = this.historyStr.length = this.historyDom.length = this.position;
if (length > MAX_HISTORY_ENTRIES) {
this.historyStr.shift();
this.historyDom.shift();
this.position--;
}
this.position++;
var range = this.composer.selection.getRange(),
node = range.startContainer || this.element,
offset = range.startOffset || 0,
element,
position;
if (node.nodeType === wysihtml5.ELEMENT_NODE) {
element = node;
} else {
element = node.parentNode;
position = this.getChildNodeIndex(element, node);
}
element.setAttribute(DATA_ATTR_OFFSET, offset);
if (typeof(position) !== "undefined") {
element.setAttribute(DATA_ATTR_NODE, position);
}
var clone = this.element.cloneNode(!!currentHtml);
this.historyDom.push(clone);
this.historyStr.push(currentHtml);
element.removeAttribute(DATA_ATTR_OFFSET);
element.removeAttribute(DATA_ATTR_NODE);
},
undo: function() {
this.transact();
if (!this.undoPossible()) {
return;
}
this.set(this.historyDom[--this.position - 1]);
this.editor.fire("undo:composer");
},
redo: function() {
if (!this.redoPossible()) {
return;
}
this.set(this.historyDom[++this.position - 1]);
this.editor.fire("redo:composer");
},
undoPossible: function() {
return this.position > 1;
},
redoPossible: function() {
return this.position < this.historyStr.length;
},
set: function(historyEntry) {
this.element.innerHTML = "";
var i = 0,
childNodes = historyEntry.childNodes,
length = historyEntry.childNodes.length;
for (; i<length; i++) {
this.element.appendChild(childNodes[i].cloneNode(true));
}
// Restore selection
var offset,
node,
position;
if (historyEntry.hasAttribute(DATA_ATTR_OFFSET)) {
offset = historyEntry.getAttribute(DATA_ATTR_OFFSET);
position = historyEntry.getAttribute(DATA_ATTR_NODE);
node = this.element;
} else {
node = this.element.querySelector("[" + DATA_ATTR_OFFSET + "]") || this.element;
offset = node.getAttribute(DATA_ATTR_OFFSET);
position = node.getAttribute(DATA_ATTR_NODE);
node.removeAttribute(DATA_ATTR_OFFSET);
node.removeAttribute(DATA_ATTR_NODE);
}
if (position !== null) {
node = this.getChildNodeByIndex(node, +position);
}
this.composer.selection.set(node, offset);
},
getChildNodeIndex: function(parent, child) {
var i = 0,
childNodes = parent.childNodes,
length = childNodes.length;
for (; i<length; i++) {
if (childNodes[i] === child) {
return i;
}
}
},
getChildNodeByIndex: function(parent, index) {
return parent.childNodes[index];
}
});
})(wysihtml5);