如何覆盖ckeditor中按钮的处理程序?

时间:2010-07-28 06:29:01

标签: save override ckeditor

我想为保存按钮设置一个自定义处理程序。

如何覆盖默认命令?

5 个答案:

答案 0 :(得分:10)

current top answer搞砸了我的工具栏分组(将保存按钮放在最后),而other answer在ckeditor v4中无效。

以下是如何在ckeditor 4中执行此操作:

HTML:

<textarea id="CKEditor1"></textarea>

的javascript:

<script>
    // Need to wait for the ckeditor instance to finish initialization
    // because CKEDITOR.instances.editor.commands is an empty object
    // if you try to use it immediately after CKEDITOR.replace('editor');
    CKEDITOR.on('instanceReady', function (ev) {

        // Create a new command with the desired exec function
        var editor = ev.editor;
        var overridecmd = new CKEDITOR.command(editor, {
            exec: function(editor){
                // Replace this with your desired save button code
                alert(editor.document.getBody().getHtml());
            }
        });

        // Replace the old save's exec function with the new one
        ev.editor.commands.save.exec = overridecmd.exec;
    });

    CKEDITOR.replace('CKEditor1');

</script>

答案 1 :(得分:4)

CKEDITOR.plugins.registered['save']=
    {
     init : function( editor )
     {
        var command = editor.addCommand( 'save',
           {
              modes : { wysiwyg:1, source:1 },
              exec : function( editor ) {
                 //YOUR CODE
              }
           }
        );
        editor.ui.addButton( 'Save',{label : 'YOUR LABEL',command : 'save'});
     }
    }

答案 2 :(得分:2)

如果只想覆盖一个实例的 save 命令,可以尝试以下代码:

var editor = $('#myTextarea').ckeditorGet(); // Retrieving CKeditor instance with jQuery
editor.getCommand('save').exec = function(editor) { 
    // Do whatever you need to
    ...
    return true;
};

这适用于任何CKEditor命令。

答案 3 :(得分:0)

function configureEditor(id) {
    var editor = CKEDITOR.replace(id);
    editor.on("instanceReady", function () {
        // overwrite the default save function
        editor.addCommand("save", {
            modes: { wysiwyg: 1, source: 1 },
            exec: function () {
                // get the editor content
                var theData = editor.getData();
                alert("insert your code here");
            }
        });
        editor.ui.addButton('Save', { label: 'My Save', command: 'save', enabled: 'true' });
        var saveButton = $('#cke_' + id).find('.cke_button__save');
        saveButton.removeClass('cke_button_disabled');
    });
}

答案 4 :(得分:0)

在CKEditor 4中,保存插件可以取消。如果不确定,可以随时查看source。您可以取消事件并在处理程序中应用您自己的逻辑,如下例所示:

//assuming editor is a CKEDITOR.editor instance
editor.on('save', function (event) {
    event.cancel();
    //your custom command logic
    //(you can access the editor instance through event.editor)
});

我建议不要创建新命令并用它替换默认命令,因为这是一个不必要的解决方法。