最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CKEditor, initialize widget added with insertElement - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Nov 27, 2013 at 9:00 mistenktmistenkt 2,3923 gold badges16 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17

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.

发布评论

评论列表(0)

  1. 暂无评论