CKEDITOR.setData prevents events from attaching with the .on function

I created several custom plugins, but only one of them listens for key events on the keyboard. Below in the code you can see the setting for setting events. (and this is kind of basic)

Now I have the following problem: if I set my data using editor.setData in the ListReady instance. then .on functions are not installed.

I tried replacing the contentDom with an instanceReady event, but that also does not fix it.

if I set the data manually using: editor.document.getBody (). setHtml (html), no problem. and all events are attached without any problems.

CKEDITOR.plugins.add( 'myPlugin', { lang: '', // %REMOVE_LINE_CORE% init: function( editor ) { //Bind events if the Dom is ready! editor.on( 'contentDom', function() { //keydown editor.document.on('keydown', function(e) { 

Does anyone know why this is happening? Does the setData function install only html or does it reload the editor or something else?

I looked at this Ckeditor Source, but I think this is not code that has anything to do with the setData function.

I do not ask permission. I like to understand why this is happening.

+7
source share
1 answer

Editor#contentDom launched each time a new internal document is created. In the editor with the frame editor#setData() is replaced not only body.innerHTML , but the entire document, so the contentDom launched every time.

This way, your code adds a new listener on each setData() , but you do not delete the old one. For unknown reasons, none of these two listeners are running on keydown . I found out about this recently, and I can not explain this fact.

In any case, you need to disable all listeners on editor#contentDomUnload . Fortunately, there is a convenient way to do this using editable#attachListener .

 editor.on( 'contentDom', function() { var editable = editor.editable(); editable.attachListener( editor.document, 'keydown', function() { ... } ); } ); 

The listener is automatically disconnected at the next contentDomUnload .

+12
source

All Articles