After setting text in the editor, I need to set the focus to the end of the text. How to do it? I can not understand
editor.setData('text');
editor.editing.view.focus(); // focus to set the beginning, but on the end
Example: /
// 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=".1.0/classic/ckeditor.js"></script>
<div id="editor">
<p>Editor content goes here.</p>
</div>
<br>
<button id="focusSet">Focus</button>
After setting text in the editor, I need to set the focus to the end of the text. How to do it? I can not understand
editor.setData('text');
editor.editing.view.focus(); // focus to set the beginning, but on the end
Example: https://jsfiddle/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./ckeditor5/11.1.0/classic/ckeditor.js"></script>
<div id="editor">
<p>Editor content goes here.</p>
</div>
<br>
<button id="focusSet">Focus</button>
Share
Improve this question
edited Oct 15, 2018 at 12:44
Szymon Cofalik
1,1608 silver badges10 bronze badges
asked Oct 12, 2018 at 10:46
DarkRiDDeRDarkRiDDeR
331 silver badge4 bronze badges
5
- Possible duplicate of How to set cursor position to end of text in CKEditor? – Reinstate Monica Cellio Commented Oct 12, 2018 at 10:54
- In that problem, the old version of editor 4 – DarkRiDDeR Commented Oct 12, 2018 at 11:00
- Did none of the solutions on that question work? – Reinstate Monica Cellio Commented Oct 12, 2018 at 11:00
- yes, does not work – DarkRiDDeR Commented Oct 12, 2018 at 11:03
- Please modify the question to include a minimal reproducible example – Reinstate Monica Cellio Commented Oct 12, 2018 at 11:04
1 Answer
Reset to default 7To set selection in CKE5, you need to do it in "change block" where you have an access to the writer:
editor.model.change( writer => {
writer.setSelection( ... );
} );
There are a couple of ways to set selection, see docs: https://ckeditor./docs/ckeditor5/latest/api/module_engine_model_writer-Writer.html#function-setSelection
Basically, you need some reference when setting selection. For example, if you want to set it after a given model node, you need a reference to that node and then you can use it like this:
writer.setSelection( myNode, 'after' );
If you want to set it at the end of the content you can use the document root:
writer.setSelection( editor.model.document.getRoot(), 'end' );