如何在CKEditor5中设置选择?

Dar*_*DeR 3 javascript ckeditor5

在编辑器中设置文本后,我需要将焦点设置到文本末尾。怎么做?我无法理解

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

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

editor.setData('text');
editor.editing.view.focus(); // focus to set the beginning, but on the end
Run Code Online (Sandbox Code Playgroud)
// 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();
};
Run Code Online (Sandbox Code Playgroud)

Szy*_*lik 7

要在 CKE5 中设置选择,您需要在“更改块”中执行此操作,您可以访问编写器:

editor.model.change( writer => {
    writer.setSelection( ... );
} );
Run Code Online (Sandbox Code Playgroud)

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

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

writer.setSelection( myNode, 'after' );
Run Code Online (Sandbox Code Playgroud)

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

writer.setSelection( editor.model.document.getRoot(), 'end' );
Run Code Online (Sandbox Code Playgroud)