CKEDITOR风格,带有独特的id

时间:2013-08-19 19:09:42

标签: javascript ckeditor

我正在使用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只生成一次。如何在每次执行命令时获取要计算的属性?

1 个答案:

答案 0 :(得分:4)

尝试以下代码(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 );
        }
    }
} );

部分must-read info about Advanced Content Filter + editor.applyStyle。如果您的内容可能在受限内容类型的实例中使用,请考虑为您的内容定义requiredContent