2

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

2 Answers 2

0

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';
2
  • Hi, and thanks for commenting. If I do this, I get the error: "Uncaught TypeError: Cannot read property 'getEditor' of undefined", which opens a whole new can of worms. Commented Mar 23, 2016 at 11:19
  • Did anything else change? I looked up that error, and it seems that error relates to the textarea not being found that you are calling in your replace command.
    – imvain2
    Commented Mar 23, 2016 at 12:56
0

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.

1
  • 2
    Modifing style sets after editor initialization brings problems: 1. ACF creates its configuration based on provided styles - e.g. allows elements that are included in the styles definition. 2. There is no information what to do with content already having styles that are being removed (should the content be removed or unstyled?) The best way of dealing with this is to destroy the editor and create a new one with updated styles configuration. Also, post your example on JSFiddle/JSBin to make it easier to investigate the incorrect-destroy error. Commented Mar 25, 2016 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.