具有唯一id的CKEDITOR样式

CKEDITOR style with unique id

本文关键字:CKEDITOR 样式 id 唯一      更新时间:2023-09-26

我使用的是CKEDITOR样式系统——我想创建一个分配唯一属性的样式。

我有一个简单的插件,它调用我创建的样式:

     editor.addCommand( 'tag', {
            exec: function( editor ) {
               var randnumber = Math.floor((Math.random()*1000000000)+1);
               var mysqldatetime = new Date();
               CKEDITOR.config.tag = { element : 'span', attributes : { 'class': 'tag-'+randnumber, 'data-datetime' : mysqldatetime, 'data-tag': 'tag' } };
               var style = new CKEDITOR.style( editor.config.tag );
               editor.addCommand( 'tag', new CKEDITOR.styleCommand( style ) );
    }
});

但是datetime和randomnumber只生成一次。如何获得每次执行命令时要计算的属性?

尝试以下代码(jsFiddle):

CKEDITOR.replace( 'editor1', {
    plugins: 'wysiwygarea,sourcearea,toolbar,basicstyles,link',
    on: {
        pluginsLoaded: function() {
            var cmd = this.addCommand( 'tag', {
                canUndo: true,
                modes: { wysiwyg:1 },
                // Otherwise the editor will purge your custom stuff.
                allowedContent: 'span(*)[data-datetime,data-tag]{color}',
                exec: function( editor ) {
                    var randnumber = Math.floor( ( Math.random() * 1000000000 ) + 1 ),
                        mysqldatetime = new Date();
                    // Alway apply a different style.
                    editor.applyStyle( new CKEDITOR.style( {
                        element: 'span',
                        attributes: {
                            'class': 'tag-' + randnumber,
                            'data-datetime': mysqldatetime,
                            'data-tag': 'tag'
                        },
                        styles: {
                            color: 'red'
                        }
                    } ) );
                }
            } );
            // This is a custom command, so we need to register it.
            this.addFeature( cmd );
        }
    }
} );

一些人必须阅读有关Advanced Content Filter+editor.applyStyle的信息。如果您的内容可能在受限内容类型的实例中使用,请考虑为其定义requiredContent