Failed to reset selection after a view.updateState() call

I got a similar issue that i had before (for which i asked this question).

In that time, the selection is lost after calling view.updateState() as the node to which is attached the current custom node view gets updated which will reinstanciate the custom node view as a consequence and that’s how both the selection and the view focus are lost (because the selection was inside the previous instance of the node view, in its dom element…)

Now i again have the same issue in another custom node view and i’ve tried a workaround to fix it by storing the current selection in an outer scope variable (just after the view.updateState() call and then in the next custom node reinstanciation, i reset the same saved selection. But strangely it’s not working. The cursor will be blinking (it seems) at the position 0 !

In this codesandbox i did reproduce the issue.

As you can see in this screenshot, when i type at the end of the second line, i’ll get the selection back in the begining of the first line.

In tableCellView the as you can see, i’ve tired to create the new selection with either resolved positions or the constructor or with pos and TextSelection.create()

  setTimeout(() => {
      inner.focus();
      // Here the selection will be reset
      const lastSelection = getTableCellViewLastSelection();
      if (lastSelection) {
        const { state } = view;
        let { tr, doc } = state;
        
        // Here i tried to create the selection with resolved positions and the constructor
        // const newTextSelection = new TextSelection(lastSelection.$anchor, lastSelection.$head);
        
        // And here i tried with lastSelection.$from.pos and TextSelection.create()
        const newTextSelection = TextSelection.create(
          doc,
          lastSelection.$from.pos
        );
        tr = tr.setSelection(newTextSelection);
        setTableCellViewLastSelection(undefined);
        view.updateState(state.apply(tr));
      }
    }, 150);

If you create a whole separate nested editor inside your node view (as opposed to providing a contentDOM property and letting the editor manage the content), ProseMirror isn’t going to be able to help you with selection management and such. In this case, wouldn’t the contentDOM approach work a lot better?

My bad ! Now i understood why the selection didn’t get back in the correct position. It’s because the part of the document that is inside the custom node view isn’t managed by the editor… I’ll try the nested editor solution. Thanks