Replace styles in CKEditor - ckeditor

I'm building out a simple site building tool using CKEditor. The tool has the ability to choose from and set palettes, which should be reflected in the styles drop down of CKEditor. However, it seems to me that styles cannot be overwritten in CKEditor. The code I have at the moment is:
CKEDITOR.stylesSet.add( 'styles', [
// Block-level styles
{ name: 'blah 1', element: 'h2', styles: { 'color': '#xxxxxx' } },
{ name: 'blah 2', element: 'h3', styles: { 'color': '#xxxxxx' } },
{ name: 'blah 3' , element: 'h4', styles: { 'color': '#xxxxxx' } },
{ name: 'blah 4' , element: 'h5', styles: { 'color': '#xxxxxx' } },
] );
CKEDITOR.config.stylesSet = 'styles';
Now, if I repeat this with new styles, I get:
ckeditor.js:232 Uncaught Error: [CKEDITOR.resourceManager.add] The resource name "styles" is already registered.
I've tried using CKEDITOR.replace, but this doesn't fix the issue. I guess, the obvious solution is to iterate the style name with each use; style1, style2, style3... but that's not very resource friendly. Does anyone have an actual solution for this?
Thanks,
Lee

have you tried renaming styles to default?
I use this and it works, mine loads an external style file. But same array structure.
CKEDITOR.config.stylesSet = 'default:http://' + window.location.host + '/folder/fckeditor.styles.js';

So, I figured out a solution by always destroying the panel, if it exists, before re-creating it. For instance:
if (CKEDITOR.instances['footer-' + i]) {
CKEDITOR.instances['footer-' + i].destroy(true);
}
var editor = CKEDITOR.inline('footer-' + i, {
stylesSet: [
// Block-level styles
{ name: 'Blue Title', element: 'h2', styles: { 'color': 'Blue' } },
{ name: 'Red Title' , element: 'h3', styles: { 'color': 'Red' } },
{ name: 'Brown Title' , element: 'h4', styles: { 'color': 'Red' } },
{ name: 'Purple Title' , element: 'h5', styles: { 'color': 'Red' } }
]
});
Now, this does throw up a warning each time, saying:
[CKEDITOR] For more information about this error go to http://docs.ckeditor.com/#!/guide/dev_errors-section-editor-incorrect-destroy
However, theres no clean way to do this otherwise with the CKEditor API, so since it works, I'm marking it as the answer.

Related

CKEditor 5 - Style definition for UL

I have the style plugin installed on ckeditor5 and for elements with one level, like a paragraph, it works great but with something like a list, it doesn't (or doesn't for me!) The reason seems to be that for something like
<ul>
<li>Hello</li>
<li>World</li>
</ul>
When in the editor, it sees you as in the li, because if I do my definitions as such
var definitions =
[
{
name: 'Info box',
element: 'p',
classes: [ 'info-box' ]
},
{
name: 'Item List',
element: 'li',
classes: [ 'item-list' ]
},
];
The style is enabled when I'm in the list but when I pick it, it puts the style on the li, when I need it on the ul.
If I make the definition like this
var definitions =
[
{
name: 'Info box',
element: 'p',
classes: [ 'info-box' ]
},
{
name: 'Item List',
element: 'ul',
classes: [ 'item-list' ]
},
];
It's never enabled, because it's never seen as in the UL, just the LI.
Any pointers would be much appreciated.

How to change CKEditor styles dynamically via script?

I a project I work on we need to restrict users with predefined styles that can be changed by the user.
In my jsFiddle the function setStyle() works. You can try out by uncommentting the setStyle([]). It seems to me that I can use the config.stylesSet = assignment only once.
The function to change the style does not work after a button click nor after setTimeout.
Is there any way to set styles via a script in CKE4 more than once?
CKEDITOR.stylesSet.add( 'my_styles', [
// Block-level styles
{ name: 'Blue Title', element: 'h2', styles: { 'color': 'Blue' } },
{ name: 'Red Title' , element: 'h3', styles: { 'color': 'Red' } },
// Inline styles
{ name: 'CSS Style', element: 'span', attributes: { 'class': 'my_style' } },
{ name: 'Marker: Yellow', element: 'span', styles: { 'background-color': 'Yellow' } }
] );
CKEDITOR.stylesSet.add( 'my_styles2', [
// Block-level styles
{ name: 'Default Title', element: 'h2', styles: { 'color': 'Red' } },
// Inline styles
{ name: 'CSS Style', element: 'span', attributes: { 'class': 'my_style' } },
] );
var ckeditor = CKEDITOR.inline('editor1', {
removePlugins: 'exportpdf,sourcearea',
fillEmptyBlocks : false,
});
// setStyle([])
setTimeout(() => {
console. log("resetting styles")
setStyle([])
}, 5000);
function setStyle(style){
// ckeditor.config.stylesSet = style
CKEDITOR.config.stylesSet = style
// console.log(ckeditor)
console.log(style)
}

CKEditor 4 - How to add H2, H3, etc to toolbar

Looking at these docs: https://ckeditor.com/docs/ckeditor4/latest/features/styles.html
So adding a style set should go something like this, but I can't get the H2 or H3 items to appear in my toolbar:
CKEDITOR.stylesSet.add( 'my_styles', [
// Block-level styles
{ name: 'Blue Title', element: 'h2', styles: { 'color': 'Blue' } },
{ name: 'Red Title' , element: 'h3', styles: { 'color': 'Red' } },
// Inline styles
{ name: 'CSS Style', element: 'span', attributes: { 'class': 'my_style' } },
{ name: 'Marker: Yellow', element: 'span', styles: { 'background-color': 'Yellow' } }
] );
CKEDITOR.editorConfig = function( config ) {
config.extraPlugins = 'firstname,MediaEmbed,justify,image';
//config.forcePasteAsPlainText = true;
config.extraAllowedContent = 'iframe[*]';
config.toolbar = 'Normal';
config.toolbar_Normal = [
{ name: 'basicstyles', items: [ 'Bold', 'Italic','Underline','Strike' ] },
{ name: 'paragraph', items: [ 'NumberedList', 'BulletedList', 'JustifyLeft', 'JustifyCenter', 'JustifyRight' ] }
];
config.toolbar_Emails = [
{ name: 'basicstyles', items: [ 'Bold', 'Italic','Underline','Strike' ] },
{ name: 'paragraph', items: [ 'NumberedList', 'BulletedList', 'JustifyLeft', 'JustifyCenter', 'JustifyRight' ] }
];
config.removeDialogTabs = 'link:advanced;link:target';
config.stylesSet = 'my_styles';
};
As mentioned in the docs
Open the config.js file available in your ckeditor directory, and edit the config.format_tags entry in the following way to display the text formatting toolbar.
// Enable all default text formats:
config.format_tags = 'p;h1;h2;h3;h4;h5;h6;pre;address;div';
// Enable a limited set of text formats:
config.format_tags = 'p;h1;h2;pre;div';

ckeditor 4.13 how to disable content transformation

when I paste stuff from word to ckeditor(my version is 4.13.0) - it is pain in the ass how to set the right configuration.
I know that a lot of spans are coming from word so I try to escape them by changing text colors and text formats to 'font' instead like this
//default for font and color are span but change to font because need to escape span when paste from word
config.fontSize_style = {
element: 'font',
styles: { 'font-size': '#(size)' },
overrides: [ { element: 'font', attributes: { 'size': null } } ]
};
config.font_style = {
element: 'font',
styles: { 'font-family': '#(family)' },
overrides: [ { element: 'font', attributes: { 'face': null } } ]
};
config.colorButton_foreStyle = {
element: 'font',
attributes: { 'color': '#(color)' }
};
config.colorButton_backStyle = {
element: 'font',
styles: { 'background-color': '#(color)' }
};
but I still need span for specific class like
config.allowedContent =
'h1 h2 h3 h4 strong u strike sub sup blockquote em ul ol li p ;table[*]{*}(*);font[*]{*}(*);s[*]{*}(*); td[*]{*}(*); th[*]{*}(*); tr[*]{*}(*);' +
'a[!href,target,class,onclick,id](*);' +
'img[!src,alt,width,height];' +
'span[!class](update,textnote,andy,footnote);' +
'div[*]{margin-left,margin-right,text-align}(*);'
;
but when I copy and paste some stuff ckeditor tranforms
<span style="background-color:white>
into <font style="background-color:white>
I definitely don't want that - how can I disable it when paste from word?
I tried set config.pastefilter differently but nothing works!!

Create style that can be applied to div in ckeditor?

My ckeditor WYSIWYG has the option to create divs. Im trying to create a style that can be put in the 'Style' dropdown:
Ive tried adding the following to my config.js but it seems to have no effect.
CKEDITOR.stylesSet.add( 'default',
[
// Inline styles
{ name : 'Titulo Explicacion Servicio', element : 'div', attributes : { 'class' : 'titulo_explicacion_servicio' } },
{ name : 'Texto Explicacion Servicio', element : 'div', attributes : { 'class' : 'texto_explicacion_servicio' } },
{ name : 'Texto Fondo Foto', element : 'div', attributes : { 'class' : 'intro_fondo_foto' } }
]);
I doubt it makes any difference but this is for a Drupal site.
I found what I needed to change in styles.js
CKEDITOR.stylesSet.add( 'default', [
{ name: 'Italic Title', element: 'h2', styles: { 'font-style': 'italic' } },
{ name: 'Subtitle', element: 'h3', styles: { 'color': '#aaa', 'font-style': 'italic' } },
{
name: 'Image caption', //THESE NEXT 4 LINES I CHANGED
element: 'div',
attributes: {
class: 'caption-mine'
}
},

Resources