如何在CKEditor5中设置选择?

How to set selection in CKEditor5?

在编辑器中设置文本后,我需要将焦点设置到文本的末尾。怎么做?看不懂

editor.setData('text');
editor.editing.view.focus(); // focus to set the beginning, but on the end

示例:https://jsfiddle.net/ogm5s6k7/1/

// Editor configuration.
ClassicEditor
  .create( document.querySelector( '#editor' ))
  .then( editor => {
  window.editor = editor;
})
  .catch( error => {
  console.error( error );
});

document.getElementById('focusSet').onclick = function(e) {
  window.editor.setData('text');
  window.editor.editing.view.focus();
};
<script src="https://cdn.ckeditor.com/ckeditor5/11.1.0/classic/ckeditor.js"></script>
<div id="editor">
  <p>Editor content goes here.</p>
</div>
<br>
<button id="focusSet">Focus</button>

要在 CKE5 中设置选择,您需要在 "change block" 中进行,您可以访问作者:

editor.model.change( writer => {
    writer.setSelection( ... );
} );

有几种设置选择的方法,请参阅文档:https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_model_writer-Writer.html#function-setSelection

基本上在设置选择的时候需要一些参考。例如,如果你想在给定的模型节点之后设置它,你需要对该节点的引用,然后你可以像这样使用它:

writer.setSelection( myNode, 'after' );

如果你想把它设置在内容的末尾你可以使用文档根目录:

writer.setSelection( editor.model.document.getRoot(), 'end' );