Reputation: 145
I am using the CKEDITOR styles system -- I'd like to create a style that assigns a unique attribute.
I have a simple plugin that calls a style I create:
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 ) );
}
});
But the datetime and randomnumber are only generated once. How can I get the attributes to be calculated each time the command is executed?
Upvotes: 2
Views: 746
Reputation: 15895
Try the following code (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 );
}
}
} );
Some must-read info about Advanced Content Filter + editor.applyStyle. Also consider defining requiredContent
for your content if it may be used within instances of restricted content types.
Upvotes: 3