-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathundo_unit.js
More file actions
284 lines (260 loc) · 8.78 KB
/
undo_unit.js
File metadata and controls
284 lines (260 loc) · 8.78 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
editing.UndoUnit = (function() {
'use strict';
function ASSERT_EDITING_IN_PROGRESS(undoUnit) {
if (!undoUnit.endingSelection_)
return;
throw new Error("You can't mutate DOM tree once you set ending selection.");
}
/**
* @constructor
* @final
* @struct
* @param {!Document} contextDocument
* @param {string} name A name for this undoUnit for error message.
* @param {!editing.ImmutableSelection} selection
*/
function UndoUnit(contextDocument, name, selection) {
/** @private @type {!Document} */
this.document_ = contextDocument;
/** @private @type {?editing.ImmutableSelection} */
this.endingSelection_ = null;
/** @private @type {string} */
this.name_ = name;
/** @private @type {!Array.<!editing.Operation>} */
this.operations_ = [];
// We don't make ending selection as starting selection here. Because,
// |ImmutableSelection| doesn't track DOM modification during command
// execution.
/** @private @type {?editing.ImmutableSelection} */
this.startingSelection_ = selection;
/** @private @type {!Map.<!Element, !editing.SetStyle>} */
this.styledElements_ = new Map();
Object.seal(this);
}
// Forward type declarations for closure compiler.
UndoUnit.prototype = /** @struct */ {
/**
* @this {!UndoUnit}
* @param {!Node} parentNode
* @param {!Node} newChild
*/
appendChild: function(parentNode, newChild) {},
/**
* @this {!UndoUnit}
* @param {!Node} parentNode
* @param {!Node} newChild
* @param {?Node} refChild
*/
insertBefore: function(parentNode, newChild, refChild) {},
/**
* @this {!UndoUnit}
* @param {!Node} parentNode
* @param {!Node} oldChild
*/
removeChild: function(parentNode, oldChild) {},
};
/**
* @this {!UndoUnit}
* @param {!Node} parentNode
* @param {!Node} newChild
*/
function appendChild(parentNode, newChild) {
ASSERT_EDITING_IN_PROGRESS(this);
if (newChild.parentNode)
this.removeChild(newChild.parentNode, newChild);
var operation = new editing.AppendChild(parentNode, newChild);
this.operations_.push(operation);
operation.execute();
}
/**
* @this {!UndoUnit}
* @return {!editing.ImmutableSelection}
*/
function endingSelection() {
if (!this.endingSelection_)
throw new Error('You should set ending selection at end of command.');
return this.endingSelection_;
}
/**
* @this {!UndoUnit}
* @param {!Node} parentNode
* @param {!Node} newChild
* @param {?Node} refChild
*/
function insertBefore(parentNode, newChild, refChild) {
ASSERT_EDITING_IN_PROGRESS(this);
if (!refChild) {
this.appendChild(parentNode, newChild);
return;
}
if (parentNode !== refChild.parentNode)
throw new Error('Parent of refChild ' + refChild + ' must be ' +
parentNode);
if (newChild.parentNode)
this.removeChild(newChild.parentNode, newChild);
var operation = new editing.InsertBefore(parentNode, newChild, refChild);
this.operations_.push(operation);
operation.execute();
}
/**
* @this {!UndoUnit}
* @param {!Element} element
* @param {string} name
*/
function removeAttribute(element, name) {
ASSERT_EDITING_IN_PROGRESS(this);
console.assert(typeof(name) == 'string',
'Attribute name must be string rather than ' + name);
if (!element.hasAttribute(name))
return;
var operation = new editing.RemoveAttribute(element, name);
this.operations_.push(operation);
operation.execute();
}
/**
* @this {!UndoUnit}
* @param {!Node} parentNode
* @param {!Node} oldChild
*/
function removeChild(parentNode, oldChild) {
ASSERT_EDITING_IN_PROGRESS(this);
if (oldChild.parentNode !== parentNode)
throw new Error('A parent of oldChild ' + oldChild + ' must be ' +
oldChild.parentNode.outerHTML +
' instead of ' + parentNode.outerHTML);
var operation = new editing.RemoveChild(parentNode, oldChild);
this.operations_.push(operation);
operation.execute();
}
/**
* @this {!UndoUnit}
* @param {!Node} parentNode
* @param {!Node} newChild
* @param {!Node} oldChild
*/
function replaceChild(parentNode, newChild, oldChild) {
ASSERT_EDITING_IN_PROGRESS(this);
if (oldChild.parentNode !== parentNode)
throw new Error('A parent of oldChild ' + oldChild + ' must be ' +
oldChild.parentNode.outerHTML +
' instead of ' + parentNode.outerHTML);
if (newChild.parentNode)
this.removeChild(newChild.parentNode, newChild);
var operation = new editing.ReplaceChild(parentNode, newChild, oldChild);
this.operations_.push(operation);
operation.execute();
}
/**
* @this {!UndoUnit}
* @param {!Element} element
* @param {string} propertyName
*/
function removeStyle(element, propertyName) {
console.assert(editing.dom.isElement(element));
this.setStyle(element, propertyName, '');
}
/**
* @this {!UndoUnit}
* @param {!Element} element
* @param {string} name
* @param {string} newValue
*/
function setAttribute(element, name, newValue) {
console.assert(editing.dom.isElement(element),
'Node ' + element + ' must be an Element.');
ASSERT_EDITING_IN_PROGRESS(this);
var operation = new editing.SetAttribute(element, name, newValue);
this.operations_.push(operation);
operation.execute();
}
/**
* @this {!UndoUnit}
* @param {!editing.ImmutableSelection} selection
*/
function setEndingSelection(selection) {
if (this.endingSelection_)
throw new Error('ending selection is already set.');
var anchorNode = selection.anchorNode;
var anchorOffset = selection.anchorOffset;
var focusNode = selection.focusNode;
var focusOffset = selection.focusOffset;
if (!anchorNode)
throw new Error('Can not set null anchor node to ending ');
if (!focusNode)
throw new Error('Can not set null focus node to ending ');
if (!this.document_.contains(anchorNode)) {
throw new Error('Can not set anchor node not in document ' +
anchorNode + ' parent=' + anchorNode.parentNode);
}
if (anchorOffset < 0 ||
anchorOffset > editing.dom.maxOffset(anchorNode)) {
throw new Error('Invalid anchor offset ' + anchorOffset +
' on ' + anchorNode +
' max=' + editing.dom.maxOffset(anchorNode));
}
if (!this.document_.contains(focusNode)) {
throw new Error('Can not set focus node not in document ' +
focusNode);
}
if (focusOffset < 0 || focusOffset > editing.dom.maxOffset(focusNode)) {
throw new Error('Invalid focus offset ' + focusOffset +
' on ' + focusNode +
' max=' + editing.dom.maxOffset(focusNode));
}
this.endingSelection_ = selection;
}
/**
* @this {!UndoUnit}
* @param {!Element} element
* @param {string} propertyName
* @param {string} newValue
*/
function setStyle(element, propertyName, newValue) {
console.assert(editing.dom.isElement(element));
var operation = this.styledElements_.get(element);
if (!operation) {
operation = new editing.SetStyle(element);
this.operations_.push(operation);
this.styledElements_.set(element, operation);
}
operation.setProperty(propertyName, newValue);
operation.execute();
}
/**
* @this {!UndoUnit}
* @param {!Text} node
* @param {number} offset
* @return {!Text}
*/
function splitText(node, offset) {
ASSERT_EDITING_IN_PROGRESS(this);
var newNode = /** @type {!Text} */(node.splitText(offset));
this.operations_.push(new editing.SplitText(node, newNode));
return newNode;
}
UndoUnit.prototype = /** @struct */ {
// Selection after executing editing command. This |ImmutableSelection| is
// put into undo stack for redo operation. See also |startingSelection|
get endingSelection() { return endingSelection.call(this); },
get document() { return this.document_; },
get operations() { return this.operations_; },
get name() { return this.name_; },
get startingSelection() { return this.startingSelection_; },
appendChild: appendChild,
constructor: UndoUnit,
insertBefore: insertBefore,
removeAttribute: removeAttribute,
removeChild: removeChild,
removeStyle: removeStyle,
replaceChild: replaceChild,
setAttribute: setAttribute,
setEndingSelection: setEndingSelection,
setStyle: setStyle,
splitText: splitText,
};
Object.freeze(UndoUnit.prototype);
return UndoUnit;
})();