The standard center button only applies a in-line style to the HTML. But how can I add a class 'text-center' to a paragraph or selection by clicking the following icon? I went ahead and created a custom button on CkEditor.
(function($) {
CKEDITOR.plugins.add( 'center', {
icons: 'center',
init: function( editor ) {
editor.addCommand( 'center', genCommand());
editor.ui.addButton( 'center', {
label: 'Align text on center',
command: 'center',
toolbar: 'center'
});
if ( editor.contextMenu ) {
editor.addMenuGroup( 'centerGroup' );
editor.addMenuItem( 'centerItem', {
label: 'Change link',
icon: this.path + 'icons/center.png',
command: 'center',
group: 'centerGroup'
});
editor.contextMenu.addListener( function( element ) {
if ( element.getAscendant( 'center', true ) ) {
return { abbrItem: CKEDITOR.TRISTATE_OFF };
}
});
}
CKEDITOR.dialog.add( 'centerDialog', this.path + 'dialogs/center.js' );
}
});
function genCommand( name ) {
return {
exec: function( editor ) {
editor.insertHtml('<p class="text-center>' + editor.getSelection().getNative() + '</p>');
},
};
}
})(jQuery);
What it is doing is nothing. In the case there is a selection, it deletes the selection and markup.
What I need:
- If there is a line saying "text", I need
<p class="text-center">text</p>
- In the case there is a selection, make 1 out of the selection