|
@@ -17,7 +17,7 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Operation to split {@link module:engine/model/element~Element an element} at given
|
|
* Operation to split {@link module:engine/model/element~Element an element} at given
|
|
|
- * {@link module:engine/model/operation/splitoperation~SplitOperation#position position} into two elements,
|
|
|
|
|
|
|
+ * {@link module:engine/model/operation/splitoperation~SplitOperation#splitPosition split position} into two elements,
|
|
|
* both containing a part of the element's original content.
|
|
* both containing a part of the element's original content.
|
|
|
*
|
|
*
|
|
|
* @extends module:engine/model/operation/operation~Operation
|
|
* @extends module:engine/model/operation/operation~Operation
|
|
@@ -26,25 +26,25 @@ export default class SplitOperation extends Operation {
|
|
|
/**
|
|
/**
|
|
|
* Creates a split operation.
|
|
* Creates a split operation.
|
|
|
*
|
|
*
|
|
|
- * @param {module:engine/model/position~Position} position Position at which an element should be split.
|
|
|
|
|
|
|
+ * @param {module:engine/model/position~Position} splitPosition Position at which an element should be split.
|
|
|
* @param {Number} howMany Total offset size of elements that are in the split element after `position`.
|
|
* @param {Number} howMany Total offset size of elements that are in the split element after `position`.
|
|
|
* @param {module:engine/model/position~Position|null} graveyardPosition Position in the graveyard root before the element which
|
|
* @param {module:engine/model/position~Position|null} graveyardPosition Position in the graveyard root before the element which
|
|
|
* should be used as a parent of the nodes after `position`. If it is not set, a copy of the the `position` parent will be used.
|
|
* should be used as a parent of the nodes after `position`. If it is not set, a copy of the the `position` parent will be used.
|
|
|
* @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
|
|
* @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
|
|
|
* can be applied or `null` if the operation operates on detached (non-document) tree.
|
|
* can be applied or `null` if the operation operates on detached (non-document) tree.
|
|
|
*/
|
|
*/
|
|
|
- constructor( position, howMany, graveyardPosition, baseVersion ) {
|
|
|
|
|
|
|
+ constructor( splitPosition, howMany, graveyardPosition, baseVersion ) {
|
|
|
super( baseVersion );
|
|
super( baseVersion );
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Position at which an element should be split.
|
|
* Position at which an element should be split.
|
|
|
*
|
|
*
|
|
|
- * @member {module:engine/model/position~Position} module:engine/model/operation/splitoperation~SplitOperation#position
|
|
|
|
|
|
|
+ * @member {module:engine/model/position~Position} module:engine/model/operation/splitoperation~SplitOperation#splitPosition
|
|
|
*/
|
|
*/
|
|
|
- this.position = Position.createFromPosition( position );
|
|
|
|
|
|
|
+ this.splitPosition = Position.createFromPosition( splitPosition );
|
|
|
// Keep position sticking to the next node. This way any new content added at the place where the element is split
|
|
// Keep position sticking to the next node. This way any new content added at the place where the element is split
|
|
|
// will be left in the original element.
|
|
// will be left in the original element.
|
|
|
- this.position.stickiness = 'toNext';
|
|
|
|
|
|
|
+ this.splitPosition.stickiness = 'toNext';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Total offset size of elements that are in the split element after `position`.
|
|
* Total offset size of elements that are in the split element after `position`.
|
|
@@ -54,6 +54,14 @@ export default class SplitOperation extends Operation {
|
|
|
this.howMany = howMany;
|
|
this.howMany = howMany;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Position at which the clone of split element (or element from graveyard) will be inserted.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @member {module:engine/model/position~Position} module:engine/model/operation/splitoperation~SplitOperation#insertionPosition
|
|
|
|
|
+ */
|
|
|
|
|
+ this.insertionPosition = SplitOperation.getInsertionPosition( splitPosition );
|
|
|
|
|
+ this.insertionPosition.stickiness = 'toNone';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* Position in the graveyard root before the element which should be used as a parent of the nodes after `position`.
|
|
* Position in the graveyard root before the element which should be used as a parent of the nodes after `position`.
|
|
|
* If it is not set, a copy of the the `position` parent will be used.
|
|
* If it is not set, a copy of the the `position` parent will be used.
|
|
|
*
|
|
*
|
|
@@ -76,27 +84,6 @@ export default class SplitOperation extends Operation {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Position after the split element.
|
|
|
|
|
- *
|
|
|
|
|
- * This is a position at which the clone of split element (or element from graveyard) will be inserted.
|
|
|
|
|
- *
|
|
|
|
|
- * @readonly
|
|
|
|
|
- * @type {module:engine/model/position~Position}
|
|
|
|
|
- */
|
|
|
|
|
- get insertionPosition() {
|
|
|
|
|
- const path = this.position.path.slice( 0, -1 );
|
|
|
|
|
- path[ path.length - 1 ]++;
|
|
|
|
|
-
|
|
|
|
|
- let pos = new Position( this.position.root, path );
|
|
|
|
|
-
|
|
|
|
|
- if ( this.graveyardPosition && this.graveyardPosition.root == pos.root ) {
|
|
|
|
|
- pos = pos._getTransformedByDeletion( this.graveyardPosition, 1 );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return pos;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
* Position inside the new clone of a split element.
|
|
* Position inside the new clone of a split element.
|
|
|
*
|
|
*
|
|
|
* This is a position where nodes that are after the split position will be moved to.
|
|
* This is a position where nodes that are after the split position will be moved to.
|
|
@@ -105,30 +92,23 @@ export default class SplitOperation extends Operation {
|
|
|
* @type {module:engine/model/position~Position}
|
|
* @type {module:engine/model/position~Position}
|
|
|
*/
|
|
*/
|
|
|
get moveTargetPosition() {
|
|
get moveTargetPosition() {
|
|
|
- const path = this.position.path.slice( 0, -1 );
|
|
|
|
|
- path[ path.length - 1 ]++;
|
|
|
|
|
|
|
+ const path = this.insertionPosition.path.slice();
|
|
|
path.push( 0 );
|
|
path.push( 0 );
|
|
|
|
|
|
|
|
- let pos = new Position( this.position.root, path );
|
|
|
|
|
-
|
|
|
|
|
- if ( this.graveyardPosition && this.graveyardPosition.root == pos.root ) {
|
|
|
|
|
- pos = pos._getTransformedByDeletion( this.graveyardPosition, 1 );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return pos;
|
|
|
|
|
|
|
+ return new Position( this.insertionPosition.root, path );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Artificial range that contains all the nodes from the split element that will be moved to the new element.
|
|
* Artificial range that contains all the nodes from the split element that will be moved to the new element.
|
|
|
- * The range starts at {@link ~#position} and ends in the same parent, at `POSITIVE_INFINITY` offset.
|
|
|
|
|
|
|
+ * The range starts at {@link ~#splitPosition} and ends in the same parent, at `POSITIVE_INFINITY` offset.
|
|
|
*
|
|
*
|
|
|
* @readonly
|
|
* @readonly
|
|
|
* @type {module:engine/model/range~Range}
|
|
* @type {module:engine/model/range~Range}
|
|
|
*/
|
|
*/
|
|
|
get movedRange() {
|
|
get movedRange() {
|
|
|
- const end = this.position.getShiftedBy( Number.POSITIVE_INFINITY );
|
|
|
|
|
|
|
+ const end = this.splitPosition.getShiftedBy( Number.POSITIVE_INFINITY );
|
|
|
|
|
|
|
|
- return new Range( this.position, end );
|
|
|
|
|
|
|
+ return new Range( this.splitPosition, end );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -137,7 +117,10 @@ export default class SplitOperation extends Operation {
|
|
|
* @returns {module:engine/model/operation/splitoperation~SplitOperation} Clone of this operation.
|
|
* @returns {module:engine/model/operation/splitoperation~SplitOperation} Clone of this operation.
|
|
|
*/
|
|
*/
|
|
|
clone() {
|
|
clone() {
|
|
|
- return new this.constructor( this.position, this.howMany, this.graveyardPosition, this.baseVersion );
|
|
|
|
|
|
|
+ const split = new this.constructor( this.splitPosition, this.howMany, this.graveyardPosition, this.baseVersion );
|
|
|
|
|
+ split.insertionPosition = this.insertionPosition;
|
|
|
|
|
+
|
|
|
|
|
+ return split;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -146,18 +129,18 @@ export default class SplitOperation extends Operation {
|
|
|
* @returns {module:engine/model/operation/mergeoperation~MergeOperation}
|
|
* @returns {module:engine/model/operation/mergeoperation~MergeOperation}
|
|
|
*/
|
|
*/
|
|
|
getReversed() {
|
|
getReversed() {
|
|
|
- const graveyard = this.position.root.document.graveyard;
|
|
|
|
|
|
|
+ const graveyard = this.splitPosition.root.document.graveyard;
|
|
|
const graveyardPosition = new Position( graveyard, [ 0 ] );
|
|
const graveyardPosition = new Position( graveyard, [ 0 ] );
|
|
|
|
|
|
|
|
- return new MergeOperation( this.moveTargetPosition, this.howMany, this.position, graveyardPosition, this.baseVersion + 1 );
|
|
|
|
|
|
|
+ return new MergeOperation( this.moveTargetPosition, this.howMany, this.splitPosition, graveyardPosition, this.baseVersion + 1 );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @inheritDoc
|
|
* @inheritDoc
|
|
|
*/
|
|
*/
|
|
|
_validate() {
|
|
_validate() {
|
|
|
- const element = this.position.parent;
|
|
|
|
|
- const offset = this.position.offset;
|
|
|
|
|
|
|
+ const element = this.splitPosition.parent;
|
|
|
|
|
+ const offset = this.splitPosition.offset;
|
|
|
|
|
|
|
|
// Validate whether split operation has correct parameters.
|
|
// Validate whether split operation has correct parameters.
|
|
|
if ( !element || element.maxOffset < offset ) {
|
|
if ( !element || element.maxOffset < offset ) {
|
|
@@ -174,7 +157,7 @@ export default class SplitOperation extends Operation {
|
|
|
* @error split-operation-split-in-root
|
|
* @error split-operation-split-in-root
|
|
|
*/
|
|
*/
|
|
|
throw new CKEditorError( 'split-operation-split-in-root: Cannot split root element.' );
|
|
throw new CKEditorError( 'split-operation-split-in-root: Cannot split root element.' );
|
|
|
- } else if ( this.howMany != element.maxOffset - this.position.offset ) {
|
|
|
|
|
|
|
+ } else if ( this.howMany != element.maxOffset - this.splitPosition.offset ) {
|
|
|
/**
|
|
/**
|
|
|
* Split operation specifies wrong number of nodes to move.
|
|
* Split operation specifies wrong number of nodes to move.
|
|
|
*
|
|
*
|
|
@@ -195,7 +178,7 @@ export default class SplitOperation extends Operation {
|
|
|
* @inheritDoc
|
|
* @inheritDoc
|
|
|
*/
|
|
*/
|
|
|
_execute() {
|
|
_execute() {
|
|
|
- const splitElement = this.position.parent;
|
|
|
|
|
|
|
+ const splitElement = this.splitPosition.parent;
|
|
|
|
|
|
|
|
if ( this.graveyardPosition ) {
|
|
if ( this.graveyardPosition ) {
|
|
|
_move( Range.createFromPositionAndShift( this.graveyardPosition, 1 ), this.insertionPosition );
|
|
_move( Range.createFromPositionAndShift( this.graveyardPosition, 1 ), this.insertionPosition );
|
|
@@ -205,7 +188,9 @@ export default class SplitOperation extends Operation {
|
|
|
_insert( this.insertionPosition, newElement );
|
|
_insert( this.insertionPosition, newElement );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const sourceRange = Range.createFromParentsAndOffsets( splitElement, this.position.offset, splitElement, splitElement.maxOffset );
|
|
|
|
|
|
|
+ const sourceRange = Range.createFromParentsAndOffsets(
|
|
|
|
|
+ splitElement, this.splitPosition.offset, splitElement, splitElement.maxOffset
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
_move( sourceRange, this.moveTargetPosition );
|
|
_move( sourceRange, this.moveTargetPosition );
|
|
|
}
|
|
}
|
|
@@ -216,7 +201,8 @@ export default class SplitOperation extends Operation {
|
|
|
toJSON() {
|
|
toJSON() {
|
|
|
const json = super.toJSON();
|
|
const json = super.toJSON();
|
|
|
|
|
|
|
|
- json.position = this.position.toJSON();
|
|
|
|
|
|
|
+ json.splitPosition = this.splitPosition.toJSON();
|
|
|
|
|
+ json.insertionPosition = this.insertionPosition.toJSON();
|
|
|
|
|
|
|
|
if ( this.graveyardPosition ) {
|
|
if ( this.graveyardPosition ) {
|
|
|
json.graveyardPosition = this.graveyardPosition.toJSON();
|
|
json.graveyardPosition = this.graveyardPosition.toJSON();
|
|
@@ -233,6 +219,20 @@ export default class SplitOperation extends Operation {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Helper function that returns a default insertion position basing on given `splitPosition`. The default insertion
|
|
|
|
|
+ * position is after the split element.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {module:engine/model/position~Position} splitPosition
|
|
|
|
|
+ * @returns {module:engine/model/position~Position}
|
|
|
|
|
+ */
|
|
|
|
|
+ static getInsertionPosition( splitPosition ) {
|
|
|
|
|
+ const path = splitPosition.path.slice( 0, -1 );
|
|
|
|
|
+ path[ path.length - 1 ]++;
|
|
|
|
|
+
|
|
|
|
|
+ return new Position( splitPosition.root, path );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* Creates `SplitOperation` object from deserilized object, i.e. from parsed JSON string.
|
|
* Creates `SplitOperation` object from deserilized object, i.e. from parsed JSON string.
|
|
|
*
|
|
*
|
|
|
* @param {Object} json Deserialized JSON object.
|
|
* @param {Object} json Deserialized JSON object.
|
|
@@ -240,9 +240,13 @@ export default class SplitOperation extends Operation {
|
|
|
* @returns {module:engine/model/operation/splitoperation~SplitOperation}
|
|
* @returns {module:engine/model/operation/splitoperation~SplitOperation}
|
|
|
*/
|
|
*/
|
|
|
static fromJSON( json, document ) {
|
|
static fromJSON( json, document ) {
|
|
|
- const position = Position.fromJSON( json.position, document );
|
|
|
|
|
|
|
+ const splitPosition = Position.fromJSON( json.splitPosition, document );
|
|
|
|
|
+ const insertionPosition = Position.fromJSON( json.insertionPosition, document );
|
|
|
const graveyardPosition = json.graveyardPosition ? Position.fromJSON( json.graveyardPosition, document ) : null;
|
|
const graveyardPosition = json.graveyardPosition ? Position.fromJSON( json.graveyardPosition, document ) : null;
|
|
|
|
|
|
|
|
- return new this( position, json.howMany, graveyardPosition, json.baseVersion );
|
|
|
|
|
|
|
+ const split = new this( splitPosition, json.howMany, graveyardPosition, json.baseVersion );
|
|
|
|
|
+ split.insertionPosition = insertionPosition;
|
|
|
|
|
+
|
|
|
|
|
+ return split;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|