CKEDITOR.setData prevents attaching of events with .on function

北城以北 提交于 2019-12-29 07:36:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16054070/ckeditor-setdata-prevents-attaching-of-events-with-on-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!