| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- 'use strict';
- import LiveRange from './liverange.js';
- import Range from './range.js';
- import Position from './position.js';
- import CharacterProxy from './characterproxy.js';
- import toMap from '../../utils/tomap.js';
- import Selection from './selection.js';
- const storePrefix = 'selection:';
- /**
- * `LiveSelection` is a special type of {@link engine.model.Selection selection} that listens to changes on a
- * {@link engine.model.Document document} and has it ranges updated accordingly. Internal implementation of this
- * mechanism bases on {@link engine.model.LiveRange live ranges}.
- *
- * Differences between {@link engine.model.Selection} and `LiveSelection` are three:
- * * there is always a range in `LiveSelection`, even if no ranges were added - in this case, there is a
- * "default range" in selection which is a collapsed range set at the beginning of the {@link engine.model.Document document},
- * * ranges added to this selection updates automatically when the document changes,
- * * live selection may have attributes.
- *
- * @memberOf engine.model
- */
- export default class LiveSelection extends Selection {
- /**
- * Creates an empty document selection for given {@link engine.model.Document}.
- *
- * @param {engine.model.Document} document Document which owns this selection.
- */
- constructor( document ) {
- super();
- /**
- * Document which owns this selection.
- *
- * @private
- * @member {engine.model.Document} engine.model.Selection#_document
- */
- this._document = document;
- /**
- * List of attributes set on current selection.
- *
- * @protected
- * @member {Map} engine.model.LiveSelection#_attrs
- */
- this._attrs = new Map();
- }
- /**
- * @inheritDoc
- */
- get isCollapsed() {
- const length = this._ranges.length;
- return length === 0 ? true : super.isCollapsed;
- }
- /**
- * @inheritDoc
- */
- get anchor() {
- return super.anchor || this._getDefaultRange().start;
- }
- /**
- * @inheritDoc
- */
- get focus() {
- return super.focus || this._getDefaultRange().start;
- }
- /**
- * @inheritDoc
- */
- get rangeCount() {
- return this._ranges.length ? this._ranges.length : 1;
- }
- /**
- * Unbinds all events previously bound by document selection.
- */
- destroy() {
- for ( let i = 0; i < this._ranges.length; i++ ) {
- this._ranges[ i ].detach();
- }
- }
- /**
- * @inheritDoc
- */
- *getRanges() {
- if ( this._ranges.length ) {
- yield *super.getRanges();
- } else {
- yield this._getDefaultRange();
- }
- }
- /**
- * @inheritDoc
- */
- getFirstRange() {
- return super.getFirstRange() || this._getDefaultRange();
- }
- /**
- * @inheritDoc
- */
- removeAllRanges() {
- this.destroy();
- super.removeAllRanges();
- }
- /**
- * @inheritDoc
- */
- setRanges( newRanges, isLastBackward ) {
- this.destroy();
- super.setRanges( newRanges, isLastBackward );
- }
- /**
- * Removes all attributes from the selection.
- *
- * @fires engine.model.LiveSelection#change:attribute
- */
- clearAttributes() {
- this._attrs.clear();
- this._setStoredAttributesTo( new Map() );
- this.fire( 'change:attribute' );
- }
- /**
- * Gets an attribute value for given key or `undefined` if that attribute is not set on the selection.
- *
- * @param {String} key Key of attribute to look for.
- * @returns {*} Attribute value or `undefined`.
- */
- getAttribute( key ) {
- return this._attrs.get( key );
- }
- /**
- * Returns iterator that iterates over this selection attributes.
- *
- * @returns {Iterable.<*>}
- */
- getAttributes() {
- return this._attrs[ Symbol.iterator ]();
- }
- /**
- * Checks if the selection has an attribute for given key.
- *
- * @param {String} key Key of attribute to check.
- * @returns {Boolean} `true` if attribute with given key is set on selection, `false` otherwise.
- */
- hasAttribute( key ) {
- return this._attrs.has( key );
- }
- /**
- * Removes an attribute with given key from the selection.
- *
- * @fires engine.model.LiveSelection#change:attribute
- * @param {String} key Key of attribute to remove.
- */
- removeAttribute( key ) {
- this._attrs.delete( key );
- this._removeStoredAttribute( key );
- this.fire( 'change:attribute' );
- }
- /**
- * Sets attribute on the selection. If attribute with the same key already is set, it overwrites its values.
- *
- * @fires engine.model.LiveSelection#change:attribute
- * @param {String} key Key of attribute to set.
- * @param {*} value Attribute value.
- */
- setAttribute( key, value ) {
- this._attrs.set( key, value );
- this._storeAttribute( key, value );
- this.fire( 'change:attribute' );
- }
- /**
- * Removes all attributes from the selection and sets given attributes.
- *
- * @fires engine.model.LiveSelection#change:attribute
- * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
- */
- setAttributesTo( attrs ) {
- this._attrs = toMap( attrs );
- this._setStoredAttributesTo( this._attrs );
- this.fire( 'change:attribute' );
- }
- /**
- * Creates and returns an instance of {@link engine.model.LiveSelection} that is a clone of given selection,
- * meaning that it has same ranges and same direction as it.
- *
- * @params {engine.model.Selection} otherSelection Selection to be cloned.
- * @returns {engine.model.LiveSelection} `LiveSelection` instance that is a clone of given selection.
- */
- /**
- * @inheritDoc
- */
- _popRange() {
- this._ranges.pop().detach();
- }
- /**
- * @inheritDoc
- */
- _pushRange( range ) {
- this._checkRange( range );
- this._ranges.push( LiveRange.createFromRange( range ) );
- }
- /**
- * Returns a default range for this selection. The default range is a collapsed range that starts and ends
- * at the beginning of this selection's document {@link engine.model.Document#_getDefaultRoot default root}.
- * This "artificial" range is important for algorithms that base on selection, so they won't break or need
- * special logic if there are no real ranges in the selection.
- *
- * @private
- * @returns {engine.model.Range}
- */
- _getDefaultRange() {
- const defaultRoot = this._document._getDefaultRoot();
- // Find the first position where the selection can be put.
- for ( let position of Range.createFromElement( defaultRoot ).getPositions() ) {
- if ( this._document.schema.check( { name: '$text', inside: position } ) ) {
- return new Range( position, position );
- }
- }
- const position = new Position( defaultRoot, [ 0 ] );
- return new Range( position, position );
- }
- /**
- * Iterates through all attributes stored in current selection's parent.
- *
- * @returns {Iterable.<*>}
- */
- *_getStoredAttributes() {
- const selectionParent = this.getFirstPosition().parent;
- if ( this.isCollapsed && selectionParent.getChildCount() === 0 ) {
- for ( let attr of selectionParent.getAttributes() ) {
- if ( attr[ 0 ].indexOf( storePrefix ) === 0 ) {
- const realKey = attr[ 0 ].substr( storePrefix.length );
- yield [ realKey, attr[ 1 ] ];
- }
- }
- }
- }
- /**
- * Removes attribute with given key from attributes stored in current selection's parent node.
- *
- * @private
- * @param {String} key Key of attribute to remove.
- */
- _removeStoredAttribute( key ) {
- const selectionParent = this.getFirstPosition().parent;
- if ( this.isCollapsed && selectionParent.getChildCount() === 0 ) {
- const storeKey = LiveSelection._getStoreAttributeKey( key );
- this._document.enqueueChanges( () => {
- this._document.batch().removeAttr( storeKey, selectionParent );
- } );
- }
- }
- /**
- * Stores given attribute key and value in current selection's parent node if the selection is collapsed and
- * the parent node is empty.
- *
- * @private
- * @param {String} key Key of attribute to set.
- * @param {*} value Attribute value.
- */
- _storeAttribute( key, value ) {
- const selectionParent = this.getFirstPosition().parent;
- if ( this.isCollapsed && selectionParent.getChildCount() === 0 ) {
- const storeKey = LiveSelection._getStoreAttributeKey( key );
- this._document.enqueueChanges( () => {
- this._document.batch().setAttr( storeKey, value, selectionParent );
- } );
- }
- }
- /**
- * Sets selection attributes stored in current selection's parent node to given set of attributes.
- *
- * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
- * @private
- */
- _setStoredAttributesTo( attrs ) {
- const selectionParent = this.getFirstPosition().parent;
- if ( this.isCollapsed && selectionParent.getChildCount() === 0 ) {
- this._document.enqueueChanges( () => {
- const batch = this._document.batch();
- for ( let attr of this._getStoredAttributes() ) {
- const storeKey = LiveSelection._getStoreAttributeKey( attr[ 0 ] );
- batch.removeAttr( storeKey, selectionParent );
- }
- for ( let attr of attrs ) {
- const storeKey = LiveSelection._getStoreAttributeKey( attr[ 0 ] );
- batch.setAttr( storeKey, attr[ 1 ], selectionParent );
- }
- } );
- }
- }
- /**
- * Updates this selection attributes according to it's ranges and the document.
- *
- * @fires engine.model.LiveSelection#change:attribute
- * @protected
- */
- _updateAttributes() {
- const position = this.getFirstPosition();
- const positionParent = position.parent;
- let attrs = null;
- if ( !this.isCollapsed ) {
- // 1. If selection is a range...
- const range = this.getFirstRange();
- // ...look for a first character node in that range and take attributes from it.
- for ( let item of range ) {
- // This is not an optimal solution because of https://github.com/ckeditor/ckeditor5-engine/issues/454.
- // It can be done better by using `break;` instead of checking `attrs === null`.
- if ( item.type == 'TEXT' && attrs === null ) {
- attrs = item.item.getAttributes();
- }
- }
- } else {
- // 2. If the selection is a caret or the range does not contain a character node...
- const nodeBefore = positionParent.getChild( position.offset - 1 );
- const nodeAfter = positionParent.getChild( position.offset );
- // ...look at the node before caret and take attributes from it if it is a character node.
- attrs = getAttrsIfCharacter( nodeBefore );
- // 3. If not, look at the node after caret...
- if ( !attrs ) {
- attrs = getAttrsIfCharacter( nodeAfter );
- }
- // 4. If not, try to find the first character on the left, that is in the same node.
- if ( !attrs ) {
- let node = nodeBefore;
- while ( node && !attrs ) {
- node = node.previousSibling;
- attrs = getAttrsIfCharacter( node );
- }
- }
- // 5. If not found, try to find the first character on the right, that is in the same node.
- if ( !attrs ) {
- let node = nodeAfter;
- while ( node && !attrs ) {
- node = node.nextSibling;
- attrs = getAttrsIfCharacter( node );
- }
- }
- // 6. If not found, selection should retrieve attributes from parent.
- if ( !attrs ) {
- attrs = this._getStoredAttributes();
- }
- }
- if ( attrs ) {
- this._attrs = new Map( attrs );
- } else {
- this.clearAttributes();
- }
- function getAttrsIfCharacter( node ) {
- if ( node instanceof CharacterProxy ) {
- return node.getAttributes();
- }
- return null;
- }
- this.fire( 'change:attribute' );
- }
- /**
- * Generates and returns an attribute key for selection attributes store, basing on original attribute key.
- *
- * @param {String} key Attribute key to convert.
- * @returns {String} Converted attribute key, applicable for selection store.
- */
- static _getStoreAttributeKey( key ) {
- return storePrefix + key;
- }
- }
- /**
- * Fired whenever selection attributes are changed.
- *
- * @event engine.model.LiveSelection#change:attribute
- */
|