| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /**
- * @module engine/model/liveposition
- */
- import Position from './position';
- import Range from './range';
- import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- /**
- * `LivePosition` is a type of {@link module:engine/model/position~Position Position}
- * that updates itself as {@link module:engine/model/document~Document document}
- * is changed through operations. It may be used as a bookmark.
- *
- * **Note:** Contrary to {@link module:engine/model/position~Position}, `LivePosition` works only in roots that are
- * {@link module:engine/model/rootelement~RootElement}.
- * If {@link module:engine/model/documentfragment~DocumentFragment} is passed, error will be thrown.
- *
- * **Note:** Be very careful when dealing with `LivePosition`. Each `LivePosition` instance bind events that might
- * have to be unbound.
- * Use {@link module:engine/model/liveposition~LivePosition#detach} whenever you don't need `LivePosition` anymore.
- *
- * @extends module:engine/model/position~Position
- */
- export default class LivePosition extends Position {
- /**
- * Creates a live position.
- *
- * @see module:engine/model/position~Position
- * @param {module:engine/model/rootelement~RootElement} root
- * @param {Array.<Number>} path
- * @param {module:engine/model/position~PositionStickiness} [stickiness] Defaults to `'sticksToNext'`.
- * See {@link module:engine/model/liveposition~LivePosition#stickiness}.
- */
- constructor( root, path, stickiness ) {
- super( root, path );
- if ( !this.root.is( 'rootElement' ) ) {
- /**
- * LivePosition's root has to be an instance of RootElement.
- *
- * @error liveposition-root-not-rootelement
- */
- throw new CKEditorError(
- 'model-liveposition-root-not-rootelement: LivePosition\'s root has to be an instance of RootElement.'
- );
- }
- /**
- * Flag representing `LivePosition` stickiness. `LivePosition` might be sticking to previous node or next node.
- * Whenever some nodes are inserted at the same position as `LivePosition`, `stickiness` is checked to decide if
- * LivePosition should be moved. Similar applies when a range of nodes is moved and one of it's boundary
- * position is same as `LivePosition`.
- *
- * Examples:
- *
- * Insert:
- * Position is at | and we insert at the same position, marked as ^:
- * - | sticks to previous node: `<p>f|^oo</p>` => `<p>f|baroo</p>`
- * - | sticks to next node: `<p>f^|oo</p>` => `<p>fbar|oo</p>`
- *
- * Move:
- * Position is at | and range [ ] is moved to position ^:
- * - | sticks to previous node: `<p>f|[oo]</p><p>b^ar</p>` => `<p>f|</p><p>booar</p>`
- * - | sticks to next node: `<p>f|[oo]</p><p>b^ar</p>` => `<p>f</p><p>b|ooar</p>`
- *
- * @member {module:engine/model/position~PositionStickiness} module:engine/model/liveposition~LivePosition#stickiness
- */
- this.stickiness = stickiness || 'sticksToNext';
- bindWithDocument.call( this );
- }
- /**
- * Unbinds all events previously bound by `LivePosition`. Use it whenever you don't need `LivePosition` instance
- * anymore (i.e. when leaving scope in which it was declared or before re-assigning variable that was
- * referring to it).
- */
- detach() {
- this.stopListening();
- }
- /**
- * @static
- * @method module:engine/model/liveposition~LivePosition.createAfter
- * @see module:engine/model/position~Position.createAfter
- * @param {module:engine/model/node~Node} node
- * @returns {module:engine/model/liveposition~LivePosition}
- */
- /**
- * @static
- * @method module:engine/model/liveposition~LivePosition.createBefore
- * @see module:engine/model/position~Position.createBefore
- * @param {module:engine/model/node~Node} node
- * @returns {module:engine/model/liveposition~LivePosition}
- */
- /**
- * @static
- * @method module:engine/model/liveposition~LivePosition.createFromParentAndOffset
- * @see module:engine/model/position~Position.createFromParentAndOffset
- * @param {module:engine/model/element~Element} parent
- * @param {Number} offset
- * @returns {module:engine/model/liveposition~LivePosition}
- */
- /**
- * @static
- * @method module:engine/model/liveposition~LivePosition.createFromPosition
- * @see module:engine/model/position~Position.createFromPosition
- * @param {module:engine/model/position~Position} position
- * @returns {module:engine/model/liveposition~LivePosition}
- */
- /**
- * Fired when `LivePosition` instance is changed due to changes on {@link module:engine/model/document~Document}.
- *
- * @event module:engine/model/liveposition~LivePosition#change
- * @param {module:engine/model/position~Position} oldPosition Position equal to this live position before it got changed.
- */
- }
- /**
- * Binds this `LivePosition` to the {@link module:engine/model/document~Document document} that owns
- * this position's {@link module:engine/model/position~Position#root root}.
- *
- * @ignore
- * @private
- * @method module:engine/model/liveposition~LivePosition.bindWithDocument
- */
- function bindWithDocument() {
- // Operation types handled by LivePosition (these are operations that change model tree structure).
- const supportedTypes = new Set( [ 'insert', 'move', 'remove', 'reinsert' ] );
- this.listenTo(
- this.root.document.model,
- 'applyOperation',
- ( event, args ) => {
- const operation = args[ 0 ];
- if ( !operation.isDocumentOperation ) {
- return;
- }
- if ( supportedTypes.has( operation.type ) ) {
- transform.call( this, operation );
- }
- },
- { priority: 'low' }
- );
- }
- /**
- * Updates this position accordingly to the updates applied to the model. Bases on change events.
- *
- * @ignore
- * @private
- * @method transform
- * @param {module:engine/model/operation/operation~Operation} operation Executed operation.
- */
- function transform( operation ) {
- /* eslint-disable no-case-declarations */
- let range;
- let position;
- if ( operation.type == 'insert' ) {
- range = Range.createFromPositionAndShift( operation.position, operation.nodes.maxOffset );
- } else {
- range = Range.createFromPositionAndShift( operation.getMovedRangeStart(), operation.howMany );
- position = operation.sourcePosition;
- }
- const howMany = range.end.offset - range.start.offset;
- let transformed;
- switch ( operation.type ) {
- case 'insert':
- const insertBefore = this.stickiness == 'sticksToNext';
- transformed = this._getTransformedByInsertion( range.start, howMany, insertBefore );
- break;
- case 'move':
- case 'remove':
- case 'reinsert':
- const originalRange = Range.createFromPositionAndShift( position, howMany );
- const gotMoved = originalRange.containsPosition( this ) ||
- ( originalRange.start.isEqual( this ) && this.stickiness == 'sticksToNext' ) ||
- ( originalRange.end.isEqual( this ) && this.stickiness == 'sticksToPrevious' );
- // We can't use ._getTransformedByMove() because we have a different if-condition.
- if ( gotMoved ) {
- transformed = this._getCombined( position, range.start );
- } else {
- const insertBefore = this.stickiness == 'sticksToNext';
- // `Position._getTransformedByMove` is expecting `targetPosition` to be "before" move
- // (before transformation). `range.start` is already after the move happened.
- // We have to revert `targetPosition` to the state before the move.
- const targetPosition = range.start._getTransformedByInsertion( position, howMany );
- transformed = this._getTransformedByMove( position, targetPosition, howMany, insertBefore );
- }
- break;
- }
- if ( !this.isEqual( transformed ) ) {
- const oldPosition = Position.createFromPosition( this );
- this.path = transformed.path;
- this.root = transformed.root;
- this.fire( 'change', oldPosition );
- }
- /* eslint-enable no-case-declarations */
- }
- mix( LivePosition, EmitterMixin );
- /**
- * Enum representing how position is "sticking" with their neighbour nodes.
- * Possible values: `'sticksToNext'`, `'sticksToPrevious'`.
- *
- * @typedef {String} module:engine/model/position~PositionStickiness
- */
|