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

发布于 2024-09-11 16:48:51 字数 59 浏览 0 评论 0原文

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

如何覆盖默认命令?

I would like to have a custom handler for the Save button.

How can I override the default command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

客…行舟 2024-09-18 16:48:51

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

The current top answer messed up the toolbar grouping for me (put the save button at the end), and the other answer did not work in CKEditor v4.

Here's how to do it in 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>
迷迭香的记忆 2024-09-18 16:48:51
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'});
     }
    }
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'});
     }
    }
七婞 2024-09-18 16:48:51

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

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

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

If you want to override the save command for just one instance, you can try the following code:

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

This should work for any CKEditor command.

眉目亦如画i 2024-09-18 16:48:51
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');
    });
}
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');
    });
}
西瑶 2024-09-18 16:48:51

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

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

In CKEditor 4, the save plugin is meant to be cancelable. If unsure, one can always have a look at the source. You can cancel the event and apply your own logic in a handler, like in this example:

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

I would advise against creating a new command and replacing the default with it, as it is an unnecessary workaround.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文