8
0
Просмотр исходного кода

Enhanced ViewSelection#constructor(), removed ViewSelection.createFromSelection().

Maciej Bukowski 8 лет назад
Родитель
Сommit
3dfaa55a95

+ 1 - 1
packages/ckeditor5-engine/src/view/domconverter.js

@@ -108,7 +108,7 @@ export default class DomConverter {
 	 * @param {module:engine/view/selection~Selection} viewSelection
 	 * @param {module:engine/view/selection~Selection} viewSelection
 	 */
 	 */
 	bindFakeSelection( domElement, viewSelection ) {
 	bindFakeSelection( domElement, viewSelection ) {
-		this._fakeSelectionMapping.set( domElement, ViewSelection.createFromSelection( viewSelection ) );
+		this._fakeSelectionMapping.set( domElement, new ViewSelection( viewSelection ) );
 	}
 	}
 
 
 	/**
 	/**

+ 1 - 1
packages/ckeditor5-engine/src/view/observer/fakeselectionobserver.js

@@ -82,7 +82,7 @@ export default class FakeSelectionObserver extends Observer {
 	 */
 	 */
 	_handleSelectionMove( keyCode ) {
 	_handleSelectionMove( keyCode ) {
 		const selection = this.document.selection;
 		const selection = this.document.selection;
-		const newSelection = ViewSelection.createFromSelection( selection );
+		const newSelection = new ViewSelection( selection );
 		newSelection.setFake( false );
 		newSelection.setFake( false );
 
 
 		// Left or up arrow pressed - move selection to start.
 		// Left or up arrow pressed - move selection to start.

+ 29 - 20
packages/ckeditor5-engine/src/view/selection.js

@@ -36,11 +36,34 @@ export default class Selection {
 	/**
 	/**
 	 * Creates new selection instance.
 	 * Creates new selection instance.
 	 *
 	 *
-	 * @param {Iterable.<module:engine/view/range~Range>} [ranges] An optional array of ranges to set.
-	 * @param {Boolean} [isLastBackward] An optional flag describing if last added range was selected forward - from start to end
-	 * (`false`) or backward - from end to start (`true`). Defaults to `false`.
+	 * 		// Creates empty selection without ranges.
+	 *		const selection = new Selection();
+	 *
+	 *		// Creates selection at the given range.
+	 *		const range = new Range( start, end );
+	 *		const selection = new Selection( range, isBackwardSelection );
+	 *
+	 *		// Creates selection at the given ranges
+	 * 		const ranges = [ new Range( start1, end2 ), new Range( star2, end2 ) ];
+	 *		const selection = new Selection( ranges, isBackwardSelection );
+	 *
+	 *		// Creates selection from the other selection.
+	 *		const otherSelection = new Selection();
+	 *		const selection = new Selection( otherSelection );
+	 *
+	 * 		// Creates selection at the given position.
+	 *		const position = new Position( root, path );
+	 *		const selection = new Selection( position );
+	 *
+	 * 		// Creates selection at the start position of given element.
+	 *		const paragraph = writer.createElement( 'paragraph' );
+	 *		const selection = new Selection( paragraph, offset );
+	 *
+	 * @param {module:engine/view/selection~Selection|module:engine/view/position~Position|
+	 * Iterable.<module:engine/view/range~Range>|module:engine/view/range~Range|module:engine/view/item~Item} [selectable]
+	 * @param {Boolean|Number|'before'|'end'|'after'} [backwardSelectionOrOffset]
 	 */
 	 */
-	constructor( ranges, isLastBackward ) {
+	constructor( selectable, backwardSelectionOrOffset ) {
 		/**
 		/**
 		 * Stores all ranges that are selected.
 		 * Stores all ranges that are selected.
 		 *
 		 *
@@ -73,8 +96,8 @@ export default class Selection {
 		 */
 		 */
 		this._fakeSelectionLabel = '';
 		this._fakeSelectionLabel = '';
 
 
-		if ( ranges ) {
-			this._setRanges( ranges, isLastBackward );
+		if ( selectable ) {
+			this.setTo( selectable, backwardSelectionOrOffset );
 		}
 		}
 	}
 	}
 
 
@@ -521,20 +544,6 @@ export default class Selection {
 		return ( nodeAfterStart instanceof Element && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
 		return ( nodeAfterStart instanceof Element && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
 	}
 	}
 
 
-	/**
-	 * Creates and returns an instance of `Selection` that is a clone of given selection, meaning that it has same
-	 * ranges and same direction as this selection.
-	 *
-	 * @params {module:engine/view/selection~Selection} otherSelection Selection to be cloned.
-	 * @returns {module:engine/view/selection~Selection} `Selection` instance that is a clone of given selection.
-	 */
-	static createFromSelection( otherSelection ) {
-		const selection = new Selection();
-		selection.setTo( otherSelection );
-
-		return selection;
-	}
-
 	/**
 	/**
 	 * Adds a range to the selection. Added range is copied. This means that passed range is not saved in the
 	 * Adds a range to the selection. Added range is copied. This means that passed range is not saved in the
 	 * selection instance and you can safely operate on it.
 	 * selection instance and you can safely operate on it.

+ 1 - 1
packages/ckeditor5-engine/tests/view/domconverter/binding.js

@@ -282,7 +282,7 @@ describe( 'DomConverter', () => {
 		} );
 		} );
 
 
 		it( 'should keep a copy of selection', () => {
 		it( 'should keep a copy of selection', () => {
-			const selectionCopy = ViewSelection.createFromSelection( selection );
+			const selectionCopy = new ViewSelection( selection );
 
 
 			selection.setTo( ViewRange.createIn( new ViewElement() ), true );
 			selection.setTo( ViewRange.createIn( new ViewElement() ), true );
 			const bindSelection = converter.fakeSelectionToView( domEl );
 			const bindSelection = converter.fakeSelectionToView( domEl );

+ 76 - 19
packages/ckeditor5-engine/tests/view/selection.js

@@ -48,6 +48,82 @@ describe( 'Selection', () => {
 
 
 			expect( selection.isBackward ).to.be.true;
 			expect( selection.isBackward ).to.be.true;
 		} );
 		} );
+
+		it( 'should be able to create a selection from the given range and isLastBackward flag', () => {
+			const selection = new Selection( range1, true );
+
+			expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
+			expect( selection.isBackward ).to.be.true;
+		} );
+
+		it( 'should be able to create a selection from the given iterable of ranges and isLastBackward flag', () => {
+			const ranges = new Set( [ range1, range2, range3 ] );
+			const selection = new Selection( ranges, false );
+
+			expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2, range3 ] );
+			expect( selection.isBackward ).to.be.false;
+		} );
+
+		it( 'should be able to create a collapsed selection at the given position', () => {
+			const position = range1.start;
+			const selection = new Selection( position );
+
+			expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
+			expect( selection.getFirstRange().start ).to.deep.equal( position );
+			expect( selection.getFirstRange().end ).to.deep.equal( position );
+			expect( selection.isBackward ).to.be.false;
+		} );
+
+		it( 'should be able to create a collapsed selection at the given position', () => {
+			const position = range1.start;
+			const selection = new Selection( position );
+
+			expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
+			expect( selection.getFirstRange().start ).to.deep.equal( position );
+			expect( selection.getFirstRange().end ).to.deep.equal( position );
+			expect( selection.isBackward ).to.be.false;
+		} );
+
+		it( 'should be able to create a selection from the other selection', () => {
+			const otherSelection = new Selection( [ range2, range3 ], true );
+			const selection = new Selection( otherSelection );
+
+			expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range2, range3 ] );
+			expect( selection.isBackward ).to.be.true;
+		} );
+
+		it( 'should be able to create a fake selection from the other fake selection', () => {
+			const otherSelection = new Selection( [ range2, range3 ], true );
+			otherSelection.setFake( true, { label: 'foo bar baz' } );
+			const selection = new Selection( otherSelection );
+
+			expect( selection.isFake ).to.be.true;
+			expect( selection.fakeSelectionLabel ).to.equal( 'foo bar baz' );
+		} );
+
+		it( 'should throw an error when range is invalid', () => {
+			expect( () => {
+				// eslint-disable-next-line no-new
+				new Selection( [ { invalid: 'range' } ] );
+			} ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
+		} );
+
+		it( 'should throw an error when ranges intersects', () => {
+			const text = el.getChild( 0 );
+			const range2 = Range.createFromParentsAndOffsets( text, 7, text, 15 );
+
+			expect( () => {
+				// eslint-disable-next-line no-new
+				new Selection( [ range1, range2 ] );
+			} ).to.throw( CKEditorError, 'view-selection-range-intersects' );
+		} );
+
+		it( 'should throw an error when trying to set to not selectable', () => {
+			expect( () => {
+				// eslint-disable-next-line no-new
+				new Selection( {} );
+			} ).to.throw( /view-selection-setTo-not-selectable/ );
+		} );
 	} );
 	} );
 
 
 	describe( 'anchor', () => {
 	describe( 'anchor', () => {
@@ -813,25 +889,6 @@ describe( 'Selection', () => {
 		} );
 		} );
 	} );
 	} );
 
 
-	describe( 'createFromSelection', () => {
-		it( 'should return a Selection instance with same ranges and direction as given selection', () => {
-			selection.setTo( [ range1, range2 ], true );
-
-			const snapshot = Selection.createFromSelection( selection );
-
-			expect( snapshot.isBackward ).to.equal( selection.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;
-			}
-		} );
-	} );
-
 	describe( 'isFake', () => {
 	describe( 'isFake', () => {
 		it( 'should be false for newly created instance', () => {
 		it( 'should be false for newly created instance', () => {
 			expect( selection.isFake ).to.be.false;
 			expect( selection.isFake ).to.be.false;