如何在调用JavaScript函数的工具栏中添加自定义按钮?

AnA*_*ice 70 javascript ckeditor

我想在工具栏中添加一个调用JavaScript函数的按钮Tada()

关于如何添加这个的任何想法?

Mad*_*ash 106

还有一个很好的方法,允许一个人添加按钮而不创建插件.

HTML:

<textarea id="container">How are you!</textarea>
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

editor = CKEDITOR.replace('container'); // bind editor

editor.addCommand("mySimpleCommand", { // create named command
    exec: function(edt) {
        alert(edt.getData());
    }
});

editor.ui.addButton('SuperButton', { // add new button and bind our command
    label: "Click me",
    command: 'mySimpleCommand',
    toolbar: 'insert',
    icon: 'https://avatars1.githubusercontent.com/u/5500999?v=2&s=16'
});
Run Code Online (Sandbox Code Playgroud)

看看它是如何工作的:DEMO

  • 这应该是公认的答案!因为它准确而直接地回答了OP的问题,而不是将他送到<s> hell </ s>手册. (10认同)
  • 如果您打算这样做,并且想要使用配置中的自定义工具栏,除非您将"SuperButton"(在本例中)添加到自定义工具栏,否则它将不起作用.(我生命中有两个小时我永远不会回来) (4认同)
  • @ J.Bruni这并没有改变这样一个事实,即一个被接受的答案会将OP发送给文档和其他问题,而不是直接回答.这不是SE创建的东西.SE/SO不是链接列表,而是质量问题和答案.接受的答案既没有质量,也没有价值.它只是一堆链接,在行间有"帮助自己"的消息. (3认同)
  • @trejder - 我支持这一点,但我坚信这个答案在六年前的2009年,当OP的问题和接受的答案都被创建时,不适用于... (2认同)

Pek*_*ica 91

我正在为CKEditor开发一些自定义插件,这是我的书签生存包:

为了您的目的,我建议您查看目录中的一个插件_source/plugins,例如"打印"按钮.添加一个简单的Javascript函数很容易实现.您应该能够复制打印插件(将目录从_source转移到实际的插件/目录,稍后担心缩小),重命名,重命名其中的每个"打印",给按钮一个你以后使用的正确名称在工具栏设置中包含按钮,并添加您的功能.


Dav*_*veo 28

有关简单示例,请参阅此URL http://ajithmanmadhan.wordpress.com/2009/12/16/customizing-ckeditor-and-adding-a-new-toolbar-button/

有几个步骤:

1)创建一个插件文件夹

2)注册你的插件(上面的URL说是编辑ckeditor.js文件不要这样做,因为它会在下次重新启动新版本时中断.而是编辑config.js并添加一行如此

config.extraPlugins = 'pluginX,pluginY,yourPluginNameHere'; 
Run Code Online (Sandbox Code Playgroud)

3)在你的插件文件夹中创建一个名为plugin.js的JS文件这是我的代码

(function() {
    //Section 1 : Code to execute when the toolbar button is pressed
    var a = {
        exec: function(editor) {
            var theSelectedText = editor.getSelection().getNative();
            alert(theSelectedText);
        }
    },

    //Section 2 : Create the button and add the functionality to it
    b='addTags';
    CKEDITOR.plugins.add(b, {
        init: function(editor) {
            editor.addCommand(b, a);
            editor.ui.addButton("addTags", {
                label: 'Add Tag', 
                icon: this.path+"addTag.gif",
                command: b
            });
        }
    }); 
})();
Run Code Online (Sandbox Code Playgroud)

  • 似乎插件目录必须与命令命名相同. (3认同)

小智 5

如果有人感兴趣,我使用Prototype为此编写了一个解决方案.为了使按钮正确显示,我必须extraPlugins: 'ajaxsave'CKEDITOR.replace()方法调用内部指定.

这是plugin.js:

CKEDITOR.plugins.add('ajaxsave',
{
    init: function(editor)
    {
    var pluginName = 'ajaxsave';

    editor.addCommand( pluginName,
    {
        exec : function( editor )
        {
            new Ajax.Request('ajaxsave.php',
            {
                method:     "POST",
                parameters: { filename: 'index.html', editor: editor.getData() },
                onFailure:  function() { ThrowError("Error: The server has returned an unknown error"); },
                on0:        function() { ThrowError('Error: The server is not responding. Please try again.'); },
                onSuccess:  function(transport) {

                    var resp = transport.responseText;

                    //Successful processing by ckprocess.php should return simply 'OK'. 
                    if(resp == "OK") {
                        //This is a custom function I wrote to display messages. Nicer than alert() 
                        ShowPageMessage('Changes have been saved successfully!');
                    } else {
                        ShowPageMessage(resp,'10');
                    }
                }
            });
        },

        canUndo : true
    });

    editor.ui.addButton('ajaxsave',
    {
        label: 'Save',
        command: pluginName,
        className : 'cke_button_save'
    });
    }
});
Run Code Online (Sandbox Code Playgroud)


Wik*_*alc 5

CK编辑器4

官方 CKEditor 4 文档中有一些方便的教程,其中介绍了编写一个将内容插入编辑器、注册按钮并显示对话框窗口的插件:

如果您阅读了这两篇文章,请继续阅读将插件与高级内容过滤器集成

CK编辑器5

到目前为止,有一篇介绍文章可供参考:

CKEditor 5 框架:快速入门 - 创建一个简单的插件