How to add a custom button to the toolbar that calls a JavaScript function?

后端 未结 10 1857
[愿得一人]
[愿得一人] 2020-11-30 17:15

I\'d like to add a button to the toolbar that calls a JavaScript function like Tada()?

Any ideas on how to add this?

相关标签:
10条回答
  • 2020-11-30 17:31

    In case anybody is interested, I wrote a solution for this using Prototype. In order to get the button to appear correctly, I had to specify extraPlugins: 'ajaxsave' from inside the CKEDITOR.replace() method call.

    Here is the 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'
        });
        }
    });
    
    0 讨论(0)
  • 2020-11-30 17:33

    You can add the button image as follows:

    CKEDITOR.plugins.add('showtime',   //name of our plugin
    {    
        requires: ['dialog'], //requires a dialog window
        init:function(a) {
      var b="showtime";
      var c=a.addCommand(b,new CKEDITOR.dialogCommand(b));
      c.modes={wysiwyg:1,source:1}; //Enable our plugin in both modes
      c.canUndo=true;
     
      //add new button to the editor
      a.ui.addButton("showtime",
      {
       label:'Show current time',
       command:b,
       icon:this.path+"showtime.png"   //path of the icon
      });
      CKEDITOR.dialog.add(b,this.path+"dialogs/ab.js") //path of our dialog file
     }
    });
    

    Here is the actual plugin with all steps described.

    0 讨论(0)
  • 2020-11-30 17:35

    I am in the process of developing a number of custom Plugins for CKEditor and here's my survival pack of bookmarks:

    • A StackOverflow post linking to and talking about a plugins tutorial that got me started (Tim Down already mentioned this)
    • A number of ready-made plugins for FCK and CKEditor that may improve one's understanding of the system
    • The Blog of AlfonsoML, one of the developers, lots of valuable stuff there, e.g. Plugin localization in CKEditor
    • Interaction between dialog buttons and a IFrame dialog, with input from Garry Yao, one of the developers
    • The forums are not as bad as they look, there are some hidden gems there. Make sure you search there, well, if not first, then at least second or third.

    For your purpose, I would recommend look at one of the plugins in the _source/plugins directory, for example the "print" button. Adding a simple Javascript function is quite easy to achieve. You should be able to duplicate the print plugin (take the directory from _source into the actual plugins/ directory, worry about minification later), rename it, rename every mention of "print" within it, give the button a proper name you use later in your toolbar setup to include the button, and add your function.

    0 讨论(0)
  • 2020-11-30 17:40

    Also there is a nice way allowing one to add the button without creating plugin.

    html:

    <textarea id="container">How are you!</textarea>
    

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

    Check out how it works here: DEMO

    0 讨论(0)
  • 2020-11-30 17:48

    CKEditor 4

    There are handy tutorials in the official CKEditor 4 documentation, that cover writing a plugin that inserts content into the editor, registers a button and shows a dialog window:

    • Creating a CKEditor 4 Plugin in 20 Lines of Code
    • Creating a Simple CKEditor Plugin

    If you read these two, move on to Integrating Plugins with Advanced Content Filter.

    CKEditor 5

    So far there is one introduction article available:

    CKEditor 5 Framework: Quick Start - Creating a simple plugin

    0 讨论(0)
  • 2020-11-30 17:48

    You'll need to create a plug-in. The documentation for CKEditor is very poor for this, especially since I believe it has changed significantly since FCKEditor. I would suggest copying an existing plug-in and studying it. A quick google for "CKEditor plugin" also found this blog post.

    0 讨论(0)
提交回复
热议问题