简体   繁体   中英

CKEditor: Restore caret position after calling editor#setData

I have a CKEditor instance where I want to manipulate the content, and restore the caret position to where it was afterwards. The problem is that, when you call setData , it resets the caret to the beginning of the editor. This is understandable if you are changing all of the content, but I am only making minor changes to the data.

editor.on('change', function () {
  var data = editor.getData();
  // manipulate `data`
  var manipulatedData = data;
  editor.setData(manipulatedData);
});

I found a simple solution for you query. instead of adding setdata. you can use inserthtml

editor.insertHtml(manipulatedData).

Will keep the cursor at the end position after inserting the data

A simple solution is setData to '', then use insertHtml with your content. setData is asyncronius, so you must use a callback function. This is the code that works:

oEditor.setData('', {callback: function() {
oEditor.insertHtml(YOUR_HTML);
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM