ckeditor getData() returning stale data (last change missing) in source mode

删除回忆录丶 提交于 2019-12-12 10:40:52

问题


try typing in source mode on http://ckeditor.com/demo to debug use CKEDITOR.instances["editor1"].on("key", function(e) {console.log("change: " + e.editor.getData())}) in console

I always see one character less in the console, so If I have typed abc I see ab

note: using key event because of https://dev.ckeditor.com/ticket/12031Guidelines and How to detect CKEditor source mode on change event


回答1:


Writing a detailed answer here with my analysis.

As I said earlier, the getData() is being fetched even before the key stroke is being recorded as a part of the text in the Editor. That is why you see the previous data(The data that's before the key press)

I am not able to help you much with the current situation as very minimal code is exposed. I am not really sure how you are using it, and what lines are presiding it.

But, I will post in my analysis- I created a waitfunc() which just sets-timeout for 5secs. I did this to observe if the key is recorded in the editor first or the data is being taken first.

function waitfunc() {
    setTimeout(function() {
        console.log("wait")
    }, 5000)
}

CKEDITOR.instances["editor1"].on("key", function(e) {
    waitfunc();
    console.log("change: " + e.editor.getData());
    waitfunc();
})

I see the data is being taken first. And then new key pressed is added as a part of editor. I have added 'c' at the end of the paragraph.

So, please have a look at the code once(the ordering of lines of code or whatever). And if possible, share more code, I can help more than this.

Hope it helps.




回答2:


my understanding is that the "key" event is similar to onkeydown in javascript, so looking at the editor's contents on key down means looking at it before the character has been added. You should be looking at the "keyup" event, which is fired after the contents have been altered. Much like the setTimeout solution, but a little more "proper".

See here for a very similar question and code example



来源:https://stackoverflow.com/questions/33261102/ckeditor-getdata-returning-stale-data-last-change-missing-in-source-mode

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