瀏覽代碼

Changed: moved model.LiveSelection#getDefaultRange to model.Document + other slight fixes.

Szymon Cofalik 9 年之前
父節點
當前提交
642abe5f87

+ 25 - 1
packages/ckeditor5-engine/src/model/document.js

@@ -7,6 +7,8 @@
 import deltas from './delta/basic-deltas.js'; // jshint ignore:line
 import transformations from './delta/basic-transformations.js'; // jshint ignore:line
 
+import Range from './range.js';
+import Position from './position.js';
 import RootElement from './rootelement.js';
 import Batch from './batch.js';
 import History from './history.js';
@@ -313,7 +315,7 @@ export default class Document {
 	 * @protected
 	 * @returns {engine.model.RootElement} The default root for this document.
 	 */
-	_getDefaultRoot() {
+	getDefaultRoot() {
 		for ( let root of this._roots.values() ) {
 			if ( root !== this.graveyard ) {
 				return root;
@@ -324,6 +326,28 @@ export default class Document {
 	}
 
 	/**
+	 * 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's {@link engine.model.Document#_getDefaultRoot default root}.
+	 *
+	 * @protected
+	 * @returns {engine.model.Range}
+	 */
+	getDefaultRange() {
+		const defaultRoot = this.getDefaultRoot();
+
+		// Find the first position where the selection can be put.
+		for ( let position of Range.createIn( defaultRoot ).getPositions() ) {
+			if ( this.schema.check( { name: '$text', inside: position } ) ) {
+				return new Range( position, position );
+			}
+		}
+
+		const position = new Position( defaultRoot, [ 0 ] );
+
+		return new Range( position, position );
+	}
+
+	/**
 	 * Checks whether given {@link engine.model.Range range} is a valid range for
 	 * {@link engine.model.Document#selection document's selection}.
 	 *

+ 6 - 32
packages/ckeditor5-engine/src/model/liveselection.js

@@ -4,8 +4,6 @@
  */
 
 import LiveRange from './liverange.js';
-import Range from './range.js';
-import Position from './position.js';
 import Text from './text.js';
 import TextProxy from './textproxy.js';
 import toMap from '../../utils/tomap.js';
@@ -43,7 +41,7 @@ export default class LiveSelection extends Selection {
 		 * Document which owns this selection.
 		 *
 		 * @private
-		 * @member {engine.model.Document} engine.model.Selection#_document
+		 * @member {engine.model.Document} engine.model.LiveSelection#_document
 		 */
 		this._document = document;
 	}
@@ -61,14 +59,14 @@ export default class LiveSelection extends Selection {
 	 * @inheritDoc
 	 */
 	get anchor() {
-		return super.anchor || this._getDefaultRange().start;
+		return super.anchor || this._document.getDefaultRange().start;
 	}
 
 	/**
 	 * @inheritDoc
 	 */
 	get focus() {
-		return super.focus || this._getDefaultRange().start;
+		return super.focus || this._document.getDefaultRange().start;
 	}
 
 	/**
@@ -94,7 +92,7 @@ export default class LiveSelection extends Selection {
 		if ( this._ranges.length ) {
 			yield *super.getRanges();
 		} else {
-			yield this._getDefaultRange();
+			yield this._document.getDefaultRange();
 		}
 	}
 
@@ -102,14 +100,14 @@ export default class LiveSelection extends Selection {
 	 * @inheritDoc
 	 */
 	getFirstRange() {
-		return super.getFirstRange() || this._getDefaultRange();
+		return super.getFirstRange() || this._document.getDefaultRange();
 	}
 
 	/**
 	 * @inheritDoc
 	 */
 	getLastRange() {
-		return super.getLastRange() || this._getDefaultRange();
+		return super.getLastRange() || this._document.getDefaultRange();
 	}
 
 	/**
@@ -176,30 +174,6 @@ export default class LiveSelection extends Selection {
 	}
 
 	/**
-	 * 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's {@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.createIn( 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.<*>}

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

@@ -284,9 +284,9 @@ describe( 'Document', () => {
 		} );
 	} );
 
-	describe( '_getDefaultRoot', () => {
+	describe( 'getDefaultRoot', () => {
 		it( 'should return graveyard root if there are no other roots in the document', () => {
-			expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
+			expect( doc.getDefaultRoot() ).to.equal( doc.graveyard );
 		} );
 
 		it( 'should return the first root added to the document', () => {
@@ -294,7 +294,7 @@ describe( 'Document', () => {
 			doc.createRoot( '$root', 'rootB' );
 			doc.createRoot( '$root', 'rootC' );
 
-			expect( doc._getDefaultRoot() ).to.equal( rootA );
+			expect( doc.getDefaultRoot() ).to.equal( rootA );
 		} );
 	} );
 

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

@@ -284,7 +284,7 @@ describe( 'LiveSelection', () => {
 
 	// LiveSelection 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 not fire update event', () => {
+	describe( 'after applying an operation should get updated and not fire change:range event', () => {
 		let spy;
 
 		beforeEach( () => {