@ckeditor/ckeditor5-engine 30.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +17 -0
- package/README.md +30 -0
- package/package.json +70 -0
- package/src/controller/datacontroller.js +563 -0
- package/src/controller/editingcontroller.js +149 -0
- package/src/conversion/conversion.js +644 -0
- package/src/conversion/conversionhelpers.js +40 -0
- package/src/conversion/downcastdispatcher.js +914 -0
- package/src/conversion/downcasthelpers.js +1706 -0
- package/src/conversion/mapper.js +696 -0
- package/src/conversion/modelconsumable.js +329 -0
- package/src/conversion/upcastdispatcher.js +807 -0
- package/src/conversion/upcasthelpers.js +997 -0
- package/src/conversion/viewconsumable.js +623 -0
- package/src/dataprocessor/basichtmlwriter.js +32 -0
- package/src/dataprocessor/dataprocessor.jsdoc +64 -0
- package/src/dataprocessor/htmldataprocessor.js +159 -0
- package/src/dataprocessor/htmlwriter.js +22 -0
- package/src/dataprocessor/xmldataprocessor.js +161 -0
- package/src/dev-utils/model.js +482 -0
- package/src/dev-utils/operationreplayer.js +140 -0
- package/src/dev-utils/utils.js +103 -0
- package/src/dev-utils/view.js +1091 -0
- package/src/index.js +52 -0
- package/src/model/batch.js +82 -0
- package/src/model/differ.js +1282 -0
- package/src/model/document.js +483 -0
- package/src/model/documentfragment.js +390 -0
- package/src/model/documentselection.js +1261 -0
- package/src/model/element.js +438 -0
- package/src/model/history.js +138 -0
- package/src/model/item.jsdoc +14 -0
- package/src/model/liveposition.js +182 -0
- package/src/model/liverange.js +221 -0
- package/src/model/markercollection.js +553 -0
- package/src/model/model.js +934 -0
- package/src/model/node.js +507 -0
- package/src/model/nodelist.js +217 -0
- package/src/model/operation/attributeoperation.js +202 -0
- package/src/model/operation/detachoperation.js +103 -0
- package/src/model/operation/insertoperation.js +188 -0
- package/src/model/operation/markeroperation.js +154 -0
- package/src/model/operation/mergeoperation.js +216 -0
- package/src/model/operation/moveoperation.js +209 -0
- package/src/model/operation/nooperation.js +58 -0
- package/src/model/operation/operation.js +139 -0
- package/src/model/operation/operationfactory.js +49 -0
- package/src/model/operation/renameoperation.js +155 -0
- package/src/model/operation/rootattributeoperation.js +211 -0
- package/src/model/operation/splitoperation.js +254 -0
- package/src/model/operation/transform.js +2389 -0
- package/src/model/operation/utils.js +292 -0
- package/src/model/position.js +1164 -0
- package/src/model/range.js +1049 -0
- package/src/model/rootelement.js +111 -0
- package/src/model/schema.js +1851 -0
- package/src/model/selection.js +902 -0
- package/src/model/text.js +138 -0
- package/src/model/textproxy.js +279 -0
- package/src/model/treewalker.js +414 -0
- package/src/model/utils/autoparagraphing.js +77 -0
- package/src/model/utils/deletecontent.js +528 -0
- package/src/model/utils/getselectedcontent.js +150 -0
- package/src/model/utils/insertcontent.js +824 -0
- package/src/model/utils/modifyselection.js +229 -0
- package/src/model/utils/selection-post-fixer.js +297 -0
- package/src/model/writer.js +1574 -0
- package/src/view/attributeelement.js +274 -0
- package/src/view/containerelement.js +123 -0
- package/src/view/document.js +221 -0
- package/src/view/documentfragment.js +273 -0
- package/src/view/documentselection.js +387 -0
- package/src/view/domconverter.js +1437 -0
- package/src/view/downcastwriter.js +2121 -0
- package/src/view/editableelement.js +118 -0
- package/src/view/element.js +945 -0
- package/src/view/elementdefinition.jsdoc +59 -0
- package/src/view/emptyelement.js +119 -0
- package/src/view/filler.js +161 -0
- package/src/view/item.jsdoc +14 -0
- package/src/view/matcher.js +776 -0
- package/src/view/node.js +391 -0
- package/src/view/observer/arrowkeysobserver.js +58 -0
- package/src/view/observer/bubblingemittermixin.js +307 -0
- package/src/view/observer/bubblingeventinfo.js +71 -0
- package/src/view/observer/clickobserver.js +46 -0
- package/src/view/observer/compositionobserver.js +79 -0
- package/src/view/observer/domeventdata.js +82 -0
- package/src/view/observer/domeventobserver.js +99 -0
- package/src/view/observer/fakeselectionobserver.js +118 -0
- package/src/view/observer/focusobserver.js +106 -0
- package/src/view/observer/inputobserver.js +44 -0
- package/src/view/observer/keyobserver.js +83 -0
- package/src/view/observer/mouseobserver.js +56 -0
- package/src/view/observer/mutationobserver.js +345 -0
- package/src/view/observer/observer.js +118 -0
- package/src/view/observer/selectionobserver.js +242 -0
- package/src/view/placeholder.js +285 -0
- package/src/view/position.js +426 -0
- package/src/view/range.js +533 -0
- package/src/view/rawelement.js +148 -0
- package/src/view/renderer.js +1037 -0
- package/src/view/rooteditableelement.js +107 -0
- package/src/view/selection.js +718 -0
- package/src/view/styles/background.js +73 -0
- package/src/view/styles/border.js +362 -0
- package/src/view/styles/margin.js +41 -0
- package/src/view/styles/padding.js +40 -0
- package/src/view/styles/utils.js +277 -0
- package/src/view/stylesmap.js +938 -0
- package/src/view/text.js +147 -0
- package/src/view/textproxy.js +199 -0
- package/src/view/treewalker.js +496 -0
- package/src/view/uielement.js +238 -0
- package/src/view/upcastwriter.js +484 -0
- package/src/view/view.js +721 -0
- package/theme/placeholder.css +27 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module engine/model/element
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import Node from './node';
|
|
11
|
+
import NodeList from './nodelist';
|
|
12
|
+
import Text from './text';
|
|
13
|
+
import TextProxy from './textproxy';
|
|
14
|
+
import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
|
|
15
|
+
|
|
16
|
+
// @if CK_DEBUG_ENGINE // const { stringifyMap, convertMapToStringifiedObject, convertMapToTags } = require( '../dev-utils/utils' );
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Model element. Type of {@link module:engine/model/node~Node node} that has a {@link module:engine/model/element~Element#name name} and
|
|
20
|
+
* {@link module:engine/model/element~Element#getChildren child nodes}.
|
|
21
|
+
*
|
|
22
|
+
* **Important**: see {@link module:engine/model/node~Node} to read about restrictions using `Element` and `Node` API.
|
|
23
|
+
*
|
|
24
|
+
* @extends module:engine/model/node~Node
|
|
25
|
+
*/
|
|
26
|
+
export default class Element extends Node {
|
|
27
|
+
/**
|
|
28
|
+
* Creates a model element.
|
|
29
|
+
*
|
|
30
|
+
* **Note:** Constructor of this class shouldn't be used directly in the code.
|
|
31
|
+
* Use the {@link module:engine/model/writer~Writer#createElement} method instead.
|
|
32
|
+
*
|
|
33
|
+
* @protected
|
|
34
|
+
* @param {String} name Element's name.
|
|
35
|
+
* @param {Object} [attrs] Element's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
|
|
36
|
+
* @param {module:engine/model/node~Node|Iterable.<module:engine/model/node~Node>} [children]
|
|
37
|
+
* One or more nodes to be inserted as children of created element.
|
|
38
|
+
*/
|
|
39
|
+
constructor( name, attrs, children ) {
|
|
40
|
+
super( attrs );
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Element name.
|
|
44
|
+
*
|
|
45
|
+
* @readonly
|
|
46
|
+
* @member {String} module:engine/model/element~Element#name
|
|
47
|
+
*/
|
|
48
|
+
this.name = name;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* List of children nodes.
|
|
52
|
+
*
|
|
53
|
+
* @private
|
|
54
|
+
* @member {module:engine/model/nodelist~NodeList} module:engine/model/element~Element#_children
|
|
55
|
+
*/
|
|
56
|
+
this._children = new NodeList();
|
|
57
|
+
|
|
58
|
+
if ( children ) {
|
|
59
|
+
this._insertChild( 0, children );
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Number of this element's children.
|
|
65
|
+
*
|
|
66
|
+
* @readonly
|
|
67
|
+
* @type {Number}
|
|
68
|
+
*/
|
|
69
|
+
get childCount() {
|
|
70
|
+
return this._children.length;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Sum of {@link module:engine/model/node~Node#offsetSize offset sizes} of all of this element's children.
|
|
75
|
+
*
|
|
76
|
+
* @readonly
|
|
77
|
+
* @type {Number}
|
|
78
|
+
*/
|
|
79
|
+
get maxOffset() {
|
|
80
|
+
return this._children.maxOffset;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Is `true` if there are no nodes inside this element, `false` otherwise.
|
|
85
|
+
*
|
|
86
|
+
* @readonly
|
|
87
|
+
* @type {Boolean}
|
|
88
|
+
*/
|
|
89
|
+
get isEmpty() {
|
|
90
|
+
return this.childCount === 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Checks whether this object is of the given.
|
|
95
|
+
*
|
|
96
|
+
* element.is( 'element' ); // -> true
|
|
97
|
+
* element.is( 'node' ); // -> true
|
|
98
|
+
* element.is( 'model:element' ); // -> true
|
|
99
|
+
* element.is( 'model:node' ); // -> true
|
|
100
|
+
*
|
|
101
|
+
* element.is( 'view:element' ); // -> false
|
|
102
|
+
* element.is( 'documentSelection' ); // -> false
|
|
103
|
+
*
|
|
104
|
+
* Assuming that the object being checked is an element, you can also check its
|
|
105
|
+
* {@link module:engine/model/element~Element#name name}:
|
|
106
|
+
*
|
|
107
|
+
* element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element
|
|
108
|
+
* element.is( 'element', 'imageBlock' ); // -> same as above
|
|
109
|
+
* text.is( 'element', 'imageBlock' ); -> false
|
|
110
|
+
*
|
|
111
|
+
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
|
|
112
|
+
*
|
|
113
|
+
* @param {String} type Type to check.
|
|
114
|
+
* @param {String} [name] Element name.
|
|
115
|
+
* @returns {Boolean}
|
|
116
|
+
*/
|
|
117
|
+
is( type, name = null ) {
|
|
118
|
+
if ( !name ) {
|
|
119
|
+
return type === 'element' || type === 'model:element' ||
|
|
120
|
+
// From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
|
|
121
|
+
type === 'node' || type === 'model:node';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return name === this.name && ( type === 'element' || type === 'model:element' );
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Gets the child at the given index.
|
|
129
|
+
*
|
|
130
|
+
* @param {Number} index Index of child.
|
|
131
|
+
* @returns {module:engine/model/node~Node} Child node.
|
|
132
|
+
*/
|
|
133
|
+
getChild( index ) {
|
|
134
|
+
return this._children.getNode( index );
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Returns an iterator that iterates over all of this element's children.
|
|
139
|
+
*
|
|
140
|
+
* @returns {Iterable.<module:engine/model/node~Node>}
|
|
141
|
+
*/
|
|
142
|
+
getChildren() {
|
|
143
|
+
return this._children[ Symbol.iterator ]();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Returns an index of the given child node. Returns `null` if given node is not a child of this element.
|
|
148
|
+
*
|
|
149
|
+
* @param {module:engine/model/node~Node} node Child node to look for.
|
|
150
|
+
* @returns {Number} Child node's index in this element.
|
|
151
|
+
*/
|
|
152
|
+
getChildIndex( node ) {
|
|
153
|
+
return this._children.getNodeIndex( node );
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Returns the starting offset of given child. Starting offset is equal to the sum of
|
|
158
|
+
* {@link module:engine/model/node~Node#offsetSize offset sizes} of all node's siblings that are before it. Returns `null` if
|
|
159
|
+
* given node is not a child of this element.
|
|
160
|
+
*
|
|
161
|
+
* @param {module:engine/model/node~Node} node Child node to look for.
|
|
162
|
+
* @returns {Number} Child node's starting offset.
|
|
163
|
+
*/
|
|
164
|
+
getChildStartOffset( node ) {
|
|
165
|
+
return this._children.getNodeStartOffset( node );
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Returns index of a node that occupies given offset. If given offset is too low, returns `0`. If given offset is
|
|
170
|
+
* too high, returns {@link module:engine/model/element~Element#getChildIndex index after last child}.
|
|
171
|
+
*
|
|
172
|
+
* const textNode = new Text( 'foo' );
|
|
173
|
+
* const pElement = new Element( 'p' );
|
|
174
|
+
* const divElement = new Element( [ textNode, pElement ] );
|
|
175
|
+
* divElement.offsetToIndex( -1 ); // Returns 0, because offset is too low.
|
|
176
|
+
* divElement.offsetToIndex( 0 ); // Returns 0, because offset 0 is taken by `textNode` which is at index 0.
|
|
177
|
+
* divElement.offsetToIndex( 1 ); // Returns 0, because `textNode` has `offsetSize` equal to 3, so it occupies offset 1 too.
|
|
178
|
+
* divElement.offsetToIndex( 2 ); // Returns 0.
|
|
179
|
+
* divElement.offsetToIndex( 3 ); // Returns 1.
|
|
180
|
+
* divElement.offsetToIndex( 4 ); // Returns 2. There are no nodes at offset 4, so last available index is returned.
|
|
181
|
+
*
|
|
182
|
+
* @param {Number} offset Offset to look for.
|
|
183
|
+
* @returns {Number}
|
|
184
|
+
*/
|
|
185
|
+
offsetToIndex( offset ) {
|
|
186
|
+
return this._children.offsetToIndex( offset );
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns a descendant node by its path relative to this element.
|
|
191
|
+
*
|
|
192
|
+
* // <this>a<b>c</b></this>
|
|
193
|
+
* this.getNodeByPath( [ 0 ] ); // -> "a"
|
|
194
|
+
* this.getNodeByPath( [ 1 ] ); // -> <b>
|
|
195
|
+
* this.getNodeByPath( [ 1, 0 ] ); // -> "c"
|
|
196
|
+
*
|
|
197
|
+
* @param {Array.<Number>} relativePath Path of the node to find, relative to this element.
|
|
198
|
+
* @returns {module:engine/model/node~Node}
|
|
199
|
+
*/
|
|
200
|
+
getNodeByPath( relativePath ) {
|
|
201
|
+
let node = this; // eslint-disable-line consistent-this
|
|
202
|
+
|
|
203
|
+
for ( const index of relativePath ) {
|
|
204
|
+
node = node.getChild( node.offsetToIndex( index ) );
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return node;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Returns the parent element of the given name. Returns null if the element is not inside the desired parent.
|
|
212
|
+
*
|
|
213
|
+
* @param {String} parentName The name of the parent element to find.
|
|
214
|
+
* @param {Object} [options] Options object.
|
|
215
|
+
* @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included while searching.
|
|
216
|
+
* @returns {module:engine/model/element~Element|null}
|
|
217
|
+
*/
|
|
218
|
+
findAncestor( parentName, options = { includeSelf: false } ) {
|
|
219
|
+
let parent = options.includeSelf ? this : this.parent;
|
|
220
|
+
|
|
221
|
+
while ( parent ) {
|
|
222
|
+
if ( parent.name === parentName ) {
|
|
223
|
+
return parent;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
parent = parent.parent;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Converts `Element` instance to plain object and returns it. Takes care of converting all of this element's children.
|
|
234
|
+
*
|
|
235
|
+
* @returns {Object} `Element` instance converted to plain object.
|
|
236
|
+
*/
|
|
237
|
+
toJSON() {
|
|
238
|
+
const json = super.toJSON();
|
|
239
|
+
|
|
240
|
+
json.name = this.name;
|
|
241
|
+
|
|
242
|
+
if ( this._children.length > 0 ) {
|
|
243
|
+
json.children = [];
|
|
244
|
+
|
|
245
|
+
for ( const node of this._children ) {
|
|
246
|
+
json.children.push( node.toJSON() );
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return json;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Creates a copy of this element and returns it. Created element has the same name and attributes as the original element.
|
|
255
|
+
* If clone is deep, the original element's children are also cloned. If not, then empty element is returned.
|
|
256
|
+
*
|
|
257
|
+
* @protected
|
|
258
|
+
* @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
|
|
259
|
+
* element will be cloned without any child.
|
|
260
|
+
*/
|
|
261
|
+
_clone( deep = false ) {
|
|
262
|
+
const children = deep ? Array.from( this._children ).map( node => node._clone( true ) ) : null;
|
|
263
|
+
|
|
264
|
+
return new Element( this.name, this.getAttributes(), children );
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* {@link module:engine/model/element~Element#_insertChild Inserts} one or more nodes at the end of this element.
|
|
269
|
+
*
|
|
270
|
+
* @see module:engine/model/writer~Writer#append
|
|
271
|
+
* @protected
|
|
272
|
+
* @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} nodes Nodes to be inserted.
|
|
273
|
+
*/
|
|
274
|
+
_appendChild( nodes ) {
|
|
275
|
+
this._insertChild( this.childCount, nodes );
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Inserts one or more nodes at the given index and sets {@link module:engine/model/node~Node#parent parent} of these nodes
|
|
280
|
+
* to this element.
|
|
281
|
+
*
|
|
282
|
+
* @see module:engine/model/writer~Writer#insert
|
|
283
|
+
* @protected
|
|
284
|
+
* @param {Number} index Index at which nodes should be inserted.
|
|
285
|
+
* @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} items Items to be inserted.
|
|
286
|
+
*/
|
|
287
|
+
_insertChild( index, items ) {
|
|
288
|
+
const nodes = normalize( items );
|
|
289
|
+
|
|
290
|
+
for ( const node of nodes ) {
|
|
291
|
+
// If node that is being added to this element is already inside another element, first remove it from the old parent.
|
|
292
|
+
if ( node.parent !== null ) {
|
|
293
|
+
node._remove();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
node.parent = this;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
this._children._insertNodes( index, nodes );
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Removes one or more nodes starting at the given index and sets
|
|
304
|
+
* {@link module:engine/model/node~Node#parent parent} of these nodes to `null`.
|
|
305
|
+
*
|
|
306
|
+
* @see module:engine/model/writer~Writer#remove
|
|
307
|
+
* @protected
|
|
308
|
+
* @param {Number} index Index of the first node to remove.
|
|
309
|
+
* @param {Number} [howMany=1] Number of nodes to remove.
|
|
310
|
+
* @returns {Array.<module:engine/model/node~Node>} Array containing removed nodes.
|
|
311
|
+
*/
|
|
312
|
+
_removeChildren( index, howMany = 1 ) {
|
|
313
|
+
const nodes = this._children._removeNodes( index, howMany );
|
|
314
|
+
|
|
315
|
+
for ( const node of nodes ) {
|
|
316
|
+
node.parent = null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return nodes;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Creates an `Element` instance from given plain object (i.e. parsed JSON string).
|
|
324
|
+
* Converts `Element` children to proper nodes.
|
|
325
|
+
*
|
|
326
|
+
* @param {Object} json Plain object to be converted to `Element`.
|
|
327
|
+
* @returns {module:engine/model/element~Element} `Element` instance created using given plain object.
|
|
328
|
+
*/
|
|
329
|
+
static fromJSON( json ) {
|
|
330
|
+
let children = null;
|
|
331
|
+
|
|
332
|
+
if ( json.children ) {
|
|
333
|
+
children = [];
|
|
334
|
+
|
|
335
|
+
for ( const child of json.children ) {
|
|
336
|
+
if ( child.name ) {
|
|
337
|
+
// If child has name property, it is an Element.
|
|
338
|
+
children.push( Element.fromJSON( child ) );
|
|
339
|
+
} else {
|
|
340
|
+
// Otherwise, it is a Text node.
|
|
341
|
+
children.push( Text.fromJSON( child ) );
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return new Element( json.name, json.attributes, children );
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// @if CK_DEBUG_ENGINE // toString() {
|
|
350
|
+
// @if CK_DEBUG_ENGINE // return `<${ this.rootName || this.name }>`;
|
|
351
|
+
// @if CK_DEBUG_ENGINE // }
|
|
352
|
+
|
|
353
|
+
// @if CK_DEBUG_ENGINE // log() {
|
|
354
|
+
// @if CK_DEBUG_ENGINE // console.log( 'ModelElement: ' + this );
|
|
355
|
+
// @if CK_DEBUG_ENGINE // }
|
|
356
|
+
|
|
357
|
+
// @if CK_DEBUG_ENGINE // logExtended() {
|
|
358
|
+
// @if CK_DEBUG_ENGINE // console.log( `ModelElement: ${ this }, ${ this.childCount } children,
|
|
359
|
+
// @if CK_DEBUG_ENGINE // attrs: ${ convertMapToStringifiedObject( this.getAttributes() ) }` );
|
|
360
|
+
// @if CK_DEBUG_ENGINE // }
|
|
361
|
+
|
|
362
|
+
// @if CK_DEBUG_ENGINE // logAll() {
|
|
363
|
+
// @if CK_DEBUG_ENGINE // console.log( '--------------------' );
|
|
364
|
+
// @if CK_DEBUG_ENGINE //
|
|
365
|
+
// @if CK_DEBUG_ENGINE // this.logExtended();
|
|
366
|
+
// @if CK_DEBUG_ENGINE // console.log( 'List of children:' );
|
|
367
|
+
// @if CK_DEBUG_ENGINE //
|
|
368
|
+
// @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
|
|
369
|
+
// @if CK_DEBUG_ENGINE // child.log();
|
|
370
|
+
// @if CK_DEBUG_ENGINE // }
|
|
371
|
+
// @if CK_DEBUG_ENGINE // }
|
|
372
|
+
|
|
373
|
+
// @if CK_DEBUG_ENGINE // printTree( level = 0) {
|
|
374
|
+
// @if CK_DEBUG_ENGINE // let string = '';
|
|
375
|
+
|
|
376
|
+
// @if CK_DEBUG_ENGINE // string += '\t'.repeat( level );
|
|
377
|
+
// @if CK_DEBUG_ENGINE // string += `<${ this.rootName || this.name }${ convertMapToTags( this.getAttributes() ) }>`;
|
|
378
|
+
|
|
379
|
+
// @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
|
|
380
|
+
// @if CK_DEBUG_ENGINE // string += '\n';
|
|
381
|
+
|
|
382
|
+
// @if CK_DEBUG_ENGINE // if ( child.is( '$text' ) ) {
|
|
383
|
+
// @if CK_DEBUG_ENGINE // const textAttrs = convertMapToTags( child._attrs );
|
|
384
|
+
|
|
385
|
+
// @if CK_DEBUG_ENGINE // string += '\t'.repeat( level + 1 );
|
|
386
|
+
|
|
387
|
+
// @if CK_DEBUG_ENGINE // if ( textAttrs !== '' ) {
|
|
388
|
+
// @if CK_DEBUG_ENGINE // string += `<$text${ textAttrs }>` + child.data + '</$text>';
|
|
389
|
+
// @if CK_DEBUG_ENGINE // } else {
|
|
390
|
+
// @if CK_DEBUG_ENGINE // string += child.data;
|
|
391
|
+
// @if CK_DEBUG_ENGINE // }
|
|
392
|
+
// @if CK_DEBUG_ENGINE // } else {
|
|
393
|
+
// @if CK_DEBUG_ENGINE // string += child.printTree( level + 1 );
|
|
394
|
+
// @if CK_DEBUG_ENGINE // }
|
|
395
|
+
// @if CK_DEBUG_ENGINE // }
|
|
396
|
+
|
|
397
|
+
// @if CK_DEBUG_ENGINE // if ( this.childCount ) {
|
|
398
|
+
// @if CK_DEBUG_ENGINE // string += '\n' + '\t'.repeat( level );
|
|
399
|
+
// @if CK_DEBUG_ENGINE // }
|
|
400
|
+
|
|
401
|
+
// @if CK_DEBUG_ENGINE // string += `</${ this.rootName || this.name }>`;
|
|
402
|
+
|
|
403
|
+
// @if CK_DEBUG_ENGINE // return string;
|
|
404
|
+
// @if CK_DEBUG_ENGINE // }
|
|
405
|
+
|
|
406
|
+
// @if CK_DEBUG_ENGINE // logTree() {
|
|
407
|
+
// @if CK_DEBUG_ENGINE // console.log( this.printTree() );
|
|
408
|
+
// @if CK_DEBUG_ENGINE // }
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Converts strings to Text and non-iterables to arrays.
|
|
412
|
+
//
|
|
413
|
+
// @param {String|module:engine/model/item~Item|Iterable.<String|module:engine/model/item~Item>}
|
|
414
|
+
// @returns {Iterable.<module:engine/model/node~Node>}
|
|
415
|
+
function normalize( nodes ) {
|
|
416
|
+
// Separate condition because string is iterable.
|
|
417
|
+
if ( typeof nodes == 'string' ) {
|
|
418
|
+
return [ new Text( nodes ) ];
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if ( !isIterable( nodes ) ) {
|
|
422
|
+
nodes = [ nodes ];
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Array.from to enable .map() on non-arrays.
|
|
426
|
+
return Array.from( nodes )
|
|
427
|
+
.map( node => {
|
|
428
|
+
if ( typeof node == 'string' ) {
|
|
429
|
+
return new Text( node );
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if ( node instanceof TextProxy ) {
|
|
433
|
+
return new Text( node.data, node.getAttributes() );
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return node;
|
|
437
|
+
} );
|
|
438
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module engine/model/history
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `History` keeps the track of all the operations applied to the {@link module:engine/model/document~Document document}.
|
|
12
|
+
*/
|
|
13
|
+
export default class History {
|
|
14
|
+
/**
|
|
15
|
+
* Creates an empty History instance.
|
|
16
|
+
*/
|
|
17
|
+
constructor() {
|
|
18
|
+
/**
|
|
19
|
+
* Operations added to the history.
|
|
20
|
+
*
|
|
21
|
+
* @protected
|
|
22
|
+
* @member {Array.<module:engine/model/operation/operation~Operation>} module:engine/model/history~History#_operations
|
|
23
|
+
*/
|
|
24
|
+
this._operations = [];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Holds an information which {@link module:engine/model/operation/operation~Operation operation} undoes which
|
|
28
|
+
* {@link module:engine/model/operation/operation~Operation operation}.
|
|
29
|
+
*
|
|
30
|
+
* Keys of the map are "undoing operations", that is operations that undone some other operations. For each key, the
|
|
31
|
+
* value is an operation that has been undone by the "undoing operation".
|
|
32
|
+
*
|
|
33
|
+
* @private
|
|
34
|
+
* @member {Map} module:engine/model/history~History#_undoPairs
|
|
35
|
+
*/
|
|
36
|
+
this._undoPairs = new Map();
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Holds all undone operations.
|
|
40
|
+
*
|
|
41
|
+
* @private
|
|
42
|
+
* @member {Set.<module:engine/model/operation/operation~Operation>} module:engine/model/history~History#_undoneOperations
|
|
43
|
+
*/
|
|
44
|
+
this._undoneOperations = new Set();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Adds an operation to the history.
|
|
49
|
+
*
|
|
50
|
+
* @param {module:engine/model/operation/operation~Operation} operation Operation to add.
|
|
51
|
+
*/
|
|
52
|
+
addOperation( operation ) {
|
|
53
|
+
if ( this._operations.includes( operation ) ) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this._operations.push( operation );
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns operations added to the history.
|
|
62
|
+
*
|
|
63
|
+
* @param {Number} [from=Number.NEGATIVE_INFINITY] Base version from which operations should be returned (inclusive).
|
|
64
|
+
* Defaults to `Number.NEGATIVE_INFINITY`, which means that operations from the first one will be returned.
|
|
65
|
+
* @param {Number} [to=Number.POSITIVE_INFINITY] Base version up to which operations should be returned (exclusive).
|
|
66
|
+
* Defaults to `Number.POSITIVE_INFINITY` which means that operations up to the last one will be returned.
|
|
67
|
+
* @returns {Array.<module:engine/model/operation/operation~Operation>} Operations added to the history.
|
|
68
|
+
*/
|
|
69
|
+
getOperations( from = Number.NEGATIVE_INFINITY, to = Number.POSITIVE_INFINITY ) {
|
|
70
|
+
const operations = [];
|
|
71
|
+
|
|
72
|
+
for ( const operation of this._operations ) {
|
|
73
|
+
if ( operation.baseVersion >= from && operation.baseVersion < to ) {
|
|
74
|
+
operations.push( operation );
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return operations;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns operation from the history that bases on given `baseVersion`.
|
|
83
|
+
*
|
|
84
|
+
* @param {Number} baseVersion Base version of the operation to get.
|
|
85
|
+
* @returns {module:engine/model/operation/operation~Operation|undefined} Operation with given base version or `undefined` if
|
|
86
|
+
* there is no such operation in history.
|
|
87
|
+
*/
|
|
88
|
+
getOperation( baseVersion ) {
|
|
89
|
+
for ( const operation of this._operations ) {
|
|
90
|
+
if ( operation.baseVersion == baseVersion ) {
|
|
91
|
+
return operation;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Marks in history that one operation is an operation that is undoing the other operation. By marking operation this way,
|
|
98
|
+
* history is keeping more context information about operations, which helps in operational transformation.
|
|
99
|
+
*
|
|
100
|
+
* @param {module:engine/model/operation/operation~Operation} undoneOperation Operation which is undone by `undoingOperation`.
|
|
101
|
+
* @param {module:engine/model/operation/operation~Operation} undoingOperation Operation which undoes `undoneOperation`.
|
|
102
|
+
*/
|
|
103
|
+
setOperationAsUndone( undoneOperation, undoingOperation ) {
|
|
104
|
+
this._undoPairs.set( undoingOperation, undoneOperation );
|
|
105
|
+
this._undoneOperations.add( undoneOperation );
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Checks whether given `operation` is undoing any other operation.
|
|
110
|
+
*
|
|
111
|
+
* @param {module:engine/model/operation/operation~Operation} operation Operation to check.
|
|
112
|
+
* @returns {Boolean} `true` if given `operation` is undoing any other operation, `false` otherwise.
|
|
113
|
+
*/
|
|
114
|
+
isUndoingOperation( operation ) {
|
|
115
|
+
return this._undoPairs.has( operation );
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Checks whether given `operation` has been undone by any other operation.
|
|
120
|
+
*
|
|
121
|
+
* @param {module:engine/model/operation/operation~Operation} operation Operation to check.
|
|
122
|
+
* @returns {Boolean} `true` if given `operation` has been undone any other operation, `false` otherwise.
|
|
123
|
+
*/
|
|
124
|
+
isUndoneOperation( operation ) {
|
|
125
|
+
return this._undoneOperations.has( operation );
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* For given `undoingOperation`, returns the operation which has been undone by it.
|
|
130
|
+
*
|
|
131
|
+
* @param {module:engine/model/operation/operation~Operation} undoingOperation
|
|
132
|
+
* @returns {module:engine/model/operation/operation~Operation|undefined} Operation that has been undone by given
|
|
133
|
+
* `undoingOperation` or `undefined` if given `undoingOperation` is not undoing any other operation.
|
|
134
|
+
*/
|
|
135
|
+
getUndoneOperation( undoingOperation ) {
|
|
136
|
+
return this._undoPairs.get( undoingOperation );
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module engine/model/item
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Item is a {@link module:engine/model/node~Node} or {@link module:engine/model/textproxy~TextProxy}.
|
|
12
|
+
*
|
|
13
|
+
* @typedef {module:engine/model/node~Node|module:engine/model/textproxy~TextProxy} module:engine/model/item~Item
|
|
14
|
+
*/
|