Use of ckeditor “key” CKEDITOR.instances.editor.on('key', function (e){

故事扮演 提交于 2019-12-05 21:41:55
TARKUS

This works:

CKEDITOR.instances['editor'].on('contentDom', function() {
    CKEDITOR.instances['editor'].document.on('keyup', function(event) {
        document.getElementById("preview").innerHTML = CKEDITOR.instances.editor.getData();
    });
});

I'll wait a bit before checking as answer, in case others would like to contribute.

This is a correct way:

editor.on( 'contentDom', function() {
    editor.editable().attachListener( 'keyup', editor.document, function( evt ) {
        // ...
    } );
} );

There are some rules regarding listening on DOM events. See:

Also, I'd rather avoid calling editor.getData() on every keyup. This method is not very lightweight - it does a lot more than simple reading .innerHTML. Thus, you should think of periodic update (when the editor is focused) or the onchange plugin.

EpokK

I think you can use onChange plugin : http://ckeditor.com/addon/onchange

...
on: {
    change: function(event) {
        // your code
    }
}
...

its work for me

CKEDITOR.on('instanceCreated', function(e) {
    e.editor.on('contentDom', function() {
        var editable = e.editor.editable();
        editable.attachListener(editable, 'keyup', function() {
            var content = editable.getData()
            //your content               
        });
    });
}); 
tomas

For me it was not working because keyup has not been fired. I combined this answer here with the suggestion to use setTimeout here. And now it works for me. Additionally I'm using the key event in source code view and change event in WYSIWYG view. I ignore CTL and CMD (1114203, 1114129) to be able to correctly recognize c&p in source code view. 200 ms seems enough time for my case.

  CKEDITOR.instances.editor.on('key', function (e) {
    if ([1114203, 1114129].indexOf(e.data.keyCode) === -1) {
      setTimeout(() => {
        let data = CKEDITOR.instances.editor.getData()
        // do something with data
      }, 200)
    }
  }.bind(this))

  CKEDITOR.instances.editor.on('change', function () {
    let data = CKEDITOR.instances.editor.getData()
    // do something with data
  }.bind(this))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!