@ckeditor/ckeditor5-engine 38.1.1 → 38.2.0-alpha.1
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/package.json +3 -2
- package/src/controller/datacontroller.d.ts +334 -334
- package/src/controller/datacontroller.js +481 -481
- package/src/controller/editingcontroller.d.ts +98 -98
- package/src/controller/editingcontroller.js +191 -191
- package/src/conversion/conversion.d.ts +478 -478
- package/src/conversion/conversion.js +601 -601
- package/src/conversion/conversionhelpers.d.ts +26 -26
- package/src/conversion/conversionhelpers.js +32 -32
- package/src/conversion/downcastdispatcher.d.ts +547 -547
- package/src/conversion/downcastdispatcher.js +538 -538
- package/src/conversion/downcasthelpers.d.ts +1226 -1226
- package/src/conversion/downcasthelpers.js +2183 -2183
- package/src/conversion/mapper.d.ts +503 -503
- package/src/conversion/mapper.js +536 -536
- package/src/conversion/modelconsumable.d.ts +201 -201
- package/src/conversion/modelconsumable.js +333 -333
- package/src/conversion/upcastdispatcher.d.ts +492 -492
- package/src/conversion/upcastdispatcher.js +460 -460
- package/src/conversion/upcasthelpers.d.ts +499 -499
- package/src/conversion/upcasthelpers.js +950 -950
- package/src/conversion/viewconsumable.d.ts +369 -369
- package/src/conversion/viewconsumable.js +532 -532
- package/src/dataprocessor/basichtmlwriter.d.ts +18 -18
- package/src/dataprocessor/basichtmlwriter.js +19 -19
- package/src/dataprocessor/dataprocessor.d.ts +61 -61
- package/src/dataprocessor/dataprocessor.js +5 -5
- package/src/dataprocessor/htmldataprocessor.d.ts +76 -76
- package/src/dataprocessor/htmldataprocessor.js +96 -96
- package/src/dataprocessor/htmlwriter.d.ts +16 -16
- package/src/dataprocessor/htmlwriter.js +5 -5
- package/src/dataprocessor/xmldataprocessor.d.ts +90 -90
- package/src/dataprocessor/xmldataprocessor.js +108 -108
- package/src/dev-utils/model.d.ts +124 -124
- package/src/dev-utils/model.js +395 -395
- package/src/dev-utils/operationreplayer.d.ts +51 -51
- package/src/dev-utils/operationreplayer.js +112 -112
- package/src/dev-utils/utils.d.ts +37 -37
- package/src/dev-utils/utils.js +73 -73
- package/src/dev-utils/view.d.ts +319 -319
- package/src/dev-utils/view.js +967 -967
- package/src/index.d.ts +113 -113
- package/src/index.js +77 -77
- package/src/model/batch.d.ts +106 -106
- package/src/model/batch.js +96 -96
- package/src/model/differ.d.ts +373 -373
- package/src/model/differ.js +1090 -1090
- package/src/model/document.d.ts +266 -264
- package/src/model/document.js +356 -356
- package/src/model/documentfragment.d.ts +200 -200
- package/src/model/documentfragment.js +306 -306
- package/src/model/documentselection.d.ts +420 -420
- package/src/model/documentselection.js +990 -987
- package/src/model/element.d.ts +165 -165
- package/src/model/element.js +281 -281
- package/src/model/history.d.ts +114 -114
- package/src/model/history.js +207 -207
- package/src/model/item.d.ts +14 -14
- package/src/model/item.js +5 -5
- package/src/model/liveposition.d.ts +77 -77
- package/src/model/liveposition.js +93 -93
- package/src/model/liverange.d.ts +102 -102
- package/src/model/liverange.js +120 -120
- package/src/model/markercollection.d.ts +335 -335
- package/src/model/markercollection.js +403 -403
- package/src/model/model.d.ts +920 -920
- package/src/model/model.js +843 -843
- package/src/model/node.d.ts +256 -256
- package/src/model/node.js +375 -375
- package/src/model/nodelist.d.ts +91 -91
- package/src/model/nodelist.js +163 -163
- package/src/model/operation/attributeoperation.d.ts +103 -103
- package/src/model/operation/attributeoperation.js +148 -148
- package/src/model/operation/detachoperation.d.ts +60 -60
- package/src/model/operation/detachoperation.js +77 -77
- package/src/model/operation/insertoperation.d.ts +90 -90
- package/src/model/operation/insertoperation.js +135 -135
- package/src/model/operation/markeroperation.d.ts +91 -91
- package/src/model/operation/markeroperation.js +107 -107
- package/src/model/operation/mergeoperation.d.ts +100 -100
- package/src/model/operation/mergeoperation.js +167 -167
- package/src/model/operation/moveoperation.d.ts +96 -96
- package/src/model/operation/moveoperation.js +164 -164
- package/src/model/operation/nooperation.d.ts +38 -38
- package/src/model/operation/nooperation.js +48 -48
- package/src/model/operation/operation.d.ts +96 -96
- package/src/model/operation/operation.js +62 -62
- package/src/model/operation/operationfactory.d.ts +18 -18
- package/src/model/operation/operationfactory.js +44 -44
- package/src/model/operation/renameoperation.d.ts +83 -83
- package/src/model/operation/renameoperation.js +115 -115
- package/src/model/operation/rootattributeoperation.d.ts +98 -98
- package/src/model/operation/rootattributeoperation.js +155 -155
- package/src/model/operation/rootoperation.d.ts +80 -80
- package/src/model/operation/rootoperation.js +114 -114
- package/src/model/operation/splitoperation.d.ts +109 -109
- package/src/model/operation/splitoperation.js +194 -194
- package/src/model/operation/transform.d.ts +100 -100
- package/src/model/operation/transform.js +1985 -1985
- package/src/model/operation/utils.d.ts +71 -71
- package/src/model/operation/utils.js +213 -213
- package/src/model/position.d.ts +539 -539
- package/src/model/position.js +979 -979
- package/src/model/range.d.ts +458 -458
- package/src/model/range.js +875 -875
- package/src/model/rootelement.d.ts +54 -54
- package/src/model/rootelement.js +68 -68
- package/src/model/schema.d.ts +1186 -1176
- package/src/model/schema.js +1237 -1237
- package/src/model/selection.d.ts +482 -482
- package/src/model/selection.js +789 -789
- package/src/model/text.d.ts +66 -66
- package/src/model/text.js +85 -85
- package/src/model/textproxy.d.ts +144 -144
- package/src/model/textproxy.js +189 -189
- package/src/model/treewalker.d.ts +186 -186
- package/src/model/treewalker.js +244 -244
- package/src/model/typecheckable.d.ts +285 -285
- package/src/model/typecheckable.js +16 -16
- package/src/model/utils/autoparagraphing.d.ts +37 -37
- package/src/model/utils/autoparagraphing.js +64 -64
- package/src/model/utils/deletecontent.d.ts +58 -58
- package/src/model/utils/deletecontent.js +488 -488
- package/src/model/utils/findoptimalinsertionrange.d.ts +32 -32
- package/src/model/utils/findoptimalinsertionrange.js +57 -57
- package/src/model/utils/getselectedcontent.d.ts +30 -30
- package/src/model/utils/getselectedcontent.js +125 -125
- package/src/model/utils/insertcontent.d.ts +46 -46
- package/src/model/utils/insertcontent.js +705 -705
- package/src/model/utils/insertobject.d.ts +44 -44
- package/src/model/utils/insertobject.js +139 -139
- package/src/model/utils/modifyselection.d.ts +48 -48
- package/src/model/utils/modifyselection.js +186 -186
- package/src/model/utils/selection-post-fixer.d.ts +74 -74
- package/src/model/utils/selection-post-fixer.js +260 -260
- package/src/model/writer.d.ts +851 -851
- package/src/model/writer.js +1306 -1306
- package/src/view/attributeelement.d.ts +108 -108
- package/src/view/attributeelement.js +184 -184
- package/src/view/containerelement.d.ts +49 -49
- package/src/view/containerelement.js +80 -80
- package/src/view/datatransfer.d.ts +79 -79
- package/src/view/datatransfer.js +98 -98
- package/src/view/document.d.ts +184 -184
- package/src/view/document.js +120 -120
- package/src/view/documentfragment.d.ts +149 -149
- package/src/view/documentfragment.js +228 -228
- package/src/view/documentselection.d.ts +306 -306
- package/src/view/documentselection.js +256 -256
- package/src/view/domconverter.d.ts +634 -650
- package/src/view/domconverter.js +1407 -1373
- package/src/view/downcastwriter.d.ts +996 -996
- package/src/view/downcastwriter.js +1696 -1696
- package/src/view/editableelement.d.ts +52 -52
- package/src/view/editableelement.js +61 -61
- package/src/view/element.d.ts +468 -468
- package/src/view/element.js +724 -724
- package/src/view/elementdefinition.d.ts +87 -87
- package/src/view/elementdefinition.js +5 -5
- package/src/view/emptyelement.d.ts +41 -41
- package/src/view/emptyelement.js +73 -73
- package/src/view/filler.d.ts +111 -111
- package/src/view/filler.js +150 -148
- package/src/view/item.d.ts +14 -14
- package/src/view/item.js +5 -5
- package/src/view/matcher.d.ts +486 -486
- package/src/view/matcher.js +507 -507
- package/src/view/node.d.ts +163 -163
- package/src/view/node.js +228 -228
- package/src/view/observer/arrowkeysobserver.d.ts +45 -45
- package/src/view/observer/arrowkeysobserver.js +40 -40
- package/src/view/observer/bubblingemittermixin.d.ts +166 -166
- package/src/view/observer/bubblingemittermixin.js +172 -172
- package/src/view/observer/bubblingeventinfo.d.ts +47 -47
- package/src/view/observer/bubblingeventinfo.js +37 -37
- package/src/view/observer/clickobserver.d.ts +43 -43
- package/src/view/observer/clickobserver.js +29 -29
- package/src/view/observer/compositionobserver.d.ts +82 -82
- package/src/view/observer/compositionobserver.js +60 -60
- package/src/view/observer/domeventdata.d.ts +50 -50
- package/src/view/observer/domeventdata.js +47 -47
- package/src/view/observer/domeventobserver.d.ts +73 -73
- package/src/view/observer/domeventobserver.js +79 -79
- package/src/view/observer/fakeselectionobserver.d.ts +47 -47
- package/src/view/observer/fakeselectionobserver.js +91 -91
- package/src/view/observer/focusobserver.d.ts +82 -82
- package/src/view/observer/focusobserver.js +86 -86
- package/src/view/observer/inputobserver.d.ts +86 -86
- package/src/view/observer/inputobserver.js +164 -164
- package/src/view/observer/keyobserver.d.ts +66 -66
- package/src/view/observer/keyobserver.js +39 -39
- package/src/view/observer/mouseobserver.d.ts +89 -89
- package/src/view/observer/mouseobserver.js +29 -29
- package/src/view/observer/mutationobserver.d.ts +86 -86
- package/src/view/observer/mutationobserver.js +206 -206
- package/src/view/observer/observer.d.ts +89 -89
- package/src/view/observer/observer.js +84 -84
- package/src/view/observer/selectionobserver.d.ts +148 -148
- package/src/view/observer/selectionobserver.js +202 -202
- package/src/view/observer/tabobserver.d.ts +46 -46
- package/src/view/observer/tabobserver.js +42 -42
- package/src/view/placeholder.d.ts +85 -85
- package/src/view/placeholder.js +230 -230
- package/src/view/position.d.ts +189 -189
- package/src/view/position.js +324 -324
- package/src/view/range.d.ts +279 -279
- package/src/view/range.js +430 -430
- package/src/view/rawelement.d.ts +73 -73
- package/src/view/rawelement.js +105 -105
- package/src/view/renderer.d.ts +265 -265
- package/src/view/renderer.js +1000 -1000
- package/src/view/rooteditableelement.d.ts +41 -41
- package/src/view/rooteditableelement.js +69 -69
- package/src/view/selection.d.ts +375 -375
- package/src/view/selection.js +559 -559
- package/src/view/styles/background.d.ts +33 -33
- package/src/view/styles/background.js +74 -74
- package/src/view/styles/border.d.ts +43 -43
- package/src/view/styles/border.js +316 -316
- package/src/view/styles/margin.d.ts +29 -29
- package/src/view/styles/margin.js +34 -34
- package/src/view/styles/padding.d.ts +29 -29
- package/src/view/styles/padding.js +34 -34
- package/src/view/styles/utils.d.ts +93 -93
- package/src/view/styles/utils.js +219 -219
- package/src/view/stylesmap.d.ts +675 -675
- package/src/view/stylesmap.js +766 -766
- package/src/view/text.d.ts +74 -74
- package/src/view/text.js +93 -93
- package/src/view/textproxy.d.ts +97 -97
- package/src/view/textproxy.js +124 -124
- package/src/view/treewalker.d.ts +195 -195
- package/src/view/treewalker.js +327 -327
- package/src/view/typecheckable.d.ts +448 -448
- package/src/view/typecheckable.js +19 -19
- package/src/view/uielement.d.ts +96 -96
- package/src/view/uielement.js +182 -182
- package/src/view/upcastwriter.d.ts +417 -417
- package/src/view/upcastwriter.js +359 -359
- package/src/view/view.d.ts +480 -480
- package/src/view/view.js +534 -534
package/src/model/differ.js
CHANGED
|
@@ -1,1090 +1,1090 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module engine/model/differ
|
|
7
|
-
*/
|
|
8
|
-
import Position from './position';
|
|
9
|
-
import Range from './range';
|
|
10
|
-
/**
|
|
11
|
-
* Calculates the difference between two model states.
|
|
12
|
-
*
|
|
13
|
-
* Receives operations that are to be applied on the model document. Marks parts of the model document tree which
|
|
14
|
-
* are changed and saves the state of these elements before the change. Then, it compares saved elements with the
|
|
15
|
-
* changed elements, after all changes are applied on the model document. Calculates the diff between saved
|
|
16
|
-
* elements and new ones and returns a change set.
|
|
17
|
-
*/
|
|
18
|
-
export default class Differ {
|
|
19
|
-
/**
|
|
20
|
-
* Creates a `Differ` instance.
|
|
21
|
-
*
|
|
22
|
-
* @param markerCollection Model's marker collection.
|
|
23
|
-
*/
|
|
24
|
-
constructor(markerCollection) {
|
|
25
|
-
/**
|
|
26
|
-
* A map that stores changes that happened in a given element.
|
|
27
|
-
*
|
|
28
|
-
* The keys of the map are references to the model elements.
|
|
29
|
-
* The values of the map are arrays with changes that were done on this element.
|
|
30
|
-
*/
|
|
31
|
-
this._changesInElement = new Map();
|
|
32
|
-
/**
|
|
33
|
-
* A map that stores "element's children snapshots". A snapshot is representing children of a given element before
|
|
34
|
-
* the first change was applied on that element. Snapshot items are objects with two properties: `name`,
|
|
35
|
-
* containing the element name (or `'$text'` for a text node) and `attributes` which is a map of the node's attributes.
|
|
36
|
-
*/
|
|
37
|
-
this._elementSnapshots = new Map();
|
|
38
|
-
/**
|
|
39
|
-
* A map that stores all changed markers.
|
|
40
|
-
*
|
|
41
|
-
* The keys of the map are marker names.
|
|
42
|
-
* The values of the map are objects with the following properties:
|
|
43
|
-
* - `oldMarkerData`,
|
|
44
|
-
* - `newMarkerData`.
|
|
45
|
-
*/
|
|
46
|
-
this._changedMarkers = new Map();
|
|
47
|
-
/**
|
|
48
|
-
* A map that stores all roots that have been changed.
|
|
49
|
-
*
|
|
50
|
-
* The keys are the names of the roots while value represents the changes.
|
|
51
|
-
*/
|
|
52
|
-
this._changedRoots = new Map();
|
|
53
|
-
/**
|
|
54
|
-
* Stores the number of changes that were processed. Used to order the changes chronologically. It is important
|
|
55
|
-
* when changes are sorted.
|
|
56
|
-
*/
|
|
57
|
-
this._changeCount = 0;
|
|
58
|
-
/**
|
|
59
|
-
* For efficiency purposes, `Differ` stores the change set returned by the differ after {@link #getChanges} call.
|
|
60
|
-
* Cache is reset each time a new operation is buffered. If the cache has not been reset, {@link #getChanges} will
|
|
61
|
-
* return the cached value instead of calculating it again.
|
|
62
|
-
*
|
|
63
|
-
* This property stores those changes that did not take place in graveyard root.
|
|
64
|
-
*/
|
|
65
|
-
this._cachedChanges = null;
|
|
66
|
-
/**
|
|
67
|
-
* For efficiency purposes, `Differ` stores the change set returned by the differ after the {@link #getChanges} call.
|
|
68
|
-
* The cache is reset each time a new operation is buffered. If the cache has not been reset, {@link #getChanges} will
|
|
69
|
-
* return the cached value instead of calculating it again.
|
|
70
|
-
*
|
|
71
|
-
* This property stores all changes evaluated by `Differ`, including those that took place in the graveyard.
|
|
72
|
-
*/
|
|
73
|
-
this._cachedChangesWithGraveyard = null;
|
|
74
|
-
/**
|
|
75
|
-
* Set of model items that were marked to get refreshed in {@link #_refreshItem}.
|
|
76
|
-
*/
|
|
77
|
-
this._refreshedItems = new Set();
|
|
78
|
-
this._markerCollection = markerCollection;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Informs whether there are any changes buffered in `Differ`.
|
|
82
|
-
*/
|
|
83
|
-
get isEmpty() {
|
|
84
|
-
return this._changesInElement.size == 0 && this._changedMarkers.size == 0 && this._changedRoots.size == 0;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Buffers the given operation. An operation has to be buffered before it is executed.
|
|
88
|
-
*
|
|
89
|
-
* @param operationToBuffer An operation to buffer.
|
|
90
|
-
*/
|
|
91
|
-
bufferOperation(operationToBuffer) {
|
|
92
|
-
// Below we take an operation, check its type, then use its parameters in marking (private) methods.
|
|
93
|
-
// The general rule is to not mark elements inside inserted element. All inserted elements are re-rendered.
|
|
94
|
-
// Marking changes in them would cause a "double" changing then.
|
|
95
|
-
//
|
|
96
|
-
const operation = operationToBuffer;
|
|
97
|
-
switch (operation.type) {
|
|
98
|
-
case 'insert': {
|
|
99
|
-
if (this._isInInsertedElement(operation.position.parent)) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
this._markInsert(operation.position.parent, operation.position.offset, operation.nodes.maxOffset);
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
case 'addAttribute':
|
|
106
|
-
case 'removeAttribute':
|
|
107
|
-
case 'changeAttribute': {
|
|
108
|
-
for (const item of operation.range.getItems({ shallow: true })) {
|
|
109
|
-
if (this._isInInsertedElement(item.parent)) {
|
|
110
|
-
continue;
|
|
111
|
-
}
|
|
112
|
-
this._markAttribute(item);
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
case 'remove':
|
|
117
|
-
case 'move':
|
|
118
|
-
case 'reinsert': {
|
|
119
|
-
// When range is moved to the same position then not mark it as a change.
|
|
120
|
-
// See: https://github.com/ckeditor/ckeditor5-engine/issues/1664.
|
|
121
|
-
if (operation.sourcePosition.isEqual(operation.targetPosition) ||
|
|
122
|
-
operation.sourcePosition.getShiftedBy(operation.howMany).isEqual(operation.targetPosition)) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
const sourceParentInserted = this._isInInsertedElement(operation.sourcePosition.parent);
|
|
126
|
-
const targetParentInserted = this._isInInsertedElement(operation.targetPosition.parent);
|
|
127
|
-
if (!sourceParentInserted) {
|
|
128
|
-
this._markRemove(operation.sourcePosition.parent, operation.sourcePosition.offset, operation.howMany);
|
|
129
|
-
}
|
|
130
|
-
if (!targetParentInserted) {
|
|
131
|
-
this._markInsert(operation.targetPosition.parent, operation.getMovedRangeStart().offset, operation.howMany);
|
|
132
|
-
}
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
case 'rename': {
|
|
136
|
-
if (this._isInInsertedElement(operation.position.parent)) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
this._markRemove(operation.position.parent, operation.position.offset, 1);
|
|
140
|
-
this._markInsert(operation.position.parent, operation.position.offset, 1);
|
|
141
|
-
const range = Range._createFromPositionAndShift(operation.position, 1);
|
|
142
|
-
for (const marker of this._markerCollection.getMarkersIntersectingRange(range)) {
|
|
143
|
-
const markerData = marker.getData();
|
|
144
|
-
this.bufferMarkerChange(marker.name, markerData, markerData);
|
|
145
|
-
}
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
case 'split': {
|
|
149
|
-
const splitElement = operation.splitPosition.parent;
|
|
150
|
-
// Mark that children of the split element were removed.
|
|
151
|
-
if (!this._isInInsertedElement(splitElement)) {
|
|
152
|
-
this._markRemove(splitElement, operation.splitPosition.offset, operation.howMany);
|
|
153
|
-
}
|
|
154
|
-
// Mark that the new element (split copy) was inserted.
|
|
155
|
-
if (!this._isInInsertedElement(operation.insertionPosition.parent)) {
|
|
156
|
-
this._markInsert(operation.insertionPosition.parent, operation.insertionPosition.offset, 1);
|
|
157
|
-
}
|
|
158
|
-
// If the split took the element from the graveyard, mark that the element from the graveyard was removed.
|
|
159
|
-
if (operation.graveyardPosition) {
|
|
160
|
-
this._markRemove(operation.graveyardPosition.parent, operation.graveyardPosition.offset, 1);
|
|
161
|
-
}
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
case 'merge': {
|
|
165
|
-
// Mark that the merged element was removed.
|
|
166
|
-
const mergedElement = operation.sourcePosition.parent;
|
|
167
|
-
if (!this._isInInsertedElement(mergedElement.parent)) {
|
|
168
|
-
this._markRemove(mergedElement.parent, mergedElement.startOffset, 1);
|
|
169
|
-
}
|
|
170
|
-
// Mark that the merged element was inserted into graveyard.
|
|
171
|
-
const graveyardParent = operation.graveyardPosition.parent;
|
|
172
|
-
this._markInsert(graveyardParent, operation.graveyardPosition.offset, 1);
|
|
173
|
-
// Mark that children of merged element were inserted at new parent.
|
|
174
|
-
const mergedIntoElement = operation.targetPosition.parent;
|
|
175
|
-
if (!this._isInInsertedElement(mergedIntoElement)) {
|
|
176
|
-
this._markInsert(mergedIntoElement, operation.targetPosition.offset, mergedElement.maxOffset);
|
|
177
|
-
}
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
case 'detachRoot':
|
|
181
|
-
case 'addRoot': {
|
|
182
|
-
this._bufferRootStateChange(operation.rootName, operation.isAdd);
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
185
|
-
case 'addRootAttribute':
|
|
186
|
-
case 'removeRootAttribute':
|
|
187
|
-
case 'changeRootAttribute': {
|
|
188
|
-
const rootName = operation.root.rootName;
|
|
189
|
-
this._bufferRootAttributeChange(rootName, operation.key, operation.oldValue, operation.newValue);
|
|
190
|
-
break;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
// Clear cache after each buffered operation as it is no longer valid.
|
|
194
|
-
this._cachedChanges = null;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Buffers a marker change.
|
|
198
|
-
*
|
|
199
|
-
* @param markerName The name of the marker that changed.
|
|
200
|
-
* @param oldMarkerData Marker data before the change.
|
|
201
|
-
* @param newMarkerData Marker data after the change.
|
|
202
|
-
*/
|
|
203
|
-
bufferMarkerChange(markerName, oldMarkerData, newMarkerData) {
|
|
204
|
-
const buffered = this._changedMarkers.get(markerName);
|
|
205
|
-
if (!buffered) {
|
|
206
|
-
this._changedMarkers.set(markerName, {
|
|
207
|
-
newMarkerData,
|
|
208
|
-
oldMarkerData
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
buffered.newMarkerData = newMarkerData;
|
|
213
|
-
if (buffered.oldMarkerData.range == null && newMarkerData.range == null) {
|
|
214
|
-
// The marker is going to be removed (`newMarkerData.range == null`) but it did not exist before the first buffered change
|
|
215
|
-
// (`buffered.oldMarkerData.range == null`). In this case, do not keep the marker in buffer at all.
|
|
216
|
-
this._changedMarkers.delete(markerName);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Returns all markers that should be removed as a result of buffered changes.
|
|
222
|
-
*
|
|
223
|
-
* @returns Markers to remove. Each array item is an object containing the `name` and `range` properties.
|
|
224
|
-
*/
|
|
225
|
-
getMarkersToRemove() {
|
|
226
|
-
const result = [];
|
|
227
|
-
for (const [name, change] of this._changedMarkers) {
|
|
228
|
-
if (change.oldMarkerData.range != null) {
|
|
229
|
-
result.push({ name, range: change.oldMarkerData.range });
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return result;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Returns all markers which should be added as a result of buffered changes.
|
|
236
|
-
*
|
|
237
|
-
* @returns Markers to add. Each array item is an object containing the `name` and `range` properties.
|
|
238
|
-
*/
|
|
239
|
-
getMarkersToAdd() {
|
|
240
|
-
const result = [];
|
|
241
|
-
for (const [name, change] of this._changedMarkers) {
|
|
242
|
-
if (change.newMarkerData.range != null) {
|
|
243
|
-
result.push({ name, range: change.newMarkerData.range });
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return result;
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Returns all markers which changed.
|
|
250
|
-
*/
|
|
251
|
-
getChangedMarkers() {
|
|
252
|
-
return Array.from(this._changedMarkers).map(([name, change]) => ({
|
|
253
|
-
name,
|
|
254
|
-
data: {
|
|
255
|
-
oldRange: change.oldMarkerData.range,
|
|
256
|
-
newRange: change.newMarkerData.range
|
|
257
|
-
}
|
|
258
|
-
}));
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Checks whether some of the buffered changes affect the editor data.
|
|
262
|
-
*
|
|
263
|
-
* Types of changes which affect the editor data:
|
|
264
|
-
*
|
|
265
|
-
* * model structure changes,
|
|
266
|
-
* * attribute changes,
|
|
267
|
-
* * a root is added or detached,
|
|
268
|
-
* * changes of markers which were defined as `affectsData`,
|
|
269
|
-
* * changes of markers' `affectsData` property.
|
|
270
|
-
*/
|
|
271
|
-
hasDataChanges() {
|
|
272
|
-
if (this._changesInElement.size > 0) {
|
|
273
|
-
return true;
|
|
274
|
-
}
|
|
275
|
-
if (this._changedRoots.size > 0) {
|
|
276
|
-
return true;
|
|
277
|
-
}
|
|
278
|
-
for (const { newMarkerData, oldMarkerData } of this._changedMarkers.values()) {
|
|
279
|
-
if (newMarkerData.affectsData !== oldMarkerData.affectsData) {
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
if (newMarkerData.affectsData) {
|
|
283
|
-
const markerAdded = newMarkerData.range && !oldMarkerData.range;
|
|
284
|
-
const markerRemoved = !newMarkerData.range && oldMarkerData.range;
|
|
285
|
-
const markerChanged = newMarkerData.range && oldMarkerData.range && !newMarkerData.range.isEqual(oldMarkerData.range);
|
|
286
|
-
if (markerAdded || markerRemoved || markerChanged) {
|
|
287
|
-
return true;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
return false;
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Calculates the diff between the old model tree state (the state before the first buffered operations since the last {@link #reset}
|
|
295
|
-
* call) and the new model tree state (actual one). It should be called after all buffered operations are executed.
|
|
296
|
-
*
|
|
297
|
-
* The diff set is returned as an array of {@link module:engine/model/differ~DiffItem diff items}, each describing a change done
|
|
298
|
-
* on the model. The items are sorted by the position on which the change happened. If a position
|
|
299
|
-
* {@link module:engine/model/position~Position#isBefore is before} another one, it will be on an earlier index in the diff set.
|
|
300
|
-
*
|
|
301
|
-
* **Note**: Elements inside inserted element will not have a separate diff item, only the top most element change will be reported.
|
|
302
|
-
*
|
|
303
|
-
* Because calculating the diff is a costly operation, the result is cached. If no new operation was buffered since the
|
|
304
|
-
* previous {@link #getChanges} call, the next call will return the cached value.
|
|
305
|
-
*
|
|
306
|
-
* @param options Additional options.
|
|
307
|
-
* @param options.includeChangesInGraveyard If set to `true`, also changes that happened
|
|
308
|
-
* in the graveyard root will be returned. By default, changes in the graveyard root are not returned.
|
|
309
|
-
* @returns Diff between the old and the new model tree state.
|
|
310
|
-
*/
|
|
311
|
-
getChanges(options = {}) {
|
|
312
|
-
// If there are cached changes, just return them instead of calculating changes again.
|
|
313
|
-
if (this._cachedChanges) {
|
|
314
|
-
if (options.includeChangesInGraveyard) {
|
|
315
|
-
return this._cachedChangesWithGraveyard.slice();
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
return this._cachedChanges.slice();
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
// Will contain returned results.
|
|
322
|
-
let diffSet = [];
|
|
323
|
-
// Check all changed elements.
|
|
324
|
-
for (const element of this._changesInElement.keys()) {
|
|
325
|
-
// Get changes for this element and sort them.
|
|
326
|
-
const changes = this._changesInElement.get(element).sort((a, b) => {
|
|
327
|
-
if (a.offset === b.offset) {
|
|
328
|
-
if (a.type != b.type) {
|
|
329
|
-
// If there are multiple changes at the same position, "remove" change should be first.
|
|
330
|
-
// If the order is different, for example, we would first add some nodes and then removed them
|
|
331
|
-
// (instead of the nodes that we should remove).
|
|
332
|
-
return a.type == 'remove' ? -1 : 1;
|
|
333
|
-
}
|
|
334
|
-
return 0;
|
|
335
|
-
}
|
|
336
|
-
return a.offset < b.offset ? -1 : 1;
|
|
337
|
-
});
|
|
338
|
-
// Get children of this element before any change was applied on it.
|
|
339
|
-
const snapshotChildren = this._elementSnapshots.get(element);
|
|
340
|
-
// Get snapshot of current element's children.
|
|
341
|
-
const elementChildren = _getChildrenSnapshot(element.getChildren());
|
|
342
|
-
// Generate actions basing on changes done on element.
|
|
343
|
-
const actions = _generateActionsFromChanges(snapshotChildren.length, changes);
|
|
344
|
-
let i = 0; // Iterator in `elementChildren` array -- iterates through current children of element.
|
|
345
|
-
let j = 0; // Iterator in `snapshotChildren` array -- iterates through old children of element.
|
|
346
|
-
// Process every action.
|
|
347
|
-
for (const action of actions) {
|
|
348
|
-
if (action === 'i') {
|
|
349
|
-
// Generate diff item for this element and insert it into the diff set.
|
|
350
|
-
diffSet.push(this._getInsertDiff(element, i, elementChildren[i]));
|
|
351
|
-
i++;
|
|
352
|
-
}
|
|
353
|
-
else if (action === 'r') {
|
|
354
|
-
// Generate diff item for this element and insert it into the diff set.
|
|
355
|
-
diffSet.push(this._getRemoveDiff(element, i, snapshotChildren[j]));
|
|
356
|
-
j++;
|
|
357
|
-
}
|
|
358
|
-
else if (action === 'a') {
|
|
359
|
-
// Take attributes from saved and current children.
|
|
360
|
-
const elementAttributes = elementChildren[i].attributes;
|
|
361
|
-
const snapshotAttributes = snapshotChildren[j].attributes;
|
|
362
|
-
let range;
|
|
363
|
-
if (elementChildren[i].name == '$text') {
|
|
364
|
-
range = new Range(Position._createAt(element, i), Position._createAt(element, i + 1));
|
|
365
|
-
}
|
|
366
|
-
else {
|
|
367
|
-
const index = element.offsetToIndex(i);
|
|
368
|
-
range = new Range(Position._createAt(element, i), Position._createAt(element.getChild(index), 0));
|
|
369
|
-
}
|
|
370
|
-
// Generate diff items for this change (there might be multiple attributes changed and
|
|
371
|
-
// there is a single diff for each of them) and insert them into the diff set.
|
|
372
|
-
diffSet.push(...this._getAttributesDiff(range, snapshotAttributes, elementAttributes));
|
|
373
|
-
i++;
|
|
374
|
-
j++;
|
|
375
|
-
}
|
|
376
|
-
else {
|
|
377
|
-
// `action` is 'equal'. Child not changed.
|
|
378
|
-
i++;
|
|
379
|
-
j++;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
// Then, sort the changes by the position (change at position before other changes is first).
|
|
384
|
-
diffSet.sort((a, b) => {
|
|
385
|
-
// If the change is in different root, we don't care much, but we'd like to have all changes in given
|
|
386
|
-
// root "together" in the array. So let's just sort them by the root name. It does not matter which root
|
|
387
|
-
// will be processed first.
|
|
388
|
-
if (a.position.root != b.position.root) {
|
|
389
|
-
return a.position.root.rootName < b.position.root.rootName ? -1 : 1;
|
|
390
|
-
}
|
|
391
|
-
// If change happens at the same position...
|
|
392
|
-
if (a.position.isEqual(b.position)) {
|
|
393
|
-
// Keep chronological order of operations.
|
|
394
|
-
return a.changeCount - b.changeCount;
|
|
395
|
-
}
|
|
396
|
-
// If positions differ, position "on the left" should be earlier in the result.
|
|
397
|
-
return a.position.isBefore(b.position) ? -1 : 1;
|
|
398
|
-
});
|
|
399
|
-
// Glue together multiple changes (mostly on text nodes).
|
|
400
|
-
for (let i = 1, prevIndex = 0; i < diffSet.length; i++) {
|
|
401
|
-
const prevDiff = diffSet[prevIndex];
|
|
402
|
-
const thisDiff = diffSet[i];
|
|
403
|
-
// Glue remove changes if they happen on text on same position.
|
|
404
|
-
const isConsecutiveTextRemove = prevDiff.type == 'remove' && thisDiff.type == 'remove' &&
|
|
405
|
-
prevDiff.name == '$text' && thisDiff.name == '$text' &&
|
|
406
|
-
prevDiff.position.isEqual(thisDiff.position);
|
|
407
|
-
// Glue insert changes if they happen on text on consecutive fragments.
|
|
408
|
-
const isConsecutiveTextAdd = prevDiff.type == 'insert' && thisDiff.type == 'insert' &&
|
|
409
|
-
prevDiff.name == '$text' && thisDiff.name == '$text' &&
|
|
410
|
-
prevDiff.position.parent == thisDiff.position.parent &&
|
|
411
|
-
prevDiff.position.offset + prevDiff.length == thisDiff.position.offset;
|
|
412
|
-
// Glue attribute changes if they happen on consecutive fragments and have same key, old value and new value.
|
|
413
|
-
const isConsecutiveAttributeChange = prevDiff.type == 'attribute' && thisDiff.type == 'attribute' &&
|
|
414
|
-
prevDiff.position.parent == thisDiff.position.parent &&
|
|
415
|
-
prevDiff.range.isFlat && thisDiff.range.isFlat &&
|
|
416
|
-
(prevDiff.position.offset + prevDiff.length) == thisDiff.position.offset &&
|
|
417
|
-
prevDiff.attributeKey == thisDiff.attributeKey &&
|
|
418
|
-
prevDiff.attributeOldValue == thisDiff.attributeOldValue &&
|
|
419
|
-
prevDiff.attributeNewValue == thisDiff.attributeNewValue;
|
|
420
|
-
if (isConsecutiveTextRemove || isConsecutiveTextAdd || isConsecutiveAttributeChange) {
|
|
421
|
-
prevDiff.length++;
|
|
422
|
-
if (isConsecutiveAttributeChange) {
|
|
423
|
-
prevDiff.range.end = prevDiff.range.end.getShiftedBy(1);
|
|
424
|
-
}
|
|
425
|
-
diffSet[i] = null;
|
|
426
|
-
}
|
|
427
|
-
else {
|
|
428
|
-
prevIndex = i;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
diffSet = diffSet.filter(v => v);
|
|
432
|
-
// Remove `changeCount` property from diff items. It is used only for sorting and is internal thing.
|
|
433
|
-
for (const item of diffSet) {
|
|
434
|
-
delete item.changeCount;
|
|
435
|
-
if (item.type == 'attribute') {
|
|
436
|
-
delete item.position;
|
|
437
|
-
delete item.length;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
this._changeCount = 0;
|
|
441
|
-
// Cache changes.
|
|
442
|
-
this._cachedChangesWithGraveyard = diffSet;
|
|
443
|
-
this._cachedChanges = diffSet.filter(_changesInGraveyardFilter);
|
|
444
|
-
if (options.includeChangesInGraveyard) {
|
|
445
|
-
return this._cachedChangesWithGraveyard.slice();
|
|
446
|
-
}
|
|
447
|
-
else {
|
|
448
|
-
return this._cachedChanges.slice();
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Returns all roots that have changed (either were attached, or detached, or their attributes changed).
|
|
453
|
-
*
|
|
454
|
-
* @returns Diff between the old and the new roots state.
|
|
455
|
-
*/
|
|
456
|
-
getChangedRoots() {
|
|
457
|
-
return Array.from(this._changedRoots.values()).map(diffItem => {
|
|
458
|
-
const entry = { ...diffItem };
|
|
459
|
-
if (entry.state !== undefined) {
|
|
460
|
-
// The root was attached or detached -- do not return its attributes changes.
|
|
461
|
-
// If the root was attached, it should be handled as a whole, together with its attributes, the same way as model nodes.
|
|
462
|
-
// If the root was detached, its attributes should be discarded anyway.
|
|
463
|
-
//
|
|
464
|
-
// Keep in mind that filtering must happen on this stage (when retrieving changes). If filtering happens on-the-fly as
|
|
465
|
-
// the attributes change, it may lead to incorrect situation, e.g.: detach root, change attribute, re-attach root.
|
|
466
|
-
// In this case, attribute change cannot be filtered. After the root is re-attached, the attribute change must be kept.
|
|
467
|
-
delete entry.attributes;
|
|
468
|
-
}
|
|
469
|
-
return entry;
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
/**
|
|
473
|
-
* Returns a set of model items that were marked to get refreshed.
|
|
474
|
-
*/
|
|
475
|
-
getRefreshedItems() {
|
|
476
|
-
return new Set(this._refreshedItems);
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Resets `Differ`. Removes all buffered changes.
|
|
480
|
-
*/
|
|
481
|
-
reset() {
|
|
482
|
-
this._changesInElement.clear();
|
|
483
|
-
this._elementSnapshots.clear();
|
|
484
|
-
this._changedMarkers.clear();
|
|
485
|
-
this._changedRoots.clear();
|
|
486
|
-
this._refreshedItems = new Set();
|
|
487
|
-
this._cachedChanges = null;
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Buffers the root state change after the root was attached or detached
|
|
491
|
-
*/
|
|
492
|
-
_bufferRootStateChange(rootName, isAttached) {
|
|
493
|
-
if (!this._changedRoots.has(rootName)) {
|
|
494
|
-
this._changedRoots.set(rootName, { name: rootName, state: isAttached ? 'attached' : 'detached' });
|
|
495
|
-
return;
|
|
496
|
-
}
|
|
497
|
-
const diffItem = this._changedRoots.get(rootName);
|
|
498
|
-
if (diffItem.state !== undefined) {
|
|
499
|
-
// Root `state` can only toggle between of the values ('attached' or 'detached') and no value. It cannot be any other way,
|
|
500
|
-
// because if the root was originally attached it can only become detached. Then, if it is re-attached in the same batch of
|
|
501
|
-
// changes, it gets back to "no change" (which means no value). Same if the root was originally detached.
|
|
502
|
-
delete diffItem.state;
|
|
503
|
-
if (diffItem.attributes === undefined) {
|
|
504
|
-
// If there is no `state` change and no `attributes` change, remove the entry.
|
|
505
|
-
this._changedRoots.delete(rootName);
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
else {
|
|
509
|
-
diffItem.state = isAttached ? 'attached' : 'detached';
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
/**
|
|
513
|
-
* Buffers a root attribute change.
|
|
514
|
-
*/
|
|
515
|
-
_bufferRootAttributeChange(rootName, key, oldValue, newValue) {
|
|
516
|
-
const diffItem = this._changedRoots.get(rootName) || { name: rootName };
|
|
517
|
-
const attrs = diffItem.attributes || {};
|
|
518
|
-
if (attrs[key]) {
|
|
519
|
-
// If this attribute or metadata was already changed earlier and is changed again, check to what value it is changed.
|
|
520
|
-
const attrEntry = attrs[key];
|
|
521
|
-
if (newValue === attrEntry.oldValue) {
|
|
522
|
-
// If it was changed back to the old value, remove the entry.
|
|
523
|
-
delete attrs[key];
|
|
524
|
-
}
|
|
525
|
-
else {
|
|
526
|
-
// If it was changed to a different value, update the entry.
|
|
527
|
-
attrEntry.newValue = newValue;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
// If this attribute or metadata was not set earlier, add an entry.
|
|
532
|
-
attrs[key] = { oldValue, newValue };
|
|
533
|
-
}
|
|
534
|
-
if (Object.entries(attrs).length === 0) {
|
|
535
|
-
// If attributes or metadata changes set became empty, remove it from the diff item.
|
|
536
|
-
delete diffItem.attributes;
|
|
537
|
-
if (diffItem.state === undefined) {
|
|
538
|
-
// If there is no `state` change and no `attributes` change, remove the entry.
|
|
539
|
-
this._changedRoots.delete(rootName);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
else {
|
|
543
|
-
// Make sure that, if a new object in the structure was created, it gets set.
|
|
544
|
-
diffItem.attributes = attrs;
|
|
545
|
-
this._changedRoots.set(rootName, diffItem);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Marks the given `item` in differ to be "refreshed". It means that the item will be marked as removed and inserted
|
|
550
|
-
* in the differ changes set, so it will be effectively re-converted when the differ changes are handled by a dispatcher.
|
|
551
|
-
*
|
|
552
|
-
* @internal
|
|
553
|
-
* @param item Item to refresh.
|
|
554
|
-
*/
|
|
555
|
-
_refreshItem(item) {
|
|
556
|
-
if (this._isInInsertedElement(item.parent)) {
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
this._markRemove(item.parent, item.startOffset, item.offsetSize);
|
|
560
|
-
this._markInsert(item.parent, item.startOffset, item.offsetSize);
|
|
561
|
-
this._refreshedItems.add(item);
|
|
562
|
-
const range = Range._createOn(item);
|
|
563
|
-
for (const marker of this._markerCollection.getMarkersIntersectingRange(range)) {
|
|
564
|
-
const markerData = marker.getData();
|
|
565
|
-
this.bufferMarkerChange(marker.name, markerData, markerData);
|
|
566
|
-
}
|
|
567
|
-
// Clear cache after each buffered operation as it is no longer valid.
|
|
568
|
-
this._cachedChanges = null;
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* Saves and handles an insert change.
|
|
572
|
-
*/
|
|
573
|
-
_markInsert(parent, offset, howMany) {
|
|
574
|
-
const changeItem = { type: 'insert', offset, howMany, count: this._changeCount++ };
|
|
575
|
-
this._markChange(parent, changeItem);
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Saves and handles a remove change.
|
|
579
|
-
*/
|
|
580
|
-
_markRemove(parent, offset, howMany) {
|
|
581
|
-
const changeItem = { type: 'remove', offset, howMany, count: this._changeCount++ };
|
|
582
|
-
this._markChange(parent, changeItem);
|
|
583
|
-
this._removeAllNestedChanges(parent, offset, howMany);
|
|
584
|
-
}
|
|
585
|
-
/**
|
|
586
|
-
* Saves and handles an attribute change.
|
|
587
|
-
*/
|
|
588
|
-
_markAttribute(item) {
|
|
589
|
-
const changeItem = { type: 'attribute', offset: item.startOffset, howMany: item.offsetSize, count: this._changeCount++ };
|
|
590
|
-
this._markChange(item.parent, changeItem);
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Saves and handles a model change.
|
|
594
|
-
*/
|
|
595
|
-
_markChange(parent, changeItem) {
|
|
596
|
-
// First, make a snapshot of this parent's children (it will be made only if it was not made before).
|
|
597
|
-
this._makeSnapshot(parent);
|
|
598
|
-
// Then, get all changes that already were done on the element (empty array if this is the first change).
|
|
599
|
-
const changes = this._getChangesForElement(parent);
|
|
600
|
-
// Then, look through all the changes, and transform them or the new change.
|
|
601
|
-
this._handleChange(changeItem, changes);
|
|
602
|
-
// Add the new change.
|
|
603
|
-
changes.push(changeItem);
|
|
604
|
-
// Remove incorrect changes. During transformation some change might be, for example, included in another.
|
|
605
|
-
// In that case, the change will have `howMany` property set to `0` or less. We need to remove those changes.
|
|
606
|
-
for (let i = 0; i < changes.length; i++) {
|
|
607
|
-
if (changes[i].howMany < 1) {
|
|
608
|
-
changes.splice(i, 1);
|
|
609
|
-
i--;
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
/**
|
|
614
|
-
* Gets an array of changes that have already been saved for a given element.
|
|
615
|
-
*/
|
|
616
|
-
_getChangesForElement(element) {
|
|
617
|
-
let changes;
|
|
618
|
-
if (this._changesInElement.has(element)) {
|
|
619
|
-
changes = this._changesInElement.get(element);
|
|
620
|
-
}
|
|
621
|
-
else {
|
|
622
|
-
changes = [];
|
|
623
|
-
this._changesInElement.set(element, changes);
|
|
624
|
-
}
|
|
625
|
-
return changes;
|
|
626
|
-
}
|
|
627
|
-
/**
|
|
628
|
-
* Saves a children snapshot for a given element.
|
|
629
|
-
*/
|
|
630
|
-
_makeSnapshot(element) {
|
|
631
|
-
if (!this._elementSnapshots.has(element)) {
|
|
632
|
-
this._elementSnapshots.set(element, _getChildrenSnapshot(element.getChildren()));
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* For a given newly saved change, compares it with a change already done on the element and modifies the incoming
|
|
637
|
-
* change and/or the old change.
|
|
638
|
-
*
|
|
639
|
-
* @param inc Incoming (new) change.
|
|
640
|
-
* @param changes An array containing all the changes done on that element.
|
|
641
|
-
*/
|
|
642
|
-
_handleChange(inc, changes) {
|
|
643
|
-
// We need a helper variable that will store how many nodes are to be still handled for this change item.
|
|
644
|
-
// `nodesToHandle` (how many nodes still need to be handled) and `howMany` (how many nodes were affected)
|
|
645
|
-
// needs to be differentiated.
|
|
646
|
-
//
|
|
647
|
-
// This comes up when there are multiple changes that are affected by `inc` change item.
|
|
648
|
-
//
|
|
649
|
-
// For example: assume two insert changes: `{ offset: 2, howMany: 1 }` and `{ offset: 5, howMany: 1 }`.
|
|
650
|
-
// Assume that `inc` change is remove `{ offset: 2, howMany: 2, nodesToHandle: 2 }`.
|
|
651
|
-
//
|
|
652
|
-
// Then, we:
|
|
653
|
-
// - "forget" about first insert change (it is "eaten" by remove),
|
|
654
|
-
// - because of that, at the end we will want to remove only one node (`nodesToHandle = 1`),
|
|
655
|
-
// - but still we have to change offset of the second insert change from `5` to `3`!
|
|
656
|
-
//
|
|
657
|
-
// So, `howMany` does not change throughout items transformation and keeps information about how many nodes were affected,
|
|
658
|
-
// while `nodesToHandle` means how many nodes need to be handled after the change item is transformed by other changes.
|
|
659
|
-
inc.nodesToHandle = inc.howMany;
|
|
660
|
-
for (const old of changes) {
|
|
661
|
-
const incEnd = inc.offset + inc.howMany;
|
|
662
|
-
const oldEnd = old.offset + old.howMany;
|
|
663
|
-
if (inc.type == 'insert') {
|
|
664
|
-
if (old.type == 'insert') {
|
|
665
|
-
if (inc.offset <= old.offset) {
|
|
666
|
-
old.offset += inc.howMany;
|
|
667
|
-
}
|
|
668
|
-
else if (inc.offset < oldEnd) {
|
|
669
|
-
old.howMany += inc.nodesToHandle;
|
|
670
|
-
inc.nodesToHandle = 0;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
if (old.type == 'remove') {
|
|
674
|
-
if (inc.offset < old.offset) {
|
|
675
|
-
old.offset += inc.howMany;
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
if (old.type == 'attribute') {
|
|
679
|
-
if (inc.offset <= old.offset) {
|
|
680
|
-
old.offset += inc.howMany;
|
|
681
|
-
}
|
|
682
|
-
else if (inc.offset < oldEnd) {
|
|
683
|
-
// This case is more complicated, because attribute change has to be split into two.
|
|
684
|
-
// Example (assume that uppercase and lowercase letters mean different attributes):
|
|
685
|
-
//
|
|
686
|
-
// initial state: abcxyz
|
|
687
|
-
// attribute change: aBCXYz
|
|
688
|
-
// incoming insert: aBCfooXYz
|
|
689
|
-
//
|
|
690
|
-
// Change ranges cannot intersect because each item has to be described exactly (it was either
|
|
691
|
-
// not changed, inserted, removed, or its attribute was changed). That's why old attribute
|
|
692
|
-
// change has to be split and both parts has to be handled separately from now on.
|
|
693
|
-
const howMany = old.howMany;
|
|
694
|
-
old.howMany = inc.offset - old.offset;
|
|
695
|
-
// Add the second part of attribute change to the beginning of processed array so it won't
|
|
696
|
-
// be processed again in this loop.
|
|
697
|
-
changes.unshift({
|
|
698
|
-
type: 'attribute',
|
|
699
|
-
offset: incEnd,
|
|
700
|
-
howMany: howMany - old.howMany,
|
|
701
|
-
count: this._changeCount++
|
|
702
|
-
});
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
if (inc.type == 'remove') {
|
|
707
|
-
if (old.type == 'insert') {
|
|
708
|
-
if (incEnd <= old.offset) {
|
|
709
|
-
old.offset -= inc.howMany;
|
|
710
|
-
}
|
|
711
|
-
else if (incEnd <= oldEnd) {
|
|
712
|
-
if (inc.offset < old.offset) {
|
|
713
|
-
const intersectionLength = incEnd - old.offset;
|
|
714
|
-
old.offset = inc.offset;
|
|
715
|
-
old.howMany -= intersectionLength;
|
|
716
|
-
inc.nodesToHandle -= intersectionLength;
|
|
717
|
-
}
|
|
718
|
-
else {
|
|
719
|
-
old.howMany -= inc.nodesToHandle;
|
|
720
|
-
inc.nodesToHandle = 0;
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
else {
|
|
724
|
-
if (inc.offset <= old.offset) {
|
|
725
|
-
inc.nodesToHandle -= old.howMany;
|
|
726
|
-
old.howMany = 0;
|
|
727
|
-
}
|
|
728
|
-
else if (inc.offset < oldEnd) {
|
|
729
|
-
const intersectionLength = oldEnd - inc.offset;
|
|
730
|
-
old.howMany -= intersectionLength;
|
|
731
|
-
inc.nodesToHandle -= intersectionLength;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
if (old.type == 'remove') {
|
|
736
|
-
if (incEnd <= old.offset) {
|
|
737
|
-
old.offset -= inc.howMany;
|
|
738
|
-
}
|
|
739
|
-
else if (inc.offset < old.offset) {
|
|
740
|
-
inc.nodesToHandle += old.howMany;
|
|
741
|
-
old.howMany = 0;
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
if (old.type == 'attribute') {
|
|
745
|
-
if (incEnd <= old.offset) {
|
|
746
|
-
old.offset -= inc.howMany;
|
|
747
|
-
}
|
|
748
|
-
else if (inc.offset < old.offset) {
|
|
749
|
-
const intersectionLength = incEnd - old.offset;
|
|
750
|
-
old.offset = inc.offset;
|
|
751
|
-
old.howMany -= intersectionLength;
|
|
752
|
-
}
|
|
753
|
-
else if (inc.offset < oldEnd) {
|
|
754
|
-
if (incEnd <= oldEnd) {
|
|
755
|
-
// On first sight in this case we don't need to split attribute operation into two.
|
|
756
|
-
// However the changes set is later converted to actions (see `_generateActionsFromChanges`).
|
|
757
|
-
// For that reason, no two changes may intersect.
|
|
758
|
-
// So we cannot have an attribute change that "contains" remove change.
|
|
759
|
-
// Attribute change needs to be split.
|
|
760
|
-
const howMany = old.howMany;
|
|
761
|
-
old.howMany = inc.offset - old.offset;
|
|
762
|
-
const howManyAfter = howMany - old.howMany - inc.nodesToHandle;
|
|
763
|
-
// Add the second part of attribute change to the beginning of processed array so it won't
|
|
764
|
-
// be processed again in this loop.
|
|
765
|
-
changes.unshift({
|
|
766
|
-
type: 'attribute',
|
|
767
|
-
offset: inc.offset,
|
|
768
|
-
howMany: howManyAfter,
|
|
769
|
-
count: this._changeCount++
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
|
-
else {
|
|
773
|
-
old.howMany -= oldEnd - inc.offset;
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
if (inc.type == 'attribute') {
|
|
779
|
-
// In case of attribute change, `howMany` should be kept same as `nodesToHandle`. It's not an error.
|
|
780
|
-
if (old.type == 'insert') {
|
|
781
|
-
if (inc.offset < old.offset && incEnd > old.offset) {
|
|
782
|
-
if (incEnd > oldEnd) {
|
|
783
|
-
// This case is similar to a case described when incoming change was insert and old change was attribute.
|
|
784
|
-
// See comment above.
|
|
785
|
-
//
|
|
786
|
-
// This time incoming change is attribute. We need to split incoming change in this case too.
|
|
787
|
-
// However this time, the second part of the attribute change needs to be processed further
|
|
788
|
-
// because there might be other changes that it collides with.
|
|
789
|
-
const attributePart = {
|
|
790
|
-
type: 'attribute',
|
|
791
|
-
offset: oldEnd,
|
|
792
|
-
howMany: incEnd - oldEnd,
|
|
793
|
-
count: this._changeCount++
|
|
794
|
-
};
|
|
795
|
-
this._handleChange(attributePart, changes);
|
|
796
|
-
changes.push(attributePart);
|
|
797
|
-
}
|
|
798
|
-
inc.nodesToHandle = old.offset - inc.offset;
|
|
799
|
-
inc.howMany = inc.nodesToHandle;
|
|
800
|
-
}
|
|
801
|
-
else if (inc.offset >= old.offset && inc.offset < oldEnd) {
|
|
802
|
-
if (incEnd > oldEnd) {
|
|
803
|
-
inc.nodesToHandle = incEnd - oldEnd;
|
|
804
|
-
inc.offset = oldEnd;
|
|
805
|
-
}
|
|
806
|
-
else {
|
|
807
|
-
inc.nodesToHandle = 0;
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
if (old.type == 'remove') {
|
|
812
|
-
// This is a case when attribute change "contains" remove change.
|
|
813
|
-
// The attribute change needs to be split into two because changes cannot intersect.
|
|
814
|
-
if (inc.offset < old.offset && incEnd > old.offset) {
|
|
815
|
-
const attributePart = {
|
|
816
|
-
type: 'attribute',
|
|
817
|
-
offset: old.offset,
|
|
818
|
-
howMany: incEnd - old.offset,
|
|
819
|
-
count: this._changeCount++
|
|
820
|
-
};
|
|
821
|
-
this._handleChange(attributePart, changes);
|
|
822
|
-
changes.push(attributePart);
|
|
823
|
-
inc.nodesToHandle = old.offset - inc.offset;
|
|
824
|
-
inc.howMany = inc.nodesToHandle;
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
if (old.type == 'attribute') {
|
|
828
|
-
// There are only two conflicting scenarios possible here:
|
|
829
|
-
if (inc.offset >= old.offset && incEnd <= oldEnd) {
|
|
830
|
-
// `old` change includes `inc` change, or they are the same.
|
|
831
|
-
inc.nodesToHandle = 0;
|
|
832
|
-
inc.howMany = 0;
|
|
833
|
-
inc.offset = 0;
|
|
834
|
-
}
|
|
835
|
-
else if (inc.offset <= old.offset && incEnd >= oldEnd) {
|
|
836
|
-
// `inc` change includes `old` change.
|
|
837
|
-
old.howMany = 0;
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
inc.howMany = inc.nodesToHandle;
|
|
843
|
-
delete inc.nodesToHandle;
|
|
844
|
-
}
|
|
845
|
-
/**
|
|
846
|
-
* Returns an object with a single insert change description.
|
|
847
|
-
*
|
|
848
|
-
* @param parent The element in which the change happened.
|
|
849
|
-
* @param offset The offset at which change happened.
|
|
850
|
-
* @param elementSnapshot The snapshot of the removed element a character.
|
|
851
|
-
* @returns The diff item.
|
|
852
|
-
*/
|
|
853
|
-
_getInsertDiff(parent, offset, elementSnapshot) {
|
|
854
|
-
return {
|
|
855
|
-
type: 'insert',
|
|
856
|
-
position: Position._createAt(parent, offset),
|
|
857
|
-
name: elementSnapshot.name,
|
|
858
|
-
attributes: new Map(elementSnapshot.attributes),
|
|
859
|
-
length: 1,
|
|
860
|
-
changeCount: this._changeCount++
|
|
861
|
-
};
|
|
862
|
-
}
|
|
863
|
-
/**
|
|
864
|
-
* Returns an object with a single remove change description.
|
|
865
|
-
*
|
|
866
|
-
* @param parent The element in which change happened.
|
|
867
|
-
* @param offset The offset at which change happened.
|
|
868
|
-
* @param elementSnapshot The snapshot of the removed element a character.
|
|
869
|
-
* @returns The diff item.
|
|
870
|
-
*/
|
|
871
|
-
_getRemoveDiff(parent, offset, elementSnapshot) {
|
|
872
|
-
return {
|
|
873
|
-
type: 'remove',
|
|
874
|
-
position: Position._createAt(parent, offset),
|
|
875
|
-
name: elementSnapshot.name,
|
|
876
|
-
attributes: new Map(elementSnapshot.attributes),
|
|
877
|
-
length: 1,
|
|
878
|
-
changeCount: this._changeCount++
|
|
879
|
-
};
|
|
880
|
-
}
|
|
881
|
-
/**
|
|
882
|
-
* Returns an array of objects where each one is a single attribute change description.
|
|
883
|
-
*
|
|
884
|
-
* @param range The range where the change happened.
|
|
885
|
-
* @param oldAttributes A map, map iterator or compatible object that contains attributes before the change.
|
|
886
|
-
* @param newAttributes A map, map iterator or compatible object that contains attributes after the change.
|
|
887
|
-
* @returns An array containing one or more diff items.
|
|
888
|
-
*/
|
|
889
|
-
_getAttributesDiff(range, oldAttributes, newAttributes) {
|
|
890
|
-
// Results holder.
|
|
891
|
-
const diffs = [];
|
|
892
|
-
// Clone new attributes as we will be performing changes on this object.
|
|
893
|
-
newAttributes = new Map(newAttributes);
|
|
894
|
-
// Look through old attributes.
|
|
895
|
-
for (const [key, oldValue] of oldAttributes) {
|
|
896
|
-
// Check what is the new value of the attribute (or if it was removed).
|
|
897
|
-
const newValue = newAttributes.has(key) ? newAttributes.get(key) : null;
|
|
898
|
-
// If values are different (or attribute was removed)...
|
|
899
|
-
if (newValue !== oldValue) {
|
|
900
|
-
// Add diff item.
|
|
901
|
-
diffs.push({
|
|
902
|
-
type: 'attribute',
|
|
903
|
-
position: range.start,
|
|
904
|
-
range: range.clone(),
|
|
905
|
-
length: 1,
|
|
906
|
-
attributeKey: key,
|
|
907
|
-
attributeOldValue: oldValue,
|
|
908
|
-
attributeNewValue: newValue,
|
|
909
|
-
changeCount: this._changeCount++
|
|
910
|
-
});
|
|
911
|
-
}
|
|
912
|
-
// Prevent returning two diff items for the same change.
|
|
913
|
-
newAttributes.delete(key);
|
|
914
|
-
}
|
|
915
|
-
// Look through new attributes that weren't handled above.
|
|
916
|
-
for (const [key, newValue] of newAttributes) {
|
|
917
|
-
// Each of them is a new attribute. Add diff item.
|
|
918
|
-
diffs.push({
|
|
919
|
-
type: 'attribute',
|
|
920
|
-
position: range.start,
|
|
921
|
-
range: range.clone(),
|
|
922
|
-
length: 1,
|
|
923
|
-
attributeKey: key,
|
|
924
|
-
attributeOldValue: null,
|
|
925
|
-
attributeNewValue: newValue,
|
|
926
|
-
changeCount: this._changeCount++
|
|
927
|
-
});
|
|
928
|
-
}
|
|
929
|
-
return diffs;
|
|
930
|
-
}
|
|
931
|
-
/**
|
|
932
|
-
* Checks whether given element or any of its parents is an element that is buffered as an inserted element.
|
|
933
|
-
*/
|
|
934
|
-
_isInInsertedElement(element) {
|
|
935
|
-
const parent = element.parent;
|
|
936
|
-
if (!parent) {
|
|
937
|
-
return false;
|
|
938
|
-
}
|
|
939
|
-
const changes = this._changesInElement.get(parent);
|
|
940
|
-
const offset = element.startOffset;
|
|
941
|
-
if (changes) {
|
|
942
|
-
for (const change of changes) {
|
|
943
|
-
if (change.type == 'insert' && offset >= change.offset && offset < change.offset + change.howMany) {
|
|
944
|
-
return true;
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
|
-
return this._isInInsertedElement(parent);
|
|
949
|
-
}
|
|
950
|
-
/**
|
|
951
|
-
* Removes deeply all buffered changes that are registered in elements from range specified by `parent`, `offset`
|
|
952
|
-
* and `howMany`.
|
|
953
|
-
*/
|
|
954
|
-
_removeAllNestedChanges(parent, offset, howMany) {
|
|
955
|
-
const range = new Range(Position._createAt(parent, offset), Position._createAt(parent, offset + howMany));
|
|
956
|
-
for (const item of range.getItems({ shallow: true })) {
|
|
957
|
-
if (item.is('element')) {
|
|
958
|
-
this._elementSnapshots.delete(item);
|
|
959
|
-
this._changesInElement.delete(item);
|
|
960
|
-
this._removeAllNestedChanges(item, 0, item.maxOffset);
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
/**
|
|
966
|
-
* Returns an array that is a copy of passed child list with the exception that text nodes are split to one or more
|
|
967
|
-
* objects, each representing one character and attributes set on that character.
|
|
968
|
-
*/
|
|
969
|
-
function _getChildrenSnapshot(children) {
|
|
970
|
-
const snapshot = [];
|
|
971
|
-
for (const child of children) {
|
|
972
|
-
if (child.is('$text')) {
|
|
973
|
-
for (let i = 0; i < child.data.length; i++) {
|
|
974
|
-
snapshot.push({
|
|
975
|
-
name: '$text',
|
|
976
|
-
attributes: new Map(child.getAttributes())
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
else {
|
|
981
|
-
snapshot.push({
|
|
982
|
-
name: child.name,
|
|
983
|
-
attributes: new Map(child.getAttributes())
|
|
984
|
-
});
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
return snapshot;
|
|
988
|
-
}
|
|
989
|
-
/**
|
|
990
|
-
* Generates array of actions for given changes set.
|
|
991
|
-
* It simulates what `diff` function does.
|
|
992
|
-
* Generated actions are:
|
|
993
|
-
* - 'e' for 'equal' - when item at that position did not change,
|
|
994
|
-
* - 'i' for 'insert' - when item at that position was inserted,
|
|
995
|
-
* - 'r' for 'remove' - when item at that position was removed,
|
|
996
|
-
* - 'a' for 'attribute' - when item at that position has it attributes changed.
|
|
997
|
-
*
|
|
998
|
-
* Example (assume that uppercase letters have bold attribute, compare with function code):
|
|
999
|
-
*
|
|
1000
|
-
* children before: fooBAR
|
|
1001
|
-
* children after: foxybAR
|
|
1002
|
-
*
|
|
1003
|
-
* changes: type: remove, offset: 1, howMany: 1
|
|
1004
|
-
* type: insert, offset: 2, howMany: 2
|
|
1005
|
-
* type: attribute, offset: 4, howMany: 1
|
|
1006
|
-
*
|
|
1007
|
-
* expected actions: equal (f), remove (o), equal (o), insert (x), insert (y), attribute (b), equal (A), equal (R)
|
|
1008
|
-
*
|
|
1009
|
-
* steps taken by th script:
|
|
1010
|
-
*
|
|
1011
|
-
* 1. change = "type: remove, offset: 1, howMany: 1"; offset = 0; oldChildrenHandled = 0
|
|
1012
|
-
* 1.1 between this change and the beginning is one not-changed node, fill with one equal action, one old child has been handled
|
|
1013
|
-
* 1.2 this change removes one node, add one remove action
|
|
1014
|
-
* 1.3 change last visited `offset` to 1
|
|
1015
|
-
* 1.4 since an old child has been removed, one more old child has been handled
|
|
1016
|
-
* 1.5 actions at this point are: equal, remove
|
|
1017
|
-
*
|
|
1018
|
-
* 2. change = "type: insert, offset: 2, howMany: 2"; offset = 1; oldChildrenHandled = 2
|
|
1019
|
-
* 2.1 between this change and previous change is one not-changed node, add equal action, another one old children has been handled
|
|
1020
|
-
* 2.2 this change inserts two nodes, add two insert actions
|
|
1021
|
-
* 2.3 change last visited offset to the end of the inserted range, that is 4
|
|
1022
|
-
* 2.4 actions at this point are: equal, remove, equal, insert, insert
|
|
1023
|
-
*
|
|
1024
|
-
* 3. change = "type: attribute, offset: 4, howMany: 1"; offset = 4, oldChildrenHandled = 3
|
|
1025
|
-
* 3.1 between this change and previous change are no not-changed nodes
|
|
1026
|
-
* 3.2 this change changes one node, add one attribute action
|
|
1027
|
-
* 3.3 change last visited `offset` to the end of change range, that is 5
|
|
1028
|
-
* 3.4 since an old child has been changed, one more old child has been handled
|
|
1029
|
-
* 3.5 actions at this point are: equal, remove, equal, insert, insert, attribute
|
|
1030
|
-
*
|
|
1031
|
-
* 4. after loop oldChildrenHandled = 4, oldChildrenLength = 6 (fooBAR is 6 characters)
|
|
1032
|
-
* 4.1 fill up with two equal actions
|
|
1033
|
-
*
|
|
1034
|
-
* The result actions are: equal, remove, equal, insert, insert, attribute, equal, equal.
|
|
1035
|
-
*/
|
|
1036
|
-
function _generateActionsFromChanges(oldChildrenLength, changes) {
|
|
1037
|
-
const actions = [];
|
|
1038
|
-
let offset = 0;
|
|
1039
|
-
let oldChildrenHandled = 0;
|
|
1040
|
-
// Go through all buffered changes.
|
|
1041
|
-
for (const change of changes) {
|
|
1042
|
-
// First, fill "holes" between changes with "equal" actions.
|
|
1043
|
-
if (change.offset > offset) {
|
|
1044
|
-
for (let i = 0; i < change.offset - offset; i++) {
|
|
1045
|
-
actions.push('e');
|
|
1046
|
-
}
|
|
1047
|
-
oldChildrenHandled += change.offset - offset;
|
|
1048
|
-
}
|
|
1049
|
-
// Then, fill up actions accordingly to change type.
|
|
1050
|
-
if (change.type == 'insert') {
|
|
1051
|
-
for (let i = 0; i < change.howMany; i++) {
|
|
1052
|
-
actions.push('i');
|
|
1053
|
-
}
|
|
1054
|
-
// The last handled offset is after inserted range.
|
|
1055
|
-
offset = change.offset + change.howMany;
|
|
1056
|
-
}
|
|
1057
|
-
else if (change.type == 'remove') {
|
|
1058
|
-
for (let i = 0; i < change.howMany; i++) {
|
|
1059
|
-
actions.push('r');
|
|
1060
|
-
}
|
|
1061
|
-
// The last handled offset is at the position where the nodes were removed.
|
|
1062
|
-
offset = change.offset;
|
|
1063
|
-
// We removed `howMany` old nodes, update `oldChildrenHandled`.
|
|
1064
|
-
oldChildrenHandled += change.howMany;
|
|
1065
|
-
}
|
|
1066
|
-
else {
|
|
1067
|
-
actions.push(...'a'.repeat(change.howMany).split(''));
|
|
1068
|
-
// The last handled offset is at the position after the changed range.
|
|
1069
|
-
offset = change.offset + change.howMany;
|
|
1070
|
-
// We changed `howMany` old nodes, update `oldChildrenHandled`.
|
|
1071
|
-
oldChildrenHandled += change.howMany;
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
// Fill "equal" actions at the end of actions set. Use `oldChildrenHandled` to see how many children
|
|
1075
|
-
// has not been changed / removed at the end of their parent.
|
|
1076
|
-
if (oldChildrenHandled < oldChildrenLength) {
|
|
1077
|
-
for (let i = 0; i < oldChildrenLength - oldChildrenHandled - offset; i++) {
|
|
1078
|
-
actions.push('e');
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
return actions;
|
|
1082
|
-
}
|
|
1083
|
-
/**
|
|
1084
|
-
* Filter callback for Array.filter that filters out change entries that are in graveyard.
|
|
1085
|
-
*/
|
|
1086
|
-
function _changesInGraveyardFilter(entry) {
|
|
1087
|
-
const posInGy = 'position' in entry && entry.position.root.rootName == '$graveyard';
|
|
1088
|
-
const rangeInGy = 'range' in entry && entry.range.root.rootName == '$graveyard';
|
|
1089
|
-
return !posInGy && !rangeInGy;
|
|
1090
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module engine/model/differ
|
|
7
|
+
*/
|
|
8
|
+
import Position from './position.js';
|
|
9
|
+
import Range from './range.js';
|
|
10
|
+
/**
|
|
11
|
+
* Calculates the difference between two model states.
|
|
12
|
+
*
|
|
13
|
+
* Receives operations that are to be applied on the model document. Marks parts of the model document tree which
|
|
14
|
+
* are changed and saves the state of these elements before the change. Then, it compares saved elements with the
|
|
15
|
+
* changed elements, after all changes are applied on the model document. Calculates the diff between saved
|
|
16
|
+
* elements and new ones and returns a change set.
|
|
17
|
+
*/
|
|
18
|
+
export default class Differ {
|
|
19
|
+
/**
|
|
20
|
+
* Creates a `Differ` instance.
|
|
21
|
+
*
|
|
22
|
+
* @param markerCollection Model's marker collection.
|
|
23
|
+
*/
|
|
24
|
+
constructor(markerCollection) {
|
|
25
|
+
/**
|
|
26
|
+
* A map that stores changes that happened in a given element.
|
|
27
|
+
*
|
|
28
|
+
* The keys of the map are references to the model elements.
|
|
29
|
+
* The values of the map are arrays with changes that were done on this element.
|
|
30
|
+
*/
|
|
31
|
+
this._changesInElement = new Map();
|
|
32
|
+
/**
|
|
33
|
+
* A map that stores "element's children snapshots". A snapshot is representing children of a given element before
|
|
34
|
+
* the first change was applied on that element. Snapshot items are objects with two properties: `name`,
|
|
35
|
+
* containing the element name (or `'$text'` for a text node) and `attributes` which is a map of the node's attributes.
|
|
36
|
+
*/
|
|
37
|
+
this._elementSnapshots = new Map();
|
|
38
|
+
/**
|
|
39
|
+
* A map that stores all changed markers.
|
|
40
|
+
*
|
|
41
|
+
* The keys of the map are marker names.
|
|
42
|
+
* The values of the map are objects with the following properties:
|
|
43
|
+
* - `oldMarkerData`,
|
|
44
|
+
* - `newMarkerData`.
|
|
45
|
+
*/
|
|
46
|
+
this._changedMarkers = new Map();
|
|
47
|
+
/**
|
|
48
|
+
* A map that stores all roots that have been changed.
|
|
49
|
+
*
|
|
50
|
+
* The keys are the names of the roots while value represents the changes.
|
|
51
|
+
*/
|
|
52
|
+
this._changedRoots = new Map();
|
|
53
|
+
/**
|
|
54
|
+
* Stores the number of changes that were processed. Used to order the changes chronologically. It is important
|
|
55
|
+
* when changes are sorted.
|
|
56
|
+
*/
|
|
57
|
+
this._changeCount = 0;
|
|
58
|
+
/**
|
|
59
|
+
* For efficiency purposes, `Differ` stores the change set returned by the differ after {@link #getChanges} call.
|
|
60
|
+
* Cache is reset each time a new operation is buffered. If the cache has not been reset, {@link #getChanges} will
|
|
61
|
+
* return the cached value instead of calculating it again.
|
|
62
|
+
*
|
|
63
|
+
* This property stores those changes that did not take place in graveyard root.
|
|
64
|
+
*/
|
|
65
|
+
this._cachedChanges = null;
|
|
66
|
+
/**
|
|
67
|
+
* For efficiency purposes, `Differ` stores the change set returned by the differ after the {@link #getChanges} call.
|
|
68
|
+
* The cache is reset each time a new operation is buffered. If the cache has not been reset, {@link #getChanges} will
|
|
69
|
+
* return the cached value instead of calculating it again.
|
|
70
|
+
*
|
|
71
|
+
* This property stores all changes evaluated by `Differ`, including those that took place in the graveyard.
|
|
72
|
+
*/
|
|
73
|
+
this._cachedChangesWithGraveyard = null;
|
|
74
|
+
/**
|
|
75
|
+
* Set of model items that were marked to get refreshed in {@link #_refreshItem}.
|
|
76
|
+
*/
|
|
77
|
+
this._refreshedItems = new Set();
|
|
78
|
+
this._markerCollection = markerCollection;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Informs whether there are any changes buffered in `Differ`.
|
|
82
|
+
*/
|
|
83
|
+
get isEmpty() {
|
|
84
|
+
return this._changesInElement.size == 0 && this._changedMarkers.size == 0 && this._changedRoots.size == 0;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Buffers the given operation. An operation has to be buffered before it is executed.
|
|
88
|
+
*
|
|
89
|
+
* @param operationToBuffer An operation to buffer.
|
|
90
|
+
*/
|
|
91
|
+
bufferOperation(operationToBuffer) {
|
|
92
|
+
// Below we take an operation, check its type, then use its parameters in marking (private) methods.
|
|
93
|
+
// The general rule is to not mark elements inside inserted element. All inserted elements are re-rendered.
|
|
94
|
+
// Marking changes in them would cause a "double" changing then.
|
|
95
|
+
//
|
|
96
|
+
const operation = operationToBuffer;
|
|
97
|
+
switch (operation.type) {
|
|
98
|
+
case 'insert': {
|
|
99
|
+
if (this._isInInsertedElement(operation.position.parent)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this._markInsert(operation.position.parent, operation.position.offset, operation.nodes.maxOffset);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case 'addAttribute':
|
|
106
|
+
case 'removeAttribute':
|
|
107
|
+
case 'changeAttribute': {
|
|
108
|
+
for (const item of operation.range.getItems({ shallow: true })) {
|
|
109
|
+
if (this._isInInsertedElement(item.parent)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
this._markAttribute(item);
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case 'remove':
|
|
117
|
+
case 'move':
|
|
118
|
+
case 'reinsert': {
|
|
119
|
+
// When range is moved to the same position then not mark it as a change.
|
|
120
|
+
// See: https://github.com/ckeditor/ckeditor5-engine/issues/1664.
|
|
121
|
+
if (operation.sourcePosition.isEqual(operation.targetPosition) ||
|
|
122
|
+
operation.sourcePosition.getShiftedBy(operation.howMany).isEqual(operation.targetPosition)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const sourceParentInserted = this._isInInsertedElement(operation.sourcePosition.parent);
|
|
126
|
+
const targetParentInserted = this._isInInsertedElement(operation.targetPosition.parent);
|
|
127
|
+
if (!sourceParentInserted) {
|
|
128
|
+
this._markRemove(operation.sourcePosition.parent, operation.sourcePosition.offset, operation.howMany);
|
|
129
|
+
}
|
|
130
|
+
if (!targetParentInserted) {
|
|
131
|
+
this._markInsert(operation.targetPosition.parent, operation.getMovedRangeStart().offset, operation.howMany);
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
case 'rename': {
|
|
136
|
+
if (this._isInInsertedElement(operation.position.parent)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this._markRemove(operation.position.parent, operation.position.offset, 1);
|
|
140
|
+
this._markInsert(operation.position.parent, operation.position.offset, 1);
|
|
141
|
+
const range = Range._createFromPositionAndShift(operation.position, 1);
|
|
142
|
+
for (const marker of this._markerCollection.getMarkersIntersectingRange(range)) {
|
|
143
|
+
const markerData = marker.getData();
|
|
144
|
+
this.bufferMarkerChange(marker.name, markerData, markerData);
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case 'split': {
|
|
149
|
+
const splitElement = operation.splitPosition.parent;
|
|
150
|
+
// Mark that children of the split element were removed.
|
|
151
|
+
if (!this._isInInsertedElement(splitElement)) {
|
|
152
|
+
this._markRemove(splitElement, operation.splitPosition.offset, operation.howMany);
|
|
153
|
+
}
|
|
154
|
+
// Mark that the new element (split copy) was inserted.
|
|
155
|
+
if (!this._isInInsertedElement(operation.insertionPosition.parent)) {
|
|
156
|
+
this._markInsert(operation.insertionPosition.parent, operation.insertionPosition.offset, 1);
|
|
157
|
+
}
|
|
158
|
+
// If the split took the element from the graveyard, mark that the element from the graveyard was removed.
|
|
159
|
+
if (operation.graveyardPosition) {
|
|
160
|
+
this._markRemove(operation.graveyardPosition.parent, operation.graveyardPosition.offset, 1);
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
case 'merge': {
|
|
165
|
+
// Mark that the merged element was removed.
|
|
166
|
+
const mergedElement = operation.sourcePosition.parent;
|
|
167
|
+
if (!this._isInInsertedElement(mergedElement.parent)) {
|
|
168
|
+
this._markRemove(mergedElement.parent, mergedElement.startOffset, 1);
|
|
169
|
+
}
|
|
170
|
+
// Mark that the merged element was inserted into graveyard.
|
|
171
|
+
const graveyardParent = operation.graveyardPosition.parent;
|
|
172
|
+
this._markInsert(graveyardParent, operation.graveyardPosition.offset, 1);
|
|
173
|
+
// Mark that children of merged element were inserted at new parent.
|
|
174
|
+
const mergedIntoElement = operation.targetPosition.parent;
|
|
175
|
+
if (!this._isInInsertedElement(mergedIntoElement)) {
|
|
176
|
+
this._markInsert(mergedIntoElement, operation.targetPosition.offset, mergedElement.maxOffset);
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case 'detachRoot':
|
|
181
|
+
case 'addRoot': {
|
|
182
|
+
this._bufferRootStateChange(operation.rootName, operation.isAdd);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case 'addRootAttribute':
|
|
186
|
+
case 'removeRootAttribute':
|
|
187
|
+
case 'changeRootAttribute': {
|
|
188
|
+
const rootName = operation.root.rootName;
|
|
189
|
+
this._bufferRootAttributeChange(rootName, operation.key, operation.oldValue, operation.newValue);
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Clear cache after each buffered operation as it is no longer valid.
|
|
194
|
+
this._cachedChanges = null;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Buffers a marker change.
|
|
198
|
+
*
|
|
199
|
+
* @param markerName The name of the marker that changed.
|
|
200
|
+
* @param oldMarkerData Marker data before the change.
|
|
201
|
+
* @param newMarkerData Marker data after the change.
|
|
202
|
+
*/
|
|
203
|
+
bufferMarkerChange(markerName, oldMarkerData, newMarkerData) {
|
|
204
|
+
const buffered = this._changedMarkers.get(markerName);
|
|
205
|
+
if (!buffered) {
|
|
206
|
+
this._changedMarkers.set(markerName, {
|
|
207
|
+
newMarkerData,
|
|
208
|
+
oldMarkerData
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
buffered.newMarkerData = newMarkerData;
|
|
213
|
+
if (buffered.oldMarkerData.range == null && newMarkerData.range == null) {
|
|
214
|
+
// The marker is going to be removed (`newMarkerData.range == null`) but it did not exist before the first buffered change
|
|
215
|
+
// (`buffered.oldMarkerData.range == null`). In this case, do not keep the marker in buffer at all.
|
|
216
|
+
this._changedMarkers.delete(markerName);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Returns all markers that should be removed as a result of buffered changes.
|
|
222
|
+
*
|
|
223
|
+
* @returns Markers to remove. Each array item is an object containing the `name` and `range` properties.
|
|
224
|
+
*/
|
|
225
|
+
getMarkersToRemove() {
|
|
226
|
+
const result = [];
|
|
227
|
+
for (const [name, change] of this._changedMarkers) {
|
|
228
|
+
if (change.oldMarkerData.range != null) {
|
|
229
|
+
result.push({ name, range: change.oldMarkerData.range });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Returns all markers which should be added as a result of buffered changes.
|
|
236
|
+
*
|
|
237
|
+
* @returns Markers to add. Each array item is an object containing the `name` and `range` properties.
|
|
238
|
+
*/
|
|
239
|
+
getMarkersToAdd() {
|
|
240
|
+
const result = [];
|
|
241
|
+
for (const [name, change] of this._changedMarkers) {
|
|
242
|
+
if (change.newMarkerData.range != null) {
|
|
243
|
+
result.push({ name, range: change.newMarkerData.range });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Returns all markers which changed.
|
|
250
|
+
*/
|
|
251
|
+
getChangedMarkers() {
|
|
252
|
+
return Array.from(this._changedMarkers).map(([name, change]) => ({
|
|
253
|
+
name,
|
|
254
|
+
data: {
|
|
255
|
+
oldRange: change.oldMarkerData.range,
|
|
256
|
+
newRange: change.newMarkerData.range
|
|
257
|
+
}
|
|
258
|
+
}));
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Checks whether some of the buffered changes affect the editor data.
|
|
262
|
+
*
|
|
263
|
+
* Types of changes which affect the editor data:
|
|
264
|
+
*
|
|
265
|
+
* * model structure changes,
|
|
266
|
+
* * attribute changes,
|
|
267
|
+
* * a root is added or detached,
|
|
268
|
+
* * changes of markers which were defined as `affectsData`,
|
|
269
|
+
* * changes of markers' `affectsData` property.
|
|
270
|
+
*/
|
|
271
|
+
hasDataChanges() {
|
|
272
|
+
if (this._changesInElement.size > 0) {
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
if (this._changedRoots.size > 0) {
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
for (const { newMarkerData, oldMarkerData } of this._changedMarkers.values()) {
|
|
279
|
+
if (newMarkerData.affectsData !== oldMarkerData.affectsData) {
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
if (newMarkerData.affectsData) {
|
|
283
|
+
const markerAdded = newMarkerData.range && !oldMarkerData.range;
|
|
284
|
+
const markerRemoved = !newMarkerData.range && oldMarkerData.range;
|
|
285
|
+
const markerChanged = newMarkerData.range && oldMarkerData.range && !newMarkerData.range.isEqual(oldMarkerData.range);
|
|
286
|
+
if (markerAdded || markerRemoved || markerChanged) {
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Calculates the diff between the old model tree state (the state before the first buffered operations since the last {@link #reset}
|
|
295
|
+
* call) and the new model tree state (actual one). It should be called after all buffered operations are executed.
|
|
296
|
+
*
|
|
297
|
+
* The diff set is returned as an array of {@link module:engine/model/differ~DiffItem diff items}, each describing a change done
|
|
298
|
+
* on the model. The items are sorted by the position on which the change happened. If a position
|
|
299
|
+
* {@link module:engine/model/position~Position#isBefore is before} another one, it will be on an earlier index in the diff set.
|
|
300
|
+
*
|
|
301
|
+
* **Note**: Elements inside inserted element will not have a separate diff item, only the top most element change will be reported.
|
|
302
|
+
*
|
|
303
|
+
* Because calculating the diff is a costly operation, the result is cached. If no new operation was buffered since the
|
|
304
|
+
* previous {@link #getChanges} call, the next call will return the cached value.
|
|
305
|
+
*
|
|
306
|
+
* @param options Additional options.
|
|
307
|
+
* @param options.includeChangesInGraveyard If set to `true`, also changes that happened
|
|
308
|
+
* in the graveyard root will be returned. By default, changes in the graveyard root are not returned.
|
|
309
|
+
* @returns Diff between the old and the new model tree state.
|
|
310
|
+
*/
|
|
311
|
+
getChanges(options = {}) {
|
|
312
|
+
// If there are cached changes, just return them instead of calculating changes again.
|
|
313
|
+
if (this._cachedChanges) {
|
|
314
|
+
if (options.includeChangesInGraveyard) {
|
|
315
|
+
return this._cachedChangesWithGraveyard.slice();
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
return this._cachedChanges.slice();
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
// Will contain returned results.
|
|
322
|
+
let diffSet = [];
|
|
323
|
+
// Check all changed elements.
|
|
324
|
+
for (const element of this._changesInElement.keys()) {
|
|
325
|
+
// Get changes for this element and sort them.
|
|
326
|
+
const changes = this._changesInElement.get(element).sort((a, b) => {
|
|
327
|
+
if (a.offset === b.offset) {
|
|
328
|
+
if (a.type != b.type) {
|
|
329
|
+
// If there are multiple changes at the same position, "remove" change should be first.
|
|
330
|
+
// If the order is different, for example, we would first add some nodes and then removed them
|
|
331
|
+
// (instead of the nodes that we should remove).
|
|
332
|
+
return a.type == 'remove' ? -1 : 1;
|
|
333
|
+
}
|
|
334
|
+
return 0;
|
|
335
|
+
}
|
|
336
|
+
return a.offset < b.offset ? -1 : 1;
|
|
337
|
+
});
|
|
338
|
+
// Get children of this element before any change was applied on it.
|
|
339
|
+
const snapshotChildren = this._elementSnapshots.get(element);
|
|
340
|
+
// Get snapshot of current element's children.
|
|
341
|
+
const elementChildren = _getChildrenSnapshot(element.getChildren());
|
|
342
|
+
// Generate actions basing on changes done on element.
|
|
343
|
+
const actions = _generateActionsFromChanges(snapshotChildren.length, changes);
|
|
344
|
+
let i = 0; // Iterator in `elementChildren` array -- iterates through current children of element.
|
|
345
|
+
let j = 0; // Iterator in `snapshotChildren` array -- iterates through old children of element.
|
|
346
|
+
// Process every action.
|
|
347
|
+
for (const action of actions) {
|
|
348
|
+
if (action === 'i') {
|
|
349
|
+
// Generate diff item for this element and insert it into the diff set.
|
|
350
|
+
diffSet.push(this._getInsertDiff(element, i, elementChildren[i]));
|
|
351
|
+
i++;
|
|
352
|
+
}
|
|
353
|
+
else if (action === 'r') {
|
|
354
|
+
// Generate diff item for this element and insert it into the diff set.
|
|
355
|
+
diffSet.push(this._getRemoveDiff(element, i, snapshotChildren[j]));
|
|
356
|
+
j++;
|
|
357
|
+
}
|
|
358
|
+
else if (action === 'a') {
|
|
359
|
+
// Take attributes from saved and current children.
|
|
360
|
+
const elementAttributes = elementChildren[i].attributes;
|
|
361
|
+
const snapshotAttributes = snapshotChildren[j].attributes;
|
|
362
|
+
let range;
|
|
363
|
+
if (elementChildren[i].name == '$text') {
|
|
364
|
+
range = new Range(Position._createAt(element, i), Position._createAt(element, i + 1));
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
const index = element.offsetToIndex(i);
|
|
368
|
+
range = new Range(Position._createAt(element, i), Position._createAt(element.getChild(index), 0));
|
|
369
|
+
}
|
|
370
|
+
// Generate diff items for this change (there might be multiple attributes changed and
|
|
371
|
+
// there is a single diff for each of them) and insert them into the diff set.
|
|
372
|
+
diffSet.push(...this._getAttributesDiff(range, snapshotAttributes, elementAttributes));
|
|
373
|
+
i++;
|
|
374
|
+
j++;
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
// `action` is 'equal'. Child not changed.
|
|
378
|
+
i++;
|
|
379
|
+
j++;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
// Then, sort the changes by the position (change at position before other changes is first).
|
|
384
|
+
diffSet.sort((a, b) => {
|
|
385
|
+
// If the change is in different root, we don't care much, but we'd like to have all changes in given
|
|
386
|
+
// root "together" in the array. So let's just sort them by the root name. It does not matter which root
|
|
387
|
+
// will be processed first.
|
|
388
|
+
if (a.position.root != b.position.root) {
|
|
389
|
+
return a.position.root.rootName < b.position.root.rootName ? -1 : 1;
|
|
390
|
+
}
|
|
391
|
+
// If change happens at the same position...
|
|
392
|
+
if (a.position.isEqual(b.position)) {
|
|
393
|
+
// Keep chronological order of operations.
|
|
394
|
+
return a.changeCount - b.changeCount;
|
|
395
|
+
}
|
|
396
|
+
// If positions differ, position "on the left" should be earlier in the result.
|
|
397
|
+
return a.position.isBefore(b.position) ? -1 : 1;
|
|
398
|
+
});
|
|
399
|
+
// Glue together multiple changes (mostly on text nodes).
|
|
400
|
+
for (let i = 1, prevIndex = 0; i < diffSet.length; i++) {
|
|
401
|
+
const prevDiff = diffSet[prevIndex];
|
|
402
|
+
const thisDiff = diffSet[i];
|
|
403
|
+
// Glue remove changes if they happen on text on same position.
|
|
404
|
+
const isConsecutiveTextRemove = prevDiff.type == 'remove' && thisDiff.type == 'remove' &&
|
|
405
|
+
prevDiff.name == '$text' && thisDiff.name == '$text' &&
|
|
406
|
+
prevDiff.position.isEqual(thisDiff.position);
|
|
407
|
+
// Glue insert changes if they happen on text on consecutive fragments.
|
|
408
|
+
const isConsecutiveTextAdd = prevDiff.type == 'insert' && thisDiff.type == 'insert' &&
|
|
409
|
+
prevDiff.name == '$text' && thisDiff.name == '$text' &&
|
|
410
|
+
prevDiff.position.parent == thisDiff.position.parent &&
|
|
411
|
+
prevDiff.position.offset + prevDiff.length == thisDiff.position.offset;
|
|
412
|
+
// Glue attribute changes if they happen on consecutive fragments and have same key, old value and new value.
|
|
413
|
+
const isConsecutiveAttributeChange = prevDiff.type == 'attribute' && thisDiff.type == 'attribute' &&
|
|
414
|
+
prevDiff.position.parent == thisDiff.position.parent &&
|
|
415
|
+
prevDiff.range.isFlat && thisDiff.range.isFlat &&
|
|
416
|
+
(prevDiff.position.offset + prevDiff.length) == thisDiff.position.offset &&
|
|
417
|
+
prevDiff.attributeKey == thisDiff.attributeKey &&
|
|
418
|
+
prevDiff.attributeOldValue == thisDiff.attributeOldValue &&
|
|
419
|
+
prevDiff.attributeNewValue == thisDiff.attributeNewValue;
|
|
420
|
+
if (isConsecutiveTextRemove || isConsecutiveTextAdd || isConsecutiveAttributeChange) {
|
|
421
|
+
prevDiff.length++;
|
|
422
|
+
if (isConsecutiveAttributeChange) {
|
|
423
|
+
prevDiff.range.end = prevDiff.range.end.getShiftedBy(1);
|
|
424
|
+
}
|
|
425
|
+
diffSet[i] = null;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
prevIndex = i;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
diffSet = diffSet.filter(v => v);
|
|
432
|
+
// Remove `changeCount` property from diff items. It is used only for sorting and is internal thing.
|
|
433
|
+
for (const item of diffSet) {
|
|
434
|
+
delete item.changeCount;
|
|
435
|
+
if (item.type == 'attribute') {
|
|
436
|
+
delete item.position;
|
|
437
|
+
delete item.length;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
this._changeCount = 0;
|
|
441
|
+
// Cache changes.
|
|
442
|
+
this._cachedChangesWithGraveyard = diffSet;
|
|
443
|
+
this._cachedChanges = diffSet.filter(_changesInGraveyardFilter);
|
|
444
|
+
if (options.includeChangesInGraveyard) {
|
|
445
|
+
return this._cachedChangesWithGraveyard.slice();
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
return this._cachedChanges.slice();
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Returns all roots that have changed (either were attached, or detached, or their attributes changed).
|
|
453
|
+
*
|
|
454
|
+
* @returns Diff between the old and the new roots state.
|
|
455
|
+
*/
|
|
456
|
+
getChangedRoots() {
|
|
457
|
+
return Array.from(this._changedRoots.values()).map(diffItem => {
|
|
458
|
+
const entry = { ...diffItem };
|
|
459
|
+
if (entry.state !== undefined) {
|
|
460
|
+
// The root was attached or detached -- do not return its attributes changes.
|
|
461
|
+
// If the root was attached, it should be handled as a whole, together with its attributes, the same way as model nodes.
|
|
462
|
+
// If the root was detached, its attributes should be discarded anyway.
|
|
463
|
+
//
|
|
464
|
+
// Keep in mind that filtering must happen on this stage (when retrieving changes). If filtering happens on-the-fly as
|
|
465
|
+
// the attributes change, it may lead to incorrect situation, e.g.: detach root, change attribute, re-attach root.
|
|
466
|
+
// In this case, attribute change cannot be filtered. After the root is re-attached, the attribute change must be kept.
|
|
467
|
+
delete entry.attributes;
|
|
468
|
+
}
|
|
469
|
+
return entry;
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Returns a set of model items that were marked to get refreshed.
|
|
474
|
+
*/
|
|
475
|
+
getRefreshedItems() {
|
|
476
|
+
return new Set(this._refreshedItems);
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Resets `Differ`. Removes all buffered changes.
|
|
480
|
+
*/
|
|
481
|
+
reset() {
|
|
482
|
+
this._changesInElement.clear();
|
|
483
|
+
this._elementSnapshots.clear();
|
|
484
|
+
this._changedMarkers.clear();
|
|
485
|
+
this._changedRoots.clear();
|
|
486
|
+
this._refreshedItems = new Set();
|
|
487
|
+
this._cachedChanges = null;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Buffers the root state change after the root was attached or detached
|
|
491
|
+
*/
|
|
492
|
+
_bufferRootStateChange(rootName, isAttached) {
|
|
493
|
+
if (!this._changedRoots.has(rootName)) {
|
|
494
|
+
this._changedRoots.set(rootName, { name: rootName, state: isAttached ? 'attached' : 'detached' });
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
const diffItem = this._changedRoots.get(rootName);
|
|
498
|
+
if (diffItem.state !== undefined) {
|
|
499
|
+
// Root `state` can only toggle between of the values ('attached' or 'detached') and no value. It cannot be any other way,
|
|
500
|
+
// because if the root was originally attached it can only become detached. Then, if it is re-attached in the same batch of
|
|
501
|
+
// changes, it gets back to "no change" (which means no value). Same if the root was originally detached.
|
|
502
|
+
delete diffItem.state;
|
|
503
|
+
if (diffItem.attributes === undefined) {
|
|
504
|
+
// If there is no `state` change and no `attributes` change, remove the entry.
|
|
505
|
+
this._changedRoots.delete(rootName);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
diffItem.state = isAttached ? 'attached' : 'detached';
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Buffers a root attribute change.
|
|
514
|
+
*/
|
|
515
|
+
_bufferRootAttributeChange(rootName, key, oldValue, newValue) {
|
|
516
|
+
const diffItem = this._changedRoots.get(rootName) || { name: rootName };
|
|
517
|
+
const attrs = diffItem.attributes || {};
|
|
518
|
+
if (attrs[key]) {
|
|
519
|
+
// If this attribute or metadata was already changed earlier and is changed again, check to what value it is changed.
|
|
520
|
+
const attrEntry = attrs[key];
|
|
521
|
+
if (newValue === attrEntry.oldValue) {
|
|
522
|
+
// If it was changed back to the old value, remove the entry.
|
|
523
|
+
delete attrs[key];
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
// If it was changed to a different value, update the entry.
|
|
527
|
+
attrEntry.newValue = newValue;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
// If this attribute or metadata was not set earlier, add an entry.
|
|
532
|
+
attrs[key] = { oldValue, newValue };
|
|
533
|
+
}
|
|
534
|
+
if (Object.entries(attrs).length === 0) {
|
|
535
|
+
// If attributes or metadata changes set became empty, remove it from the diff item.
|
|
536
|
+
delete diffItem.attributes;
|
|
537
|
+
if (diffItem.state === undefined) {
|
|
538
|
+
// If there is no `state` change and no `attributes` change, remove the entry.
|
|
539
|
+
this._changedRoots.delete(rootName);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
// Make sure that, if a new object in the structure was created, it gets set.
|
|
544
|
+
diffItem.attributes = attrs;
|
|
545
|
+
this._changedRoots.set(rootName, diffItem);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Marks the given `item` in differ to be "refreshed". It means that the item will be marked as removed and inserted
|
|
550
|
+
* in the differ changes set, so it will be effectively re-converted when the differ changes are handled by a dispatcher.
|
|
551
|
+
*
|
|
552
|
+
* @internal
|
|
553
|
+
* @param item Item to refresh.
|
|
554
|
+
*/
|
|
555
|
+
_refreshItem(item) {
|
|
556
|
+
if (this._isInInsertedElement(item.parent)) {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
this._markRemove(item.parent, item.startOffset, item.offsetSize);
|
|
560
|
+
this._markInsert(item.parent, item.startOffset, item.offsetSize);
|
|
561
|
+
this._refreshedItems.add(item);
|
|
562
|
+
const range = Range._createOn(item);
|
|
563
|
+
for (const marker of this._markerCollection.getMarkersIntersectingRange(range)) {
|
|
564
|
+
const markerData = marker.getData();
|
|
565
|
+
this.bufferMarkerChange(marker.name, markerData, markerData);
|
|
566
|
+
}
|
|
567
|
+
// Clear cache after each buffered operation as it is no longer valid.
|
|
568
|
+
this._cachedChanges = null;
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Saves and handles an insert change.
|
|
572
|
+
*/
|
|
573
|
+
_markInsert(parent, offset, howMany) {
|
|
574
|
+
const changeItem = { type: 'insert', offset, howMany, count: this._changeCount++ };
|
|
575
|
+
this._markChange(parent, changeItem);
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Saves and handles a remove change.
|
|
579
|
+
*/
|
|
580
|
+
_markRemove(parent, offset, howMany) {
|
|
581
|
+
const changeItem = { type: 'remove', offset, howMany, count: this._changeCount++ };
|
|
582
|
+
this._markChange(parent, changeItem);
|
|
583
|
+
this._removeAllNestedChanges(parent, offset, howMany);
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Saves and handles an attribute change.
|
|
587
|
+
*/
|
|
588
|
+
_markAttribute(item) {
|
|
589
|
+
const changeItem = { type: 'attribute', offset: item.startOffset, howMany: item.offsetSize, count: this._changeCount++ };
|
|
590
|
+
this._markChange(item.parent, changeItem);
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Saves and handles a model change.
|
|
594
|
+
*/
|
|
595
|
+
_markChange(parent, changeItem) {
|
|
596
|
+
// First, make a snapshot of this parent's children (it will be made only if it was not made before).
|
|
597
|
+
this._makeSnapshot(parent);
|
|
598
|
+
// Then, get all changes that already were done on the element (empty array if this is the first change).
|
|
599
|
+
const changes = this._getChangesForElement(parent);
|
|
600
|
+
// Then, look through all the changes, and transform them or the new change.
|
|
601
|
+
this._handleChange(changeItem, changes);
|
|
602
|
+
// Add the new change.
|
|
603
|
+
changes.push(changeItem);
|
|
604
|
+
// Remove incorrect changes. During transformation some change might be, for example, included in another.
|
|
605
|
+
// In that case, the change will have `howMany` property set to `0` or less. We need to remove those changes.
|
|
606
|
+
for (let i = 0; i < changes.length; i++) {
|
|
607
|
+
if (changes[i].howMany < 1) {
|
|
608
|
+
changes.splice(i, 1);
|
|
609
|
+
i--;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Gets an array of changes that have already been saved for a given element.
|
|
615
|
+
*/
|
|
616
|
+
_getChangesForElement(element) {
|
|
617
|
+
let changes;
|
|
618
|
+
if (this._changesInElement.has(element)) {
|
|
619
|
+
changes = this._changesInElement.get(element);
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
changes = [];
|
|
623
|
+
this._changesInElement.set(element, changes);
|
|
624
|
+
}
|
|
625
|
+
return changes;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Saves a children snapshot for a given element.
|
|
629
|
+
*/
|
|
630
|
+
_makeSnapshot(element) {
|
|
631
|
+
if (!this._elementSnapshots.has(element)) {
|
|
632
|
+
this._elementSnapshots.set(element, _getChildrenSnapshot(element.getChildren()));
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* For a given newly saved change, compares it with a change already done on the element and modifies the incoming
|
|
637
|
+
* change and/or the old change.
|
|
638
|
+
*
|
|
639
|
+
* @param inc Incoming (new) change.
|
|
640
|
+
* @param changes An array containing all the changes done on that element.
|
|
641
|
+
*/
|
|
642
|
+
_handleChange(inc, changes) {
|
|
643
|
+
// We need a helper variable that will store how many nodes are to be still handled for this change item.
|
|
644
|
+
// `nodesToHandle` (how many nodes still need to be handled) and `howMany` (how many nodes were affected)
|
|
645
|
+
// needs to be differentiated.
|
|
646
|
+
//
|
|
647
|
+
// This comes up when there are multiple changes that are affected by `inc` change item.
|
|
648
|
+
//
|
|
649
|
+
// For example: assume two insert changes: `{ offset: 2, howMany: 1 }` and `{ offset: 5, howMany: 1 }`.
|
|
650
|
+
// Assume that `inc` change is remove `{ offset: 2, howMany: 2, nodesToHandle: 2 }`.
|
|
651
|
+
//
|
|
652
|
+
// Then, we:
|
|
653
|
+
// - "forget" about first insert change (it is "eaten" by remove),
|
|
654
|
+
// - because of that, at the end we will want to remove only one node (`nodesToHandle = 1`),
|
|
655
|
+
// - but still we have to change offset of the second insert change from `5` to `3`!
|
|
656
|
+
//
|
|
657
|
+
// So, `howMany` does not change throughout items transformation and keeps information about how many nodes were affected,
|
|
658
|
+
// while `nodesToHandle` means how many nodes need to be handled after the change item is transformed by other changes.
|
|
659
|
+
inc.nodesToHandle = inc.howMany;
|
|
660
|
+
for (const old of changes) {
|
|
661
|
+
const incEnd = inc.offset + inc.howMany;
|
|
662
|
+
const oldEnd = old.offset + old.howMany;
|
|
663
|
+
if (inc.type == 'insert') {
|
|
664
|
+
if (old.type == 'insert') {
|
|
665
|
+
if (inc.offset <= old.offset) {
|
|
666
|
+
old.offset += inc.howMany;
|
|
667
|
+
}
|
|
668
|
+
else if (inc.offset < oldEnd) {
|
|
669
|
+
old.howMany += inc.nodesToHandle;
|
|
670
|
+
inc.nodesToHandle = 0;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (old.type == 'remove') {
|
|
674
|
+
if (inc.offset < old.offset) {
|
|
675
|
+
old.offset += inc.howMany;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
if (old.type == 'attribute') {
|
|
679
|
+
if (inc.offset <= old.offset) {
|
|
680
|
+
old.offset += inc.howMany;
|
|
681
|
+
}
|
|
682
|
+
else if (inc.offset < oldEnd) {
|
|
683
|
+
// This case is more complicated, because attribute change has to be split into two.
|
|
684
|
+
// Example (assume that uppercase and lowercase letters mean different attributes):
|
|
685
|
+
//
|
|
686
|
+
// initial state: abcxyz
|
|
687
|
+
// attribute change: aBCXYz
|
|
688
|
+
// incoming insert: aBCfooXYz
|
|
689
|
+
//
|
|
690
|
+
// Change ranges cannot intersect because each item has to be described exactly (it was either
|
|
691
|
+
// not changed, inserted, removed, or its attribute was changed). That's why old attribute
|
|
692
|
+
// change has to be split and both parts has to be handled separately from now on.
|
|
693
|
+
const howMany = old.howMany;
|
|
694
|
+
old.howMany = inc.offset - old.offset;
|
|
695
|
+
// Add the second part of attribute change to the beginning of processed array so it won't
|
|
696
|
+
// be processed again in this loop.
|
|
697
|
+
changes.unshift({
|
|
698
|
+
type: 'attribute',
|
|
699
|
+
offset: incEnd,
|
|
700
|
+
howMany: howMany - old.howMany,
|
|
701
|
+
count: this._changeCount++
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
if (inc.type == 'remove') {
|
|
707
|
+
if (old.type == 'insert') {
|
|
708
|
+
if (incEnd <= old.offset) {
|
|
709
|
+
old.offset -= inc.howMany;
|
|
710
|
+
}
|
|
711
|
+
else if (incEnd <= oldEnd) {
|
|
712
|
+
if (inc.offset < old.offset) {
|
|
713
|
+
const intersectionLength = incEnd - old.offset;
|
|
714
|
+
old.offset = inc.offset;
|
|
715
|
+
old.howMany -= intersectionLength;
|
|
716
|
+
inc.nodesToHandle -= intersectionLength;
|
|
717
|
+
}
|
|
718
|
+
else {
|
|
719
|
+
old.howMany -= inc.nodesToHandle;
|
|
720
|
+
inc.nodesToHandle = 0;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
else {
|
|
724
|
+
if (inc.offset <= old.offset) {
|
|
725
|
+
inc.nodesToHandle -= old.howMany;
|
|
726
|
+
old.howMany = 0;
|
|
727
|
+
}
|
|
728
|
+
else if (inc.offset < oldEnd) {
|
|
729
|
+
const intersectionLength = oldEnd - inc.offset;
|
|
730
|
+
old.howMany -= intersectionLength;
|
|
731
|
+
inc.nodesToHandle -= intersectionLength;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (old.type == 'remove') {
|
|
736
|
+
if (incEnd <= old.offset) {
|
|
737
|
+
old.offset -= inc.howMany;
|
|
738
|
+
}
|
|
739
|
+
else if (inc.offset < old.offset) {
|
|
740
|
+
inc.nodesToHandle += old.howMany;
|
|
741
|
+
old.howMany = 0;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
if (old.type == 'attribute') {
|
|
745
|
+
if (incEnd <= old.offset) {
|
|
746
|
+
old.offset -= inc.howMany;
|
|
747
|
+
}
|
|
748
|
+
else if (inc.offset < old.offset) {
|
|
749
|
+
const intersectionLength = incEnd - old.offset;
|
|
750
|
+
old.offset = inc.offset;
|
|
751
|
+
old.howMany -= intersectionLength;
|
|
752
|
+
}
|
|
753
|
+
else if (inc.offset < oldEnd) {
|
|
754
|
+
if (incEnd <= oldEnd) {
|
|
755
|
+
// On first sight in this case we don't need to split attribute operation into two.
|
|
756
|
+
// However the changes set is later converted to actions (see `_generateActionsFromChanges`).
|
|
757
|
+
// For that reason, no two changes may intersect.
|
|
758
|
+
// So we cannot have an attribute change that "contains" remove change.
|
|
759
|
+
// Attribute change needs to be split.
|
|
760
|
+
const howMany = old.howMany;
|
|
761
|
+
old.howMany = inc.offset - old.offset;
|
|
762
|
+
const howManyAfter = howMany - old.howMany - inc.nodesToHandle;
|
|
763
|
+
// Add the second part of attribute change to the beginning of processed array so it won't
|
|
764
|
+
// be processed again in this loop.
|
|
765
|
+
changes.unshift({
|
|
766
|
+
type: 'attribute',
|
|
767
|
+
offset: inc.offset,
|
|
768
|
+
howMany: howManyAfter,
|
|
769
|
+
count: this._changeCount++
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
old.howMany -= oldEnd - inc.offset;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
if (inc.type == 'attribute') {
|
|
779
|
+
// In case of attribute change, `howMany` should be kept same as `nodesToHandle`. It's not an error.
|
|
780
|
+
if (old.type == 'insert') {
|
|
781
|
+
if (inc.offset < old.offset && incEnd > old.offset) {
|
|
782
|
+
if (incEnd > oldEnd) {
|
|
783
|
+
// This case is similar to a case described when incoming change was insert and old change was attribute.
|
|
784
|
+
// See comment above.
|
|
785
|
+
//
|
|
786
|
+
// This time incoming change is attribute. We need to split incoming change in this case too.
|
|
787
|
+
// However this time, the second part of the attribute change needs to be processed further
|
|
788
|
+
// because there might be other changes that it collides with.
|
|
789
|
+
const attributePart = {
|
|
790
|
+
type: 'attribute',
|
|
791
|
+
offset: oldEnd,
|
|
792
|
+
howMany: incEnd - oldEnd,
|
|
793
|
+
count: this._changeCount++
|
|
794
|
+
};
|
|
795
|
+
this._handleChange(attributePart, changes);
|
|
796
|
+
changes.push(attributePart);
|
|
797
|
+
}
|
|
798
|
+
inc.nodesToHandle = old.offset - inc.offset;
|
|
799
|
+
inc.howMany = inc.nodesToHandle;
|
|
800
|
+
}
|
|
801
|
+
else if (inc.offset >= old.offset && inc.offset < oldEnd) {
|
|
802
|
+
if (incEnd > oldEnd) {
|
|
803
|
+
inc.nodesToHandle = incEnd - oldEnd;
|
|
804
|
+
inc.offset = oldEnd;
|
|
805
|
+
}
|
|
806
|
+
else {
|
|
807
|
+
inc.nodesToHandle = 0;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
if (old.type == 'remove') {
|
|
812
|
+
// This is a case when attribute change "contains" remove change.
|
|
813
|
+
// The attribute change needs to be split into two because changes cannot intersect.
|
|
814
|
+
if (inc.offset < old.offset && incEnd > old.offset) {
|
|
815
|
+
const attributePart = {
|
|
816
|
+
type: 'attribute',
|
|
817
|
+
offset: old.offset,
|
|
818
|
+
howMany: incEnd - old.offset,
|
|
819
|
+
count: this._changeCount++
|
|
820
|
+
};
|
|
821
|
+
this._handleChange(attributePart, changes);
|
|
822
|
+
changes.push(attributePart);
|
|
823
|
+
inc.nodesToHandle = old.offset - inc.offset;
|
|
824
|
+
inc.howMany = inc.nodesToHandle;
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
if (old.type == 'attribute') {
|
|
828
|
+
// There are only two conflicting scenarios possible here:
|
|
829
|
+
if (inc.offset >= old.offset && incEnd <= oldEnd) {
|
|
830
|
+
// `old` change includes `inc` change, or they are the same.
|
|
831
|
+
inc.nodesToHandle = 0;
|
|
832
|
+
inc.howMany = 0;
|
|
833
|
+
inc.offset = 0;
|
|
834
|
+
}
|
|
835
|
+
else if (inc.offset <= old.offset && incEnd >= oldEnd) {
|
|
836
|
+
// `inc` change includes `old` change.
|
|
837
|
+
old.howMany = 0;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
inc.howMany = inc.nodesToHandle;
|
|
843
|
+
delete inc.nodesToHandle;
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Returns an object with a single insert change description.
|
|
847
|
+
*
|
|
848
|
+
* @param parent The element in which the change happened.
|
|
849
|
+
* @param offset The offset at which change happened.
|
|
850
|
+
* @param elementSnapshot The snapshot of the removed element a character.
|
|
851
|
+
* @returns The diff item.
|
|
852
|
+
*/
|
|
853
|
+
_getInsertDiff(parent, offset, elementSnapshot) {
|
|
854
|
+
return {
|
|
855
|
+
type: 'insert',
|
|
856
|
+
position: Position._createAt(parent, offset),
|
|
857
|
+
name: elementSnapshot.name,
|
|
858
|
+
attributes: new Map(elementSnapshot.attributes),
|
|
859
|
+
length: 1,
|
|
860
|
+
changeCount: this._changeCount++
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Returns an object with a single remove change description.
|
|
865
|
+
*
|
|
866
|
+
* @param parent The element in which change happened.
|
|
867
|
+
* @param offset The offset at which change happened.
|
|
868
|
+
* @param elementSnapshot The snapshot of the removed element a character.
|
|
869
|
+
* @returns The diff item.
|
|
870
|
+
*/
|
|
871
|
+
_getRemoveDiff(parent, offset, elementSnapshot) {
|
|
872
|
+
return {
|
|
873
|
+
type: 'remove',
|
|
874
|
+
position: Position._createAt(parent, offset),
|
|
875
|
+
name: elementSnapshot.name,
|
|
876
|
+
attributes: new Map(elementSnapshot.attributes),
|
|
877
|
+
length: 1,
|
|
878
|
+
changeCount: this._changeCount++
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Returns an array of objects where each one is a single attribute change description.
|
|
883
|
+
*
|
|
884
|
+
* @param range The range where the change happened.
|
|
885
|
+
* @param oldAttributes A map, map iterator or compatible object that contains attributes before the change.
|
|
886
|
+
* @param newAttributes A map, map iterator or compatible object that contains attributes after the change.
|
|
887
|
+
* @returns An array containing one or more diff items.
|
|
888
|
+
*/
|
|
889
|
+
_getAttributesDiff(range, oldAttributes, newAttributes) {
|
|
890
|
+
// Results holder.
|
|
891
|
+
const diffs = [];
|
|
892
|
+
// Clone new attributes as we will be performing changes on this object.
|
|
893
|
+
newAttributes = new Map(newAttributes);
|
|
894
|
+
// Look through old attributes.
|
|
895
|
+
for (const [key, oldValue] of oldAttributes) {
|
|
896
|
+
// Check what is the new value of the attribute (or if it was removed).
|
|
897
|
+
const newValue = newAttributes.has(key) ? newAttributes.get(key) : null;
|
|
898
|
+
// If values are different (or attribute was removed)...
|
|
899
|
+
if (newValue !== oldValue) {
|
|
900
|
+
// Add diff item.
|
|
901
|
+
diffs.push({
|
|
902
|
+
type: 'attribute',
|
|
903
|
+
position: range.start,
|
|
904
|
+
range: range.clone(),
|
|
905
|
+
length: 1,
|
|
906
|
+
attributeKey: key,
|
|
907
|
+
attributeOldValue: oldValue,
|
|
908
|
+
attributeNewValue: newValue,
|
|
909
|
+
changeCount: this._changeCount++
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
// Prevent returning two diff items for the same change.
|
|
913
|
+
newAttributes.delete(key);
|
|
914
|
+
}
|
|
915
|
+
// Look through new attributes that weren't handled above.
|
|
916
|
+
for (const [key, newValue] of newAttributes) {
|
|
917
|
+
// Each of them is a new attribute. Add diff item.
|
|
918
|
+
diffs.push({
|
|
919
|
+
type: 'attribute',
|
|
920
|
+
position: range.start,
|
|
921
|
+
range: range.clone(),
|
|
922
|
+
length: 1,
|
|
923
|
+
attributeKey: key,
|
|
924
|
+
attributeOldValue: null,
|
|
925
|
+
attributeNewValue: newValue,
|
|
926
|
+
changeCount: this._changeCount++
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
return diffs;
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Checks whether given element or any of its parents is an element that is buffered as an inserted element.
|
|
933
|
+
*/
|
|
934
|
+
_isInInsertedElement(element) {
|
|
935
|
+
const parent = element.parent;
|
|
936
|
+
if (!parent) {
|
|
937
|
+
return false;
|
|
938
|
+
}
|
|
939
|
+
const changes = this._changesInElement.get(parent);
|
|
940
|
+
const offset = element.startOffset;
|
|
941
|
+
if (changes) {
|
|
942
|
+
for (const change of changes) {
|
|
943
|
+
if (change.type == 'insert' && offset >= change.offset && offset < change.offset + change.howMany) {
|
|
944
|
+
return true;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
return this._isInInsertedElement(parent);
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Removes deeply all buffered changes that are registered in elements from range specified by `parent`, `offset`
|
|
952
|
+
* and `howMany`.
|
|
953
|
+
*/
|
|
954
|
+
_removeAllNestedChanges(parent, offset, howMany) {
|
|
955
|
+
const range = new Range(Position._createAt(parent, offset), Position._createAt(parent, offset + howMany));
|
|
956
|
+
for (const item of range.getItems({ shallow: true })) {
|
|
957
|
+
if (item.is('element')) {
|
|
958
|
+
this._elementSnapshots.delete(item);
|
|
959
|
+
this._changesInElement.delete(item);
|
|
960
|
+
this._removeAllNestedChanges(item, 0, item.maxOffset);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Returns an array that is a copy of passed child list with the exception that text nodes are split to one or more
|
|
967
|
+
* objects, each representing one character and attributes set on that character.
|
|
968
|
+
*/
|
|
969
|
+
function _getChildrenSnapshot(children) {
|
|
970
|
+
const snapshot = [];
|
|
971
|
+
for (const child of children) {
|
|
972
|
+
if (child.is('$text')) {
|
|
973
|
+
for (let i = 0; i < child.data.length; i++) {
|
|
974
|
+
snapshot.push({
|
|
975
|
+
name: '$text',
|
|
976
|
+
attributes: new Map(child.getAttributes())
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
else {
|
|
981
|
+
snapshot.push({
|
|
982
|
+
name: child.name,
|
|
983
|
+
attributes: new Map(child.getAttributes())
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return snapshot;
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Generates array of actions for given changes set.
|
|
991
|
+
* It simulates what `diff` function does.
|
|
992
|
+
* Generated actions are:
|
|
993
|
+
* - 'e' for 'equal' - when item at that position did not change,
|
|
994
|
+
* - 'i' for 'insert' - when item at that position was inserted,
|
|
995
|
+
* - 'r' for 'remove' - when item at that position was removed,
|
|
996
|
+
* - 'a' for 'attribute' - when item at that position has it attributes changed.
|
|
997
|
+
*
|
|
998
|
+
* Example (assume that uppercase letters have bold attribute, compare with function code):
|
|
999
|
+
*
|
|
1000
|
+
* children before: fooBAR
|
|
1001
|
+
* children after: foxybAR
|
|
1002
|
+
*
|
|
1003
|
+
* changes: type: remove, offset: 1, howMany: 1
|
|
1004
|
+
* type: insert, offset: 2, howMany: 2
|
|
1005
|
+
* type: attribute, offset: 4, howMany: 1
|
|
1006
|
+
*
|
|
1007
|
+
* expected actions: equal (f), remove (o), equal (o), insert (x), insert (y), attribute (b), equal (A), equal (R)
|
|
1008
|
+
*
|
|
1009
|
+
* steps taken by th script:
|
|
1010
|
+
*
|
|
1011
|
+
* 1. change = "type: remove, offset: 1, howMany: 1"; offset = 0; oldChildrenHandled = 0
|
|
1012
|
+
* 1.1 between this change and the beginning is one not-changed node, fill with one equal action, one old child has been handled
|
|
1013
|
+
* 1.2 this change removes one node, add one remove action
|
|
1014
|
+
* 1.3 change last visited `offset` to 1
|
|
1015
|
+
* 1.4 since an old child has been removed, one more old child has been handled
|
|
1016
|
+
* 1.5 actions at this point are: equal, remove
|
|
1017
|
+
*
|
|
1018
|
+
* 2. change = "type: insert, offset: 2, howMany: 2"; offset = 1; oldChildrenHandled = 2
|
|
1019
|
+
* 2.1 between this change and previous change is one not-changed node, add equal action, another one old children has been handled
|
|
1020
|
+
* 2.2 this change inserts two nodes, add two insert actions
|
|
1021
|
+
* 2.3 change last visited offset to the end of the inserted range, that is 4
|
|
1022
|
+
* 2.4 actions at this point are: equal, remove, equal, insert, insert
|
|
1023
|
+
*
|
|
1024
|
+
* 3. change = "type: attribute, offset: 4, howMany: 1"; offset = 4, oldChildrenHandled = 3
|
|
1025
|
+
* 3.1 between this change and previous change are no not-changed nodes
|
|
1026
|
+
* 3.2 this change changes one node, add one attribute action
|
|
1027
|
+
* 3.3 change last visited `offset` to the end of change range, that is 5
|
|
1028
|
+
* 3.4 since an old child has been changed, one more old child has been handled
|
|
1029
|
+
* 3.5 actions at this point are: equal, remove, equal, insert, insert, attribute
|
|
1030
|
+
*
|
|
1031
|
+
* 4. after loop oldChildrenHandled = 4, oldChildrenLength = 6 (fooBAR is 6 characters)
|
|
1032
|
+
* 4.1 fill up with two equal actions
|
|
1033
|
+
*
|
|
1034
|
+
* The result actions are: equal, remove, equal, insert, insert, attribute, equal, equal.
|
|
1035
|
+
*/
|
|
1036
|
+
function _generateActionsFromChanges(oldChildrenLength, changes) {
|
|
1037
|
+
const actions = [];
|
|
1038
|
+
let offset = 0;
|
|
1039
|
+
let oldChildrenHandled = 0;
|
|
1040
|
+
// Go through all buffered changes.
|
|
1041
|
+
for (const change of changes) {
|
|
1042
|
+
// First, fill "holes" between changes with "equal" actions.
|
|
1043
|
+
if (change.offset > offset) {
|
|
1044
|
+
for (let i = 0; i < change.offset - offset; i++) {
|
|
1045
|
+
actions.push('e');
|
|
1046
|
+
}
|
|
1047
|
+
oldChildrenHandled += change.offset - offset;
|
|
1048
|
+
}
|
|
1049
|
+
// Then, fill up actions accordingly to change type.
|
|
1050
|
+
if (change.type == 'insert') {
|
|
1051
|
+
for (let i = 0; i < change.howMany; i++) {
|
|
1052
|
+
actions.push('i');
|
|
1053
|
+
}
|
|
1054
|
+
// The last handled offset is after inserted range.
|
|
1055
|
+
offset = change.offset + change.howMany;
|
|
1056
|
+
}
|
|
1057
|
+
else if (change.type == 'remove') {
|
|
1058
|
+
for (let i = 0; i < change.howMany; i++) {
|
|
1059
|
+
actions.push('r');
|
|
1060
|
+
}
|
|
1061
|
+
// The last handled offset is at the position where the nodes were removed.
|
|
1062
|
+
offset = change.offset;
|
|
1063
|
+
// We removed `howMany` old nodes, update `oldChildrenHandled`.
|
|
1064
|
+
oldChildrenHandled += change.howMany;
|
|
1065
|
+
}
|
|
1066
|
+
else {
|
|
1067
|
+
actions.push(...'a'.repeat(change.howMany).split(''));
|
|
1068
|
+
// The last handled offset is at the position after the changed range.
|
|
1069
|
+
offset = change.offset + change.howMany;
|
|
1070
|
+
// We changed `howMany` old nodes, update `oldChildrenHandled`.
|
|
1071
|
+
oldChildrenHandled += change.howMany;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
// Fill "equal" actions at the end of actions set. Use `oldChildrenHandled` to see how many children
|
|
1075
|
+
// has not been changed / removed at the end of their parent.
|
|
1076
|
+
if (oldChildrenHandled < oldChildrenLength) {
|
|
1077
|
+
for (let i = 0; i < oldChildrenLength - oldChildrenHandled - offset; i++) {
|
|
1078
|
+
actions.push('e');
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
return actions;
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Filter callback for Array.filter that filters out change entries that are in graveyard.
|
|
1085
|
+
*/
|
|
1086
|
+
function _changesInGraveyardFilter(entry) {
|
|
1087
|
+
const posInGy = 'position' in entry && entry.position.root.rootName == '$graveyard';
|
|
1088
|
+
const rangeInGy = 'range' in entry && entry.range.root.rootName == '$graveyard';
|
|
1089
|
+
return !posInGy && !rangeInGy;
|
|
1090
|
+
}
|