Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKEditor 4 custom dropdown

We are trying to add a custom dropdown to CKeditor that will have a list of pre-set values we will find and replace, for the life of me, I cannot find a simple way to do this. Looking at TinyMCE it is very easy:

http://www.tinymce.com/tryit/menubutton.php

Is there a simple solution like this for CKEditor, we would rather not have to swap CKeditor out for TinyMCE just for this feature. I found a plugin called PlaceHolder but that doesn't quite do what we want and to be honest, I was hoping to do this without plugins and just configure on initialization the way TinyMCE does.

Many thanks for any insight.

like image 748
user351711 Avatar asked Apr 15 '15 08:04

user351711


1 Answers

It's all about editor.ui.addRichCombo (JSFiddle):

CKEDITOR.replace( 'editor', {
    toolbarGroups: [
        { name: 'mode' },
        { name: 'basicstyles' }
    ],    
    on: {
        pluginsLoaded: function() {
            var editor = this,
                config = editor.config;

            editor.ui.addRichCombo( 'my-combo', {
                label: 'My Dropdown Label',
                title: 'My Dropdown Title',
                toolbar: 'basicstyles,0',

                panel: {               
                    css: [ CKEDITOR.skin.getPath( 'editor' ) ].concat( config.contentsCss ),
                    multiSelect: false,
                    attributes: { 'aria-label': 'My Dropdown Title' }
                },

                init: function() {    
                    this.startGroup( 'My Dropdown Group #1' );
                    this.add( 'foo', 'Foo!' );
                    this.add( 'bar', 'Bar!' );                    

                    this.startGroup( 'My Dropdown Group #2' );
                    this.add( 'ping', 'Ping!' );
                    this.add( 'pong', 'Pong!' );                    

                },

                onClick: function( value ) {
                    editor.focus();
                    editor.fire( 'saveSnapshot' );

                    editor.insertHtml( value );

                    editor.fire( 'saveSnapshot' );
                }
            } );        
        }        
    }
} );
like image 108
oleq Avatar answered Nov 16 '22 17:11

oleq