Created
September 28, 2018 00:43
-
-
Save bradleyayers/2a7a2baa377e6619ea8aade687299beb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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