CKeditor自定义数据插入

时间:2014-07-08 09:20:41

标签: javascript ckeditor cleditor

对于我目前的项目,我使用了cleditor,但想要更改为ckeditor。我的问题是我需要一些自定义下拉列表,我不知道如何在ckeditor中实现。让我举个例子:

图片1:

enter image description here 图2:

enter image description here

所以基本上我需要一个包含一些图像的下拉菜单,当我点击某个图像时,它会将其插入到编辑器textarea中。

1 个答案:

答案 0 :(得分:3)

这很简单。请查看此JSFiddle并使用以下代码:

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

            // Helper function. Coverts color name into colorful <span>.
            function colorfulSpan( color ) {
                return '<span style="color:' + color + '">' + color + '</span>';
            }

            // AllowedContent rule for the contents inserted by the combo.
            // As this sample combo inserts <span> of certain style only, it's quite short.
            // See: http://docs.ckeditor.com/#!/guide/dev_advanced_content_filter
            var acfRules = 'span{color}';

            // Let the party on!            
            editor.ui.addRichCombo( 'myCombo', {
                label: 'My combo\'s label',
                title: 'My combo',
                toolbar: 'styles',

                // Registers a certain type of contents in the editor.
                allowedContent: acfRules,

                // The minimal content that must be allowed in the editor for this combo to work.
                requiredContent: acfRules,

                // Combo iherits from the panel. Set up it first so all styles
                // of the contents are available in the panel.
                panel: {
                    css: [ CKEDITOR.skin.getPath( 'editor' ) ].concat( config.contentsCss ),
                    multiSelect: false
                },

                // Let's populate the list of available items.
                init: function() {
                    this.startGroup( 'My custom panel group' );

                    var items = [ 'red', 'orange', 'blue', 'green' ];

                    for ( var i = 0; i < items.length; i++ ) {
                        var item = items[ i ];

                        // Add entry to the panel.
                        this.add( item, colorfulSpan( item ), item );
                    }
                },

                // This is what happens when the item is clicked.
                onClick: function( value ) {
                    editor.focus();

                    // Register undo snapshot.
                    editor.fire( 'saveSnapshot' );

                    // Insert a colorful <span>.
                    editor.insertHtml( colorfulSpan( value ) );                    

                    // Register another undo snapshot. The insertion becomes undoable.
                    editor.fire( 'saveSnapshot' );
                },

                // The value of the combo may need to be updated, i.e. according to the selection.
                // This is where such listener is supposed to be created.
                onRender: function() {
                    //editor.on( 'selectionChange', function( ev ) {
                    //    var currentValue = this.getValue();
                    //    ...
                    //    this.setValue( value );
                    //}, this );
                },

                // The contents of the combo may change, i.e. according to the selection.
                // This is where it supposed to be updated.
                onOpen: function() {
                },

                // Disable the combo if the current editor's filter does not allow
                // the type of contents that the combo delivers (i.e. widget's nested editable).
                // See: http://docs.ckeditor.com/#!/guide/dev_widgets.
                refresh: function() {
                    if ( !editor.activeFilter.check( acfRules ) )
                        this.setState( CKEDITOR.TRISTATE_DISABLED );
                }
            } );            
        }
    }
} );