John Smith
John Smith

Reputation: 851

CKEditor enabling/disabling buttons via js

I'm trying to disable some buttons from CKEditor's toolbar, here is my code

CKEDITOR.instances.MY_INSTANCE.commands.bold.disable();

it disables BOLD button on page load, but when I click inside CKEditor's editor area, BOLD button is activated. Also, I've tried CKEDITOR.instances.MY_INSTANCE.commands.bold.setState(CKEDITOR.TRISTATE_DISABLED);

but result is the same. Has anyone faced the same issue?

ps. and how to correctly disable text transformation buttons(f.e. - capitilize), because it needs several parameters

Upvotes: 0

Views: 1248

Answers (1)

Piotr Jasiun
Piotr Jasiun

Reputation: 1006

It is tricky, because at the moment there is no single entry point where all buttons states should be set. Now every plugin can set state of the button what will change your state. But you can listen on state event, which will be fired every time state is changed:

var boldCommand = editor.getCommand( 'bold' );

boldCommand.on( 'state', function( evt ) {
    if( something ) {
        boldCommand.setState( CKEDITOR.TRISTATE_DISABLED );
    }
} );

The problem is when you want to re-enable button state, because it may be difficult to guest what state you should set and there is no simple solution for this problem. You can listen on every event bold command can change its state (selectionChange, mode, readOnly...), save the state of the button and use that state when you re-enable it. There is a ticket to make it simpler), but it is a pretty big task.

Upvotes: 2

Related Questions