Getting the position of a node inserted with replaceSelectionWith

How can I get the position of a node that’s inserted using Transaction.replaceSelectionWith()? That is, given something like this:

const node  = . . . ;
const tr =;
assert(tr.doc.nodeAt(???) === node);

how can I know what ??? is? I’ve tried things like tr.selection.from on the idea that I had just replaced the selection, but I can’t find a pattern to the relationship between tr.selection.from and where he inserted node actually goes in the document.

So is there a correct way to know the inserted node’s position? Do I need to perhaps examine the steps in the transaction?

There is some ‘fitting’ going on in replaceSelectionWith, finding an appropriate range to replace based on the context and schema constraints, so indeed, it’s hard to predict the position of the node, so you may need to inspect the step generated and iterate through the range it replaces to find your node.

1 Like