Updating Attrs for paragraph node with setNodeMarkup, is clearing my child nodes

I am trying to set some custom attrs on my paragraph node. According to my schema, the paragraph node has a span node as a child which further has inline text leaf as child

I tried doing dispatch(state.tr.setNodeMarkup($from.pos, schema.node.paragraph, {foo: ‘bar’ }))

I does updates the paragraph node with those attributes, but it also deletes the inner child span and text nodes.

Does anyone has any thoughts how this can be done better?

I am using latest version of prosemirror

setNodeMarkup is supposed to only change the outer node, and leave its content intact. Is the schema you get the paragraph node type from the same one that your document uses?

Yes. The paragraph which i get is of the same type.

When i debugged further, i found that the pos which is being sent to the setNodeMarkup gets resolved to be of the text node, hence the behaviour.

To fix this, i would need to send the pos of the parent paragraph node.

So now i am trying to figure out how to get the parent pos. currently i am getting my node position (which apparently is text node) from state.selection.$from

Thanks!

Look into ResolvedPos (the class of the thing in $from) — it allows you to query the start of parent nodes.

yes. I do use the resolvedRos ( $from.pos) and then grab the parent node. But then i need to compute the parent node’s pos to send to the setNodeMarkup

I figured it out. I simply used $from.before(1) to get the absolute position of my paragraph parent to the selected text leaf node.

Thanks for prompt replies.

2 Likes