CKEditor 4 custom dropdown
Asked Answered
I

1

6

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.

Intemerate answered 15/4, 2015 at 8:53 Comment(0)
F
15

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' );
                }
            } );        
        }        
    }
} );
Fermin answered 15/4, 2015 at 10:15 Comment(2)
Thank you Oleq, used many of your solutions from stackoverflow while working with ckeditor. Thank you!Microphysics
Thank you for this answer. I have been looking around for ages for this solution. I am wondering however, when you click the "My Dropdown"-button in your fiddle, the whole content inside the panel is being focused (you see a blue line at the bottom of the opened panel). How do you make the first item focused?Italian

© 2022 - 2024 — McMap. All rights reserved.