检查ckeditor 4中单击了哪个按钮

时间:2017-06-23 10:07:57

标签: javascript ckeditor

我在画布上添加了两个按钮

Button1的

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

将Button2

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

这是命令定义

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

该按钮都将调用相同的命令。在命令定义中是否有任何方法可以使用哪个按钮来命令命令?

1 个答案:

答案 0 :(得分:1)

在查看了addButtonaddCommand的文档之后,我不认为有一种方法可以确定exec回调中的调用按钮。 command definition有一个可选的data参数,但from the source我无法看到这是由按钮的点击功能提供的。

但当然,您可以为不同的按钮使用不同的command

请参阅此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');
    } 
});

对于可以使用的不同按钮使用相同的功能:

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