|
@@ -5,554 +5,555 @@
|
|
|
|
|
|
|
|
'use strict';
|
|
'use strict';
|
|
|
|
|
|
|
|
-CKEDITOR.define( [ 'treemodel/rootelement', 'utils', 'ckeditorerror' ], ( RootElement, utils, CKEditorError ) => {
|
|
|
|
|
- const SAME = 0;
|
|
|
|
|
- const AFTER = 1;
|
|
|
|
|
- const BEFORE = 2;
|
|
|
|
|
- const DIFFERENT = 3;
|
|
|
|
|
|
|
+import RootElement from './rootelement.js';
|
|
|
|
|
+import CKEditorError from '../ckeditorerror.js';
|
|
|
|
|
+import arrayUtils from '../lib/lodash/array.js';
|
|
|
|
|
+import utils from '../utils.js';
|
|
|
|
|
|
|
|
|
|
+const SAME = 0;
|
|
|
|
|
+const AFTER = 1;
|
|
|
|
|
+const BEFORE = 2;
|
|
|
|
|
+const DIFFERENT = 3;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Position in the tree. Position is always located before or after a node.
|
|
|
|
|
+ * See {@link #path} property for more information.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @class treeModel.Position
|
|
|
|
|
+ */
|
|
|
|
|
+export default class Position {
|
|
|
/**
|
|
/**
|
|
|
- * Position in the tree. Position is always located before or after a node.
|
|
|
|
|
- * See {@link #path} property for more information.
|
|
|
|
|
|
|
+ * Creates a position.
|
|
|
*
|
|
*
|
|
|
- * @class treeModel.Position
|
|
|
|
|
|
|
+ * @param {treeModel.RootElement} root Root element for the path. Note that this element can not have a parent.
|
|
|
|
|
+ * @param {Array.<Number>} path Position path. Must contain at least one item. See {@link #path} property for more information.
|
|
|
|
|
+ * @constructor
|
|
|
*/
|
|
*/
|
|
|
- class Position {
|
|
|
|
|
- /**
|
|
|
|
|
- * Creates a position.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.RootElement} root Root element for the path. Note that this element can not have a parent.
|
|
|
|
|
- * @param {Array.<Number>} path Position path. Must contain at least one item. See {@link #path} property for more information.
|
|
|
|
|
- * @constructor
|
|
|
|
|
- */
|
|
|
|
|
- constructor( root, path ) {
|
|
|
|
|
- if ( !( root instanceof RootElement ) ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * Position root has to be an instance of RootElement.
|
|
|
|
|
- *
|
|
|
|
|
- * @error position-root-not-rootelement
|
|
|
|
|
- * @param root
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError( 'position-root-not-rootelement: Position root has to be an instance of RootElement.', { root: root } );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ constructor( root, path ) {
|
|
|
|
|
+ if ( !( root instanceof RootElement ) ) {
|
|
|
/**
|
|
/**
|
|
|
- * Root element for the path. Note that this element can not have a parent.
|
|
|
|
|
|
|
+ * Position root has to be an instance of RootElement.
|
|
|
*
|
|
*
|
|
|
- * @type {treeModel.RootElement}
|
|
|
|
|
|
|
+ * @error position-root-not-rootelement
|
|
|
|
|
+ * @param root
|
|
|
*/
|
|
*/
|
|
|
- this.root = root;
|
|
|
|
|
-
|
|
|
|
|
- if ( !( path instanceof Array ) || path.length === 0 ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * Position path must be an Array with at least one item.
|
|
|
|
|
- *
|
|
|
|
|
- * @error position-path-incorrect
|
|
|
|
|
- * @param path
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError( 'position-path-incorrect: Position path must be an Array with at least one item.', { path: path } );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Position of the node it the tree. For example:
|
|
|
|
|
- *
|
|
|
|
|
- * root
|
|
|
|
|
- * |- p Before: [ 0 ] After: [ 1 ]
|
|
|
|
|
- * |- ul Before: [ 1 ] After: [ 2 ]
|
|
|
|
|
- * |- li Before: [ 1, 0 ] After: [ 1, 1 ]
|
|
|
|
|
- * | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
|
|
|
|
|
- * | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
|
|
|
|
|
- * | |- o Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
|
|
|
|
|
- * |- li Before: [ 1, 1 ] After: [ 1, 2 ]
|
|
|
|
|
- * |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
|
|
|
|
|
- * |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
|
|
|
|
|
- * |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
|
|
|
|
|
- *
|
|
|
|
|
- * @type {Number[]}
|
|
|
|
|
- */
|
|
|
|
|
- this.path = path;
|
|
|
|
|
|
|
+ throw new CKEditorError( 'position-root-not-rootelement: Position root has to be an instance of RootElement.', { root: root } );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Node directly after the position.
|
|
|
|
|
|
|
+ * Root element for the path. Note that this element can not have a parent.
|
|
|
*
|
|
*
|
|
|
- * @readonly
|
|
|
|
|
- * @property {treeModel.Node}
|
|
|
|
|
|
|
+ * @type {treeModel.RootElement}
|
|
|
*/
|
|
*/
|
|
|
- get nodeAfter() {
|
|
|
|
|
- return this.parent.getChild( this.offset ) || null;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ this.root = root;
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Node directly before the position.
|
|
|
|
|
- *
|
|
|
|
|
- * @readonly
|
|
|
|
|
- * @type {treeModel.Node}
|
|
|
|
|
- */
|
|
|
|
|
- get nodeBefore() {
|
|
|
|
|
- return this.parent.getChild( this.offset - 1 ) || null;
|
|
|
|
|
|
|
+ if ( !( path instanceof Array ) || path.length === 0 ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Position path must be an Array with at least one item.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @error position-path-incorrect
|
|
|
|
|
+ * @param path
|
|
|
|
|
+ */
|
|
|
|
|
+ throw new CKEditorError( 'position-path-incorrect: Position path must be an Array with at least one item.', { path: path } );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Offset at which the position is located in the {@link #parent}.
|
|
|
|
|
- *
|
|
|
|
|
- * @readonly
|
|
|
|
|
- * @property {Number} offset
|
|
|
|
|
|
|
+ * Position of the node it the tree. For example:
|
|
|
|
|
+ *
|
|
|
|
|
+ * root
|
|
|
|
|
+ * |- p Before: [ 0 ] After: [ 1 ]
|
|
|
|
|
+ * |- ul Before: [ 1 ] After: [ 2 ]
|
|
|
|
|
+ * |- li Before: [ 1, 0 ] After: [ 1, 1 ]
|
|
|
|
|
+ * | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
|
|
|
|
|
+ * | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
|
|
|
|
|
+ * | |- o Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
|
|
|
|
|
+ * |- li Before: [ 1, 1 ] After: [ 1, 2 ]
|
|
|
|
|
+ * |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
|
|
|
|
|
+ * |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
|
|
|
|
|
+ * |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
|
|
|
|
|
+ *
|
|
|
|
|
+ * @type {Number[]}
|
|
|
*/
|
|
*/
|
|
|
- get offset() {
|
|
|
|
|
- return utils.last( this.path );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ this.path = path;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Sets offset in the parent, which is the last element of the path.
|
|
|
|
|
- */
|
|
|
|
|
- set offset( newOffset ) {
|
|
|
|
|
- this.path[ this.path.length - 1 ] = newOffset;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Node directly after the position.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @property {treeModel.Node}
|
|
|
|
|
+ */
|
|
|
|
|
+ get nodeAfter() {
|
|
|
|
|
+ return this.parent.getChild( this.offset ) || null;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Parent element of the position. The position is located at {@link #offset} in this element.
|
|
|
|
|
- *
|
|
|
|
|
- * @readonly
|
|
|
|
|
- * @property {treeModel.Element} parent
|
|
|
|
|
- */
|
|
|
|
|
- get parent() {
|
|
|
|
|
- let parent = this.root;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Node directly before the position.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @type {treeModel.Node}
|
|
|
|
|
+ */
|
|
|
|
|
+ get nodeBefore() {
|
|
|
|
|
+ return this.parent.getChild( this.offset - 1 ) || null;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- let i, len;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Offset at which the position is located in the {@link #parent}.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @property {Number} offset
|
|
|
|
|
+ */
|
|
|
|
|
+ get offset() {
|
|
|
|
|
+ return arrayUtils.last( this.path );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- for ( i = 0, len = this.path.length - 1; i < len; i++ ) {
|
|
|
|
|
- parent = parent.getChild( this.path[ i ] );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets offset in the parent, which is the last element of the path.
|
|
|
|
|
+ */
|
|
|
|
|
+ set offset( newOffset ) {
|
|
|
|
|
+ this.path[ this.path.length - 1 ] = newOffset;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return parent;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Parent element of the position. The position is located at {@link #offset} in this element.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @property {treeModel.Element} parent
|
|
|
|
|
+ */
|
|
|
|
|
+ get parent() {
|
|
|
|
|
+ let parent = this.root;
|
|
|
|
|
+
|
|
|
|
|
+ let i, len;
|
|
|
|
|
+
|
|
|
|
|
+ for ( i = 0, len = this.path.length - 1; i < len; i++ ) {
|
|
|
|
|
+ parent = parent.getChild( this.path[ i ] );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Checks whether this position is before or after given position.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
- * @returns {Number} A flag indicating whether this position is {@link #BEFORE} or
|
|
|
|
|
- * {@link #AFTER} or {@link #SAME} as given position. If positions are in different roots,
|
|
|
|
|
- * {@link #DIFFERENT} flag is returned.
|
|
|
|
|
- */
|
|
|
|
|
- compareWith( otherPosition ) {
|
|
|
|
|
- if ( this.root != otherPosition.root ) {
|
|
|
|
|
- return DIFFERENT;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return parent;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- const result = utils.compareArrays( this.path, otherPosition.path );
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Checks whether this position is before or after given position.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
+ * @returns {Number} A flag indicating whether this position is {@link #BEFORE} or
|
|
|
|
|
+ * {@link #AFTER} or {@link #SAME} as given position. If positions are in different roots,
|
|
|
|
|
+ * {@link #DIFFERENT} flag is returned.
|
|
|
|
|
+ */
|
|
|
|
|
+ compareWith( otherPosition ) {
|
|
|
|
|
+ if ( this.root != otherPosition.root ) {
|
|
|
|
|
+ return DIFFERENT;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- switch ( result ) {
|
|
|
|
|
- case utils.compareArrays.SAME:
|
|
|
|
|
- return SAME;
|
|
|
|
|
|
|
+ const result = utils.compareArrays( this.path, otherPosition.path );
|
|
|
|
|
|
|
|
- case utils.compareArrays.PREFIX:
|
|
|
|
|
- return BEFORE;
|
|
|
|
|
|
|
+ switch ( result ) {
|
|
|
|
|
+ case utils.compareArrays.SAME:
|
|
|
|
|
+ return SAME;
|
|
|
|
|
|
|
|
- case utils.compareArrays.EXTENSION:
|
|
|
|
|
- return AFTER;
|
|
|
|
|
|
|
+ case utils.compareArrays.PREFIX:
|
|
|
|
|
+ return BEFORE;
|
|
|
|
|
|
|
|
- default:
|
|
|
|
|
- if ( this.path[ result ] < otherPosition.path[ result ] ) {
|
|
|
|
|
- return BEFORE;
|
|
|
|
|
- } else {
|
|
|
|
|
- return AFTER;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ case utils.compareArrays.EXTENSION:
|
|
|
|
|
+ return AFTER;
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Returns the path to the parent, which is the {@link treeModel.Position#path} without the last element.
|
|
|
|
|
- *
|
|
|
|
|
- * This method returns the parent path even if the parent does not exists.
|
|
|
|
|
- *
|
|
|
|
|
- * @returns {Number[]} Path to the parent.
|
|
|
|
|
- */
|
|
|
|
|
- getParentPath() {
|
|
|
|
|
- return this.path.slice( 0, -1 );
|
|
|
|
|
|
|
+ default:
|
|
|
|
|
+ if ( this.path[ result ] < otherPosition.path[ result ] ) {
|
|
|
|
|
+ return BEFORE;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return AFTER;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Returns a new instance of Position with offset incremented by `shift` value.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {Number} shift How position offset should get changed. Accepts negative values.
|
|
|
|
|
- * @returns {treeModel.Position} Shifted position.
|
|
|
|
|
- */
|
|
|
|
|
- getShiftedBy( shift ) {
|
|
|
|
|
- let shifted = Position.createFromPosition( this );
|
|
|
|
|
-
|
|
|
|
|
- let offset = shifted.offset + shift;
|
|
|
|
|
- shifted.offset = offset < 0 ? 0 : offset;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns the path to the parent, which is the {@link treeModel.Position#path} without the last element.
|
|
|
|
|
+ *
|
|
|
|
|
+ * This method returns the parent path even if the parent does not exists.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @returns {Number[]} Path to the parent.
|
|
|
|
|
+ */
|
|
|
|
|
+ getParentPath() {
|
|
|
|
|
+ return this.path.slice( 0, -1 );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return shifted;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns a new instance of Position with offset incremented by `shift` value.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {Number} shift How position offset should get changed. Accepts negative values.
|
|
|
|
|
+ * @returns {treeModel.Position} Shifted position.
|
|
|
|
|
+ */
|
|
|
|
|
+ getShiftedBy( shift ) {
|
|
|
|
|
+ let shifted = Position.createFromPosition( this );
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Returns this position after being updated by removing `howMany` nodes starting from `deletePosition`.
|
|
|
|
|
- * It may happen that this position is in a removed node. If that is the case, `null` is returned instead.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} deletePosition Position before the first removed node.
|
|
|
|
|
- * @param {Number} howMany How many nodes are removed.
|
|
|
|
|
- * @returns {treeModel.Position|null} Transformed position or `null`.
|
|
|
|
|
- */
|
|
|
|
|
- getTransformedByDeletion( deletePosition, howMany ) {
|
|
|
|
|
- let transformed = Position.createFromPosition( this );
|
|
|
|
|
|
|
+ let offset = shifted.offset + shift;
|
|
|
|
|
+ shifted.offset = offset < 0 ? 0 : offset;
|
|
|
|
|
|
|
|
- // This position can't be affected if deletion was in a different root.
|
|
|
|
|
- if ( this.root != deletePosition.root ) {
|
|
|
|
|
- return transformed;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return shifted;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if ( utils.compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.SAME ) {
|
|
|
|
|
- // If nodes are removed from the node that is pointed by this position...
|
|
|
|
|
- if ( deletePosition.offset < this.offset ) {
|
|
|
|
|
- // And are removed from before an offset of that position...
|
|
|
|
|
- // Decrement the offset accordingly.
|
|
|
|
|
- if ( deletePosition.offset + howMany > this.offset ) {
|
|
|
|
|
- transformed.offset = deletePosition.offset;
|
|
|
|
|
- } else {
|
|
|
|
|
- transformed.offset -= howMany;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } else if ( utils.compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.PREFIX ) {
|
|
|
|
|
- // If nodes are removed from a node that is on a path to this position...
|
|
|
|
|
- const i = deletePosition.path.length - 1;
|
|
|
|
|
-
|
|
|
|
|
- if ( deletePosition.offset < this.path[ i ] ) {
|
|
|
|
|
- // And are removed from before next node of that path...
|
|
|
|
|
- if ( deletePosition.offset + howMany > this.path[ i ] ) {
|
|
|
|
|
- // If the next node of that path is removed return null
|
|
|
|
|
- // because the node containing this position got removed.
|
|
|
|
|
- return null;
|
|
|
|
|
- } else {
|
|
|
|
|
- // Otherwise, decrement index on that path.
|
|
|
|
|
- transformed.path[ i ] -= howMany;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns this position after being updated by removing `howMany` nodes starting from `deletePosition`.
|
|
|
|
|
+ * It may happen that this position is in a removed node. If that is the case, `null` is returned instead.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} deletePosition Position before the first removed node.
|
|
|
|
|
+ * @param {Number} howMany How many nodes are removed.
|
|
|
|
|
+ * @returns {treeModel.Position|null} Transformed position or `null`.
|
|
|
|
|
+ */
|
|
|
|
|
+ getTransformedByDeletion( deletePosition, howMany ) {
|
|
|
|
|
+ let transformed = Position.createFromPosition( this );
|
|
|
|
|
|
|
|
|
|
+ // This position can't be affected if deletion was in a different root.
|
|
|
|
|
+ if ( this.root != deletePosition.root ) {
|
|
|
return transformed;
|
|
return transformed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Returns this position after being updated by inserting `howMany` nodes at `insertPosition`.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} insertPosition Position where nodes are inserted.
|
|
|
|
|
- * @param {Number} howMany How many nodes are inserted.
|
|
|
|
|
- * @param {Boolean} insertBefore Flag indicating whether nodes are inserted before or after `insertPosition`.
|
|
|
|
|
- * This is important only when `insertPosition` and this position are same. If that is the case and the flag is
|
|
|
|
|
- * set to `true`, this position will get transformed. If the flag is set to `false`, it won't.
|
|
|
|
|
- * @returns {treeModel.Position} Transformed position.
|
|
|
|
|
- */
|
|
|
|
|
- getTransformedByInsertion( insertPosition, howMany, insertBefore ) {
|
|
|
|
|
- let transformed = Position.createFromPosition( this );
|
|
|
|
|
-
|
|
|
|
|
- // This position can't be affected if insertion was in a different root.
|
|
|
|
|
- if ( this.root != insertPosition.root ) {
|
|
|
|
|
- return transformed;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if ( utils.compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.SAME ) {
|
|
|
|
|
- // If nodes are inserted in the node that is pointed by this position...
|
|
|
|
|
- if ( insertPosition.offset < this.offset || ( insertPosition.offset == this.offset && insertBefore ) ) {
|
|
|
|
|
- // And are inserted before an offset of that position...
|
|
|
|
|
- // "Push" this positions offset.
|
|
|
|
|
- transformed.offset += howMany;
|
|
|
|
|
|
|
+ if ( utils.compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.SAME ) {
|
|
|
|
|
+ // If nodes are removed from the node that is pointed by this position...
|
|
|
|
|
+ if ( deletePosition.offset < this.offset ) {
|
|
|
|
|
+ // And are removed from before an offset of that position...
|
|
|
|
|
+ // Decrement the offset accordingly.
|
|
|
|
|
+ if ( deletePosition.offset + howMany > this.offset ) {
|
|
|
|
|
+ transformed.offset = deletePosition.offset;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ transformed.offset -= howMany;
|
|
|
}
|
|
}
|
|
|
- } else if ( utils.compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.PREFIX ) {
|
|
|
|
|
- // If nodes are inserted in a node that is on a path to this position...
|
|
|
|
|
- const i = insertPosition.path.length - 1;
|
|
|
|
|
-
|
|
|
|
|
- if ( insertPosition.offset <= this.path[ i ] ) {
|
|
|
|
|
- // And are inserted before next node of that path...
|
|
|
|
|
- // "Push" the index on that path.
|
|
|
|
|
- transformed.path[ i ] += howMany;
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if ( utils.compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.PREFIX ) {
|
|
|
|
|
+ // If nodes are removed from a node that is on a path to this position...
|
|
|
|
|
+ const i = deletePosition.path.length - 1;
|
|
|
|
|
+
|
|
|
|
|
+ if ( deletePosition.offset < this.path[ i ] ) {
|
|
|
|
|
+ // And are removed from before next node of that path...
|
|
|
|
|
+ if ( deletePosition.offset + howMany > this.path[ i ] ) {
|
|
|
|
|
+ // If the next node of that path is removed return null
|
|
|
|
|
+ // because the node containing this position got removed.
|
|
|
|
|
+ return null;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Otherwise, decrement index on that path.
|
|
|
|
|
+ transformed.path[ i ] -= howMany;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return transformed;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Returns this position after being updated by moving `howMany` attributes from `sourcePosition` to `targetPosition`.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} sourcePosition Position before the first element to move.
|
|
|
|
|
- * @param {treeModel.Position} targetPosition Position where moved elements will be inserted.
|
|
|
|
|
- * @param {Number} howMany How many consecutive nodes to move, starting from `sourcePosition`.
|
|
|
|
|
- * @param {Boolean} insertBefore Flag indicating whether moved nodes are pasted before or after `insertPosition`.
|
|
|
|
|
- * This is important only when `targetPosition` and this position are same. If that is the case and the flag is
|
|
|
|
|
- * set to `true`, this position will get transformed by range insertion. If the flag is set to `false`, it won't.
|
|
|
|
|
- * @returns {treeModel.Position} Transformed position.
|
|
|
|
|
- */
|
|
|
|
|
- getTransformedByMove( sourcePosition, targetPosition, howMany, insertBefore ) {
|
|
|
|
|
- // Moving a range removes nodes from their original position. We acknowledge this by proper transformation.
|
|
|
|
|
- let transformed = this.getTransformedByDeletion( sourcePosition, howMany );
|
|
|
|
|
-
|
|
|
|
|
- if ( transformed !== null ) {
|
|
|
|
|
- // This position is not inside a removed node.
|
|
|
|
|
- // Next step is to reflect pasting nodes, which might further affect the position.
|
|
|
|
|
- transformed = transformed.getTransformedByInsertion( targetPosition, howMany, insertBefore );
|
|
|
|
|
- } else {
|
|
|
|
|
- // This position is inside a removed node. In this case, we are unable to simply transform it by range insertion.
|
|
|
|
|
- // Instead, we calculate a combination of this position, move source position and target position.
|
|
|
|
|
- transformed = this._getCombined( sourcePosition, targetPosition );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return transformed;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns this position after being updated by inserting `howMany` nodes at `insertPosition`.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} insertPosition Position where nodes are inserted.
|
|
|
|
|
+ * @param {Number} howMany How many nodes are inserted.
|
|
|
|
|
+ * @param {Boolean} insertBefore Flag indicating whether nodes are inserted before or after `insertPosition`.
|
|
|
|
|
+ * This is important only when `insertPosition` and this position are same. If that is the case and the flag is
|
|
|
|
|
+ * set to `true`, this position will get transformed. If the flag is set to `false`, it won't.
|
|
|
|
|
+ * @returns {treeModel.Position} Transformed position.
|
|
|
|
|
+ */
|
|
|
|
|
+ getTransformedByInsertion( insertPosition, howMany, insertBefore ) {
|
|
|
|
|
+ let transformed = Position.createFromPosition( this );
|
|
|
|
|
|
|
|
|
|
+ // This position can't be affected if insertion was in a different root.
|
|
|
|
|
+ if ( this.root != insertPosition.root ) {
|
|
|
return transformed;
|
|
return transformed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Checks whether this position is after given position.
|
|
|
|
|
- *
|
|
|
|
|
- * **Note:** see {treeModel.Position#isBefore}.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
- * @returns {Boolean} True if this position is after given position.
|
|
|
|
|
- */
|
|
|
|
|
- isAfter( otherPosition ) {
|
|
|
|
|
- return this.compareWith( otherPosition ) == AFTER;
|
|
|
|
|
|
|
+ if ( utils.compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.SAME ) {
|
|
|
|
|
+ // If nodes are inserted in the node that is pointed by this position...
|
|
|
|
|
+ if ( insertPosition.offset < this.offset || ( insertPosition.offset == this.offset && insertBefore ) ) {
|
|
|
|
|
+ // And are inserted before an offset of that position...
|
|
|
|
|
+ // "Push" this positions offset.
|
|
|
|
|
+ transformed.offset += howMany;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if ( utils.compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.PREFIX ) {
|
|
|
|
|
+ // If nodes are inserted in a node that is on a path to this position...
|
|
|
|
|
+ const i = insertPosition.path.length - 1;
|
|
|
|
|
+
|
|
|
|
|
+ if ( insertPosition.offset <= this.path[ i ] ) {
|
|
|
|
|
+ // And are inserted before next node of that path...
|
|
|
|
|
+ // "Push" the index on that path.
|
|
|
|
|
+ transformed.path[ i ] += howMany;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Checks whether this position is before given position.
|
|
|
|
|
- *
|
|
|
|
|
- * **Note:** watch out when using negation of the value returned by this method, because the negation will also
|
|
|
|
|
- * be `true` if positions are in different roots and you might not expect this. You should probably use
|
|
|
|
|
- * `a.isAfter( b ) || a.isEqual( b )` or `!a.isBefore( p ) && a.root == b.root` in most scenarios. If your
|
|
|
|
|
- * condition uses multiple `isAfter` and `isBefore` checks, build them so they do not use negated values, i.e.:
|
|
|
|
|
- *
|
|
|
|
|
- * if ( a.isBefore( b ) && c.isAfter( d ) ) {
|
|
|
|
|
- * // do A.
|
|
|
|
|
- * } else {
|
|
|
|
|
- * // do B.
|
|
|
|
|
- * }
|
|
|
|
|
- *
|
|
|
|
|
- * or, if you have only one if-branch:
|
|
|
|
|
- *
|
|
|
|
|
- * if ( !( a.isBefore( b ) && c.isAfter( d ) ) {
|
|
|
|
|
- * // do B.
|
|
|
|
|
- * }
|
|
|
|
|
- *
|
|
|
|
|
- * rather than:
|
|
|
|
|
- *
|
|
|
|
|
- * if ( !a.isBefore( b ) || && !c.isAfter( d ) ) {
|
|
|
|
|
- * // do B.
|
|
|
|
|
- * } else {
|
|
|
|
|
- * // do A.
|
|
|
|
|
- * }
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
- * @returns {Boolean} True if this position is before given position.
|
|
|
|
|
- */
|
|
|
|
|
- isBefore( otherPosition ) {
|
|
|
|
|
- return this.compareWith( otherPosition ) == BEFORE;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return transformed;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Checks whether this position equals given position.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
- * @returns {Boolean} True if positions are same.
|
|
|
|
|
- */
|
|
|
|
|
- isEqual( otherPosition ) {
|
|
|
|
|
- return this.compareWith( otherPosition ) == SAME;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns this position after being updated by moving `howMany` attributes from `sourcePosition` to `targetPosition`.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} sourcePosition Position before the first element to move.
|
|
|
|
|
+ * @param {treeModel.Position} targetPosition Position where moved elements will be inserted.
|
|
|
|
|
+ * @param {Number} howMany How many consecutive nodes to move, starting from `sourcePosition`.
|
|
|
|
|
+ * @param {Boolean} insertBefore Flag indicating whether moved nodes are pasted before or after `insertPosition`.
|
|
|
|
|
+ * This is important only when `targetPosition` and this position are same. If that is the case and the flag is
|
|
|
|
|
+ * set to `true`, this position will get transformed by range insertion. If the flag is set to `false`, it won't.
|
|
|
|
|
+ * @returns {treeModel.Position} Transformed position.
|
|
|
|
|
+ */
|
|
|
|
|
+ getTransformedByMove( sourcePosition, targetPosition, howMany, insertBefore ) {
|
|
|
|
|
+ // Moving a range removes nodes from their original position. We acknowledge this by proper transformation.
|
|
|
|
|
+ let transformed = this.getTransformedByDeletion( sourcePosition, howMany );
|
|
|
|
|
+
|
|
|
|
|
+ if ( transformed !== null ) {
|
|
|
|
|
+ // This position is not inside a removed node.
|
|
|
|
|
+ // Next step is to reflect pasting nodes, which might further affect the position.
|
|
|
|
|
+ transformed = transformed.getTransformedByInsertion( targetPosition, howMany, insertBefore );
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // This position is inside a removed node. In this case, we are unable to simply transform it by range insertion.
|
|
|
|
|
+ // Instead, we calculate a combination of this position, move source position and target position.
|
|
|
|
|
+ transformed = this._getCombined( sourcePosition, targetPosition );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Checks whether this position is touching given position. Positions touch when there are no characters
|
|
|
|
|
- * or empty nodes in a range between them. Technically, those positions are not equal but in many cases
|
|
|
|
|
- * they are very similar or even indistinguishable when they touch.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
- * @returns {Boolean} True if positions touch.
|
|
|
|
|
- */
|
|
|
|
|
- isTouching( otherPosition ) {
|
|
|
|
|
- let left = null;
|
|
|
|
|
- let right = null;
|
|
|
|
|
- let compare = this.compareWith( otherPosition );
|
|
|
|
|
-
|
|
|
|
|
- switch ( compare ) {
|
|
|
|
|
- case SAME:
|
|
|
|
|
- return true;
|
|
|
|
|
-
|
|
|
|
|
- case BEFORE:
|
|
|
|
|
- left = this;
|
|
|
|
|
- right = otherPosition;
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case AFTER:
|
|
|
|
|
- left = otherPosition;
|
|
|
|
|
- right = this;
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- default:
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- while ( left.path.length + right.path.length ) {
|
|
|
|
|
- if ( left.isEqual( right ) ) {
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if ( left.path.length > right.path.length ) {
|
|
|
|
|
- if ( left.nodeAfter !== null ) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return transformed;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- left.path = left.path.slice( 0, -1 );
|
|
|
|
|
- left.offset++;
|
|
|
|
|
- } else {
|
|
|
|
|
- if ( right.nodeBefore !== null ) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Checks whether this position is after given position.
|
|
|
|
|
+ *
|
|
|
|
|
+ * **Note:** see {treeModel.Position#isBefore}.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
+ * @returns {Boolean} True if this position is after given position.
|
|
|
|
|
+ */
|
|
|
|
|
+ isAfter( otherPosition ) {
|
|
|
|
|
+ return this.compareWith( otherPosition ) == AFTER;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- right.path = right.path.slice( 0, -1 );
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Checks whether this position is before given position.
|
|
|
|
|
+ *
|
|
|
|
|
+ * **Note:** watch out when using negation of the value returned by this method, because the negation will also
|
|
|
|
|
+ * be `true` if positions are in different roots and you might not expect this. You should probably use
|
|
|
|
|
+ * `a.isAfter( b ) || a.isEqual( b )` or `!a.isBefore( p ) && a.root == b.root` in most scenarios. If your
|
|
|
|
|
+ * condition uses multiple `isAfter` and `isBefore` checks, build them so they do not use negated values, i.e.:
|
|
|
|
|
+ *
|
|
|
|
|
+ * if ( a.isBefore( b ) && c.isAfter( d ) ) {
|
|
|
|
|
+ * // do A.
|
|
|
|
|
+ * } else {
|
|
|
|
|
+ * // do B.
|
|
|
|
|
+ * }
|
|
|
|
|
+ *
|
|
|
|
|
+ * or, if you have only one if-branch:
|
|
|
|
|
+ *
|
|
|
|
|
+ * if ( !( a.isBefore( b ) && c.isAfter( d ) ) {
|
|
|
|
|
+ * // do B.
|
|
|
|
|
+ * }
|
|
|
|
|
+ *
|
|
|
|
|
+ * rather than:
|
|
|
|
|
+ *
|
|
|
|
|
+ * if ( !a.isBefore( b ) || && !c.isAfter( d ) ) {
|
|
|
|
|
+ * // do B.
|
|
|
|
|
+ * } else {
|
|
|
|
|
+ * // do A.
|
|
|
|
|
+ * }
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
+ * @returns {Boolean} True if this position is before given position.
|
|
|
|
|
+ */
|
|
|
|
|
+ isBefore( otherPosition ) {
|
|
|
|
|
+ return this.compareWith( otherPosition ) == BEFORE;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Creates a new position after given node.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Node} node Node the position should be directly after.
|
|
|
|
|
- * @returns {treeModel.Position}
|
|
|
|
|
- */
|
|
|
|
|
- static createAfter( node ) {
|
|
|
|
|
- if ( !node.parent ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * You can not make position after root.
|
|
|
|
|
- *
|
|
|
|
|
- * @error position-after-root
|
|
|
|
|
- * @param {treeModel.Node} root
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Checks whether this position equals given position.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
+ * @returns {Boolean} True if positions are same.
|
|
|
|
|
+ */
|
|
|
|
|
+ isEqual( otherPosition ) {
|
|
|
|
|
+ return this.compareWith( otherPosition ) == SAME;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return this.createFromParentAndOffset( node.parent, node.getIndex() + 1 );
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Checks whether this position is touching given position. Positions touch when there are no characters
|
|
|
|
|
+ * or empty nodes in a range between them. Technically, those positions are not equal but in many cases
|
|
|
|
|
+ * they are very similar or even indistinguishable when they touch.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Position} otherPosition Position to compare with.
|
|
|
|
|
+ * @returns {Boolean} True if positions touch.
|
|
|
|
|
+ */
|
|
|
|
|
+ isTouching( otherPosition ) {
|
|
|
|
|
+ let left = null;
|
|
|
|
|
+ let right = null;
|
|
|
|
|
+ let compare = this.compareWith( otherPosition );
|
|
|
|
|
+
|
|
|
|
|
+ switch ( compare ) {
|
|
|
|
|
+ case SAME:
|
|
|
|
|
+ return true;
|
|
|
|
|
+
|
|
|
|
|
+ case BEFORE:
|
|
|
|
|
+ left = this;
|
|
|
|
|
+ right = otherPosition;
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case AFTER:
|
|
|
|
|
+ left = otherPosition;
|
|
|
|
|
+ right = this;
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Creates a new position before the given node.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.node} node Node the position should be directly before.
|
|
|
|
|
- * @returns {treeModel.Position}
|
|
|
|
|
- */
|
|
|
|
|
- static createBefore( node ) {
|
|
|
|
|
- if ( !node.parent ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * You can not make position before root.
|
|
|
|
|
- *
|
|
|
|
|
- * @error position-before-root
|
|
|
|
|
- * @param {treeModel.Node} root
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
|
|
|
|
|
|
|
+ while ( left.path.length + right.path.length ) {
|
|
|
|
|
+ if ( left.isEqual( right ) ) {
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return this.createFromParentAndOffset( node.parent, node.getIndex() );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Creates a new position from the parent element and the offset in that element.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Element} parent Position parent element.
|
|
|
|
|
- * @param {Number} offset Position offset.
|
|
|
|
|
- * @returns {treeModel.Position}
|
|
|
|
|
- */
|
|
|
|
|
- static createFromParentAndOffset( parent, offset ) {
|
|
|
|
|
- const path = parent.getPath();
|
|
|
|
|
|
|
+ if ( left.path.length > right.path.length ) {
|
|
|
|
|
+ if ( left.nodeAfter !== null ) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- path.push( offset );
|
|
|
|
|
|
|
+ left.path = left.path.slice( 0, -1 );
|
|
|
|
|
+ left.offset++;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if ( right.nodeBefore !== null ) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return new this( parent.root, path );
|
|
|
|
|
|
|
+ right.path = right.path.slice( 0, -1 );
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Creates and returns a new instance of Position, which is equal to passed position.
|
|
|
|
|
- *
|
|
|
|
|
- * @param {treeModel.Position} position Position to be cloned.
|
|
|
|
|
- * @returns {treeModel.Position}
|
|
|
|
|
- */
|
|
|
|
|
- static createFromPosition( position ) {
|
|
|
|
|
- return new this( position.root, position.path.slice() );
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Creates a new position after given node.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {treeModel.Node} node Node the position should be directly after.
|
|
|
|
|
+ * @returns {treeModel.Position}
|
|
|
|
|
+ */
|
|
|
|
|
+ static createAfter( node ) {
|
|
|
|
|
+ if ( !node.parent ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * You can not make position after root.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @error position-after-root
|
|
|
|
|
+ * @param {treeModel.Node} root
|
|
|
|
|
+ */
|
|
|
|
|
+ throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Returns a new position that is a combination of this position and given positions. The combined
|
|
|
|
|
- * position is this position transformed by moving a range starting at `from` to `to` position.
|
|
|
|
|
- * It is expected that this position is inside the moved range.
|
|
|
|
|
- *
|
|
|
|
|
- * In other words, this method in a smart way "cuts out" `source` path from this position and
|
|
|
|
|
- * injects `target` path in it's place, while doing necessary fixes in order to get a correct path.
|
|
|
|
|
- *
|
|
|
|
|
- * Example:
|
|
|
|
|
- *
|
|
|
|
|
- * let original = new Position( root, [ 2, 3, 1 ] );
|
|
|
|
|
- * let source = new Position( root, [ 2, 2 ] );
|
|
|
|
|
- * let target = new Position( otherRoot, [ 1, 1, 3 ] );
|
|
|
|
|
- * let combined = original.getCombined( source, target );
|
|
|
|
|
- * // combined.path is [ 1, 1, 4, 1 ], combined.root is otherRoot
|
|
|
|
|
- *
|
|
|
|
|
- * Explanation:
|
|
|
|
|
- *
|
|
|
|
|
- * We have a position `[ 2, 3, 1 ]` and move some nodes from `[ 2, 2 ]` to `[ 1, 1, 3 ]`. The original position
|
|
|
|
|
- * was inside moved nodes and now should point to the new place. The moved nodes will be after
|
|
|
|
|
- * positions `[ 1, 1, 3 ]`, `[ 1, 1, 4 ]`, `[ 1, 1, 5 ]`. Since our position was in the second moved node,
|
|
|
|
|
- * the transformed position will be in a sub-tree of a node at `[ 1, 1, 4 ]`. Looking at original path, we
|
|
|
|
|
- * took care of `[ 2, 3 ]` part of it. Now we have to add the rest of the original path to the transformed path.
|
|
|
|
|
- * Finally, the transformed position will point to `[ 1, 1, 4, 1 ]`.
|
|
|
|
|
- *
|
|
|
|
|
- * @protected
|
|
|
|
|
- * @param {treeModel.Position} source Beginning of the moved range.
|
|
|
|
|
- * @param {treeModel.Position} target Position where the range is moved.
|
|
|
|
|
- * @returns {treeModel.Position} Combined position.
|
|
|
|
|
- */
|
|
|
|
|
- _getCombined( source, target ) {
|
|
|
|
|
- const i = source.path.length - 1;
|
|
|
|
|
-
|
|
|
|
|
- // The first part of a path to combined position is a path to the place where nodes were moved.
|
|
|
|
|
- let combined = Position.createFromPosition( target );
|
|
|
|
|
-
|
|
|
|
|
- // Then we have to update the rest of the path.
|
|
|
|
|
-
|
|
|
|
|
- // Fix the offset because this position might be after `from` position and we have to reflect that.
|
|
|
|
|
- combined.offset = combined.offset + this.path[ i ] - source.offset;
|
|
|
|
|
-
|
|
|
|
|
- // Then, add the rest of the path.
|
|
|
|
|
- // If this position is at the same level as `from` position nothing will get added.
|
|
|
|
|
- combined.path = combined.path.concat( this.path.slice( i + 1 ) );
|
|
|
|
|
-
|
|
|
|
|
- return combined;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return this.createFromParentAndOffset( node.parent, node.getIndex() + 1 );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Flag for "is after" relation between Positions.
|
|
|
|
|
|
|
+ * Creates a new position before the given node.
|
|
|
*
|
|
*
|
|
|
- * @type {Number}
|
|
|
|
|
|
|
+ * @param {treeModel.node} node Node the position should be directly before.
|
|
|
|
|
+ * @returns {treeModel.Position}
|
|
|
*/
|
|
*/
|
|
|
- Position.AFTER = AFTER;
|
|
|
|
|
|
|
+ static createBefore( node ) {
|
|
|
|
|
+ if ( !node.parent ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * You can not make position before root.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @error position-before-root
|
|
|
|
|
+ * @param {treeModel.Node} root
|
|
|
|
|
+ */
|
|
|
|
|
+ throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return this.createFromParentAndOffset( node.parent, node.getIndex() );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Flag for "is before" relation between Positions.
|
|
|
|
|
|
|
+ * Creates a new position from the parent element and the offset in that element.
|
|
|
*
|
|
*
|
|
|
- * @type {Number}
|
|
|
|
|
|
|
+ * @param {treeModel.Element} parent Position parent element.
|
|
|
|
|
+ * @param {Number} offset Position offset.
|
|
|
|
|
+ * @returns {treeModel.Position}
|
|
|
*/
|
|
*/
|
|
|
- Position.BEFORE = BEFORE;
|
|
|
|
|
|
|
+ static createFromParentAndOffset( parent, offset ) {
|
|
|
|
|
+ const path = parent.getPath();
|
|
|
|
|
+
|
|
|
|
|
+ path.push( offset );
|
|
|
|
|
+
|
|
|
|
|
+ return new this( parent.root, path );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Flag for "are in different roots" relation between Positions.
|
|
|
|
|
|
|
+ * Creates and returns a new instance of Position, which is equal to passed position.
|
|
|
*
|
|
*
|
|
|
- * @type {Number}
|
|
|
|
|
|
|
+ * @param {treeModel.Position} position Position to be cloned.
|
|
|
|
|
+ * @returns {treeModel.Position}
|
|
|
*/
|
|
*/
|
|
|
- Position.DIFFERENT = DIFFERENT;
|
|
|
|
|
|
|
+ static createFromPosition( position ) {
|
|
|
|
|
+ return new this( position.root, position.path.slice() );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Flag for "are same" relation between Positions.
|
|
|
|
|
|
|
+ * Returns a new position that is a combination of this position and given positions. The combined
|
|
|
|
|
+ * position is this position transformed by moving a range starting at `from` to `to` position.
|
|
|
|
|
+ * It is expected that this position is inside the moved range.
|
|
|
|
|
+ *
|
|
|
|
|
+ * In other words, this method in a smart way "cuts out" `source` path from this position and
|
|
|
|
|
+ * injects `target` path in it's place, while doing necessary fixes in order to get a correct path.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Example:
|
|
|
|
|
+ *
|
|
|
|
|
+ * let original = new Position( root, [ 2, 3, 1 ] );
|
|
|
|
|
+ * let source = new Position( root, [ 2, 2 ] );
|
|
|
|
|
+ * let target = new Position( otherRoot, [ 1, 1, 3 ] );
|
|
|
|
|
+ * let combined = original.getCombined( source, target );
|
|
|
|
|
+ * // combined.path is [ 1, 1, 4, 1 ], combined.root is otherRoot
|
|
|
*
|
|
*
|
|
|
- * @type {Number}
|
|
|
|
|
|
|
+ * Explanation:
|
|
|
|
|
+ *
|
|
|
|
|
+ * We have a position `[ 2, 3, 1 ]` and move some nodes from `[ 2, 2 ]` to `[ 1, 1, 3 ]`. The original position
|
|
|
|
|
+ * was inside moved nodes and now should point to the new place. The moved nodes will be after
|
|
|
|
|
+ * positions `[ 1, 1, 3 ]`, `[ 1, 1, 4 ]`, `[ 1, 1, 5 ]`. Since our position was in the second moved node,
|
|
|
|
|
+ * the transformed position will be in a sub-tree of a node at `[ 1, 1, 4 ]`. Looking at original path, we
|
|
|
|
|
+ * took care of `[ 2, 3 ]` part of it. Now we have to add the rest of the original path to the transformed path.
|
|
|
|
|
+ * Finally, the transformed position will point to `[ 1, 1, 4, 1 ]`.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @protected
|
|
|
|
|
+ * @param {treeModel.Position} source Beginning of the moved range.
|
|
|
|
|
+ * @param {treeModel.Position} target Position where the range is moved.
|
|
|
|
|
+ * @returns {treeModel.Position} Combined position.
|
|
|
*/
|
|
*/
|
|
|
- Position.SAME = SAME;
|
|
|
|
|
|
|
+ _getCombined( source, target ) {
|
|
|
|
|
+ const i = source.path.length - 1;
|
|
|
|
|
+
|
|
|
|
|
+ // The first part of a path to combined position is a path to the place where nodes were moved.
|
|
|
|
|
+ let combined = Position.createFromPosition( target );
|
|
|
|
|
+
|
|
|
|
|
+ // Then we have to update the rest of the path.
|
|
|
|
|
|
|
|
- return Position;
|
|
|
|
|
-} );
|
|
|
|
|
|
|
+ // Fix the offset because this position might be after `from` position and we have to reflect that.
|
|
|
|
|
+ combined.offset = combined.offset + this.path[ i ] - source.offset;
|
|
|
|
|
+
|
|
|
|
|
+ // Then, add the rest of the path.
|
|
|
|
|
+ // If this position is at the same level as `from` position nothing will get added.
|
|
|
|
|
+ combined.path = combined.path.concat( this.path.slice( i + 1 ) );
|
|
|
|
|
+
|
|
|
|
|
+ return combined;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Flag for "is after" relation between Positions.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @type {Number}
|
|
|
|
|
+ */
|
|
|
|
|
+Position.AFTER = AFTER;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Flag for "is before" relation between Positions.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @type {Number}
|
|
|
|
|
+ */
|
|
|
|
|
+Position.BEFORE = BEFORE;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Flag for "are in different roots" relation between Positions.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @type {Number}
|
|
|
|
|
+ */
|
|
|
|
|
+Position.DIFFERENT = DIFFERENT;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Flag for "are same" relation between Positions.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @type {Number}
|
|
|
|
|
+ */
|
|
|
|
|
+Position.SAME = SAME;
|