Placing a cursor after a newly inserted node

Hello!

I’m working on a generic insert function for inserting nodes into a doc. After inserting a node, I want to be able to place the cursor selection after the newly inserted node based on where tr.insert places the slice. My current version works, and is something along these lines:

const finaliseInsert = (tr: Transaction, nodeLength: number) => {
  const lastStep = tr.steps[tr.steps.length - 1] as ReplaceStep;
  let to = getStepRange(tr) || 0;
  const cursorPos = lastStep.to + lastStep.slice.openStart + nodeLength;
  return tr
    .setSelection(new TextSelection(tr.doc.resolve(cursorPos)))
    .scrollIntoView();
};

However, the to and slice properties of a ReplaceStep are private. The to can be reconstructed, but I could not find anyway to get slice. Is there a more idiomatic way of doing this? If not, could slice be exposed?

Thanks :slight_smile:

This is kind of awkward to do, but you can use step maps for it. See this code for an example.