Hi.
What’s the best way to find the absolute position of a node inside a doc? For now, I’m using the brute-force approach to find the position.
function findNodePosition(doc, target: Node) {
let result = -1;
doc.descendants((node, pos) => {
if (target === node) {
result = pos;
return false;
}
});
return result;
}
Couple things that that also help the performance
-
Is it safe to cache (memoize) the result given the same inputs (
doc
andnode
)? I’d assume that as long as the same node and document is checked, the same position should be returned? -
Is there any short path that I may take without looking up all dependents? For example, it seems straightforward skip all the nodes that don’t contain the target node.
Thanks.