setNodeMarkup and deselect

Hello, there is any way to change nodeview attrs without deselecting?

You can either recreate the selection after you change the attributes, or create a custom step type that does this but returns an empty step map (so that the node doesn’t count as recreated).

Thanks for the quick response, it helped! I am creating something similar to footnote exmaple, but instead inner editor I used default input.

My handle change looks like this now:

 private handleOnChange = (attrs: InputTaskAttr) => {
const { tr } = this.view.state;
const nodePos = (this.getPos as () => number)();

const parentSelection = NodeSelection.create(
  tr.doc,
  tr.selection.from - tr.selection.$anchor.parentOffset - 1,
);

tr.setNodeMarkup(nodePos, undefined, attrs)
  .setSelection(NodeSelection.create(tr.doc, parentSelection.to - 2));

this.view.dispatch(tr);

};

I’m the new in prose mirror, will this work as expected or was there something I might have missed?

I’m not sure about the position arithmetic (what is parentSelection and why can’t you use nodePos to create the node selection)? But the technique of calling setSelection with the correct node selection right after making your change should work.

1 Like