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

问题描述 投票:0回答:5

我想要一个用于保存按钮的自定义处理程序。

如何覆盖默认命令?

save overriding ckeditor
5个回答
11
投票

当前最佳答案弄乱了我的工具栏分组(将保存按钮放在最后),并且其他答案在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>

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
投票

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

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

这应该适用于任何 CKEditor 命令。


1
投票
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');
    });
}

0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.