How's the focus moving to the end?

#1

How’s the focus moving to the end? image

0 Likes

#2

You can call tr.setSelection(TextSelection.create(tr.doc, pos)) to manually set the selection position. To get the pos of end of the line, try snippet below:

const { from, to } = state.selection
let result = {}
state.tr.doc.nodesBetween(from, to, (node, pos) => {
  // get end pos of node here
  console.log(node, pos)
})
0 Likes

#3

Thank you for your answer, are you Chinese,look at your id like Chinese.:grinning:

0 Likes

#4

In case it helps anyone, I’ve implemented moving the cursor to the end of the editor with this:

const tr = view.state.tr;

const lastPosition = view.state.doc.content.size;
const selection = TextSelection.create(tr.doc, lastPosition);

view.dispatch(tr.setSelection(selection));
view.focus();
0 Likes

#5

The end might not be a valid text selection, so it’s better to use Selection.atEnd for this.

1 Like