Browse Source

Renamed LiveSelection to DocumentSelection.

Piotrek Koszuliński 8 years ago
parent
commit
858e6a4917

+ 4 - 4
packages/ckeditor5-engine/src/model/document.js

@@ -18,7 +18,7 @@ import Position from './position';
 import RootElement from './rootelement';
 import Batch from './batch';
 import History from './history';
-import LiveSelection from './liveselection';
+import DocumentSelection from './documentselection';
 import Schema from './schema';
 import TreeWalker from './treewalker';
 import MarkerCollection from './markercollection';
@@ -90,9 +90,9 @@ export default class Document {
 		 * Selection done on this document.
 		 *
 		 * @readonly
-		 * @member {module:engine/model/liveselection~LiveSelection}
+		 * @member {module:engine/model/documentselection~DocumentSelection}
 		 */
-		this.selection = new LiveSelection( this );
+		this.selection = new DocumentSelection( this );
 
 		/**
 		 * Array of pending changes. See: {@link #enqueueChanges}.
@@ -368,7 +368,7 @@ export default class Document {
 		const json = clone( this );
 
 		// Due to circular references we need to remove parent reference.
-		json.selection = '[engine.model.LiveSelection]';
+		json.selection = '[engine.model.DocumentSelection]';
 
 		return json;
 	}

+ 26 - 23
packages/ckeditor5-engine/src/model/liveselection.js

@@ -4,7 +4,7 @@
  */
 
 /**
- * @module engine/model/liveselection
+ * @module engine/model/documentselection
  */
 
 import Position from './position';
@@ -25,17 +25,20 @@ const attrOpTypes = new Set(
 );
 
 /**
- * `LiveSelection` is a type of {@link module:engine/model/selection~Selection selection} that listens to changes on a
- * {@link module:engine/model/document~Document document} and has it ranges updated accordingly. Internal implementation of this
- * mechanism bases on {@link module:engine/model/liverange~LiveRange live ranges}.
+ * `DocumentSelection` is a special selection which is used as the
+ * {@link module:engine/model/document~Document#selection document's selection}.
+ * There can be only one instance of `DocumentSelection` per document.
  *
- * Differences between {@link module:engine/model/selection~Selection} and `LiveSelection` are:
- * * there is always a range in `LiveSelection` - even if no ranges were added there is a
- * {@link module:engine/model/liveselection~LiveSelection#_getDefaultRange "default range"} present in the selection,
+ * `DocumentSelection` is automatically updated upon changes in the {@link module:engine/model/document~Document document}
+ * to always contain valid ranges. Its attributes are inherited from the text unless set explicitly.
+ *
+ * Differences between {@link module:engine/model/selection~Selection} and `DocumentSelection` are:
+ * * there is always a range in `DocumentSelection` - even if no ranges were added there is a "default range"
+ * present in the selection,
  * * ranges added to this selection updates automatically when the document changes,
- * * attributes of `LiveSelection` are updated automatically according to selection ranges.
+ * * attributes of `DocumentSelection` are updated automatically according to selection ranges.
  *
- * Since `LiveSelection` uses {@link module:engine/model/liverange~LiveRange live ranges}
+ * Since `DocumentSelection` uses {@link module:engine/model/liverange~LiveRange live ranges}
  * and is updated when {@link module:engine/model/document~Document document}
  * changes, it cannot be set on {@link module:engine/model/node~Node nodes}
  * that are inside {@link module:engine/model/documentfragment~DocumentFragment document fragment}.
@@ -44,7 +47,7 @@ const attrOpTypes = new Set(
  *
  * @extends module:engine/model/selection~Selection
  */
-export default class LiveSelection extends Selection {
+export default class DocumentSelection extends Selection {
 	/**
 	 * Creates an empty live selection for given {@link module:engine/model/document~Document}.
 	 *
@@ -57,7 +60,7 @@ export default class LiveSelection extends Selection {
 		 * Document which owns this selection.
 		 *
 		 * @protected
-		 * @member {module:engine/model/document~Document} module:engine/model/liveselection~LiveSelection#_document
+		 * @member {module:engine/model/document~Document} module:engine/model/documentselection~DocumentSelection#_document
 		 */
 		this._document = document;
 
@@ -65,11 +68,11 @@ export default class LiveSelection extends Selection {
 		 * Keeps mapping of attribute name to priority with which the attribute got modified (added/changed/removed)
 		 * last time. Possible values of priority are: `'low'` and `'normal'`.
 		 *
-		 * Priorities are used by internal `LiveSelection` mechanisms. All attributes set using `LiveSelection`
+		 * Priorities are used by internal `DocumentSelection` mechanisms. All attributes set using `DocumentSelection`
 		 * attributes API are set with `'normal'` priority.
 		 *
 		 * @private
-		 * @member {Map} module:engine/model/liveselection~LiveSelection#_attributePriority
+		 * @member {Map} module:engine/model/documentselection~DocumentSelection#_attributePriority
 		 */
 		this._attributePriority = new Map();
 
@@ -238,11 +241,11 @@ export default class LiveSelection extends Selection {
 	}
 
 	/**
-	 * Creates and returns an instance of `LiveSelection` that is a clone of given selection, meaning that it has same
+	 * Creates and returns an instance of `DocumentSelection` that is a clone of given selection, meaning that it has same
 	 * ranges and same direction as this selection.
 	 *
 	 * @params {module:engine/model/selection~Selection} otherSelection Selection to be cloned.
-	 * @returns {module:engine/model/liveselection~LiveSelection} `Selection` instance that is a clone of given selection.
+	 * @returns {module:engine/model/documentselection~DocumentSelection} `Selection` instance that is a clone of given selection.
 	 */
 	static createFromSelection( otherSelection ) {
 		const selection = new this( otherSelection._document );
@@ -383,7 +386,7 @@ export default class LiveSelection extends Selection {
 	}
 
 	/**
-	 * Internal method for setting `LiveSelection` attribute. Supports attribute priorities (through `directChange`
+	 * Internal method for setting `DocumentSelection` attribute. Supports attribute priorities (through `directChange`
 	 * parameter).
 	 *
 	 * @private
@@ -417,7 +420,7 @@ export default class LiveSelection extends Selection {
 	}
 
 	/**
-	 * Internal method for removing `LiveSelection` attribute. Supports attribute priorities (through `directChange`
+	 * Internal method for removing `DocumentSelection` attribute. Supports attribute priorities (through `directChange`
 	 * parameter).
 	 *
 	 * @private
@@ -449,7 +452,7 @@ export default class LiveSelection extends Selection {
 	}
 
 	/**
-	 * Internal method for setting multiple `LiveSelection` attributes. Supports attribute priorities (through
+	 * Internal method for setting multiple `DocumentSelection` attributes. Supports attribute priorities (through
 	 * `directChange` parameter).
 	 *
 	 * @private
@@ -512,7 +515,7 @@ export default class LiveSelection extends Selection {
 	 * @param {String} key Key of attribute to remove.
 	 */
 	_removeStoredAttribute( key ) {
-		const storeKey = LiveSelection._getStoreAttributeKey( key );
+		const storeKey = DocumentSelection._getStoreAttributeKey( key );
 
 		this._document.batch().removeAttribute( this.anchor.parent, storeKey );
 	}
@@ -525,7 +528,7 @@ export default class LiveSelection extends Selection {
 	 * @param {*} value Attribute value.
 	 */
 	_storeAttribute( key, value ) {
-		const storeKey = LiveSelection._getStoreAttributeKey( key );
+		const storeKey = DocumentSelection._getStoreAttributeKey( key );
 
 		this._document.batch().setAttribute( this.anchor.parent, storeKey, value );
 	}
@@ -541,13 +544,13 @@ export default class LiveSelection extends Selection {
 		const batch = this._document.batch();
 
 		for ( const [ oldKey ] of this._getStoredAttributes() ) {
-			const storeKey = LiveSelection._getStoreAttributeKey( oldKey );
+			const storeKey = DocumentSelection._getStoreAttributeKey( oldKey );
 
 			batch.removeAttribute( selectionParent, storeKey );
 		}
 
 		for ( const [ key, value ] of attrs ) {
-			const storeKey = LiveSelection._getStoreAttributeKey( key );
+			const storeKey = DocumentSelection._getStoreAttributeKey( key );
 
 			batch.setAttribute( selectionParent, storeKey, value );
 		}
@@ -668,7 +671,7 @@ export default class LiveSelection extends Selection {
  * @event change:attribute
  */
 
-// Helper function for {@link module:engine/model/liveselection~LiveSelection#_updateAttributes}. It takes model item, checks whether
+// Helper function for {@link module:engine/model/documentselection~DocumentSelection#_updateAttributes}. It takes model item, checks whether
 // it is a text node (or text proxy) and if so, returns it's attributes. If not, returns `null`.
 //
 // @param {module:engine/model/item~Item|null}  node

+ 1 - 1
packages/ckeditor5-engine/tests/model/document/document.js

@@ -558,6 +558,6 @@ describe( 'Document', () => {
 	} );
 
 	it( 'should be correctly converted to json', () => {
-		expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.LiveSelection]' );
+		expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.DocumentSelection]' );
 	} );
 } );

+ 18 - 18
packages/ckeditor5-engine/tests/model/liveselection.js

@@ -9,7 +9,7 @@ import Text from '../../src/model/text';
 import Range from '../../src/model/range';
 import Position from '../../src/model/position';
 import LiveRange from '../../src/model/liverange';
-import LiveSelection from '../../src/model/liveselection';
+import DocumentSelection from '../../src/model/documentselection';
 import InsertOperation from '../../src/model/operation/insertoperation';
 import MoveOperation from '../../src/model/operation/moveoperation';
 import RemoveOperation from '../../src/model/operation/removeoperation';
@@ -25,7 +25,7 @@ import log from '@ckeditor/ckeditor5-utils/src/log';
 
 testUtils.createSinonSandbox();
 
-describe( 'LiveSelection', () => {
+describe( 'DocumentSelection', () => {
 	let doc, root, selection, liveRange, range;
 
 	beforeEach( () => {
@@ -374,14 +374,14 @@ describe( 'LiveSelection', () => {
 	} );
 
 	describe( 'createFromSelection()', () => {
-		it( 'should return a LiveSelection instance', () => {
+		it( 'should return a DocumentSelection instance', () => {
 			selection.addRange( range, true );
 
-			expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
+			expect( DocumentSelection.createFromSelection( selection ) ).to.be.instanceof( DocumentSelection );
 		} );
 	} );
 
-	// LiveSelection uses LiveRanges so here are only simple test to see if integration is
+	// DocumentSelection uses LiveRanges so here are only simple test to see if integration is
 	// working well, without getting into complicated corner cases.
 	describe( 'after applying an operation should get updated and fire events', () => {
 		let spyRange;
@@ -698,7 +698,7 @@ describe( 'LiveSelection', () => {
 
 				expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
 
-				expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
+				expect( emptyP.getAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
 			} );
 		} );
 
@@ -735,8 +735,8 @@ describe( 'LiveSelection', () => {
 				expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
 				expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
 
-				expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
-				expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
+				expect( emptyP.getAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
+				expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
 			} );
 		} );
 
@@ -748,7 +748,7 @@ describe( 'LiveSelection', () => {
 
 				expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
 
-				expect( fullP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
+				expect( fullP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
 			} );
 
 			it( 'should remove stored attribute if the selection is in empty node', () => {
@@ -758,7 +758,7 @@ describe( 'LiveSelection', () => {
 
 				expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
 
-				expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
+				expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
 			} );
 		} );
 
@@ -773,8 +773,8 @@ describe( 'LiveSelection', () => {
 				expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
 				expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
 
-				expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
-				expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
+				expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
+				expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
 			} );
 		} );
 
@@ -893,7 +893,7 @@ describe( 'LiveSelection', () => {
 			it( 'ignores attributes inside an object if selection contains that object', () => {
 				setData( doc, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
 
-				const liveSelection = LiveSelection.createFromSelection( selection );
+				const liveSelection = DocumentSelection.createFromSelection( selection );
 
 				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );
@@ -901,7 +901,7 @@ describe( 'LiveSelection', () => {
 			it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
 				setData( doc, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
 
-				const liveSelection = LiveSelection.createFromSelection( selection );
+				const liveSelection = DocumentSelection.createFromSelection( selection );
 
 				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );
@@ -909,7 +909,7 @@ describe( 'LiveSelection', () => {
 			it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
 				setData( doc, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
 
-				const liveSelection = LiveSelection.createFromSelection( selection );
+				const liveSelection = DocumentSelection.createFromSelection( selection );
 
 				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );
@@ -917,7 +917,7 @@ describe( 'LiveSelection', () => {
 			it( 'reads attributes from text even if the selection contains an object', () => {
 				setData( doc, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
 
-				const liveSelection = LiveSelection.createFromSelection( selection );
+				const liveSelection = DocumentSelection.createFromSelection( selection );
 
 				expect( liveSelection.getAttribute( 'bold' ) ).to.equal( true );
 			} );
@@ -925,7 +925,7 @@ describe( 'LiveSelection', () => {
 			it( 'reads attributes when the entire selection inside an object', () => {
 				setData( doc, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
 
-				const liveSelection = LiveSelection.createFromSelection( selection );
+				const liveSelection = DocumentSelection.createFromSelection( selection );
 
 				expect( liveSelection.getAttribute( 'bold' ) ).to.equal( true );
 			} );
@@ -933,7 +933,7 @@ describe( 'LiveSelection', () => {
 			it( 'stops reading attributes if selection starts with an object', () => {
 				setData( doc, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
 
-				const liveSelection = LiveSelection.createFromSelection( selection );
+				const liveSelection = DocumentSelection.createFromSelection( selection );
 
 				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );