问题
I've been working around with the CKEditor 5 with:
var mySelection = editor.getSelection();
to get selected text and being able to save it to a database for example. I wanted to know if there is an easier way to save selected text to a database and then after recovering the text a way to set it as selected automatically in the editor window.
An easy way to save selection and set selection again in the same text. Is there a plugin or something similar?
Regards
回答1:
This is the way to get stuff that is selected. You should get a selection as you did, get a range from it (
.getFirstRange()
), usefor ( const item of range.getItems() )
to iterate through all the items in the range, check if an item is a text node (item.is( 'textProxy' )
) and if it is, add it's data to the result (result = result + item.data
). That way you can obtain the text inside the selection.To restore something on a part of a content, you would have to save the model range in the database and then restore it and do something with it. It is okay but you would need to guarantee that the content will not change between saving range and the content (so the range won't get outdated).
I am not sure what feature are you trying to implement but it looks like you could use Markers
来源:https://stackoverflow.com/questions/51518889/ckeditor-5-save-selected-text-and-set-it-again-as-selected-after-recover-the-con