Get selection parent context

Hi, and thanks for the great work done here.

I have a grammar where text can be wrapped in several semantic sections, and I wonder how to know in what kind section is wrapped some selected text.

Node API cannot help here as there is not reference to parent (for good reason). selection.$from allows to get parentNode, but the nodes I’m looking for are at higher levels. selection.$path is not part of public API…

So what is the proper method to check parent context of a selection?

Thank you.

It looks like using $from.node(depth) can help:

const selectionStart: ResolvedPos = selection.$from;
let depth = selectionStart.depth;
let parent;
do {
  parent = selectionStart.node(depth);
  if (parent) {
    if (parent.type === state.schema.nodes.theNodeTypeImLookingFor) {
      const currentContext = parent.attrs.theAttrsImLookingFor;
      break;
    }
    depth--;
  }
} while (depth > 0 && parent);