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

Commit

Permalink
Renamed HeadingCommand's private method _updateValue to public refres…
Browse files Browse the repository at this point in the history
…hValue. Removed schema.objects checking from _checkEnabled method.
  • Loading branch information
szymonkups committed Mar 28, 2017
1 parent c65dd00 commit 3799444
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
15 changes: 3 additions & 12 deletions src/headingcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class HeadingCommand extends Command {

// Update current value each time changes are done on document.
this.listenTo( editor.document, 'changesDone', () => {
this._updateValue();
this.refreshValue();
this.refreshState();
} );

Expand Down Expand Up @@ -112,10 +112,8 @@ export default class HeadingCommand extends Command {

/**
* Updates command's {@link #value value} based on current selection.
*
* @private
*/
_updateValue() {
refreshValue() {
const block = first( this.editor.document.selection.getSelectedBlocks() );

this.value = !!block && block.is( this.modelElement );
Expand All @@ -127,17 +125,10 @@ export default class HeadingCommand extends Command {
_checkEnabled() {
const block = first( this.editor.document.selection.getSelectedBlocks() );

if ( !block ) {
return false;
}

const schema = this.editor.document.schema;
const isAllowed = schema.check( {
return !!block && this.editor.document.schema.check( {
name: this.modelElement,
inside: Position.createBefore( block )
} );

return isAllowed && !schema.objects.has( block.name );
}
}

Expand Down
28 changes: 15 additions & 13 deletions tests/headingcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ describe( 'HeadingCommand', () => {
schema.allow( { name: 'notBlock', inside: '$root' } );
schema.allow( { name: '$text', inside: 'notBlock' } );

schema.registerItem( 'object' );
schema.allow( { name: 'object', inside: '$root' } );
schema.allow( { name: '$text', inside: 'object' } );
schema.objects.add( 'object' );

root = document.getRoot();
} );
} );
Expand Down Expand Up @@ -96,6 +91,19 @@ describe( 'HeadingCommand', () => {

expect( commands[ modelElement ].value ).to.be.false;
} );

it( 'should be refreshed after calling refreshValue()', () => {
const command = commands[ modelElement ];
setData( document, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
const element = document.getRoot().getChild( 1 );

// Purposely not putting it in `document.enqueueChanges` to update command manually.
document.selection.setRanges( [ Range.createIn( element ) ] );

expect( command.value ).to.be.true;
command.refreshValue();
expect( command.value ).to.be.false;
} );
}
} );

Expand Down Expand Up @@ -243,14 +251,8 @@ describe( 'HeadingCommand', () => {
expect( command.isEnabled ).to.be.false;
} );

it( 'should be disabled if inside object', () => {
setData( document, '<object>f{}oo</object>' );

expect( command.isEnabled ).to.be.false;
} );

it( 'should be disabled if selection is placed on object', () => {
setData( document, '[<object>foo</object>]' );
it( 'should be disabled if selection is placed on non-block', () => {
setData( document, '[<notBlock>foo</notBlock>]' );

expect( command.isEnabled ).to.be.false;
} );
Expand Down

0 comments on commit 3799444

Please sign in to comment.