Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce command#disable() and command#enable() #2903

Closed
scofalik opened this issue Mar 8, 2019 · 2 comments · Fixed by ckeditor/ckeditor5-core#167 or ckeditor/ckeditor5-core#166
Closed
Assignees
Labels
package:core type:improvement This issue reports a possible enhancement of an existing feature.
Milestone

Comments

@scofalik
Copy link
Contributor

scofalik commented Mar 8, 2019

Current status on March 6, 2023

At the time of writing, Command#disable() and Command#enable() got replaced by Command#forceDisabled() and
Command#clearForceDisabled() and those are the correct way to disable or enable the command in the CKEditor 5.

Usage example:

command.isEnabled; // -> true
command.forceDisabled( 'MyFeature' );
command.isEnabled; // -> false
command.clearForceDisabled( 'MyFeature' );
command.isEnabled; // -> true

More details and examples are available in API docs:

Original issue

More and more features are using "disable all/most commands" functionality (mostly in collaboration features though). It leads to duplicating a lot of code. Additionally, it is not really intuitive to disable a command through listening to isEnabled.

Hence, I propose to add this functionality to Command API. Of course, it will need to handle multiple disabling, so one algorithm will not re-enable lock set in another algorithm. This can be simply handled through passing identifiers.

command.disable( 'myFeature' );
// ...
command.enable( 'myFeature' );

I think this is a better solution that returning enabling callback:

const enable = command.disable();
// ...
enable();

Because of how we use disabling:

// Disable all commands.
for ( const command of this.editor.commands.commands() ) {
	// Will become `command.disable( 'id' );`.
	command.on( 'set:isEnabled', forceDisable, { priority: 'highest' } );
	command.isEnabled = false;
}
// ...
for ( const command of this.editor.commands.commands() ) {
	// Will become `command.enable( 'id' );`.
	command.off( 'set:isEnabled', forceDisable );
	command.refresh();
}

If callback would be returned we would have to store all of them which doesn't make sense.

@scofalik scofalik self-assigned this Mar 8, 2019
@scofalik
Copy link
Contributor Author

scofalik commented Mar 8, 2019

When this ticket is done, we should use this new API in places where we already use commands disabling.

pjasiun referenced this issue in ckeditor/ckeditor5-core Mar 12, 2019
Feature: Introduced `Command#disable()` and `Command#enable()`. Closes #165.
@Reinmar Reinmar reopened this Mar 12, 2019
@Reinmar
Copy link
Member

Reinmar commented Mar 12, 2019

Please correct the docs:

  1. An example would be great
  2. Let's not talk about "disable stack" cause neither is this precise, nor IMO helps understanding how it works. I'd focus on explaining it shortly that a command can be disabled by multiple features (which should pass a unique id to the disable() method – e.g. the plugin name) and it will only be enabled back if all the features which disabled it enable it via enable(). Just that. Plus an example like this:
command.isEnabled; // -> true
command.disable( 'MyFeature' );
command.isEnabled; // -> false

command.enable( 'MyFeature' );
command.isEnabled; // -> true

Command disabled by multiple features:

command.disable( 'MyFeature' );
command.disable( 'OtherFeature' );

command.enable( 'MyFeature' );
command.isEnabled; // -> false

command.enable( 'OtherFeature' );
command.isEnabled; // -> true

Note: After a command is enabled, its refresh() logic decides whether isEnabled will be true. So, it may happen that the command will still be disabled.

BTW, the last sentence shows that disable()/enable() may not be the best naming.

pjasiun referenced this issue in ckeditor/ckeditor5-core Mar 12, 2019
Internal: Reworded docs and renamed `Command#disable()` and `Command#enable()` to `Command#forceDisabled()` and `Command#clearForceDisabled()`. Closes #165.
@mlewand mlewand transferred this issue from ckeditor/ckeditor5-core Oct 9, 2019
@mlewand mlewand added this to the iteration 23 milestone Oct 9, 2019
@mlewand mlewand added type:improvement This issue reports a possible enhancement of an existing feature. package:core labels Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:core type:improvement This issue reports a possible enhancement of an existing feature.
Projects
None yet
3 participants