Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown toolbar button for CKEditor 4

Is it possible to create a drop down style menu consisting of toolbar buttons?

I want to have a button on the toolbar which groups the alignment buttons (and possibly others) into a drop down menu.

Thanks

like image 713
Michael Bates Avatar asked Mar 25 '14 08:03

Michael Bates


1 Answers

The problem is not that hard but still you got to write a couple lines of code. The following logic inside pluginsLoaded can (should) be defined in init of a totally new plugin (which could be called "groupped-justify"). Otherwise, if executed too late, e.g. after toolbar was generated, the whole code makes no sense.

See the official plugin development guide to know more.

Also see the jsFiddle with a working example.

CKEDITOR.replace( 'editor', {
    plugins: 'wysiwygarea,sourcearea,basicstyles,toolbar,menu,menubutton,justify',
    on: {
        pluginsLoaded: function() {
            var editor = this,
                items = {};

            editor.addMenuGroup( 'some_group' );

            items.justifyleft = {
                label: editor.lang.justify.left,
                group: 'some_group',
                command: 'justifyleft',
                order: 1
            };

            items.justifyright = {
                label: editor.lang.justify.right,
                group: 'some_group',
                command: 'justifyright',
                order: 2
            };

            editor.addMenuItems( items );

            editor.ui.add( 'Groupped', CKEDITOR.UI_MENUBUTTON, {
                label: 'Groupped justify',
                // Disable in source mode.
                modes: { 
                    wysiwyg: 1 
                },
                icon: 'JustifyLeft',
                onMenu: function() {
                    var active = {};

                    // Make all items active.
                    for ( var p in items )
                        active[ p ] = CKEDITOR.TRISTATE_OFF;

                    return active;
                }
            } );                       
        }
    }
} );
like image 103
oleq Avatar answered Sep 21 '22 00:09

oleq