Code Guru
Code Guru

Reputation: 15598

Check which button was clicked in ckeditor 4

I have added two button on canvas using

Button1

editor.ui.addButton('button1', {
    label : editor.lang.button1,
    command : COMMAND_NAME
});

Button2

editor.ui.addButton('button2', {
    label : editor.lang.button2,
    command : COMMAND_NAME
});

And here is the command definition

editor.addCommand(COMMAND_NAME, { 
    exec: function(editorInstance) {
        // some task
    } 
});

Both the button will call the same command. Is there any way in command definition that which button called the command?

Upvotes: 0

Views: 268

Answers (1)

gus27
gus27

Reputation: 2656

After taking a look at the documentation for addButton and addCommand I don't think that there's a way to determine between the calling buttons in the exec callback. The command definition has an optional data parameter but from the source I can't see that this is provided by the button's click function.

But of course you could use different command's for different buttons.

See this JSFiddle

editor.ui.addButton('button1', {
    label : 'Button1',
    command : 'mycommand1'
});
editor.ui.addButton('button2', {
    label : 'Button2',
    command : 'mycommand2'
});
editor.addCommand('mycommand1', { 
    exec: function(editorInstance) {
        console.log('button1/command1');
    } 
});
editor.addCommand('mycommand2', { 
    exec: function(editorInstance) {
        console.log('button2/command2');
    } 
});

And to use the same function for different buttons you could use:

function mySpecialFunction(button) {
}
...
editor.addCommand('mycommand1', { 
    exec: function(editorInstance) {
        mySpecialFunction('button1');
    } 
});
editor.addCommand('mycommand2', { 
    exec: function(editorInstance) {
        mySpecialFunction('button2');
    } 
});

Upvotes: 1

Related Questions