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

Nat*_*man 5 overriding save ckeditor

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

如何覆盖默认命令?

Mat*_*hew 10

目前的顶级答案搞砸工具栏,我的分组(放在句末保存按钮),而对方的回答并没有CKEditor的V4工作.

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

HTML:

<textarea id="CKEditor1"></textarea>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


小智 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'});
     }
    }
Run Code Online (Sandbox Code Playgroud)