@ckeditor/ckeditor5-engine 35.0.1 → 35.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -4
- package/package.json +30 -24
- package/src/controller/datacontroller.js +467 -561
- package/src/controller/editingcontroller.js +168 -204
- package/src/conversion/conversion.js +541 -565
- package/src/conversion/conversionhelpers.js +24 -28
- package/src/conversion/downcastdispatcher.js +457 -686
- package/src/conversion/downcasthelpers.js +1583 -1965
- package/src/conversion/mapper.js +518 -707
- package/src/conversion/modelconsumable.js +240 -283
- package/src/conversion/upcastdispatcher.js +372 -718
- package/src/conversion/upcasthelpers.js +707 -818
- package/src/conversion/viewconsumable.js +524 -581
- package/src/dataprocessor/basichtmlwriter.js +12 -16
- package/src/dataprocessor/dataprocessor.js +5 -0
- package/src/dataprocessor/htmldataprocessor.js +100 -116
- package/src/dataprocessor/htmlwriter.js +1 -18
- package/src/dataprocessor/xmldataprocessor.js +116 -137
- package/src/dev-utils/model.js +260 -352
- package/src/dev-utils/operationreplayer.js +106 -126
- package/src/dev-utils/utils.js +34 -51
- package/src/dev-utils/view.js +632 -753
- package/src/index.js +0 -11
- package/src/model/batch.js +111 -127
- package/src/model/differ.js +988 -1233
- package/src/model/document.js +340 -449
- package/src/model/documentfragment.js +327 -364
- package/src/model/documentselection.js +996 -1189
- package/src/model/element.js +306 -410
- package/src/model/history.js +224 -262
- package/src/model/item.js +5 -0
- package/src/model/liveposition.js +84 -145
- package/src/model/liverange.js +108 -185
- package/src/model/markercollection.js +379 -480
- package/src/model/model.js +883 -1034
- package/src/model/node.js +419 -463
- package/src/model/nodelist.js +176 -201
- package/src/model/operation/attributeoperation.js +153 -182
- package/src/model/operation/detachoperation.js +64 -83
- package/src/model/operation/insertoperation.js +135 -166
- package/src/model/operation/markeroperation.js +114 -140
- package/src/model/operation/mergeoperation.js +163 -191
- package/src/model/operation/moveoperation.js +157 -187
- package/src/model/operation/nooperation.js +28 -38
- package/src/model/operation/operation.js +106 -125
- package/src/model/operation/operationfactory.js +30 -34
- package/src/model/operation/renameoperation.js +109 -135
- package/src/model/operation/rootattributeoperation.js +155 -188
- package/src/model/operation/splitoperation.js +196 -232
- package/src/model/operation/transform.js +1833 -2204
- package/src/model/operation/utils.js +140 -204
- package/src/model/position.js +980 -1053
- package/src/model/range.js +910 -1028
- package/src/model/rootelement.js +77 -97
- package/src/model/schema.js +1189 -1835
- package/src/model/selection.js +745 -862
- package/src/model/text.js +90 -114
- package/src/model/textproxy.js +204 -240
- package/src/model/treewalker.js +316 -397
- package/src/model/typecheckable.js +16 -0
- package/src/model/utils/autoparagraphing.js +32 -44
- package/src/model/utils/deletecontent.js +334 -418
- package/src/model/utils/findoptimalinsertionrange.js +25 -36
- package/src/model/utils/getselectedcontent.js +96 -118
- package/src/model/utils/insertcontent.js +757 -773
- package/src/model/utils/insertobject.js +96 -119
- package/src/model/utils/modifyselection.js +120 -158
- package/src/model/utils/selection-post-fixer.js +153 -201
- package/src/model/writer.js +1305 -1474
- package/src/view/attributeelement.js +189 -225
- package/src/view/containerelement.js +75 -85
- package/src/view/document.js +172 -215
- package/src/view/documentfragment.js +200 -249
- package/src/view/documentselection.js +338 -367
- package/src/view/domconverter.js +1370 -1617
- package/src/view/downcastwriter.js +1747 -2076
- package/src/view/editableelement.js +81 -97
- package/src/view/element.js +739 -890
- package/src/view/elementdefinition.js +5 -0
- package/src/view/emptyelement.js +82 -92
- package/src/view/filler.js +35 -50
- package/src/view/item.js +5 -0
- package/src/view/matcher.js +260 -559
- package/src/view/node.js +274 -360
- package/src/view/observer/arrowkeysobserver.js +19 -28
- package/src/view/observer/bubblingemittermixin.js +120 -263
- package/src/view/observer/bubblingeventinfo.js +47 -55
- package/src/view/observer/clickobserver.js +7 -13
- package/src/view/observer/compositionobserver.js +14 -24
- package/src/view/observer/domeventdata.js +57 -67
- package/src/view/observer/domeventobserver.js +40 -64
- package/src/view/observer/fakeselectionobserver.js +81 -96
- package/src/view/observer/focusobserver.js +45 -61
- package/src/view/observer/inputobserver.js +7 -13
- package/src/view/observer/keyobserver.js +17 -27
- package/src/view/observer/mouseobserver.js +7 -14
- package/src/view/observer/mutationobserver.js +220 -315
- package/src/view/observer/observer.js +81 -102
- package/src/view/observer/selectionobserver.js +199 -246
- package/src/view/observer/tabobserver.js +23 -36
- package/src/view/placeholder.js +128 -173
- package/src/view/position.js +350 -401
- package/src/view/range.js +453 -513
- package/src/view/rawelement.js +85 -112
- package/src/view/renderer.js +874 -1018
- package/src/view/rooteditableelement.js +80 -90
- package/src/view/selection.js +608 -689
- package/src/view/styles/background.js +43 -44
- package/src/view/styles/border.js +220 -276
- package/src/view/styles/margin.js +8 -17
- package/src/view/styles/padding.js +8 -16
- package/src/view/styles/utils.js +127 -160
- package/src/view/stylesmap.js +728 -905
- package/src/view/text.js +102 -126
- package/src/view/textproxy.js +144 -170
- package/src/view/treewalker.js +383 -479
- package/src/view/typecheckable.js +19 -0
- package/src/view/uielement.js +166 -187
- package/src/view/upcastwriter.js +395 -449
- package/src/view/view.js +569 -664
- package/src/dataprocessor/dataprocessor.jsdoc +0 -64
- package/src/model/item.jsdoc +0 -14
- package/src/view/elementdefinition.jsdoc +0 -59
- package/src/view/item.jsdoc +0 -14
|
@@ -2,15 +2,12 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module engine/model/utils/deletecontent
|
|
8
7
|
*/
|
|
9
|
-
|
|
8
|
+
import DocumentSelection from '../documentselection';
|
|
10
9
|
import LivePosition from '../liveposition';
|
|
11
10
|
import Range from '../range';
|
|
12
|
-
import DocumentSelection from '../documentselection';
|
|
13
|
-
|
|
14
11
|
/**
|
|
15
12
|
* Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
|
|
16
13
|
*
|
|
@@ -57,82 +54,66 @@ import DocumentSelection from '../documentselection';
|
|
|
57
54
|
*
|
|
58
55
|
* `[<imageBlock src="foo.jpg"></imageBlock>]` -> `<paragraph>[]</paragraph>`.
|
|
59
56
|
*/
|
|
60
|
-
export default function deleteContent(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
schema.removeDisallowedAttributes( startPosition.parent.getChildren(), writer );
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
collapseSelectionAt( writer, selection, startPosition );
|
|
123
|
-
|
|
124
|
-
// 4. Add a paragraph to set selection in it.
|
|
125
|
-
// Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
|
|
126
|
-
// If autoparagraphing is off, we assume that you know what you do so we leave the selection wherever it was.
|
|
127
|
-
if ( !options.doNotAutoparagraph && shouldAutoparagraph( schema, startPosition ) ) {
|
|
128
|
-
insertParagraph( writer, startPosition, selection, attributesForAutoparagraph );
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
startPosition.detach();
|
|
132
|
-
endPosition.detach();
|
|
133
|
-
} );
|
|
57
|
+
export default function deleteContent(model, selection, options = {}) {
|
|
58
|
+
if (selection.isCollapsed) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const selRange = selection.getFirstRange();
|
|
62
|
+
// If the selection is already removed, don't do anything.
|
|
63
|
+
if (selRange.root.rootName == '$graveyard') {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const schema = model.schema;
|
|
67
|
+
model.change(writer => {
|
|
68
|
+
// 1. Replace the entire content with paragraph.
|
|
69
|
+
// See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
|
|
70
|
+
if (!options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph(schema, selection)) {
|
|
71
|
+
replaceEntireContentWithParagraph(writer, selection);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
// Collect attributes to copy in case of autoparagraphing.
|
|
75
|
+
const attributesForAutoparagraph = {};
|
|
76
|
+
if (!options.doNotAutoparagraph) {
|
|
77
|
+
const selectedElement = selection.getSelectedElement();
|
|
78
|
+
if (selectedElement) {
|
|
79
|
+
Object.assign(attributesForAutoparagraph, schema.getAttributesWithProperty(selectedElement, 'copyOnReplace', true));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Get the live positions for the range adjusted to span only blocks selected from the user perspective.
|
|
83
|
+
const [startPosition, endPosition] = getLivePositionsForSelectedBlocks(selRange);
|
|
84
|
+
// 2. Remove the content if there is any.
|
|
85
|
+
if (!startPosition.isTouching(endPosition)) {
|
|
86
|
+
writer.remove(writer.createRange(startPosition, endPosition));
|
|
87
|
+
}
|
|
88
|
+
// 3. Merge elements in the right branch to the elements in the left branch.
|
|
89
|
+
// The only reasonable (in terms of data and selection correctness) case in which we need to do that is:
|
|
90
|
+
//
|
|
91
|
+
// <heading type=1>Fo[</heading><paragraph>]ar</paragraph> => <heading type=1>Fo^ar</heading>
|
|
92
|
+
//
|
|
93
|
+
// However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch),
|
|
94
|
+
// as it's hard to imagine what should actually be the default behavior. Usually, specific features will
|
|
95
|
+
// want to override that behavior anyway.
|
|
96
|
+
if (!options.leaveUnmerged) {
|
|
97
|
+
mergeBranches(writer, startPosition, endPosition);
|
|
98
|
+
// TMP this will be replaced with a postfixer.
|
|
99
|
+
// We need to check and strip disallowed attributes in all nested nodes because after merge
|
|
100
|
+
// some attributes could end up in a path where are disallowed.
|
|
101
|
+
//
|
|
102
|
+
// e.g. bold is disallowed for <H1>
|
|
103
|
+
// <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
|
|
104
|
+
schema.removeDisallowedAttributes(startPosition.parent.getChildren(), writer);
|
|
105
|
+
}
|
|
106
|
+
collapseSelectionAt(writer, selection, startPosition);
|
|
107
|
+
// 4. Add a paragraph to set selection in it.
|
|
108
|
+
// Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
|
|
109
|
+
// If autoparagraphing is off, we assume that you know what you do so we leave the selection wherever it was.
|
|
110
|
+
if (!options.doNotAutoparagraph && shouldAutoparagraph(schema, startPosition)) {
|
|
111
|
+
insertParagraph(writer, startPosition, selection, attributesForAutoparagraph);
|
|
112
|
+
}
|
|
113
|
+
startPosition.detach();
|
|
114
|
+
endPosition.detach();
|
|
115
|
+
});
|
|
134
116
|
}
|
|
135
|
-
|
|
136
117
|
// Returns the live positions for the range adjusted to span only blocks selected from the user perspective. Example:
|
|
137
118
|
//
|
|
138
119
|
// <heading1>[foo</heading1>
|
|
@@ -140,122 +121,105 @@ export default function deleteContent( model, selection, options = {} ) {
|
|
|
140
121
|
// <heading1>]abc</heading1> <-- this block is not considered as selected
|
|
141
122
|
//
|
|
142
123
|
// This is the same behavior as in Selection#getSelectedBlocks() "special case".
|
|
143
|
-
function getLivePositionsForSelectedBlocks(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return [
|
|
180
|
-
LivePosition.fromPosition( startPosition, 'toPrevious' ),
|
|
181
|
-
LivePosition.fromPosition( endPosition, 'toNext' )
|
|
182
|
-
];
|
|
124
|
+
function getLivePositionsForSelectedBlocks(range) {
|
|
125
|
+
const model = range.root.document.model;
|
|
126
|
+
const startPosition = range.start;
|
|
127
|
+
let endPosition = range.end;
|
|
128
|
+
// If the end of selection is at the start position of last block in the selection, then
|
|
129
|
+
// shrink it to not include that trailing block. Note that this should happen only for not empty selection.
|
|
130
|
+
if (model.hasContent(range, { ignoreMarkers: true })) {
|
|
131
|
+
const endBlock = getParentBlock(endPosition);
|
|
132
|
+
if (endBlock && endPosition.isTouching(model.createPositionAt(endBlock, 0))) {
|
|
133
|
+
// Create forward selection as a probe to find a valid position after excluding last block from the range.
|
|
134
|
+
const selection = model.createSelection(range);
|
|
135
|
+
// Modify the forward selection in backward direction to shrink it and remove first position of following block from it.
|
|
136
|
+
// This is how modifySelection works and here we are making use of it.
|
|
137
|
+
model.modifySelection(selection, { direction: 'backward' });
|
|
138
|
+
const newEndPosition = selection.getLastPosition();
|
|
139
|
+
// For such a model and selection:
|
|
140
|
+
// <paragraph>A[</paragraph><imageBlock></imageBlock><paragraph>]B</paragraph>
|
|
141
|
+
//
|
|
142
|
+
// After modifySelection(), we would end up with this:
|
|
143
|
+
// <paragraph>A[</paragraph>]<imageBlock></imageBlock><paragraph>B</paragraph>
|
|
144
|
+
//
|
|
145
|
+
// So we need to check if there is no content in the skipped range (because we want to include the <imageBlock>).
|
|
146
|
+
const skippedRange = model.createRange(newEndPosition, endPosition);
|
|
147
|
+
if (!model.hasContent(skippedRange, { ignoreMarkers: true })) {
|
|
148
|
+
endPosition = newEndPosition;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return [
|
|
153
|
+
LivePosition.fromPosition(startPosition, 'toPrevious'),
|
|
154
|
+
LivePosition.fromPosition(endPosition, 'toNext')
|
|
155
|
+
];
|
|
183
156
|
}
|
|
184
|
-
|
|
185
157
|
// Finds the lowest element in position's ancestors which is a block.
|
|
186
158
|
// Returns null if a limit element is encountered before reaching a block element.
|
|
187
|
-
function getParentBlock(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
}
|
|
159
|
+
function getParentBlock(position) {
|
|
160
|
+
const element = position.parent;
|
|
161
|
+
const schema = element.root.document.model.schema;
|
|
162
|
+
const ancestors = element.getAncestors({ parentFirst: true, includeSelf: true });
|
|
163
|
+
for (const element of ancestors) {
|
|
164
|
+
if (schema.isLimit(element)) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
if (schema.isBlock(element)) {
|
|
168
|
+
return element;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
201
171
|
}
|
|
202
|
-
|
|
203
172
|
// This function is a result of reaching the Ballmer's peak for just the right amount of time.
|
|
204
173
|
// Even I had troubles documenting it after a while and after reading it again I couldn't believe that it really works.
|
|
205
|
-
function mergeBranches(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
mergeBranchesRight( writer, startPosition, endPosition, startAncestor.parent );
|
|
254
|
-
} else {
|
|
255
|
-
mergeBranchesLeft( writer, startPosition, endPosition, startAncestor.parent );
|
|
256
|
-
}
|
|
174
|
+
function mergeBranches(writer, startPosition, endPosition) {
|
|
175
|
+
const model = writer.model;
|
|
176
|
+
// Verify if there is a need and possibility to merge.
|
|
177
|
+
if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
// If the start element on the common ancestor level is empty, and the end element on the same level is not empty
|
|
181
|
+
// then merge those to the right element so that it's properties are preserved (name, attributes).
|
|
182
|
+
// Because of OT merging is used instead of removing elements.
|
|
183
|
+
//
|
|
184
|
+
// Merge left:
|
|
185
|
+
// <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
|
|
186
|
+
// <paragraph>]bar</paragraph> -> --^
|
|
187
|
+
//
|
|
188
|
+
// Merge right:
|
|
189
|
+
// <heading1>[</heading1> ->
|
|
190
|
+
// <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
|
|
191
|
+
//
|
|
192
|
+
// Merge left:
|
|
193
|
+
// <blockQuote> -> <blockQuote>
|
|
194
|
+
// <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
|
|
195
|
+
// <paragraph>]bar</paragraph> -> --^
|
|
196
|
+
// </blockQuote> -> </blockQuote>
|
|
197
|
+
//
|
|
198
|
+
// Merge right:
|
|
199
|
+
// <blockQuote> -> <blockQuote>
|
|
200
|
+
// <heading1>[</heading1> ->
|
|
201
|
+
// <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
|
|
202
|
+
// </blockQuote> -> </blockQuote>
|
|
203
|
+
// Merging should not go deeper than common ancestor.
|
|
204
|
+
const [startAncestor, endAncestor] = getAncestorsJustBelowCommonAncestor(startPosition, endPosition);
|
|
205
|
+
// Branches can't be merged if one of the positions is directly inside a common ancestor.
|
|
206
|
+
//
|
|
207
|
+
// Example:
|
|
208
|
+
// <blockQuote>
|
|
209
|
+
// <paragraph>[foo</paragraph>]
|
|
210
|
+
// <table> ... </table>
|
|
211
|
+
// <blockQuote>
|
|
212
|
+
//
|
|
213
|
+
if (!startAncestor || !endAncestor) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (!model.hasContent(startAncestor, { ignoreMarkers: true }) && model.hasContent(endAncestor, { ignoreMarkers: true })) {
|
|
217
|
+
mergeBranchesRight(writer, startPosition, endPosition, startAncestor.parent);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
mergeBranchesLeft(writer, startPosition, endPosition, startAncestor.parent);
|
|
221
|
+
}
|
|
257
222
|
}
|
|
258
|
-
|
|
259
223
|
// Merging blocks to the left (properties of the left block are preserved).
|
|
260
224
|
// Simple example:
|
|
261
225
|
// <heading1>foo[</heading1> -> <heading1>foo[bar</heading1>]
|
|
@@ -269,72 +233,62 @@ function mergeBranches( writer, startPosition, endPosition ) {
|
|
|
269
233
|
// <paragraph>]bar</paragraph> -> ---
|
|
270
234
|
// </blockBlock> ->
|
|
271
235
|
//
|
|
272
|
-
function mergeBranchesLeft(
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// Verify if there is a need and possibility to merge next level.
|
|
330
|
-
if ( !checkShouldMerge( writer.model.schema, startPosition, endPosition ) ) {
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
|
|
335
|
-
mergeBranchesLeft( writer, startPosition, endPosition, commonAncestor );
|
|
236
|
+
function mergeBranchesLeft(writer, startPosition, endPosition, commonAncestor) {
|
|
237
|
+
const startElement = startPosition.parent;
|
|
238
|
+
const endElement = endPosition.parent;
|
|
239
|
+
// Merging reached the common ancestor element, stop here.
|
|
240
|
+
if (startElement == commonAncestor || endElement == commonAncestor) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
// Remember next positions to merge in next recursive step (also used as modification points pointers).
|
|
244
|
+
startPosition = writer.createPositionAfter(startElement);
|
|
245
|
+
endPosition = writer.createPositionBefore(endElement);
|
|
246
|
+
// Move endElement just after startElement if they aren't siblings.
|
|
247
|
+
if (!endPosition.isEqual(startPosition)) {
|
|
248
|
+
//
|
|
249
|
+
// <blockQuote> -> <blockQuote>
|
|
250
|
+
// <heading1>foo[</heading1> -> <heading1>foo</heading1>[<paragraph>bar</paragraph>
|
|
251
|
+
// </blockQuote> -> </blockQuote> ^
|
|
252
|
+
// <blockBlock> -> <blockBlock> |
|
|
253
|
+
// <paragraph>]bar</paragraph> -> ] ---
|
|
254
|
+
// </blockBlock> -> </blockBlock>
|
|
255
|
+
//
|
|
256
|
+
writer.insert(endElement, startPosition);
|
|
257
|
+
}
|
|
258
|
+
// Merge two siblings (nodes on sides of startPosition):
|
|
259
|
+
//
|
|
260
|
+
// <blockQuote> -> <blockQuote>
|
|
261
|
+
// <heading1>foo</heading1>[<paragraph>bar</paragraph> -> <heading1>foo[bar</heading1>
|
|
262
|
+
// </blockQuote> -> </blockQuote>
|
|
263
|
+
// <blockBlock> -> <blockBlock>
|
|
264
|
+
// ] -> ]
|
|
265
|
+
// </blockBlock> -> </blockBlock>
|
|
266
|
+
//
|
|
267
|
+
// Or in simple case (without moving elements in above if):
|
|
268
|
+
// <heading1>foo</heading1>[<paragraph>bar</paragraph>] -> <heading1>foo[bar</heading1>]
|
|
269
|
+
//
|
|
270
|
+
writer.merge(startPosition);
|
|
271
|
+
// Remove empty end ancestors:
|
|
272
|
+
//
|
|
273
|
+
// <blockQuote> -> <blockQuote>
|
|
274
|
+
// <heading1>foo[bar</heading1> -> <heading1>foo[bar</heading1>
|
|
275
|
+
// </blockQuote> -> </blockQuote>
|
|
276
|
+
// <blockBlock> ->
|
|
277
|
+
// ] -> ]
|
|
278
|
+
// </blockBlock> ->
|
|
279
|
+
//
|
|
280
|
+
while (endPosition.parent.isEmpty) {
|
|
281
|
+
const parentToRemove = endPosition.parent;
|
|
282
|
+
endPosition = writer.createPositionBefore(parentToRemove);
|
|
283
|
+
writer.remove(parentToRemove);
|
|
284
|
+
}
|
|
285
|
+
// Verify if there is a need and possibility to merge next level.
|
|
286
|
+
if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
// Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
|
|
290
|
+
mergeBranchesLeft(writer, startPosition, endPosition, commonAncestor);
|
|
336
291
|
}
|
|
337
|
-
|
|
338
292
|
// Merging blocks to the right (properties of the right block are preserved).
|
|
339
293
|
// Simple example:
|
|
340
294
|
// <heading1>foo[</heading1> -> --v
|
|
@@ -348,194 +302,156 @@ function mergeBranchesLeft( writer, startPosition, endPosition, commonAncestor )
|
|
|
348
302
|
// <paragraph>]bar</paragraph> -> <paragraph>foo]bar</paragraph>
|
|
349
303
|
// </blockBlock> -> </blockBlock>
|
|
350
304
|
//
|
|
351
|
-
function mergeBranchesRight(
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
//
|
|
408
|
-
mergeRight( writer, endPosition );
|
|
409
|
-
|
|
410
|
-
// Verify if there is a need and possibility to merge next level.
|
|
411
|
-
if ( !checkShouldMerge( writer.model.schema, startPosition, endPosition ) ) {
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
// Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
|
|
416
|
-
mergeBranchesRight( writer, startPosition, endPosition, commonAncestor );
|
|
305
|
+
function mergeBranchesRight(writer, startPosition, endPosition, commonAncestor) {
|
|
306
|
+
const startElement = startPosition.parent;
|
|
307
|
+
const endElement = endPosition.parent;
|
|
308
|
+
// Merging reached the common ancestor element, stop here.
|
|
309
|
+
if (startElement == commonAncestor || endElement == commonAncestor) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
// Remember next positions to merge in next recursive step (also used as modification points pointers).
|
|
313
|
+
startPosition = writer.createPositionAfter(startElement);
|
|
314
|
+
endPosition = writer.createPositionBefore(endElement);
|
|
315
|
+
// Move startElement just before endElement if they aren't siblings.
|
|
316
|
+
if (!endPosition.isEqual(startPosition)) {
|
|
317
|
+
//
|
|
318
|
+
// <blockQuote> -> <blockQuote>
|
|
319
|
+
// <heading1>foo[</heading1> -> [ ---
|
|
320
|
+
// </blockQuote> -> </blockQuote> |
|
|
321
|
+
// <blockBlock> -> <blockBlock> v
|
|
322
|
+
// <paragraph>]bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
|
|
323
|
+
// </blockBlock> -> </blockBlock>
|
|
324
|
+
//
|
|
325
|
+
writer.insert(startElement, endPosition);
|
|
326
|
+
}
|
|
327
|
+
// Remove empty end ancestors:
|
|
328
|
+
//
|
|
329
|
+
// <blockQuote> ->
|
|
330
|
+
// [ -> [
|
|
331
|
+
// </blockQuote> ->
|
|
332
|
+
// <blockBlock> -> <blockBlock>
|
|
333
|
+
// <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
|
|
334
|
+
// </blockBlock> -> </blockBlock>
|
|
335
|
+
//
|
|
336
|
+
while (startPosition.parent.isEmpty) {
|
|
337
|
+
const parentToRemove = startPosition.parent;
|
|
338
|
+
startPosition = writer.createPositionBefore(parentToRemove);
|
|
339
|
+
writer.remove(parentToRemove);
|
|
340
|
+
}
|
|
341
|
+
// Update endPosition after inserting and removing elements.
|
|
342
|
+
endPosition = writer.createPositionBefore(endElement);
|
|
343
|
+
// Merge right two siblings (nodes on sides of endPosition):
|
|
344
|
+
// ->
|
|
345
|
+
// [ -> [
|
|
346
|
+
// ->
|
|
347
|
+
// <blockBlock> -> <blockBlock>
|
|
348
|
+
// <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <paragraph>foo]bar</paragraph>
|
|
349
|
+
// </blockBlock> -> </blockBlock>
|
|
350
|
+
//
|
|
351
|
+
// Or in simple case (without moving elements in above if):
|
|
352
|
+
// [<heading1>foo</heading1>]<paragraph>bar</paragraph> -> [<heading1>foo]bar</heading1>
|
|
353
|
+
//
|
|
354
|
+
mergeRight(writer, endPosition);
|
|
355
|
+
// Verify if there is a need and possibility to merge next level.
|
|
356
|
+
if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
// Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
|
|
360
|
+
mergeBranchesRight(writer, startPosition, endPosition, commonAncestor);
|
|
417
361
|
}
|
|
418
|
-
|
|
419
362
|
// There is no right merge operation so we need to simulate it.
|
|
420
|
-
function mergeRight(
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
writer.setAttributes( Object.fromEntries( endElement.getAttributes() ), startElement );
|
|
430
|
-
|
|
431
|
-
writer.merge( position );
|
|
363
|
+
function mergeRight(writer, position) {
|
|
364
|
+
const startElement = position.nodeBefore;
|
|
365
|
+
const endElement = position.nodeAfter;
|
|
366
|
+
if (startElement.name != endElement.name) {
|
|
367
|
+
writer.rename(startElement, endElement.name);
|
|
368
|
+
}
|
|
369
|
+
writer.clearAttributes(startElement);
|
|
370
|
+
writer.setAttributes(Object.fromEntries(endElement.getAttributes()), startElement);
|
|
371
|
+
writer.merge(position);
|
|
432
372
|
}
|
|
433
|
-
|
|
434
373
|
// Verifies if merging is needed and possible. It's not needed if both positions are in the same element
|
|
435
374
|
// and it's not possible if some element is a limit or the range crosses a limit element.
|
|
436
|
-
function checkShouldMerge(
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
// E.g., we can't merge endElement into startElement in this case:
|
|
453
|
-
// <limit><startElement>x[</startElement></limit><endElement>]</endElement>
|
|
454
|
-
return isCrossingLimitElement( startPosition, endPosition, schema );
|
|
375
|
+
function checkShouldMerge(schema, startPosition, endPosition) {
|
|
376
|
+
const startElement = startPosition.parent;
|
|
377
|
+
const endElement = endPosition.parent;
|
|
378
|
+
// If both positions ended up in the same parent, then there's nothing more to merge:
|
|
379
|
+
// <$root><p>x[</p><p>]y</p></$root> => <$root><p>xy</p>[]</$root>
|
|
380
|
+
if (startElement == endElement) {
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
// If one of the positions is a limit element, then there's nothing to merge because we don't want to cross the limit boundaries.
|
|
384
|
+
if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
// Check if operations we'll need to do won't need to cross object or limit boundaries.
|
|
388
|
+
// E.g., we can't merge endElement into startElement in this case:
|
|
389
|
+
// <limit><startElement>x[</startElement></limit><endElement>]</endElement>
|
|
390
|
+
return isCrossingLimitElement(startPosition, endPosition, schema);
|
|
455
391
|
}
|
|
456
|
-
|
|
457
392
|
// Returns the elements that are the ancestors of the provided positions that are direct children of the common ancestor.
|
|
458
|
-
function getAncestorsJustBelowCommonAncestor(
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
return [ ancestorsA[ i ], ancestorsB[ i ] ];
|
|
393
|
+
function getAncestorsJustBelowCommonAncestor(positionA, positionB) {
|
|
394
|
+
const ancestorsA = positionA.getAncestors();
|
|
395
|
+
const ancestorsB = positionB.getAncestors();
|
|
396
|
+
let i = 0;
|
|
397
|
+
while (ancestorsA[i] && ancestorsA[i] == ancestorsB[i]) {
|
|
398
|
+
i++;
|
|
399
|
+
}
|
|
400
|
+
return [ancestorsA[i], ancestorsB[i]];
|
|
469
401
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
return !isTextAllowed && isParagraphAllowed;
|
|
402
|
+
function shouldAutoparagraph(schema, position) {
|
|
403
|
+
const isTextAllowed = schema.checkChild(position, '$text');
|
|
404
|
+
const isParagraphAllowed = schema.checkChild(position, 'paragraph');
|
|
405
|
+
return !isTextAllowed && isParagraphAllowed;
|
|
476
406
|
}
|
|
477
|
-
|
|
478
407
|
// Check if parents of two positions can be merged by checking if there are no limit/object
|
|
479
408
|
// boundaries between those two positions.
|
|
480
409
|
//
|
|
481
410
|
// E.g. in <bQ><p>x[]</p></bQ><widget><caption>{}</caption></widget>
|
|
482
411
|
// we'll check <p>, <bQ>, <widget> and <caption>.
|
|
483
412
|
// Usually, widget and caption are marked as objects/limits in the schema, so in this case merging will be blocked.
|
|
484
|
-
function isCrossingLimitElement(
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
return true;
|
|
413
|
+
function isCrossingLimitElement(leftPos, rightPos, schema) {
|
|
414
|
+
const rangeToCheck = new Range(leftPos, rightPos);
|
|
415
|
+
for (const value of rangeToCheck.getWalker()) {
|
|
416
|
+
if (schema.isLimit(value.item)) {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return true;
|
|
494
421
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
writer.insert( paragraph, position );
|
|
502
|
-
|
|
503
|
-
collapseSelectionAt( writer, selection, writer.createPositionAt( paragraph, 0 ) );
|
|
422
|
+
function insertParagraph(writer, position, selection, attributes = {}) {
|
|
423
|
+
const paragraph = writer.createElement('paragraph');
|
|
424
|
+
writer.model.schema.setAllowedAttributes(paragraph, attributes, writer);
|
|
425
|
+
writer.insert(paragraph, position);
|
|
426
|
+
collapseSelectionAt(writer, selection, writer.createPositionAt(paragraph, 0));
|
|
504
427
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
writer.remove( writer.createRangeIn( limitElement ) );
|
|
510
|
-
insertParagraph( writer, writer.createPositionAt( limitElement, 0 ), selection );
|
|
428
|
+
function replaceEntireContentWithParagraph(writer, selection) {
|
|
429
|
+
const limitElement = writer.model.schema.getLimitElement(selection);
|
|
430
|
+
writer.remove(writer.createRangeIn(limitElement));
|
|
431
|
+
insertParagraph(writer, writer.createPositionAt(limitElement, 0), selection);
|
|
511
432
|
}
|
|
512
|
-
|
|
513
433
|
// We want to replace the entire content with a paragraph when:
|
|
514
434
|
// * the entire content is selected,
|
|
515
435
|
// * selection contains at least two elements,
|
|
516
436
|
// * whether the paragraph is allowed in schema in the common ancestor.
|
|
517
|
-
function shouldEntireContentBeReplacedWithParagraph(
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
return false;
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
return schema.checkChild( limitElement, 'paragraph' );
|
|
437
|
+
function shouldEntireContentBeReplacedWithParagraph(schema, selection) {
|
|
438
|
+
const limitElement = schema.getLimitElement(selection);
|
|
439
|
+
if (!selection.containsEntireContent(limitElement)) {
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
const range = selection.getFirstRange();
|
|
443
|
+
if (range.start.parent == range.end.parent) {
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
return schema.checkChild(limitElement, 'paragraph');
|
|
531
447
|
}
|
|
532
|
-
|
|
533
448
|
// Helper function that sets the selection. Depending whether given `selection` is a document selection or not,
|
|
534
449
|
// uses a different method to set it.
|
|
535
|
-
function collapseSelectionAt(
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
450
|
+
function collapseSelectionAt(writer, selection, positionOrRange) {
|
|
451
|
+
if (selection instanceof DocumentSelection) {
|
|
452
|
+
writer.setSelection(positionOrRange);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
selection.setTo(positionOrRange);
|
|
456
|
+
}
|
|
541
457
|
}
|