8
0
Pārlūkot izejas kodu

Changed: Selection#getSnapshot changed to Selection.createFromSelection. Docs: Selection#_ranges is now protected.

Szymon Cofalik 9 gadi atpakaļ
vecāks
revīzija
0f2ab759f1

+ 8 - 14
packages/ckeditor5-engine/src/model/liveselection.js

@@ -128,20 +128,6 @@ export default class LiveSelection extends Selection {
 	}
 
 	/**
-	 * Creates an instance of {@link engine.model.Selection} that has same ranges and direction as this selection. Since
-	 * this will be instance of `Selection` instead of `LiveSelection`, it will not be automatically updated or rendered,
-	 * so it can be used in algorithms using and modifying selection.
-	 *
-	 * @returns {engine.model.Selection} Selection instance which ranges and direction is equal to this selection.
-	 */
-	getSnapshot() {
-		const selection = new Selection();
-		selection.setRanges( this.getRanges(), this.isBackward );
-
-		return selection;
-	}
-
-	/**
 	 * Removes all attributes from the selection.
 	 *
 	 * @fires engine.model.LiveSelection#change:attribute
@@ -223,6 +209,14 @@ export default class LiveSelection extends Selection {
 	}
 
 	/**
+	 * 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() {

+ 15 - 1
packages/ckeditor5-engine/src/model/selection.js

@@ -33,7 +33,7 @@ export default class Selection {
 		/**
 		 * Stores all ranges that are selected.
 		 *
-		 * @private
+		 * @protected
 		 * @member {Array.<engine.model.Range>} engine.model.Selection#_ranges
 		 */
 		this._ranges = [];
@@ -270,6 +270,20 @@ export default class Selection {
 	}
 
 	/**
+	 * Creates and returns an instance of {@link engine.model.Selection} 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.Selection} `Selection` instance that is a clone of given selection.
+	 */
+	static createFromSelection( otherSelection ) {
+		const selection = new this();
+		selection.setRanges( otherSelection.getRanges(), otherSelection.isBackward );
+
+		return selection;
+	}
+
+	/**
 	 * Checks if given range intersects with ranges that are already in the selection. Throws an error if it does.
 	 * This method is extracted from {@link engine.model.Selection#_pushRange } so it is easier to override it.
 	 *

+ 3 - 27
packages/ckeditor5-engine/tests/model/liveselection.js

@@ -276,35 +276,11 @@ describe( 'LiveSelection', () => {
 		} );
 	} );
 
-	describe( 'getSnapshot', () => {
-		it( 'should return a Selection instance with default range if no ranges were added', () => {
-			const snapshot = selection.getSnapshot();
-
-			expect( selection.isBackward ).to.equal( snapshot.isBackward );
-
-			const snapshotRanges = Array.from( snapshot.getRanges() );
-
-			expect( snapshotRanges.length ).to.equal( 1 );
-			expect( snapshotRanges[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
-			expect( snapshotRanges[ 0 ].end.path ).to.deep.equal( [ 0, 0 ] );
-		} );
-
-		it( 'should return a Selection instance with same ranges and direction as this selection', () => {
-			selection.addRange( liveRange );
+	describe( 'createFromSelection', () => {
+		it( 'should return a LiveSelection instance', () => {
 			selection.addRange( range, true );
 
-			const snapshot = selection.getSnapshot();
-
-			expect( selection.isBackward ).to.equal( snapshot.isBackward );
-
-			const selectionRanges = Array.from( selection.getRanges() );
-			const snapshotRanges = Array.from( snapshot.getRanges() );
-
-			expect( selectionRanges.length ).to.equal( snapshotRanges.length );
-
-			for ( let i = 0; i < selectionRanges.length; i++ ) {
-				expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
-			}
+			expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
 		} );
 	} );
 

+ 20 - 0
packages/ckeditor5-engine/tests/model/selection.js

@@ -533,4 +533,24 @@ describe( 'Selection', () => {
 			expect( position.path ).to.deep.equal( [ 1 ] );
 		} );
 	} );
+
+	describe( 'createFromSelection', () => {
+		it( 'should return a Selection instance with same ranges and direction as given selection', () => {
+			selection.addRange( liveRange );
+			selection.addRange( range, true );
+
+			const snapshot = Selection.createFromSelection( selection );
+
+			expect( selection.isBackward ).to.equal( snapshot.isBackward );
+
+			const selectionRanges = Array.from( selection.getRanges() );
+			const snapshotRanges = Array.from( snapshot.getRanges() );
+
+			expect( selectionRanges.length ).to.equal( snapshotRanges.length );
+
+			for ( let i = 0; i < selectionRanges.length; i++ ) {
+				expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
+			}
+		} );
+	} );
 } );