8
0
Pārlūkot izejas kodu

Changed view selection setFake method as protected. Created view writer method to handle setting fake selection.

Szymon Kupś 7 gadi atpakaļ
vecāks
revīzija
958d0d8bae

+ 1 - 1
packages/ckeditor5-engine/src/conversion/model-selection-to-view-converters.js

@@ -129,5 +129,5 @@ export function clearAttributes() {
  * {@link module:engine/model/selection~Selection model selection} conversion.
  */
 export function clearFakeSelection() {
-	return ( evt, data, consumable, conversionApi ) => conversionApi.viewSelection.setFake( false );
+	return ( evt, data, consumable, conversionApi ) => conversionApi.viewSelection._setFake( false );
 }

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

@@ -83,7 +83,7 @@ export default class FakeSelectionObserver extends Observer {
 	_handleSelectionMove( keyCode ) {
 		const selection = this.document.selection;
 		const newSelection = new ViewSelection( selection );
-		newSelection.setFake( false );
+		newSelection._setFake( false );
 
 		// Left or up arrow pressed - move selection to start.
 		if ( keyCode == keyCodes.arrowleft || keyCode == keyCodes.arrowup ) {

+ 4 - 3
packages/ckeditor5-engine/src/view/selection.js

@@ -109,12 +109,13 @@ export default class Selection {
 	 * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM (and be
 	 * properly handled by screen readers).
 	 *
+	 * @protected
 	 * @fires change
 	 * @param {Boolean} [value=true] If set to true selection will be marked as `fake`.
 	 * @param {Object} [options] Additional options.
 	 * @param {String} [options.label=''] Fake selection label.
 	 */
-	setFake( value = true, options = {} ) {
+	_setFake( value = true, options = {} ) {
 		this._isFake = value;
 		this._fakeSelectionLabel = value ? options.label || '' : '';
 
@@ -124,7 +125,7 @@ export default class Selection {
 	/**
 	 * Returns true if selection instance is marked as `fake`.
 	 *
-	 * @see #setFake
+	 * @see #_setFake
 	 * @returns {Boolean}
 	 */
 	get isFake() {
@@ -134,7 +135,7 @@ export default class Selection {
 	/**
 	 * Returns fake selection label.
 	 *
-	 * @see #setFake
+	 * @see #_setFake
 	 * @returns {String}
 	 */
 	get fakeSelectionLabel() {

+ 18 - 1
packages/ckeditor5-engine/src/view/writer.js

@@ -77,7 +77,24 @@ export default class Writer {
 	 * first parameter is a {@link module:engine/view/item~Item view item}.
 	 */
 	setSelectionFocus( itemOrPosition, offset ) {
-		this.model.document.selection._setFocus( itemOrPosition, offset );
+		this.document.selection._setFocus( itemOrPosition, offset );
+	}
+
+	/**
+	 * Sets {@link module:engine/view/selection~Selection selection's} to be marked as `fake`. A fake selection does
+	 * not render as browser native selection over selected elements and is hidden to the user.
+	 * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
+	 * represented in other way, for example by applying proper CSS class.
+	 *
+	 * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM (and be
+	 * properly handled by screen readers).
+	 *
+	 * @param {Boolean} [value=true] If set to true selection will be marked as `fake`.
+	 * @param {Object} [options] Additional options.
+	 * @param {String} [options.label=''] Fake selection label.
+	 */
+	setFakeSelection( value = true, options = {} ) {
+		this.document.selection._setFake( value, options );
 	}
 
 	/**

+ 1 - 1
packages/ckeditor5-engine/tests/conversion/model-selection-to-view-converters.js

@@ -480,7 +480,7 @@ describe( 'model-selection-to-view-converters', () => {
 				dispatcher.on( 'selection', clearFakeSelection() );
 
 				view.change( writer => {
-					viewSelection.setFake( true );
+					viewSelection._setFake( true );
 					dispatcher.convertSelection( docSelection, writer );
 				} );
 				expect( viewSelection.isFake ).to.be.false;

+ 2 - 2
packages/ckeditor5-engine/tests/view/manual/fakeselection.js

@@ -39,8 +39,8 @@ viewDocument.on( 'mouseup', ( evt, data ) => {
 		console.log( 'Making selection around the <strong>.' );
 
 		const range = ViewRange.createOn( viewStrong );
-		viewDocument.selection.setTo( [ range ] );
-		viewDocument.selection.setFake( true, { label: 'fake selection over bar' } );
+		viewDocument.selection._setTo( [ range ] );
+		viewDocument.selection._setFake( true, { label: 'fake selection over bar' } );
 
 		viewDocument.render();
 

+ 3 - 3
packages/ckeditor5-engine/tests/view/observer/fakeselectionobserver.js

@@ -33,7 +33,7 @@ describe( 'FakeSelectionObserver', () => {
 		root = createViewRoot( viewDocument );
 		view.attachDomRoot( domRoot );
 		observer = view.getObserver( FakeSelectionObserver );
-		viewDocument.selection.setFake();
+		viewDocument.selection._setFake();
 	} );
 
 	afterEach( () => {
@@ -41,7 +41,7 @@ describe( 'FakeSelectionObserver', () => {
 	} );
 
 	it( 'should do nothing if selection is not fake', () => {
-		viewDocument.selection.setFake( false );
+		viewDocument.selection._setFake( false );
 
 		return checkEventPrevention( keyCodes.arrowleft, false );
 	} );
@@ -200,7 +200,7 @@ describe( 'FakeSelectionObserver', () => {
 	//
 	// @param {Number} keyCode
 	function changeFakeSelectionPressing( keyCode ) {
-		viewDocument.selection.setFake();
+		viewDocument.selection._setFake();
 
 		const data = {
 			keyCode,

+ 14 - 14
packages/ckeditor5-engine/tests/view/renderer.js

@@ -122,7 +122,7 @@ describe( 'Renderer', () => {
 			renderer.markedChildren.clear();
 
 			selection._setTo( null );
-			selection.setFake( false );
+			selection._setFake( false );
 
 			selectionEditable = viewRoot;
 
@@ -1369,7 +1369,7 @@ describe( 'Renderer', () => {
 
 			it( 'should render fake selection', () => {
 				const label = 'fake selection label';
-				selection.setFake( true, { label } );
+				selection._setFake( true, { label } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
@@ -1386,7 +1386,7 @@ describe( 'Renderer', () => {
 			} );
 
 			it( 'should render &nbsp; if no selection label is provided', () => {
-				selection.setFake( true );
+				selection._setFake( true );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
@@ -1402,10 +1402,10 @@ describe( 'Renderer', () => {
 			} );
 
 			it( 'should remove fake selection container when selection is no longer fake', () => {
-				selection.setFake( true );
+				selection._setFake( true );
 				renderer.render();
 
-				selection.setFake( false );
+				selection._setFake( false );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 1 );
@@ -1421,14 +1421,14 @@ describe( 'Renderer', () => {
 			it( 'should reuse fake selection container #1', () => {
 				const label = 'fake selection label';
 
-				selection.setFake( true, { label } );
+				selection._setFake( true, { label } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
 
 				const container = domRoot.childNodes[ 1 ];
 
-				selection.setFake( true, { label } );
+				selection._setFake( true, { label } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
@@ -1442,19 +1442,19 @@ describe( 'Renderer', () => {
 			} );
 
 			it( 'should reuse fake selection container #2', () => {
-				selection.setFake( true, { label: 'label 1' } );
+				selection._setFake( true, { label: 'label 1' } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
 
 				const container = domRoot.childNodes[ 1 ];
 
-				selection.setFake( false );
+				selection._setFake( false );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 1 );
 
-				selection.setFake( true, { label: 'label 2' } );
+				selection._setFake( true, { label: 'label 2' } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
@@ -1468,14 +1468,14 @@ describe( 'Renderer', () => {
 			} );
 
 			it( 'should reuse fake selection container #3', () => {
-				selection.setFake( true, { label: 'label 1' } );
+				selection._setFake( true, { label: 'label 1' } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
 
 				const container = domRoot.childNodes[ 1 ];
 
-				selection.setFake( true, { label: 'label 2' } );
+				selection._setFake( true, { label: 'label 2' } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
@@ -1489,7 +1489,7 @@ describe( 'Renderer', () => {
 			} );
 
 			it( 'should style fake selection container properly', () => {
-				selection.setFake( true, { label: 'fake selection' } );
+				selection._setFake( true, { label: 'fake selection' } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );
@@ -1502,7 +1502,7 @@ describe( 'Renderer', () => {
 			} );
 
 			it( 'should bind fake selection container to view selection', () => {
-				selection.setFake( true, { label: 'fake selection' } );
+				selection._setFake( true, { label: 'fake selection' } );
 				renderer.render();
 
 				expect( domRoot.childNodes.length ).to.equal( 2 );

+ 16 - 16
packages/ckeditor5-engine/tests/view/selection.js

@@ -94,7 +94,7 @@ describe( 'Selection', () => {
 
 		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' } );
+			otherSelection._setFake( true, { label: 'foo bar baz' } );
 			const selection = new Selection( otherSelection );
 
 			expect( selection.isFake ).to.be.true;
@@ -511,15 +511,15 @@ describe( 'Selection', () => {
 
 		it( 'should return false if one selection is fake', () => {
 			const otherSelection = new Selection();
-			otherSelection.setFake( true );
+			otherSelection._setFake( true );
 
 			expect( selection.isEqual( otherSelection ) ).to.be.false;
 		} );
 
 		it( 'should return true if both selection are fake', () => {
 			const otherSelection = new Selection( [ range1 ] );
-			otherSelection.setFake( true );
-			selection.setFake( true );
+			otherSelection._setFake( true );
+			selection._setFake( true );
 			selection._setTo( range1 );
 
 			expect( selection.isEqual( otherSelection ) ).to.be.true;
@@ -527,8 +527,8 @@ describe( 'Selection', () => {
 
 		it( 'should return false if both selection are fake but have different label', () => {
 			const otherSelection = new Selection( [ range1 ] );
-			otherSelection.setFake( true, { label: 'foo bar baz' } );
-			selection.setFake( true );
+			otherSelection._setFake( true, { label: 'foo bar baz' } );
+			selection._setFake( true );
 			selection._setTo( range1 );
 
 			expect( selection.isEqual( otherSelection ) ).to.be.false;
@@ -709,7 +709,7 @@ describe( 'Selection', () => {
 			it( 'should set fake state and label', () => {
 				const otherSelection = new Selection();
 				const label = 'foo bar baz';
-				otherSelection.setFake( true, { label } );
+				otherSelection._setFake( true, { label } );
 				selection._setTo( otherSelection );
 
 				expect( selection.isFake ).to.be.true;
@@ -863,7 +863,7 @@ describe( 'Selection', () => {
 		} );
 	} );
 
-	describe( 'getEditableElement', () => {
+	describe( 'getEditableElement()', () => {
 		it( 'should return null if no ranges in selection', () => {
 			expect( selection.editableElement ).to.be.null;
 		} );
@@ -893,31 +893,31 @@ describe( 'Selection', () => {
 		} );
 	} );
 
-	describe( 'setFake', () => {
+	describe( '_setFake()', () => {
 		it( 'should allow to set selection to fake', () => {
-			selection.setFake( true );
+			selection._setFake( true );
 
 			expect( selection.isFake ).to.be.true;
 		} );
 
 		it( 'should allow to set fake selection label', () => {
 			const label = 'foo bar baz';
-			selection.setFake( true, { label } );
+			selection._setFake( true, { label } );
 
 			expect( selection.fakeSelectionLabel ).to.equal( label );
 		} );
 
 		it( 'should not set label when set to false', () => {
 			const label = 'foo bar baz';
-			selection.setFake( false, { label } );
+			selection._setFake( false, { label } );
 
 			expect( selection.fakeSelectionLabel ).to.equal( '' );
 		} );
 
 		it( 'should reset label when set to false', () => {
 			const label = 'foo bar baz';
-			selection.setFake( true, { label } );
-			selection.setFake( false );
+			selection._setFake( true, { label } );
+			selection._setFake( false );
 
 			expect( selection.fakeSelectionLabel ).to.equal( '' );
 		} );
@@ -930,11 +930,11 @@ describe( 'Selection', () => {
 				done();
 			} );
 
-			selection.setFake( true, { label: 'foo bar baz' } );
+			selection._setFake( true, { label: 'foo bar baz' } );
 		} );
 	} );
 
-	describe( 'getSelectedElement', () => {
+	describe( 'getSelectedElement()', () => {
 		it( 'should return selected element', () => {
 			const { selection, view } = parse( 'foo [<b>bar</b>] baz' );
 			const b = view.getChild( 1 );