【问题标题】:How do I focus the cursor at the end of inserted text in ckeditor5?如何将光标聚焦在ckeditor5中插入文本的末尾?
【发布时间】:2019-12-09 14:06:57
【问题描述】:

在下面的代码中,编辑器在按下按钮时插入了一些文本,然后编辑器聚焦光标。但是,它将光标聚焦在文本区域的开头。我希望它集中在放置在那里的文本之后。这怎么可能?虽然我查看了 API 并使用了 writer、setSelection 等,但我根本不理解它们,也未能正确使用它们,因此感谢您提供任何帮助。


<div id="editor">
  <p>Editor content goes here.</p>
</div>
<br>
<button id="focusSet">Focus</button>

<script>
  // 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>```
How would I change this code to set the cursor to the end of the text? This comes from a jsfiddle

[JsFiddle][1]


  [1]: https://jsfiddle.net/30mb2ef9/2/

【问题讨论】:

    标签: ckeditor5 writer


    【解决方案1】:

    我这样做的方法是首先设置文本,然后使用 createPositionAt 移动插入符号。

    editorInstance.setData('TEXT TO SET');
    editorInstance.model.change( writer => {
        writer.setSelection( writer.createPositionAt( editorInstance.model.document.getRoot(), 'end' ));
    });
    

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 1970-01-01
      • 2019-07-27
      • 2017-01-16
      • 1970-01-01
      • 2019-05-17
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多