@ckeditor/ckeditor5-engine 32.0.0 → 34.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +2 -2
- package/README.md +2 -1
- package/package.json +22 -22
- package/src/controller/datacontroller.js +58 -66
- package/src/controller/editingcontroller.js +82 -5
- package/src/conversion/conversion.js +14 -13
- package/src/conversion/downcastdispatcher.js +297 -366
- package/src/conversion/downcasthelpers.js +859 -80
- package/src/conversion/mapper.js +104 -59
- package/src/conversion/modelconsumable.js +84 -34
- package/src/conversion/upcastdispatcher.js +33 -6
- package/src/conversion/upcasthelpers.js +21 -2
- package/src/dataprocessor/htmldataprocessor.js +1 -1
- package/src/dev-utils/model.js +13 -11
- package/src/index.js +12 -0
- package/src/model/batch.js +12 -12
- package/src/model/differ.js +114 -68
- package/src/model/document.js +32 -31
- package/src/model/history.js +160 -22
- package/src/model/markercollection.js +28 -4
- package/src/model/model.js +122 -4
- package/src/model/schema.js +79 -10
- package/src/model/treewalker.js +2 -3
- package/src/model/utils/deletecontent.js +15 -2
- package/src/model/utils/findoptimalinsertionrange.js +68 -0
- package/src/model/utils/insertobject.js +173 -0
- package/src/model/utils/modifyselection.js +14 -7
- package/src/model/writer.js +16 -26
- package/src/view/attributeelement.js +0 -10
- package/src/view/document.js +2 -1
- package/src/view/domconverter.js +47 -11
- package/src/view/downcastwriter.js +90 -49
- package/src/view/element.js +0 -27
- package/src/view/emptyelement.js +0 -3
- package/src/view/matcher.js +2 -2
- package/src/view/observer/clickobserver.js +0 -1
- package/src/view/observer/inputobserver.js +1 -1
- package/src/view/observer/tabobserver.js +68 -0
- package/src/view/placeholder.js +1 -1
- package/src/view/rawelement.js +0 -3
- package/src/view/uielement.js +0 -3
- package/src/view/view.js +4 -0
- package/theme/placeholder.css +9 -0
package/src/model/document.js
CHANGED
|
@@ -52,24 +52,13 @@ export default class Document {
|
|
|
52
52
|
*/
|
|
53
53
|
this.model = model;
|
|
54
54
|
|
|
55
|
-
/**
|
|
56
|
-
* The document version. It starts from `0` and every operation increases the version number. It is used to ensure that
|
|
57
|
-
* operations are applied on a proper document version.
|
|
58
|
-
*
|
|
59
|
-
* If the {@link module:engine/model/operation/operation~Operation#baseVersion base version} does not match the document version,
|
|
60
|
-
* a {@link module:utils/ckeditorerror~CKEditorError model-document-applyoperation-wrong-version} error is thrown.
|
|
61
|
-
*
|
|
62
|
-
* @type {Number}
|
|
63
|
-
*/
|
|
64
|
-
this.version = 0;
|
|
65
|
-
|
|
66
55
|
/**
|
|
67
56
|
* The document's history.
|
|
68
57
|
*
|
|
69
58
|
* @readonly
|
|
70
59
|
* @type {module:engine/model/history~History}
|
|
71
60
|
*/
|
|
72
|
-
this.history = new History(
|
|
61
|
+
this.history = new History();
|
|
73
62
|
|
|
74
63
|
/**
|
|
75
64
|
* The selection in this document.
|
|
@@ -115,21 +104,6 @@ export default class Document {
|
|
|
115
104
|
// Graveyard tree root. Document always have a graveyard root, which stores removed nodes.
|
|
116
105
|
this.createRoot( '$root', graveyardName );
|
|
117
106
|
|
|
118
|
-
// First, if the operation is a document operation check if it's base version is correct.
|
|
119
|
-
this.listenTo( model, 'applyOperation', ( evt, args ) => {
|
|
120
|
-
const operation = args[ 0 ];
|
|
121
|
-
|
|
122
|
-
if ( operation.isDocumentOperation && operation.baseVersion !== this.version ) {
|
|
123
|
-
/**
|
|
124
|
-
* Only operations with matching versions can be applied.
|
|
125
|
-
*
|
|
126
|
-
* @error model-document-applyoperation-wrong-version
|
|
127
|
-
* @param {module:engine/model/operation/operation~Operation} operation
|
|
128
|
-
*/
|
|
129
|
-
throw new CKEditorError( 'model-document-applyoperation-wrong-version', this, { operation } );
|
|
130
|
-
}
|
|
131
|
-
}, { priority: 'highest' } );
|
|
132
|
-
|
|
133
107
|
// Then, still before an operation is applied on model, buffer the change in differ.
|
|
134
108
|
this.listenTo( model, 'applyOperation', ( evt, args ) => {
|
|
135
109
|
const operation = args[ 0 ];
|
|
@@ -144,7 +118,6 @@ export default class Document {
|
|
|
144
118
|
const operation = args[ 0 ];
|
|
145
119
|
|
|
146
120
|
if ( operation.isDocumentOperation ) {
|
|
147
|
-
this.version++;
|
|
148
121
|
this.history.addOperation( operation );
|
|
149
122
|
}
|
|
150
123
|
}, { priority: 'low' } );
|
|
@@ -157,19 +130,47 @@ export default class Document {
|
|
|
157
130
|
// Buffer marker changes.
|
|
158
131
|
// This is not covered in buffering operations because markers may change outside of them (when they
|
|
159
132
|
// are modified using `model.markers` collection, not through `MarkerOperation`).
|
|
160
|
-
this.listenTo( model.markers, 'update', ( evt, marker, oldRange, newRange ) => {
|
|
133
|
+
this.listenTo( model.markers, 'update', ( evt, marker, oldRange, newRange, oldMarkerData ) => {
|
|
134
|
+
// Copy the `newRange` to the new marker data as during the marker removal the range is not updated.
|
|
135
|
+
const newMarkerData = { ...marker.getData(), range: newRange };
|
|
136
|
+
|
|
161
137
|
// Whenever marker is updated, buffer that change.
|
|
162
|
-
this.differ.bufferMarkerChange( marker.name,
|
|
138
|
+
this.differ.bufferMarkerChange( marker.name, oldMarkerData, newMarkerData );
|
|
163
139
|
|
|
164
140
|
if ( oldRange === null ) {
|
|
165
141
|
// If this is a new marker, add a listener that will buffer change whenever marker changes.
|
|
166
142
|
marker.on( 'change', ( evt, oldRange ) => {
|
|
167
|
-
|
|
143
|
+
const markerData = marker.getData();
|
|
144
|
+
|
|
145
|
+
this.differ.bufferMarkerChange(
|
|
146
|
+
marker.name,
|
|
147
|
+
{ ...markerData, range: oldRange },
|
|
148
|
+
markerData
|
|
149
|
+
);
|
|
168
150
|
} );
|
|
169
151
|
}
|
|
170
152
|
} );
|
|
171
153
|
}
|
|
172
154
|
|
|
155
|
+
/**
|
|
156
|
+
* The document version. Every applied operation increases the version number. It is used to
|
|
157
|
+
* ensure that operations are applied on a proper document version.
|
|
158
|
+
*
|
|
159
|
+
* This property is equal to {@link module:engine/model/history~History#version `model.Document#history#version`}.
|
|
160
|
+
*
|
|
161
|
+
* If the {@link module:engine/model/operation/operation~Operation#baseVersion base version} does not match the document version,
|
|
162
|
+
* a {@link module:utils/ckeditorerror~CKEditorError model-document-applyoperation-wrong-version} error is thrown.
|
|
163
|
+
*
|
|
164
|
+
* @type {Number}
|
|
165
|
+
*/
|
|
166
|
+
get version() {
|
|
167
|
+
return this.history.version;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
set version( version ) {
|
|
171
|
+
this.history.version = version;
|
|
172
|
+
}
|
|
173
|
+
|
|
173
174
|
/**
|
|
174
175
|
* The graveyard tree root. A document always has a graveyard root that stores removed nodes.
|
|
175
176
|
*
|
package/src/model/history.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { CKEditorError } from '@ckeditor/ckeditor5-utils';
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
9
|
* @module engine/model/history
|
|
8
10
|
*/
|
|
@@ -18,8 +20,9 @@ export default class History {
|
|
|
18
20
|
/**
|
|
19
21
|
* Operations added to the history.
|
|
20
22
|
*
|
|
21
|
-
* @
|
|
22
|
-
* @
|
|
23
|
+
* @private
|
|
24
|
+
* @readonly
|
|
25
|
+
* @type {Array.<module:engine/model/operation/operation~Operation>}
|
|
23
26
|
*/
|
|
24
27
|
this._operations = [];
|
|
25
28
|
|
|
@@ -39,43 +42,164 @@ export default class History {
|
|
|
39
42
|
* Holds all undone operations.
|
|
40
43
|
*
|
|
41
44
|
* @private
|
|
42
|
-
* @
|
|
45
|
+
* @type {Set.<module:engine/model/operation/operation~Operation>}
|
|
43
46
|
*/
|
|
44
47
|
this._undoneOperations = new Set();
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A map that allows retrieving the operations fast based on the given base version.
|
|
51
|
+
*
|
|
52
|
+
* @private
|
|
53
|
+
* @type Map.<Number,Number>
|
|
54
|
+
*/
|
|
55
|
+
this._baseVersionToOperationIndex = new Map();
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The history version.
|
|
59
|
+
*
|
|
60
|
+
* @private
|
|
61
|
+
* @type {Number}
|
|
62
|
+
*/
|
|
63
|
+
this._version = 0;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The gap pairs kept in the <from,to> format.
|
|
67
|
+
*
|
|
68
|
+
* Anytime the `history.version` is set to a version larger than `history.version + 1`,
|
|
69
|
+
* a new <lastHistoryVersion, newHistoryVersion> entry is added to the map.
|
|
70
|
+
*
|
|
71
|
+
* @private
|
|
72
|
+
* @type Map.<number,number>
|
|
73
|
+
*/
|
|
74
|
+
this._gaps = new Map();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The version of the last operation in the history.
|
|
79
|
+
*
|
|
80
|
+
* The history version is incremented automatically when a new operation is added to the history.
|
|
81
|
+
* Setting the version manually should be done only in rare circumstances when a gap is planned
|
|
82
|
+
* between history versions. When doing so, a gap will be created and the history will accept adding
|
|
83
|
+
* an operation with base version equal to the new history version.
|
|
84
|
+
*
|
|
85
|
+
* @type {Number}
|
|
86
|
+
*/
|
|
87
|
+
get version() {
|
|
88
|
+
return this._version;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
set version( version ) {
|
|
92
|
+
// Store a gap if there are some operations already in the history and the
|
|
93
|
+
// new version does not increment the latest one.
|
|
94
|
+
if ( this._operations.length && version > this._version + 1 ) {
|
|
95
|
+
this._gaps.set( this._version, version );
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this._version = version;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The last history operation.
|
|
103
|
+
*
|
|
104
|
+
* @readonly
|
|
105
|
+
* @type {module:engine/model/operation/operation~Operation|undefined}
|
|
106
|
+
*/
|
|
107
|
+
get lastOperation() {
|
|
108
|
+
return this._operations[ this._operations.length - 1 ];
|
|
45
109
|
}
|
|
46
110
|
|
|
47
111
|
/**
|
|
48
|
-
* Adds an operation to the history.
|
|
112
|
+
* Adds an operation to the history and increments the history version.
|
|
113
|
+
*
|
|
114
|
+
* The operation's base version should be equal to the history version. Otherwise an error is thrown.
|
|
49
115
|
*
|
|
50
116
|
* @param {module:engine/model/operation/operation~Operation} operation Operation to add.
|
|
51
117
|
*/
|
|
52
118
|
addOperation( operation ) {
|
|
53
|
-
if (
|
|
54
|
-
|
|
119
|
+
if ( operation.baseVersion !== this.version ) {
|
|
120
|
+
/**
|
|
121
|
+
* Only operations with matching versions can be added to the history.
|
|
122
|
+
*
|
|
123
|
+
* @error model-document-history-addoperation-incorrect-version
|
|
124
|
+
* @param {Object} errorData The operation and the current document history version.
|
|
125
|
+
*/
|
|
126
|
+
throw new CKEditorError( 'model-document-history-addoperation-incorrect-version', this, {
|
|
127
|
+
operation,
|
|
128
|
+
historyVersion: this.version
|
|
129
|
+
} );
|
|
55
130
|
}
|
|
56
131
|
|
|
57
132
|
this._operations.push( operation );
|
|
133
|
+
this._version++;
|
|
134
|
+
|
|
135
|
+
this._baseVersionToOperationIndex.set( operation.baseVersion, this._operations.length - 1 );
|
|
58
136
|
}
|
|
59
137
|
|
|
60
138
|
/**
|
|
61
|
-
* Returns operations added to the history.
|
|
139
|
+
* Returns operations from the given range of operation base versions that were added to the history.
|
|
62
140
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* @param {Number} [
|
|
66
|
-
*
|
|
67
|
-
|
|
141
|
+
* Note that there may be gaps in operations base versions.
|
|
142
|
+
*
|
|
143
|
+
* @param {Number} [fromBaseVersion] Base version from which operations should be returned (inclusive).
|
|
144
|
+
* @param {Number} [toBaseVersion] Base version up to which operations should be returned (exclusive).
|
|
145
|
+
* @returns {Array.<module:engine/model/operation/operation~Operation>} History operations for the given range, in chronological order.
|
|
68
146
|
*/
|
|
69
|
-
getOperations(
|
|
70
|
-
|
|
147
|
+
getOperations( fromBaseVersion, toBaseVersion = this.version ) {
|
|
148
|
+
// When there is no operation in the history, return an empty array.
|
|
149
|
+
// After that we can be sure that `firstOperation`, `lastOperation` are not nullish.
|
|
150
|
+
if ( !this._operations.length ) {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const firstOperation = this._operations[ 0 ];
|
|
155
|
+
|
|
156
|
+
if ( fromBaseVersion === undefined ) {
|
|
157
|
+
fromBaseVersion = firstOperation.baseVersion;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Change exclusive `toBaseVersion` to inclusive, so it will refer to the actual index.
|
|
161
|
+
// Thanks to that mapping from base versions to operation indexes are possible.
|
|
162
|
+
let inclusiveTo = toBaseVersion - 1;
|
|
163
|
+
|
|
164
|
+
// Check if "from" or "to" point to a gap between versions.
|
|
165
|
+
// If yes, then change the incorrect position to the proper side of the gap.
|
|
166
|
+
// Thanks to it, it will be possible to get index of the operation.
|
|
167
|
+
for ( const [ gapFrom, gapTo ] of this._gaps ) {
|
|
168
|
+
if ( fromBaseVersion > gapFrom && fromBaseVersion < gapTo ) {
|
|
169
|
+
fromBaseVersion = gapTo;
|
|
170
|
+
}
|
|
71
171
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
operations.push( operation );
|
|
172
|
+
if ( inclusiveTo > gapFrom && inclusiveTo < gapTo ) {
|
|
173
|
+
inclusiveTo = gapFrom - 1;
|
|
75
174
|
}
|
|
76
175
|
}
|
|
77
176
|
|
|
78
|
-
return
|
|
177
|
+
// If the whole range is outside of the operation versions, then return an empty array.
|
|
178
|
+
if ( inclusiveTo < firstOperation.baseVersion || fromBaseVersion > this.lastOperation.baseVersion ) {
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let fromIndex = this._baseVersionToOperationIndex.get( fromBaseVersion );
|
|
183
|
+
|
|
184
|
+
// If the range starts before the first operation, then use the first operation as the range's start.
|
|
185
|
+
if ( fromIndex === undefined ) {
|
|
186
|
+
fromIndex = 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
let toIndex = this._baseVersionToOperationIndex.get( inclusiveTo );
|
|
190
|
+
|
|
191
|
+
// If the range ends after the last operation, then use the last operation as the range's end.
|
|
192
|
+
if ( toIndex === undefined ) {
|
|
193
|
+
toIndex = this._operations.length - 1;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Return the part of the history operations based on the calculated start index and end index.
|
|
197
|
+
return this._operations.slice(
|
|
198
|
+
fromIndex,
|
|
199
|
+
|
|
200
|
+
// The `toIndex` should be included in the returned operations, so add `1`.
|
|
201
|
+
toIndex + 1
|
|
202
|
+
);
|
|
79
203
|
}
|
|
80
204
|
|
|
81
205
|
/**
|
|
@@ -86,11 +210,13 @@ export default class History {
|
|
|
86
210
|
* there is no such operation in history.
|
|
87
211
|
*/
|
|
88
212
|
getOperation( baseVersion ) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
213
|
+
const operationIndex = this._baseVersionToOperationIndex.get( baseVersion );
|
|
214
|
+
|
|
215
|
+
if ( operationIndex === undefined ) {
|
|
216
|
+
return;
|
|
93
217
|
}
|
|
218
|
+
|
|
219
|
+
return this._operations[ operationIndex ];
|
|
94
220
|
}
|
|
95
221
|
|
|
96
222
|
/**
|
|
@@ -135,4 +261,16 @@ export default class History {
|
|
|
135
261
|
getUndoneOperation( undoingOperation ) {
|
|
136
262
|
return this._undoPairs.get( undoingOperation );
|
|
137
263
|
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Resets the history of operations.
|
|
267
|
+
*/
|
|
268
|
+
reset() {
|
|
269
|
+
this._version = 0;
|
|
270
|
+
this._undoPairs = new Map();
|
|
271
|
+
this._operations = [];
|
|
272
|
+
this._undoneOperations = new Set();
|
|
273
|
+
this._gaps = new Map();
|
|
274
|
+
this._baseVersionToOperationIndex = new Map();
|
|
275
|
+
}
|
|
138
276
|
}
|
|
@@ -106,6 +106,8 @@ export default class MarkerCollection {
|
|
|
106
106
|
const oldMarker = this._markers.get( markerName );
|
|
107
107
|
|
|
108
108
|
if ( oldMarker ) {
|
|
109
|
+
const oldMarkerData = oldMarker.getData();
|
|
110
|
+
|
|
109
111
|
const oldRange = oldMarker.getRange();
|
|
110
112
|
let hasChanged = false;
|
|
111
113
|
|
|
@@ -125,7 +127,7 @@ export default class MarkerCollection {
|
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
if ( hasChanged ) {
|
|
128
|
-
this.fire( 'update:' + markerName, oldMarker, oldRange, range );
|
|
130
|
+
this.fire( 'update:' + markerName, oldMarker, oldRange, range, oldMarkerData );
|
|
129
131
|
}
|
|
130
132
|
|
|
131
133
|
return oldMarker;
|
|
@@ -135,7 +137,7 @@ export default class MarkerCollection {
|
|
|
135
137
|
const marker = new Marker( markerName, liveRange, managedUsingOperations, affectsData );
|
|
136
138
|
|
|
137
139
|
this._markers.set( markerName, marker );
|
|
138
|
-
this.fire( 'update:' + markerName, marker, null, range );
|
|
140
|
+
this.fire( 'update:' + markerName, marker, null, range, { ...marker.getData(), range: null } );
|
|
139
141
|
|
|
140
142
|
return marker;
|
|
141
143
|
}
|
|
@@ -154,7 +156,7 @@ export default class MarkerCollection {
|
|
|
154
156
|
|
|
155
157
|
if ( oldMarker ) {
|
|
156
158
|
this._markers.delete( markerName );
|
|
157
|
-
this.fire( 'update:' + markerName, oldMarker, oldMarker.getRange(), null );
|
|
159
|
+
this.fire( 'update:' + markerName, oldMarker, oldMarker.getRange(), null, oldMarker.getData() );
|
|
158
160
|
|
|
159
161
|
this._destroyMarker( oldMarker );
|
|
160
162
|
|
|
@@ -188,7 +190,7 @@ export default class MarkerCollection {
|
|
|
188
190
|
|
|
189
191
|
const range = marker.getRange();
|
|
190
192
|
|
|
191
|
-
this.fire( 'update:' + markerName, marker, range, range, marker.
|
|
193
|
+
this.fire( 'update:' + markerName, marker, range, range, marker.getData() );
|
|
192
194
|
}
|
|
193
195
|
|
|
194
196
|
/**
|
|
@@ -273,11 +275,20 @@ export default class MarkerCollection {
|
|
|
273
275
|
* means that marker is just added.
|
|
274
276
|
* @param {module:engine/model/range~Range|null} newRange Marker range after update. When is not defined it
|
|
275
277
|
* means that marker is just removed.
|
|
278
|
+
* @param {module:engine/model/markercollection~MarkerData} oldMarkerData Data of the marker before the change.
|
|
276
279
|
*/
|
|
277
280
|
}
|
|
278
281
|
|
|
279
282
|
mix( MarkerCollection, EmitterMixin );
|
|
280
283
|
|
|
284
|
+
/**
|
|
285
|
+
* @typedef {Object} module:engine/model/markercollection~MarkerData
|
|
286
|
+
*
|
|
287
|
+
* @property {module:engine/model/range~Range|null} range Marker range. `null` if the marker was removed.
|
|
288
|
+
* @property {Boolean} affectsData A property defining if the marker affects data.
|
|
289
|
+
* @property {Boolean} managedUsingOperations A property defining if the marker is managed using operations.
|
|
290
|
+
*/
|
|
291
|
+
|
|
281
292
|
/**
|
|
282
293
|
* `Marker` is a continuous parts of model (like a range), is named and represent some kind of information about marked
|
|
283
294
|
* part of model document. In contrary to {@link module:engine/model/node~Node nodes}, which are building blocks of
|
|
@@ -418,6 +429,19 @@ class Marker {
|
|
|
418
429
|
return this._affectsData;
|
|
419
430
|
}
|
|
420
431
|
|
|
432
|
+
/**
|
|
433
|
+
* Returns the marker data (properties defining the marker).
|
|
434
|
+
*
|
|
435
|
+
* @returns {module:engine/model/markercollection~MarkerData}
|
|
436
|
+
*/
|
|
437
|
+
getData() {
|
|
438
|
+
return {
|
|
439
|
+
range: this.getRange(),
|
|
440
|
+
affectsData: this.affectsData,
|
|
441
|
+
managedUsingOperations: this.managedUsingOperations
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
|
|
421
445
|
/**
|
|
422
446
|
* Returns current marker start position.
|
|
423
447
|
*
|
package/src/model/model.js
CHANGED
|
@@ -21,6 +21,7 @@ import ModelSelection from './selection';
|
|
|
21
21
|
import OperationFactory from './operation/operationfactory';
|
|
22
22
|
|
|
23
23
|
import insertContent from './utils/insertcontent';
|
|
24
|
+
import insertObject from './utils/insertobject';
|
|
24
25
|
import deleteContent from './utils/deletecontent';
|
|
25
26
|
import modifySelection from './utils/modifyselection';
|
|
26
27
|
import getSelectedContent from './utils/getselectedcontent';
|
|
@@ -80,7 +81,7 @@ export default class Model {
|
|
|
80
81
|
*/
|
|
81
82
|
this._currentWriter = null;
|
|
82
83
|
|
|
83
|
-
[ 'insertContent', 'deleteContent', 'modifySelection', 'getSelectedContent', 'applyOperation' ]
|
|
84
|
+
[ 'insertContent', 'insertObject', 'deleteContent', 'modifySelection', 'getSelectedContent', 'applyOperation' ]
|
|
84
85
|
.forEach( methodName => this.decorate( methodName ) );
|
|
85
86
|
|
|
86
87
|
// Adding operation validation with `highest` priority, so it is called before any other feature would like
|
|
@@ -96,11 +97,28 @@ export default class Model {
|
|
|
96
97
|
isLimit: true
|
|
97
98
|
} );
|
|
98
99
|
|
|
100
|
+
this.schema.register( '$container', {
|
|
101
|
+
allowIn: [ '$root', '$container' ]
|
|
102
|
+
} );
|
|
103
|
+
|
|
99
104
|
this.schema.register( '$block', {
|
|
100
|
-
allowIn: '$root',
|
|
105
|
+
allowIn: [ '$root', '$container' ],
|
|
101
106
|
isBlock: true
|
|
102
107
|
} );
|
|
103
108
|
|
|
109
|
+
this.schema.register( '$blockObject', {
|
|
110
|
+
allowWhere: '$block',
|
|
111
|
+
isBlock: true,
|
|
112
|
+
isObject: true
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
this.schema.register( '$inlineObject', {
|
|
116
|
+
allowWhere: '$text',
|
|
117
|
+
allowAttributesOf: '$text',
|
|
118
|
+
isInline: true,
|
|
119
|
+
isObject: true
|
|
120
|
+
} );
|
|
121
|
+
|
|
104
122
|
this.schema.register( '$text', {
|
|
105
123
|
allowIn: '$block',
|
|
106
124
|
isInline: true,
|
|
@@ -215,7 +233,7 @@ export default class Model {
|
|
|
215
233
|
*
|
|
216
234
|
* Second, it lets you define the {@link module:engine/model/batch~Batch} into which you want to add your changes.
|
|
217
235
|
* By default, a new batch with the default {@link module:engine/model/batch~Batch#constructor batch type} is created.
|
|
218
|
-
* In the sample above, `change` and `enqueueChange` blocks will use a different batch (and a different
|
|
236
|
+
* In the sample above, the `change` and `enqueueChange` blocks will use a different batch (and a different
|
|
219
237
|
* {@link module:engine/model/writer~Writer} instance since each of them operates on a separate batch).
|
|
220
238
|
*
|
|
221
239
|
* model.enqueueChange( { isUndoable: false }, writer => {
|
|
@@ -304,6 +322,9 @@ export default class Model {
|
|
|
304
322
|
* Inserts content at the position in the editor specified by the selection, as one would expect the paste
|
|
305
323
|
* functionality to work.
|
|
306
324
|
*
|
|
325
|
+
* **Note**: If you want to insert an {@glink framework/guides/deep-dive/schema#object-elements object element}
|
|
326
|
+
* (e.g. a {@link module:widget/utils~toWidget widget}), see {@link #insertObject} instead.
|
|
327
|
+
*
|
|
307
328
|
* This is a high-level method. It takes the {@link #schema schema} into consideration when inserting
|
|
308
329
|
* the content, clears the given selection's content before inserting nodes and moves the selection
|
|
309
330
|
* to its target position at the end of the process.
|
|
@@ -435,6 +456,89 @@ export default class Model {
|
|
|
435
456
|
return insertContent( this, content, selectable, placeOrOffset );
|
|
436
457
|
}
|
|
437
458
|
|
|
459
|
+
/**
|
|
460
|
+
* Inserts an {@glink framework/guides/deep-dive/schema#object-elements object element} at a specific position in the editor content.
|
|
461
|
+
*
|
|
462
|
+
* This is a high-level API:
|
|
463
|
+
* * It takes the {@link #schema schema} into consideration,
|
|
464
|
+
* * It clears the content of passed `selectable` before inserting,
|
|
465
|
+
* * It can move the selection at the end of the process,
|
|
466
|
+
* * It will copy the selected block's attributes to preserve them upon insertion,
|
|
467
|
+
* * It can split elements or wrap inline objects with paragraphs if they are not allowed in target position,
|
|
468
|
+
* * etc.
|
|
469
|
+
*
|
|
470
|
+
* # Notes
|
|
471
|
+
*
|
|
472
|
+
* * If you want to insert a non-object content, see {@link #insertContent} instead.
|
|
473
|
+
* * For lower-level API, see {@link module:engine/model/writer~Writer `Writer`}.
|
|
474
|
+
* * Unlike {@link module:engine/model/writer~Writer `Writer`}, this method does not have to be used inside
|
|
475
|
+
* a {@link #change `change()` block}.
|
|
476
|
+
* * Inserting object into the model is not enough to make CKEditor 5 render that content to the user.
|
|
477
|
+
* CKEditor 5 implements a model-view-controller architecture and what `model.insertObject()` does
|
|
478
|
+
* is only adding nodes to the model. Additionally, you need to define
|
|
479
|
+
* {@glink framework/guides/architecture/editing-engine#conversion converters} between the model and view
|
|
480
|
+
* and define those nodes in the {@glink framework/guides/architecture/editing-engine#schema schema}.
|
|
481
|
+
*
|
|
482
|
+
* # Examples
|
|
483
|
+
*
|
|
484
|
+
* Use the following code to insert an object at the current selection and keep the selection on the inserted element:
|
|
485
|
+
*
|
|
486
|
+
* const rawHtmlEmbedElement = writer.createElement( 'rawHtml' );
|
|
487
|
+
*
|
|
488
|
+
* model.insertObject( rawHtmlEmbedElement, null, null, {
|
|
489
|
+
* setSelection: 'on'
|
|
490
|
+
* } );
|
|
491
|
+
*
|
|
492
|
+
* Use the following code to insert an object at the current selection and nudge the selection after the inserted object:
|
|
493
|
+
*
|
|
494
|
+
* const pageBreakElement = writer.createElement( 'pageBreak' );
|
|
495
|
+
*
|
|
496
|
+
* model.insertObject( pageBreakElement, null, null, {
|
|
497
|
+
* setSelection: 'after'
|
|
498
|
+
* } );
|
|
499
|
+
*
|
|
500
|
+
* Use the following code to insert an object at the current selection and avoid splitting the content (non-destructive insertion):
|
|
501
|
+
*
|
|
502
|
+
* const tableElement = writer.createElement( 'table' );
|
|
503
|
+
*
|
|
504
|
+
* model.insertObject( tableElement, null, null, {
|
|
505
|
+
* findOptimalPosition: 'auto'
|
|
506
|
+
* } );
|
|
507
|
+
*
|
|
508
|
+
* Use the following code to insert an object at the specific range (also: replace the content of the range):
|
|
509
|
+
*
|
|
510
|
+
* const tableElement = writer.createElement( 'table' );
|
|
511
|
+
* const range = model.createRangeOn( model.document.getRoot().getChild( 1 ) );
|
|
512
|
+
*
|
|
513
|
+
* model.insertObject( tableElement, range );
|
|
514
|
+
*
|
|
515
|
+
* @param {module:engine/model/element~Element} object An object to be inserted into the model document.
|
|
516
|
+
* @param {module:engine/model/selection~Selectable} [selectable=model.document.selection]
|
|
517
|
+
* A selectable where the content should be inserted. If not specified, the current
|
|
518
|
+
* {@link module:engine/model/document~Document#selection document selection} will be used instead.
|
|
519
|
+
* @param {Number|'before'|'end'|'after'|'on'|'in'} placeOrOffset Specifies the exact place or offset for the insertion to take place,
|
|
520
|
+
* relative to `selectable`.
|
|
521
|
+
* @param {Object} [options] Additional options.
|
|
522
|
+
* @param {'auto'|'before'|'after'} [options.findOptimalPosition] An option that, when set, adjusts the insertion position (relative to
|
|
523
|
+
* `selectable` and `placeOrOffset`) so that the content of `selectable` is not split upon insertion (a.k.a. non-destructive insertion).
|
|
524
|
+
* * When `'auto'`, the algorithm will decide whether to insert the object before or after `selectable` to avoid content splitting.
|
|
525
|
+
* * When `'before'`, the closest position before `selectable` will be used that will not result in content splitting.
|
|
526
|
+
* * When `'after'`, the closest position after `selectable` will be used that will not result in content splitting.
|
|
527
|
+
*
|
|
528
|
+
* Note that this option only works for block objects. Inline objects are inserted into text and do not split blocks.
|
|
529
|
+
* @param {'on'|'after'} [options.setSelection] An option that, when set, moves the
|
|
530
|
+
* {@link module:engine/model/document~Document#selection document selection} after inserting the object.
|
|
531
|
+
* * When `'on'`, the document selection will be set on the inserted object.
|
|
532
|
+
* * When `'after'`, the document selection will move to the closest text node after the inserted object. If there is no
|
|
533
|
+
* such text node, a paragraph will be created and the document selection will be moved inside it.
|
|
534
|
+
* @returns {module:engine/model/range~Range} A range which contains all the performed changes. This is a range that, if removed,
|
|
535
|
+
* would return the model to the state before the insertion. If no changes were preformed by `insertObject()`, returns a range collapsed
|
|
536
|
+
* at the insertion position.
|
|
537
|
+
*/
|
|
538
|
+
insertObject( object, selectable, placeOrOffset, options ) {
|
|
539
|
+
return insertObject( this, object, selectable, placeOrOffset, options );
|
|
540
|
+
}
|
|
541
|
+
|
|
438
542
|
/**
|
|
439
543
|
* Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
|
|
440
544
|
*
|
|
@@ -516,6 +620,7 @@ export default class Model {
|
|
|
516
620
|
* @param {Object} [options]
|
|
517
621
|
* @param {'forward'|'backward'} [options.direction='forward'] The direction in which the selection should be modified.
|
|
518
622
|
* @param {'character'|'codePoint'|'word'} [options.unit='character'] The unit by which selection should be modified.
|
|
623
|
+
* @param {Boolean} [options.treatEmojiAsSingleUnit=false] Whether multi-characer emoji sequences should be handled as single unit.
|
|
519
624
|
*/
|
|
520
625
|
modifySelection( selection, options ) {
|
|
521
626
|
modifySelection( this, selection, options );
|
|
@@ -902,12 +1007,25 @@ export default class Model {
|
|
|
902
1007
|
* listener to this event so it can be fully customized by the features.
|
|
903
1008
|
*
|
|
904
1009
|
* **Note** The `selectable` parameter for the {@link #insertContent} is optional. When `undefined` value is passed the method uses
|
|
905
|
-
*
|
|
1010
|
+
* {@link module:engine/model/document~Document#selection document selection}.
|
|
906
1011
|
*
|
|
907
1012
|
* @event insertContent
|
|
908
1013
|
* @param {Array} args The arguments passed to the original method.
|
|
909
1014
|
*/
|
|
910
1015
|
|
|
1016
|
+
/**
|
|
1017
|
+
* Event fired when the {@link #insertObject} method is called.
|
|
1018
|
+
*
|
|
1019
|
+
* The {@link #insertObject default action of that method} is implemented as a
|
|
1020
|
+
* listener to this event so it can be fully customized by the features.
|
|
1021
|
+
*
|
|
1022
|
+
* **Note** The `selectable` parameter for the {@link #insertObject} is optional. When `undefined` value is passed the method uses
|
|
1023
|
+
* {@link module:engine/model/document~Document#selection document selection}.
|
|
1024
|
+
*
|
|
1025
|
+
* @event insertObject
|
|
1026
|
+
* @param {Array} args The arguments passed to the original method.
|
|
1027
|
+
*/
|
|
1028
|
+
|
|
911
1029
|
/**
|
|
912
1030
|
* Event fired when {@link #deleteContent} method is called.
|
|
913
1031
|
*
|