8
0
فهرست منبع

Implemented Selection.collapse( pos ).

Piotrek Koszuliński 9 سال پیش
والد
کامیت
95b78fc190
2فایلهای تغییر یافته به همراه37 افزوده شده و 14 حذف شده
  1. 25 14
      packages/ckeditor5-engine/src/treemodel/selection.js
  2. 12 0
      packages/ckeditor5-engine/tests/treemodel/selection.js

+ 25 - 14
packages/ckeditor5-engine/src/treemodel/selection.js

@@ -226,28 +226,39 @@ export default class Selection {
 	 *
 	 * The `location` can be specified as:
 	 *
+	 * * a {@link engine.treeModel.Position position},
 	 * * parent element and offset (offset defaults to `0`),
 	 * * parent element and `'END'` (sets selection at the end of that element),
 	 * * node and `'BEFORE'` or `'AFTER'` (sets selection before or after the given node).
 	 *
 	 * @fires {@link engine.treeModel.Selection#change:range change:range}
-	 * @param {engine.treeModel.Node} node
-	 * @param {Number|'END'|'BEFORE'|'AFTER'} location Offset or one of the flags.
+	 * @param {engine.treeModel.Node|engine.treeModel.Position} nodeOrPosition
+	 * @param {Number|'END'|'BEFORE'|'AFTER'} [location=0] Offset or one of the flags. Used only when
+	 * first parameter is a node.
 	 */
-	collapse( node, location ) {
-		if ( location == 'END' ) {
-			location = node.getChildCount();
-		} else if ( location == 'BEFORE' ) {
-			location = node.getIndex();
-			node = node.parent;
-		} else if ( location == 'AFTER' ) {
-			location = node.getIndex() + 1;
-			node = node.parent;
-		} else if ( !location ) {
-			location = 0;
+	collapse( nodeOrPosition, location ) {
+		let node, pos;
+
+		if ( nodeOrPosition instanceof Position ) {
+			pos = nodeOrPosition;
+		} else {
+			node = nodeOrPosition;
+
+			if ( location == 'END' ) {
+				location = node.getChildCount();
+			} else if ( location == 'BEFORE' ) {
+				location = node.getIndex();
+				node = node.parent;
+			} else if ( location == 'AFTER' ) {
+				location = node.getIndex() + 1;
+				node = node.parent;
+			} else if ( !location ) {
+				location = 0;
+			}
+
+			pos = Position.createFromParentAndOffset( node, location );
 		}
 
-		const pos = Position.createFromParentAndOffset( node, location );
 		const range = new Range( pos, pos );
 
 		this.setRanges( [ range ] );

+ 12 - 0
packages/ckeditor5-engine/tests/treemodel/selection.js

@@ -258,6 +258,18 @@ describe( 'Selection', () => {
 			expect( focus ).to.have.property( 'parent', root );
 			expect( focus ).to.have.property( 'offset', 2 );
 		} );
+
+		it( 'sets selection at the specified position', () => {
+			const pos = Position.createFromParentAndOffset( root, 3 );
+
+			selection.collapse( pos );
+
+			expect( selection ).to.have.property( 'isCollapsed', true );
+
+			const focus = selection.focus;
+			expect( focus ).to.have.property( 'parent', root );
+			expect( focus ).to.have.property( 'offset', 3 );
+		} );
 	} );
 
 	describe( 'removeAllRanges', () => {