Finding top most nodes position

Hello,

I’m creating a menu that show next to the node that is currently hovered. One requirement that I only need to take the nodes that are in the root into account, e.g hovering over a paragraph that is in the li element will result in the menu shown next to the ul node.

There is tooling for this: For instance resolving the given position that we have then calling start with the depth of -2 would give us the start position of the list. This however doesn’t work for every situation as we have various depth for various nodes.

index seems like something that should be used for this, where calling it with 0 does give the top most node. The problem is that I don’t know where to go after I get the index as I need the actual position and I’m not sure how to get the actual ResolvedPos for an index.

Is there a simple way in which to find the top most nodes start position?

Resolved positions come with a depth property that tells you exactly how deep they are from the root. Also you can pass non-negative numbers to get positions at specific depths, so it sounds like $pos.before(1) is what you’re looking for.

1 Like

Thank you!