@ckeditor/ckeditor5-engine 33.0.0 → 34.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/package.json +22 -22
- package/src/conversion/downcasthelpers.js +90 -19
- package/src/conversion/mapper.js +2 -2
- package/src/conversion/upcastdispatcher.js +31 -1
- package/src/index.js +6 -0
- package/src/model/differ.js +33 -15
- package/src/model/model.js +120 -3
- 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/view/attributeelement.js +0 -10
- package/src/view/domconverter.js +16 -1
- package/src/view/downcastwriter.js +5 -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/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,
|
|
@@ -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 works only 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
|
*
|
|
@@ -903,12 +1007,25 @@ export default class Model {
|
|
|
903
1007
|
* listener to this event so it can be fully customized by the features.
|
|
904
1008
|
*
|
|
905
1009
|
* **Note** The `selectable` parameter for the {@link #insertContent} is optional. When `undefined` value is passed the method uses
|
|
906
|
-
*
|
|
1010
|
+
* {@link module:engine/model/document~Document#selection document selection}.
|
|
907
1011
|
*
|
|
908
1012
|
* @event insertContent
|
|
909
1013
|
* @param {Array} args The arguments passed to the original method.
|
|
910
1014
|
*/
|
|
911
1015
|
|
|
1016
|
+
/**
|
|
1017
|
+
* Event fired when {@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
|
+
|
|
912
1029
|
/**
|
|
913
1030
|
* Event fired when {@link #deleteContent} method is called.
|
|
914
1031
|
*
|
package/src/model/schema.js
CHANGED
|
@@ -834,6 +834,23 @@ export default class Schema {
|
|
|
834
834
|
return null;
|
|
835
835
|
}
|
|
836
836
|
|
|
837
|
+
/**
|
|
838
|
+
* Sets attributes allowed by the schema on given node.
|
|
839
|
+
*
|
|
840
|
+
* @param {module:engine/model/node~Node} node A node to set attributes on.
|
|
841
|
+
* @param {Object} attributes Attributes keys and values.
|
|
842
|
+
* @param {module:engine/model/writer~Writer} writer An instance of the model writer.
|
|
843
|
+
*/
|
|
844
|
+
setAllowedAttributes( node, attributes, writer ) {
|
|
845
|
+
const model = writer.model;
|
|
846
|
+
|
|
847
|
+
for ( const [ attributeName, attributeValue ] of Object.entries( attributes ) ) {
|
|
848
|
+
if ( model.schema.checkAttribute( node, attributeName ) ) {
|
|
849
|
+
writer.setAttribute( attributeName, attributeValue, node );
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
837
854
|
/**
|
|
838
855
|
* Removes attributes disallowed by the schema.
|
|
839
856
|
*
|
|
@@ -863,6 +880,34 @@ export default class Schema {
|
|
|
863
880
|
}
|
|
864
881
|
}
|
|
865
882
|
|
|
883
|
+
/**
|
|
884
|
+
* Gets attributes of a node that have given property.
|
|
885
|
+
*
|
|
886
|
+
* @param {module:engine/model/node~Node} node Node to get attributes from.
|
|
887
|
+
* @param {String} propertyName Name of the property that attribute must have to return it.
|
|
888
|
+
* @param {Boolean|Symbol|String|Number|Object|null|undefined} propertyValue Desired value of the property that we want to check.
|
|
889
|
+
* When `undefined` attributes will be returned if they have set a given property no matter what the value is. If specified it will
|
|
890
|
+
* return attributes which given property's value is equal to this parameter.
|
|
891
|
+
* @returns {Object} Object with attributes' names as key and attributes' values as value.
|
|
892
|
+
*/
|
|
893
|
+
getAttributesWithProperty( node, propertyName, propertyValue ) {
|
|
894
|
+
const attributes = {};
|
|
895
|
+
|
|
896
|
+
for ( const [ attributeName, attributeValue ] of node.getAttributes() ) {
|
|
897
|
+
const attributeProperties = this.getAttributeProperties( attributeName );
|
|
898
|
+
|
|
899
|
+
if ( attributeProperties[ propertyName ] === undefined ) {
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
if ( propertyValue === undefined || propertyValue === attributeProperties[ propertyName ] ) {
|
|
904
|
+
attributes[ attributeName ] = attributeValue;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
return attributes;
|
|
909
|
+
}
|
|
910
|
+
|
|
866
911
|
/**
|
|
867
912
|
* Creates an instance of the schema context.
|
|
868
913
|
*
|
|
@@ -1135,19 +1180,39 @@ mix( Schema, ObservableMixin );
|
|
|
1135
1180
|
*
|
|
1136
1181
|
* # Generic items
|
|
1137
1182
|
*
|
|
1138
|
-
* There are
|
|
1139
|
-
* They are defined as follows:
|
|
1183
|
+
* There are several generic items (classes of elements) available: `$root`, `$container`, `$block`, `$blockObject`,
|
|
1184
|
+
* `$inlineObject`, and `$text`. They are defined as follows:
|
|
1140
1185
|
*
|
|
1141
|
-
*
|
|
1186
|
+
* schema.register( '$root', {
|
|
1142
1187
|
* isLimit: true
|
|
1143
1188
|
* } );
|
|
1144
|
-
*
|
|
1145
|
-
*
|
|
1189
|
+
*
|
|
1190
|
+
* schema.register( '$container', {
|
|
1191
|
+
* allowIn: [ '$root', '$container' ]
|
|
1192
|
+
* } );
|
|
1193
|
+
*
|
|
1194
|
+
* schema.register( '$block', {
|
|
1195
|
+
* allowIn: [ '$root', '$container' ],
|
|
1146
1196
|
* isBlock: true
|
|
1147
1197
|
* } );
|
|
1148
|
-
*
|
|
1198
|
+
*
|
|
1199
|
+
* schema.register( '$blockObject', {
|
|
1200
|
+
* allowWhere: '$block',
|
|
1201
|
+
* isBlock: true,
|
|
1202
|
+
* isObject: true
|
|
1203
|
+
* } );
|
|
1204
|
+
*
|
|
1205
|
+
* schema.register( '$inlineObject', {
|
|
1206
|
+
* allowWhere: '$text',
|
|
1207
|
+
* allowAttributesOf: '$text',
|
|
1208
|
+
* isInline: true,
|
|
1209
|
+
* isObject: true
|
|
1210
|
+
* } );
|
|
1211
|
+
*
|
|
1212
|
+
* schema.register( '$text', {
|
|
1149
1213
|
* allowIn: '$block',
|
|
1150
|
-
* isInline: true
|
|
1214
|
+
* isInline: true,
|
|
1215
|
+
* isContent: true
|
|
1151
1216
|
* } );
|
|
1152
1217
|
*
|
|
1153
1218
|
* They reflect typical editor content that is contained within one root, consists of several blocks
|
|
@@ -1180,14 +1245,18 @@ mix( Schema, ObservableMixin );
|
|
|
1180
1245
|
* isBlock: true
|
|
1181
1246
|
* } );
|
|
1182
1247
|
*
|
|
1248
|
+
* The previous rule can be written in a shorter form using inheritance:
|
|
1249
|
+
*
|
|
1250
|
+
* schema.register( 'paragraph', {
|
|
1251
|
+
* inheritAllFrom: '$block'
|
|
1252
|
+
* } );
|
|
1253
|
+
*
|
|
1183
1254
|
* Make `imageBlock` a block object, which is allowed everywhere where `$block` is.
|
|
1184
1255
|
* Also, allow `src` and `alt` attributes in it:
|
|
1185
1256
|
*
|
|
1186
1257
|
* schema.register( 'imageBlock', {
|
|
1187
|
-
*
|
|
1258
|
+
* inheritAllFrom: '$blockObject',
|
|
1188
1259
|
* allowAttributes: [ 'src', 'alt' ],
|
|
1189
|
-
* isBlock: true,
|
|
1190
|
-
* isObject: true
|
|
1191
1260
|
* } );
|
|
1192
1261
|
*
|
|
1193
1262
|
* Make `caption` allowed in `imageBlock` and make it allow all the content of `$block`s (usually, `$text`).
|
package/src/model/treewalker.js
CHANGED
|
@@ -233,9 +233,8 @@ export default class TreeWalker {
|
|
|
233
233
|
|
|
234
234
|
// Get node just after the current position.
|
|
235
235
|
// Use a highly optimized version instead of checking the text node first and then getting the node after. See #6582.
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
const node = textNodeAtPosition ? textNodeAtPosition : getNodeAfterPosition( position, positionParent, textNodeAtPosition );
|
|
236
|
+
const textNodeAtPosition = getTextNodeAtPosition( position, parent );
|
|
237
|
+
const node = textNodeAtPosition ? textNodeAtPosition : getNodeAfterPosition( position, parent, textNodeAtPosition );
|
|
239
238
|
|
|
240
239
|
if ( node instanceof Element ) {
|
|
241
240
|
if ( !this.shallow ) {
|
|
@@ -80,6 +80,17 @@ export default function deleteContent( model, selection, options = {} ) {
|
|
|
80
80
|
return;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
// Collect attributes to copy in case of autoparagraphing.
|
|
84
|
+
const attributesForAutoparagraph = {};
|
|
85
|
+
|
|
86
|
+
if ( !options.doNotAutoparagraph ) {
|
|
87
|
+
const selectedElement = selection.getSelectedElement();
|
|
88
|
+
|
|
89
|
+
if ( selectedElement ) {
|
|
90
|
+
Object.assign( attributesForAutoparagraph, schema.getAttributesWithProperty( selectedElement, 'copyOnReplace', true ) );
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
83
94
|
// Get the live positions for the range adjusted to span only blocks selected from the user perspective.
|
|
84
95
|
const [ startPosition, endPosition ] = getLivePositionsForSelectedBlocks( selRange );
|
|
85
96
|
|
|
@@ -114,7 +125,7 @@ export default function deleteContent( model, selection, options = {} ) {
|
|
|
114
125
|
// Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
|
|
115
126
|
// If autoparagraphing is off, we assume that you know what you do so we leave the selection wherever it was.
|
|
116
127
|
if ( !options.doNotAutoparagraph && shouldAutoparagraph( schema, startPosition ) ) {
|
|
117
|
-
insertParagraph( writer, startPosition, selection );
|
|
128
|
+
insertParagraph( writer, startPosition, selection, attributesForAutoparagraph );
|
|
118
129
|
}
|
|
119
130
|
|
|
120
131
|
startPosition.detach();
|
|
@@ -482,9 +493,11 @@ function isCrossingLimitElement( leftPos, rightPos, schema ) {
|
|
|
482
493
|
return true;
|
|
483
494
|
}
|
|
484
495
|
|
|
485
|
-
function insertParagraph( writer, position, selection ) {
|
|
496
|
+
function insertParagraph( writer, position, selection, attributes = {} ) {
|
|
486
497
|
const paragraph = writer.createElement( 'paragraph' );
|
|
487
498
|
|
|
499
|
+
writer.model.schema.setAllowedAttributes( paragraph, attributes, writer );
|
|
500
|
+
|
|
488
501
|
writer.insert( paragraph, position );
|
|
489
502
|
|
|
490
503
|
collapseSelectionAt( writer, selection, writer.createPositionAt( paragraph, 0 ) );
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, 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
|
+
/**
|
|
7
|
+
* @module engine/model/utils/findoptimalinsertionrange
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import first from '@ckeditor/ckeditor5-utils/src/first';
|
|
11
|
+
|
|
12
|
+
// Returns a model range which is optimal (in terms of UX) for inserting a widget block.
|
|
13
|
+
//
|
|
14
|
+
// For instance, if a selection is in the middle of a paragraph, the collapsed range before this paragraph
|
|
15
|
+
// will be returned so that it is not split. If the selection is at the end of a paragraph,
|
|
16
|
+
// the collapsed range after this paragraph will be returned.
|
|
17
|
+
//
|
|
18
|
+
// Note: If the selection is placed in an empty block, the range in that block will be returned. If that range
|
|
19
|
+
// is then passed to {@link module:engine/model/model~Model#insertContent}, the block will be fully replaced
|
|
20
|
+
// by the inserted widget block.
|
|
21
|
+
//
|
|
22
|
+
// **Note:** Use {@link module:widget/utils#findOptimalInsertionRange} instead of this function outside engine.
|
|
23
|
+
// This function is only exposed to be used by {@link module:widget/utils#findOptimalInsertionRange findOptimalInsertionRange()}
|
|
24
|
+
// in `widget` package and inside `engine` package.
|
|
25
|
+
//
|
|
26
|
+
// @private
|
|
27
|
+
// @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
|
|
28
|
+
// The selection based on which the insertion position should be calculated.
|
|
29
|
+
// @param {module:engine/model/model~Model} model Model instance.
|
|
30
|
+
// @param {'auto'|'before'|'after'} [place='auto'] Place where to look for optimal insertion range.
|
|
31
|
+
// Default value `auto` will determine itself the best position for insertion.
|
|
32
|
+
// Value `before` will try to find a position before selection.
|
|
33
|
+
// Value `after` will try to find a position after selection.
|
|
34
|
+
// @returns {module:engine/model/range~Range} The optimal range.
|
|
35
|
+
export function findOptimalInsertionRange( selection, model, place = 'auto' ) {
|
|
36
|
+
const selectedElement = selection.getSelectedElement();
|
|
37
|
+
|
|
38
|
+
if ( selectedElement && model.schema.isObject( selectedElement ) && !model.schema.isInline( selectedElement ) ) {
|
|
39
|
+
if ( [ 'before', 'after' ].includes( place ) ) {
|
|
40
|
+
return model.createRange( model.createPositionAt( selectedElement, place ) );
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return model.createRangeOn( selectedElement );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const firstBlock = first( selection.getSelectedBlocks() );
|
|
47
|
+
|
|
48
|
+
// There are no block elements within ancestors (in the current limit element).
|
|
49
|
+
if ( !firstBlock ) {
|
|
50
|
+
return model.createRange( selection.focus );
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// If inserting into an empty block – return position in that block. It will get
|
|
54
|
+
// replaced with the image by insertContent(). #42.
|
|
55
|
+
if ( firstBlock.isEmpty ) {
|
|
56
|
+
return model.createRange( model.createPositionAt( firstBlock, 0 ) );
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const positionAfter = model.createPositionAfter( firstBlock );
|
|
60
|
+
|
|
61
|
+
// If selection is at the end of the block - return position after the block.
|
|
62
|
+
if ( selection.focus.isTouching( positionAfter ) ) {
|
|
63
|
+
return model.createRange( positionAfter );
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Otherwise, return position before the block.
|
|
67
|
+
return model.createRange( model.createPositionBefore( firstBlock ) );
|
|
68
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, 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
|
+
/**
|
|
7
|
+
* @module engine/model/utils/insertobject
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import first from '@ckeditor/ckeditor5-utils/src/first';
|
|
11
|
+
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
12
|
+
|
|
13
|
+
import { findOptimalInsertionRange } from './findoptimalinsertionrange';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Inserts an {@glink framework/guides/deep-dive/schema#object-elements object element} at a specific position in the editor content.
|
|
17
|
+
*
|
|
18
|
+
* **Note:** Use {@link module:engine/model/model~Model#insertObject} instead of this function.
|
|
19
|
+
* This function is only exposed to be reusable in algorithms which change the {@link module:engine/model/model~Model#insertObject}
|
|
20
|
+
* method's behavior.
|
|
21
|
+
*
|
|
22
|
+
* **Note**: For more documentation and examples, see {@link module:engine/model/model~Model#insertObject}.
|
|
23
|
+
*
|
|
24
|
+
* @param {module:engine/model/model~Model} model The model in context of which the insertion
|
|
25
|
+
* should be performed.
|
|
26
|
+
* @param {module:engine/model/element~Element} object An object to be inserted into the model document.
|
|
27
|
+
* @param {module:engine/model/selection~Selectable} [selectable=model.document.selection]
|
|
28
|
+
* A selectable where the content should be inserted. If not specified, the current
|
|
29
|
+
* {@link module:engine/model/document~Document#selection document selection} will be used instead.
|
|
30
|
+
* @param {Number|'before'|'end'|'after'|'on'|'in'} placeOrOffset Specifies the exact place or offset for the insertion to take place,
|
|
31
|
+
* relative to `selectable`.
|
|
32
|
+
* @param {Object} [options] Additional options.
|
|
33
|
+
* @param {'auto'|'before'|'after'} [options.findOptimalPosition] An option that, when set, adjusts the insertion position (relative to
|
|
34
|
+
* `selectable` and `placeOrOffset`) so that the content of `selectable` is not split upon insertion (a.k.a. non-destructive insertion).
|
|
35
|
+
* * When `'auto'`, the algorithm will decide whether to insert the object before or after `selectable` to avoid content splitting.
|
|
36
|
+
* * When `'before'`, the closest position before `selectable` will be used that will not result in content splitting.
|
|
37
|
+
* * When `'after'`, the closest position after `selectable` will be used that will not result in content splitting.
|
|
38
|
+
*
|
|
39
|
+
* Note that this option works only for block objects. Inline objects are inserted into text and do not split blocks.
|
|
40
|
+
* @param {'on'|'after'} [options.setSelection] An option that, when set, moves the
|
|
41
|
+
* {@link module:engine/model/document~Document#selection document selection} after inserting the object.
|
|
42
|
+
* * When `'on'`, the document selection will be set on the inserted object.
|
|
43
|
+
* * When `'after'`, the document selection will move to the closest text node after the inserted object. If there is no
|
|
44
|
+
* such text node, a paragraph will be created and the document selection will be moved inside it.
|
|
45
|
+
* @returns {module:engine/model/range~Range} A range which contains all the performed changes. This is a range that, if removed,
|
|
46
|
+
* would return the model to the state before the insertion. If no changes were preformed by `insertObject()`, returns a range collapsed
|
|
47
|
+
* at the insertion position.
|
|
48
|
+
*/
|
|
49
|
+
export default function insertObject( model, object, selectable, placeOrOffset, options = {} ) {
|
|
50
|
+
if ( !model.schema.isObject( object ) ) {
|
|
51
|
+
/**
|
|
52
|
+
* Tried to insert an element with {@link module:engine/model/utils/insertobject insertObject()} function
|
|
53
|
+
* that is not defined as an object in schema.
|
|
54
|
+
* See {@link module:engine/model/schema~SchemaItemDefinition#isObject `SchemaItemDefinition`}.
|
|
55
|
+
* If you want to insert content that is not an object you might want to use
|
|
56
|
+
* {@link module:engine/model/utils/insertcontent insertContent()} function.
|
|
57
|
+
* @error insertobject-element-not-an-object
|
|
58
|
+
*/
|
|
59
|
+
throw new CKEditorError( 'insertobject-element-not-an-object', model, { object } );
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Normalize selectable to a selection instance.
|
|
63
|
+
let originalSelection;
|
|
64
|
+
|
|
65
|
+
if ( !selectable ) {
|
|
66
|
+
originalSelection = model.document.selection;
|
|
67
|
+
} else if ( selectable.is( 'selection' ) ) {
|
|
68
|
+
originalSelection = selectable;
|
|
69
|
+
} else {
|
|
70
|
+
originalSelection = model.createSelection( selectable, placeOrOffset );
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Adjust the insertion selection.
|
|
74
|
+
let insertionSelection = originalSelection;
|
|
75
|
+
|
|
76
|
+
if ( options.findOptimalPosition && model.schema.isBlock( object ) ) {
|
|
77
|
+
insertionSelection = model.createSelection( findOptimalInsertionRange( originalSelection, model, options.findOptimalPosition ) );
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Collect attributes to be copied on the inserted object.
|
|
81
|
+
const firstSelectedBlock = first( originalSelection.getSelectedBlocks() );
|
|
82
|
+
const attributesToCopy = {};
|
|
83
|
+
|
|
84
|
+
if ( firstSelectedBlock ) {
|
|
85
|
+
Object.assign( attributesToCopy, model.schema.getAttributesWithProperty( firstSelectedBlock, 'copyOnReplace', true ) );
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return model.change( writer => {
|
|
89
|
+
// Remove the selected content to find out what the parent of the inserted object would be.
|
|
90
|
+
// It would be removed inside model.insertContent() anyway.
|
|
91
|
+
if ( !insertionSelection.isCollapsed ) {
|
|
92
|
+
model.deleteContent( insertionSelection, { doNotAutoparagraph: true } );
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let elementToInsert = object;
|
|
96
|
+
const insertionPositionParent = insertionSelection.anchor.parent;
|
|
97
|
+
|
|
98
|
+
// Autoparagraphing of an inline objects.
|
|
99
|
+
if (
|
|
100
|
+
!model.schema.checkChild( insertionPositionParent, object ) &&
|
|
101
|
+
model.schema.checkChild( insertionPositionParent, 'paragraph' ) &&
|
|
102
|
+
model.schema.checkChild( 'paragraph', object )
|
|
103
|
+
) {
|
|
104
|
+
elementToInsert = writer.createElement( 'paragraph' );
|
|
105
|
+
|
|
106
|
+
writer.insert( object, elementToInsert );
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Apply attributes that are allowed on the inserted object (or paragraph if autoparagraphed).
|
|
110
|
+
model.schema.setAllowedAttributes( elementToInsert, attributesToCopy, writer );
|
|
111
|
+
|
|
112
|
+
// Insert the prepared content at the optionally adjusted selection.
|
|
113
|
+
const affectedRange = model.insertContent( elementToInsert, insertionSelection );
|
|
114
|
+
|
|
115
|
+
// Nothing got inserted.
|
|
116
|
+
if ( affectedRange.isCollapsed ) {
|
|
117
|
+
return affectedRange;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if ( options.setSelection ) {
|
|
121
|
+
updateSelection( writer, object, options.setSelection, attributesToCopy );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return affectedRange;
|
|
125
|
+
} );
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Updates document selection based on given `place` parameter in relation to `contextElement` element.
|
|
129
|
+
//
|
|
130
|
+
// @private
|
|
131
|
+
// @param {module:engine/model/writer~Writer} writer An instance of the model writer.
|
|
132
|
+
// @param {module:engine/model/element~Element} contextElement An element to set attributes on.
|
|
133
|
+
// @param {'on'|'after'} place Place where selection should be set in relation to `contextElement` element.
|
|
134
|
+
// Value `on` will set selection on passed `contextElement`. Value `after` will set selection after `contextElement`.
|
|
135
|
+
// @param {Object} attributes Attributes keys and values to set on a paragraph that this function can create when
|
|
136
|
+
// `place` parameter is equal to `after` but there is no element with `$text` node to set selection in.
|
|
137
|
+
function updateSelection( writer, contextElement, place, paragraphAttributes ) {
|
|
138
|
+
const model = writer.model;
|
|
139
|
+
|
|
140
|
+
if ( place == 'after' ) {
|
|
141
|
+
let nextElement = contextElement.nextSibling;
|
|
142
|
+
|
|
143
|
+
// Check whether an element next to the inserted element is defined and can contain a text.
|
|
144
|
+
const canSetSelection = nextElement && model.schema.checkChild( nextElement, '$text' );
|
|
145
|
+
|
|
146
|
+
// If the element is missing, but a paragraph could be inserted next to the element, let's add it.
|
|
147
|
+
if ( !canSetSelection && model.schema.checkChild( contextElement.parent, 'paragraph' ) ) {
|
|
148
|
+
nextElement = writer.createElement( 'paragraph' );
|
|
149
|
+
|
|
150
|
+
model.schema.setAllowedAttributes( nextElement, paragraphAttributes, writer );
|
|
151
|
+
model.insertContent( nextElement, writer.createPositionAfter( contextElement ) );
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Put the selection inside the element, at the beginning.
|
|
155
|
+
if ( nextElement ) {
|
|
156
|
+
writer.setSelection( nextElement, 0 );
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else if ( place == 'on' ) {
|
|
160
|
+
writer.setSelection( contextElement, 'on' );
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
/**
|
|
164
|
+
* Unsupported `options.setSelection` parameter was passed
|
|
165
|
+
* to the {@link module:engine/model/utils/insertobject insertObject()} function.
|
|
166
|
+
* Check {@link module:engine/model/utils/insertobject insertObject()} API documentation for allowed
|
|
167
|
+
* `options.setSelection` parameter values.
|
|
168
|
+
*
|
|
169
|
+
* @error insertobject-invalid-place-parameter-value
|
|
170
|
+
*/
|
|
171
|
+
throw new CKEditorError( 'insertobject-invalid-place-parameter-value', model );
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -24,16 +24,6 @@ const DEFAULT_PRIORITY = 10;
|
|
|
24
24
|
* To create a new attribute element instance use the
|
|
25
25
|
* {@link module:engine/view/downcastwriter~DowncastWriter#createAttributeElement `DowncastWriter#createAttributeElement()`} method.
|
|
26
26
|
*
|
|
27
|
-
* **Note:** Attribute elements by default can wrap {@link module:engine/view/text~Text},
|
|
28
|
-
* {@link module:engine/view/emptyelement~EmptyElement}, {@link module:engine/view/uielement~UIElement},
|
|
29
|
-
* {@link module:engine/view/rawelement~RawElement} and other attribute elements with higher priority. Other elements while placed inside
|
|
30
|
-
* an attribute element will split it (or nest in case of an `AttributeElement`). This behavior can be modified by changing
|
|
31
|
-
* the `isAllowedInsideAttributeElement` option while creating
|
|
32
|
-
* {@link module:engine/view/downcastwriter~DowncastWriter#createContainerElement},
|
|
33
|
-
* {@link module:engine/view/downcastwriter~DowncastWriter#createEmptyElement},
|
|
34
|
-
* {@link module:engine/view/downcastwriter~DowncastWriter#createUIElement} or
|
|
35
|
-
* {@link module:engine/view/downcastwriter~DowncastWriter#createRawElement}.
|
|
36
|
-
*
|
|
37
27
|
* @extends module:engine/view/element~Element
|
|
38
28
|
*/
|
|
39
29
|
export default class AttributeElement extends Element {
|
package/src/view/domconverter.js
CHANGED
|
@@ -493,7 +493,22 @@ export default class DomConverter {
|
|
|
493
493
|
yield this._getBlockFiller( domDocument );
|
|
494
494
|
}
|
|
495
495
|
|
|
496
|
-
|
|
496
|
+
const transparentRendering = childView.is( 'element' ) && childView.getCustomProperty( 'dataPipeline:transparentRendering' );
|
|
497
|
+
|
|
498
|
+
if ( transparentRendering && this.renderingMode == 'data' ) {
|
|
499
|
+
yield* this.viewChildrenToDom( childView, domDocument, options );
|
|
500
|
+
} else {
|
|
501
|
+
if ( transparentRendering ) {
|
|
502
|
+
/**
|
|
503
|
+
* The `dataPipeline:transparentRendering` flag is supported only in the data pipeline.
|
|
504
|
+
*
|
|
505
|
+
* @error domconverter-transparent-rendering-unsupported-in-editing-pipeline
|
|
506
|
+
*/
|
|
507
|
+
logWarning( 'domconverter-transparent-rendering-unsupported-in-editing-pipeline', { viewElement: childView } );
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
yield this.viewToDom( childView, domDocument, options );
|
|
511
|
+
}
|
|
497
512
|
|
|
498
513
|
offset++;
|
|
499
514
|
}
|