Explorar el Código

Changed the way of restoring gravity.

Oskar Wróbel hace 8 años
padre
commit
8d4fa9e470

+ 6 - 9
packages/ckeditor5-engine/src/model/documentselection.js

@@ -397,11 +397,10 @@ export default class DocumentSelection {
 	 *
 	 * @see module:engine/model/writer~Writer#overrideGravity
 	 * @protected
-	 * @param {Function} [customRestorer] A callback function that allows to control when default gravity should be restored.
-	 * Callback takes function as a param that allow to restore the gravity.
+	 * @param {Boolean} [customRestore=false] When `true` then gravity won't be restored automatically.
 	 */
-	_overrideGravity( customRestorer ) {
-		this._selection.overrideGravity( customRestorer );
+	_overrideGravity( customRestore ) {
+		this._selection.overrideGravity( customRestore );
 	}
 
 	/**
@@ -634,15 +633,13 @@ class LiveSelection extends Selection {
 		}
 	}
 
-	overrideGravity( customRestorer ) {
+	overrideGravity( customRestore ) {
 		this._isGravityOverriden = true;
 
-		if ( typeof customRestorer == 'function' ) {
-			customRestorer( this.restoreGravity.bind( this ) );
-		} else {
+		if ( !customRestore ) {
 			this.on( 'change:range', ( evt, data ) => {
 				if ( data.directChange ) {
-					this._isGravityOverriden = false;
+					this.restoreGravity();
 					evt.off();
 				}
 			} );

+ 9 - 13
packages/ckeditor5-engine/src/model/writer.js

@@ -1017,9 +1017,8 @@ export default class Writer {
 	}
 
 	/**
-	 * Temporarily (until selection won't be changed directly by the user or using `customRestorer`) disables default
-	 * gravity behaviour that tries to get attributes from nodes surrounding the caret. When gravity is marked
-	 * as overridden then attributes from the node before the caret won't be taken into consideration while
+	 * Disables default gravity behaviour that tries to get attributes from nodes surrounding the caret. When gravity is
+	 * marked as overridden then attributes from the node before the caret won't be taken into consideration while
 	 * updating selection attributes.
 	 *
 	 * For the following model fragment:
@@ -1029,18 +1028,15 @@ export default class Writer {
 	 * Selection attribute keys before override will be equal `[ 'bold', 'linkHref' ]`
 	 * Selection attribute keys after override will be equal `[ 'bold' ]`
 	 *
-	 * As default gravity is restored just after a direct {@link module:model/documentselection~DocumentSelection#change:range} event
-	 * but it could be customised using `customRestorer` callback:
+	 * As default gravity is automatically restored just after a direct
+	 * {@link module:model/documentselection~DocumentSelection#change:range} event but this behaviour can be disabled
+	 * by passing `true` flag as param.
 	 *
-	 * 		model.change( writer => {
-	 * 			writer.overrideSelectionGravity( restore => // and gravity won't be restored until `restore` callback won't be called ).
-	 * 		} );
-	 *
-	 * @param {Function} [customRestorer] A callback function that allows to control when default gravity should be restored.
-	 * Callback takes function as a param that allow to restore the gravity.
+	 * @param {Boolean} [customRestore=false] When `true` then gravity won't be restored until
+	 * {@link ~Writer#overrideSelectionGravity} will be called directly.
 	 */
-	overrideSelectionGravity( customRestorer ) {
-		this.model.document.selection._overrideGravity( customRestorer );
+	overrideSelectionGravity( customRestore ) {
+		this.model.document.selection._overrideGravity( customRestore );
 	}
 
 	/**

+ 10 - 10
packages/ckeditor5-engine/src/utils/bindtwostepcarettoattribute.js

@@ -103,17 +103,17 @@ export default function bindTwoStepCaretToAttribute( editor, emitter, attribute
 			// event following the overriding will restore the gravity.
 			if ( isStickToAttribute( nextPosition.nodeBefore, nextPosition.nodeAfter, attribute ) ) {
 				model.change( writer => {
+					let counter = 0;
+
 					// So let's override the gravity.
-					writer.overrideSelectionGravity( restore => {
-						// But skip the following `change:range` event and restore the gravity on the next one.
-						let counter = 0;
-
-						emitter.listenTo( modelSelection, 'change:range', ( evt, data ) => {
-							if ( counter++ && data.directChange ) {
-								restore();
-								evt.off();
-							}
-						} );
+					writer.overrideSelectionGravity( true );
+
+					// But skip the following `change:range` event and restore the gravity on the next one.
+					emitter.listenTo( modelSelection, 'change:range', ( evt, data ) => {
+						if ( counter++ && data.directChange ) {
+							writer.restoreSelectionGravity();
+							evt.off();
+						}
 					} );
 				} );
 			}

+ 8 - 15
packages/ckeditor5-engine/tests/model/writer.js

@@ -2381,28 +2381,21 @@ describe( 'Writer', () => {
 			expect( model.document.selection.isGravityOverridden ).to.false;
 		} );
 
-		it( 'should allow to use custom restorer callback', () => {
+		it( 'should allow to restorer gravity in a custom way', () => {
 			const root = doc.createRoot();
 			root.appendChildren( [ new Text( 'foobar', { foo: true } ) ] );
 
 			setSelection( new Position( root, [ 1 ] ) );
 
-			overrideSelectionGravity( restore => {
-				let i = 0;
+			overrideSelectionGravity( true );
 
-				model.document.selection.on( 'change:range', () => {
-					if ( i++ > 0 ) {
-						restore();
-					}
-				} );
-			} );
-
-			// Moving selection for the first time does not restore.
+			// Moving selection does not restore gravity.
 			setSelection( new Position( root, [ 2 ] ) );
 			expect( model.document.selection.isGravityOverridden ).to.true;
 
-			// Second move does.
-			setSelection( new Position( root, [ 1 ] ) );
+			// We need to do it manually.
+			restoreSelectionGravity();
+
 			expect( model.document.selection.isGravityOverridden ).to.false;
 		} );
 	} );
@@ -2597,9 +2590,9 @@ describe( 'Writer', () => {
 		} );
 	}
 
-	function overrideSelectionGravity( customRestorer ) {
+	function overrideSelectionGravity( customRestore ) {
 		model.change( writer => {
-			writer.overrideSelectionGravity( customRestorer );
+			writer.overrideSelectionGravity( customRestore );
 		} );
 	}