Whenever I try to get the cursor position on focus, I get the old position because the state isn’t updated yet. Is there another way for watching selection changes?
new Plugin({
props: {
handleDOMEvents: {
focus: (view, event) => {
// view.state.selection.anchor is old :(
},
},
},
})
Listening for state changes is the most reliably way to do that — either by using dispatchTransaction directly, or by creating a plugin view whose update method checks for selection changes.
Right, that’ll just inform you of selection changes, not focus. If you do want focus change info + the new selection, I guess you could add a small setTimeout to your focus event handler, so that the editor has a chance to read the new selection before your code runs.