Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Introduced Writer#restoreSelectionGravity() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Feb 15, 2018
1 parent ca47fdb commit 5532102
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -391,11 +395,22 @@ 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.
*
Expand Down Expand Up @@ -466,7 +481,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;

Expand Down Expand Up @@ -605,6 +620,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 );
Expand Down
7 changes: 7 additions & 0 deletions src/model/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,13 @@ export default class Writer {
this.model.document.selection._overrideGravity();
}

/**
* Restore overridden gravity to default.
*/
restoreSelectionGravity() {
this.model.document.selection._restoreGravity();
}

/**
* @private
* @param {String} key Key of the attribute to remove.
Expand Down
30 changes: 30 additions & 0 deletions tests/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
36 changes: 36 additions & 0 deletions tests/model/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -2545,4 +2575,10 @@ describe( 'Writer', () => {
writer.overrideSelectionGravity();
} );
}

function restoreSelectionGravity() {
model.change( writer => {
writer.restoreSelectionGravity();
} );
}
} );

0 comments on commit 5532102

Please sign in to comment.