Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKEditor, initialize widget added with insertElement

I have some widgets created, they on initial startup they load fine, but i add more of this type of widget to the editor through:

ckeditorInstance.insertElement(
        CKEDITOR.dom.element.createFromHtml('<html></html>'));

It inserts just fine, but the ckeditor does not recognize the element inserted as a widget, so the widget editable fields are not getting enabled like they should.

Is there any way to make ckeditor do a rescan of its content to initialize any new widgets that it has not already initialized?

like image 611
mistenkt Avatar asked Nov 27 '13 09:11

mistenkt


1 Answers

Data formats

First of all - there's a separation for data format of a widget (how widget is represented in data) and internal format (how it is represented in editor). You may be familiar with the upcast and downcast functions - they make the transition between those formats (upcasting means transforming data format to internal format).

Upcasting and downcasting work only if data string is processed, but if you insert content using editor#insertElement there's no processing, so there's no upcasting.

Inserting

You've got two solutions:

  1. Use editor#insertHtml instead of editor#insertElement. Your widget will be upcasted before insertion, so it will be inserted in a good format.

  2. Take care of upcasting (if needed) yourself and use editor#insertElement.

Initializing

This was a first step of widget's life - it has to be inserted. Second is to be initialized.

  1. There's a bug currently (it will be fixed in two weeks), that widgets inserted by the insertHtml method are not initialized. So, at this moment, after inserting widget with editor#insertHtml you need to call:

    editor.widgets.checkWidgets()

  2. If you use editor#insertElement you need to take care of initializing widget. To do that call:

    editor.widgets.initOn( element, 'widgetName' )

In both scenarios initialize widget after inserting it.

Check out the Widgets API for more details.

See also my answer explaining how to know if an element is a widget and how to insert them into editor.

like image 50
Reinmar Avatar answered Oct 24 '22 03:10

Reinmar