CKEditor,初始化使用insertElement添加的小部件 [英] 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>'));
它插入很好,但是ckeditor无法识别作为小部件插入的元素,因此小部件可编辑字段没有像它们那样启用。
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.
有没有办法让ckeditor重新扫描其内容以初始化它尚未初始化的任何新窗口小部件?
Is there any way to make ckeditor do a rescan of its content to initialize any new widgets that it has not already initialized?
推荐答案
数据格式
首先 - 有一个分离窗口小部件的数据格式(窗口小部件如何在数据中表示)和内部格式(如何在编辑器中表示)。您可能熟悉 upcast 和 downcast 功能 - 他们制作这些格式之间的转换(向上转换意味着将数据格式转换为内部格式)。
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).
仅当处理数据字符串时,向上转换和向下转换工作,但如果你使用编辑器#insertElement
插入内容没有处理,所以没有向上转换。
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.
你有两个解决方案:
-
使用
编辑器#insertHtml
而不是编辑#insertElement
。您的小部件将在插入之前进行上传,因此它将以良好的格式插入。
Use
editor#insertHtml
instead ofeditor#insertElement
. Your widget will be upcasted before insertion, so it will be inserted in a good format.
自己处理向上转换(如果需要)并使用编辑#插入元素
。
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.
-
有一个 bug 目前(将在两周后修复),由
insertHtml
方法未初始化。所以,此时,在使用编辑器#insertHtml
插入小部件后,您需要调用:
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 witheditor#insertHtml
you need to call:
如果你使用编辑器#insertElement
你需要带关心初始化小部件。要做那个电话:
If you use editor#insertElement
you need to take care of initializing widget. To do that call:
editor.widgets.initOn(element,'widgetName')
在两种情况下在插入后初始化小部件。
查看< a href =http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.widget =nofollow noreferrer>小部件API 了解更多详情。
另见我的答案解释如何知道元素是否是窗口小部件以及如何将它们插入编辑器。
这篇关于CKEditor,初始化使用insertElement添加的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!