Why not store startPos and endPos in state for each node?

Often I want to access a specific part of the doc and do something like render a decoration, modify text, etc. This requires knowing the startPos and endPos relative to the root doc node. In order to get this, as far as I know, the best way is to either start from the currentSelection if you want to do something relative to that, or to write a function to traverse the doc tree and keep count until you hit the node you want to do something with.

I’m curious conceptually why not just store a startPos and endPos in state for each Node? It seems like a useful common thing people might want to have easy access to. The fact that it’s not there makes me suspect I’m misunderstanding something important about ProseMirror state though.

Thanks!

Nodes are immutable, and are shared between updated versions of the document, in which they may have a different position.

1 Like