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

Commit

Permalink
Merge pull request #166 from ckeditor/t/165
Browse files Browse the repository at this point in the history
Feature: Introduced `Command#disable()` and `Command#enable()`. Closes #165.
  • Loading branch information
Piotr Jasiun authored Mar 12, 2019
2 parents 8eadd14 + b16acbe commit 030ca2b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ export default class Command {
*/
this.set( 'isEnabled', false );

/**
* Holds identifiers for {@link #disable}/{@link #enable} mechanism.
*
* @type {Set.<String>}
* @private
*/
this._disableStack = new Set();

this.decorate( 'execute' );

// By default every command is refreshed when changes are applied to the model.
Expand All @@ -112,11 +120,9 @@ export default class Command {
// By default commands are disabled when the editor is in read-only mode.
this.listenTo( editor, 'change:isReadOnly', ( evt, name, value ) => {
if ( value ) {
this.on( 'set:isEnabled', forceDisable, { priority: 'highest' } );
this.isEnabled = false;
this.disable( 'readOnlyMode' );
} else {
this.off( 'set:isEnabled', forceDisable );
this.refresh();
this.enable( 'readOnlyMode' );
}
} );
}
Expand All @@ -132,6 +138,44 @@ export default class Command {
this.isEnabled = true;
}

/**
* Disables the command.
*
* "Disable stack" is supported through identifiers. Command may be disabled by a multiple features/algorithms (at once).
* When disabling a command, identifier is passed (and added to "disable stack"). The identifier is then used when
* {@link #enable enabling back} the command. The command is actually enabled only after all features {@link #enable enabled it back}.
*
* Multiple disabling with the same identifier is redundant.
*
* **Note:** some algorithms may have more complex logic when it comes to enabling or disabling certain commands. Keep in mind
* that disabling command is also possible through listening to {@link #isEnabled} change, so the command might be still disabled
* even though "disable stack" is empty.
*
* @param {String} id "Disable stack" identifier. Use the same identifier when {@link #enable enabling back} the command.
*/
disable( id ) {
this._disableStack.add( id );

if ( this._disableStack.size == 1 ) {
this.on( 'set:isEnabled', forceDisable, { priority: 'highest' } );
this.isEnabled = false;
}
}

/**
* Enables the command previously disabled through {@link #disable}. See {@link #disable}.
*
* @param {String} id "Disable stack" identifier.
*/
enable( id ) {
this._disableStack.delete( id );

if ( this._disableStack.size == 0 ) {
this.off( 'set:isEnabled', forceDisable );
this.refresh();
}
}

/**
* Executes the command.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,49 @@ describe( 'Command', () => {
}
} );
} );

describe( 'disable() / enable()', () => {
it( 'disable() should disable the command', () => {
command.disable( 'foo' );
command.isEnabled = true;

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

it( 'enable() should enable the command', () => {
command.disable( 'foo' );
command.enable( 'foo' );

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

it( 'enable() used with wrong identifier should not enable the command', () => {
command.disable( 'foo' );
command.enable( 'bar' );
command.isEnabled = true;

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

it( 'using disable() twice with the same identifier should not have any effect', () => {
command.disable( 'foo' );
command.disable( 'foo' );
command.enable( 'foo' );

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

it( 'command is enabled only after whole disable stack is empty', () => {
command.disable( 'foo' );
command.disable( 'bar' );
command.enable( 'foo' );
command.isEnabled = true;

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

command.enable( 'bar' );

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

0 comments on commit 030ca2b

Please sign in to comment.