How to Select a Node Immediately After Inserting It

I used the selection from the transaction after replacing it with a node. Seems to work for my use case, but my inline nodes are always size 1 so I haven’t tested it with variable sized nodes.

const node = schema.nodes.myNode.create({value: ''});
const tr = state.tr;
tr.replaceSelectionWith(node);
const resolvedPos = tr.doc.resolve(
  tr.selection.anchor - tr.selection.$anchor.nodeBefore.nodeSize
);
tr.setSelection(new NodeSelection(resolvedPos));
2 Likes