Ver código fonte

Introduced `Writer#restoreSelectionGravity()` method.

Oskar Wróbel 7 anos atrás
pai
commit
8800ddb531

+ 21 - 1
packages/ckeditor5-engine/src/model/documentselection.js

@@ -139,6 +139,10 @@ export default class DocumentSelection {
 		return this._selection.isBackward;
 	}
 
+	get isGravityOverridden() {
+		return this._selection._isGravityOverriden;
+	}
+
 	/**
 	 * Used for the compatibility with the {@link module:engine/model/selection~Selection#isEqual} method.
 	 *
@@ -391,12 +395,23 @@ export default class DocumentSelection {
 	/**
 	 * Temporarily and partially disables default gravity behaviour that tries to get attributes from nodes surrounding the caret.
 	 * @see module:engine/model/writer~Writer#overrideGravity
+	 *
+	 * @protected
 	 */
 	_overrideGravity() {
 		this._selection.overrideGravity();
 	}
 
 	/**
+	 * Restore overridden gravity.
+	 *
+	 * @protected
+	 */
+	_restoreGravity() {
+		this._selection.restoreGravity();
+	}
+
+	/**
 	 * Generates and returns an attribute key for selection attributes store, basing on original attribute key.
 	 *
 	 * @protected
@@ -476,7 +491,7 @@ class LiveSelection extends Selection {
 		// When is set as `true` then selection attributes on node before the caret won't be taken
 		// into consideration while updating selection attributes.
 		//
-		// @private
+		// @protected
 		// @type {Boolean}
 		this._isGravityOverriden = false;
 
@@ -629,6 +644,11 @@ class LiveSelection extends Selection {
 		this._updateAttributes();
 	}
 
+	restoreGravity() {
+		this._isGravityOverriden = false;
+		this._updateAttributes();
+	}
+
 	// Removes all attributes from the selection and sets attributes according to the surrounding nodes.
 	_refreshAttributes() {
 		this._updateAttributes( true );

+ 7 - 0
packages/ckeditor5-engine/src/model/writer.js

@@ -1033,6 +1033,13 @@ export default class Writer {
 	}
 
 	/**
+	 * Restore overridden gravity to default.
+	 */
+	restoreSelectionGravity() {
+		this.model.document.selection._restoreGravity();
+	}
+
+	/**
 	 * @private
 	 * @param {String} key Key of the attribute to remove.
 	 * @param {*} value Attribute value.

+ 30 - 0
packages/ckeditor5-engine/tests/model/documentselection.js

@@ -806,6 +806,36 @@ describe( 'DocumentSelection', () => {
 		} );
 	} );
 
+	describe( '_restoreGravity()', () => {
+		beforeEach( () => {
+			model.schema.extend( '$text', {
+				allowIn: '$root'
+			} );
+		} );
+
+		it( 'should not revert default gravity when is overridden', () => {
+			setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
+
+			selection._overrideGravity();
+
+			expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
+
+			selection._restoreGravity();
+
+			expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
+		} );
+
+		it( 'should do nothing when gravity is not overridden', () => {
+			setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
+
+			expect( () => {
+				selection._restoreGravity();
+			} ).to.not.throw();
+
+			expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
+		} );
+	} );
+
 	// DocumentSelection uses LiveRanges so here are only simple test to see if integration is
 	// working well, without getting into complicated corner cases.
 	describe( 'after applying an operation should get updated and fire events', () => {

+ 36 - 0
packages/ckeditor5-engine/tests/model/writer.js

@@ -2380,6 +2380,36 @@ describe( 'Writer', () => {
 		} );
 	} );
 
+	describe( 'restoreSelectionGravity()', () => {
+		it( 'should use DocumentSelection#_restoreGravity', () => {
+			const restoreGravitySpy = sinon.spy( DocumentSelection.prototype, '_restoreGravity' );
+
+			restoreSelectionGravity();
+
+			sinon.assert.calledOnce( restoreGravitySpy );
+			restoreGravitySpy.restore();
+		} );
+
+		it( 'should restore overridden gravity to default', () => {
+			const root = doc.createRoot();
+			root.appendChildren( [
+				new Text( 'foo', { foo: true } ),
+				new Text( 'bar', { foo: true, bar: true } ),
+				new Text( 'biz', { foo: true } )
+			] );
+
+			setSelection( new Position( root, [ 6 ] ) );
+
+			overrideSelectionGravity();
+
+			expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
+
+			restoreSelectionGravity();
+
+			expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
+		} );
+	} );
+
 	function createText( data, attributes ) {
 		return model.change( writer => {
 			return writer.createText( data, attributes );
@@ -2545,4 +2575,10 @@ describe( 'Writer', () => {
 			writer.overrideSelectionGravity();
 		} );
 	}
+
+	function restoreSelectionGravity() {
+		model.change( writer => {
+			writer.restoreSelectionGravity();
+		} );
+	}
 } );