Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKEDITOR.setData prevents attaching of events with .on function

I have build a few custom plugins, but only one is listening to key events of the keyboard. Below in the code you can see the set-up to set the events. (and it's kinda basic)

Now i have the following problem that if i set my data with editor.setData in a instanceReady listener.that the .on functions aren't set.

I did try to replace the contentDom with the instanceReady event, but that doesn't fix it either.

if i set the data manualy with: editor.document.getBody().setHtml(html), there are no problems. 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 only set the html or does it also reload the editor or something?

I did take a look at this Ckeditor Source But i think that this isn't code that has something to do with the setData function.

I'm not asking for a solution. I like to understand why it's happening.

like image 319
Spons Avatar asked Apr 17 '13 07:04

Spons


1 Answers

Editor#contentDom is fired every time new inner document is set up. In framed editor editor#setData() replaces not only body.innerHTML but entire document, so contentDom is fired every time.

Thus, your code adds new listener on every setData() but you don't remove the old one. For unclear reasons none of these two listeners are now fired on keydown. I found out this recently and I cannot explain this fact.

Anyway, you need to detach all listeners on editor#contentDomUnload. Fortunately, there's convenient way to do that by using editable#attachListener.

editor.on( 'contentDom', function() {
    var editable = editor.editable();

    editable.attachListener( editor.document, 'keydown', function() {
        ...
    } );
} );

Listener will be automatically detached on next contentDomUnload.

like image 144
Reinmar Avatar answered Nov 07 '22 20:11

Reinmar