How to find the position of an enclosing node?

I am writing a function to find the position of an enclosing node of a particular type (Page) that is an ancestor of the anchor selection position.

In my case, my schema only allows nodes of the type I am looking for (Page) as the document’s top-level child. So, I was able to find the enclosing Page’s position like so:

const $anchor = view.state.selection.$anchor
const pageIndexInDocument = $anchor.index(0)
const pagePosition = $anchor.posAtIndex(pageIndexInDocument, 0)

Is there a more idiomatic way I should consider for this search? I believe my code is fine, but I’m not sure if this is correct usage of a ResolvedPosition.


Yes, $anchor.before(1) is probably what you’re looking for.