CKEditor – custom dialog

The CKEditor is a great WYSIWYG and the best thing is that you are able to customize the interface to have buttons on the toolbars, here is a way that I created a custom button with custom image attached to it.

The editor is created with

var editor = CKEDITOR.replace( 'Content');

So from now on in the variable editor is linked to the CKEditor instance, to setup the plugins to load when the CKEditor is created you call the pluginsLoaded function

 editor.on( 'pluginsLoaded', function( ev )
         {
           if ( !CKEDITOR.dialog.exists( 'statisticsDialog' ) )
                 CKEDITOR.dialog.add( 'statisticsDialog','/statistics.js');
           // Register the command used to open the dialog.
           editor.addCommand( 'statisticsAddCmd', new CKEDITOR.dialogCommand('statisticsDialog' ) );
           editor.ui.addButton( 'Statistics',
                    {
                        label : 'Statistic',
                        command : 'statisticsAddCmd',
                        icon : '/statistics_small.png',
                    } );
});

The first part will check to make sure that there is not dialog already created with the same name, and if not then load the javascript file that is the custom built dialog. The second part is adding the command (the button clicked) to be linked to the custom dialog, and the third part is to add the actual button to the user interface (editor.ui). The addButton function links to the command to bring up the custom dialog.

Here is the custom dialog code that will be called when the user interface button is clicked.

CKEDITOR.dialog.add( 'statisticsDialog', function( editor )
{
      // Load data asynchronously.
       return {
               title : 'Pick a statistic',
               minWidth : 400,
               minHeight : 200,
               onOk : insertOntoEditor,
               contents : [
                       {
                               id : 'tab1',
                               label : 'First Tab',
                               title : 'First Tab',
                               elements :[
                                       {  id : 'graphselect',
                                               type : 'select',
                                               items : [ ["option1", "link to image"] ] ,
                               }]
                       }
               ]
       };
} );

The dialog, will load up a screen and have a one drop down box that will allow the user to choose from the options (will you need to insert more options, or dynamic created options if you wanted to) and once the OK button is clicked (onOK) it will call this function below

function insertOntoEditor()
{
       elem = this.getContentElement('tab1','graphselect');
       value = elem.getValue();
       if (value != -1)
       {
               var writer = new CKEDITOR.htmlWriter();
               var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<img src="http://'+document.domain+elem.getValue()+'"/>' );
               fragment.writeHtml( writer )
 
               // need to wrap up the graph image insertion into the ckeditor
               CKEDITOR.instances.Content.insertHtml(writer.getHtml());
       }
}

This will basically insert some code into the CKEditor from the drop down box, the “link to image” you will need to insert a link for the insertion.

3 thoughts on “CKEditor – custom dialog”

Leave a Reply

Your email address will not be published. Required fields are marked *