Skip to content

Instantly share code, notes, and snippets.

@bradleyayers
Created September 28, 2018 00:43
Show Gist options
  • Save bradleyayers/2a7a2baa377e6619ea8aade687299beb to your computer and use it in GitHub Desktop.
Save bradleyayers/2a7a2baa377e6619ea8aade687299beb to your computer and use it in GitHub Desktop.
export function after(
$pos: ResolvedPos,
predicate: (node: Node, pos: number) => boolean
): { $pos: ResolvedPos; node: Node; parent: Node } | null {
let result: { $pos: ResolvedPos; node: Node; parent: Node } | null = null;
$pos.doc.nodesBetween($pos.pos, $pos.doc.content.size, (node, pos, parent) => {
if (pos < $pos.pos) {
// Don't recurse into nodes that are completely before the position we're
// interested in.
if (pos + node.nodeSize < $pos.pos) {
return false;
}
// Skip nodes that are before the position we're interested in.
return;
}
if (predicate(node, pos)) {
result = {
$pos: $pos.doc.resolve(pos),
node: node,
parent: parent
};
}
// `false` needs to be returned repeatedly until `nodesBetween` bubbles up
// to the root and exits.
if (result !== null) {
return false;
}
// Continue
return;
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment