2017-06-23 39 views
0

我添加使用檢查哪個按鈕被點擊了CKEditor的4

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 
    } 
}); 

這兩個按鈕都會調用相同的命令。在命令定義中有什麼方法可以調用哪個按鈕?

+0

你不是有一個'在我們的函數調用event'參數? –

+0

爲什麼你不使用不同的'命令'爲不同的按鈕? – gus27

+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'); 
    } 
}); 
+0

對不起,最初錯誤的小提琴 - 現在糾正。 – gus27

+0

謝謝!!但沒有任何方法可以確定這一點? –

+0

@SunilGarg爲了確定什麼? – gus27