Get updated cursor position on focus

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.

My problem with a plugins update method is that it’s not called when I:

  1. focus the editor at pos x
  2. blur
  3. focus the editor at pos x

Update will only fire when the focused position in step 3 is different from step 1.

So I tried to use the focus event. But then the position is wrong :frowning:

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.