How to tell if an element is a Widget? (CKEditor) -
per ckeditor, initialize widget added insertelement, doing insertelement() , initializing initon(). problem of elements inserting not supposed widgets , initon() makes them widgets , context menu doesn't work right. having trouble finding properties inside item/element tell if is/is not widget can call initon().
cross-posted downstream on drupal.org here https://www.drupal.org/node/2466297
first of - element mean?
(note: in section assuming widget correctly , initialised.)
widget element
a widget can consists of many elements. 1 of them called "widget element" , element "upcasted" , can later access through widget.element
.
since ckeditor 4.5.0 there such method available:
widget.isdomwidgetelement = function( node ) { return node.type == ckeditor.node_element && node.hasattribute( 'data-widget' ); };
you can of course use code check if given node widget element.
widget wrapper
second important element widget's wrapper. created during data processing if widget element marked upcasted or when initon()
called if widget element wasn't wrapped yet. can access element through widget.wrapper
property.
since ckeditor 4.5.0 there following method available:
widget.isdomwidgetwrapper = function( node ) { return node.type == ckeditor.node_element && node.hasattribute( 'data-cke-widget-wrapper' ); };
and again - can use code already.
important note here - since mention insertelemet()
in question. explained in ckeditor, initialize widget added insertelement editor#insertelement()
not trigger data processing. therefore, element insert inserted is. means widget wrapper not created during insertion , created once call initon()
.
finding widgets element
many times want find widget instance element have (any element can inside widget). there's useful method that: getbyelement()
.
what should become widget? aka - how deal editor.insertelement()
?
you mentioned use editor.insertelement()
, don't know elements supposed widgets. should never happen. editor.insertelement()
quite low level method not data processing , upcasting magic editor.inserthtml()
does. means supposed used in different case - when want insert element have.
for instance, table plugin building table structure inserted editor. know table empty, control every bit of (other plugins should not interfere here). important it's table's plugin decision, not e.g. template's plugin decision. table's plugin control table feature, while template plugin uses tables. in such case, when have full control, can use editor.insertelement()
. know insert , supposed become widget.
in other scenarios should use editor.inserthtml()
, whole data processing layer triggered. other features widgets system, link plugin (which turns empty anchors fake objects), etc. can prepare data insert editable , integrated.
tl;dr
if plugin knows does, can use editor.insertelement()
, since knows know inserted element must become widget.
if plugin not control situation, should use editor.iserthtml()
method far more automated , turn proper elements widgets based on upcast callbacks.
Comments
Post a Comment