Skip to content

Instantly share code, notes, and snippets.

@fantactuka
Created July 5, 2023 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fantactuka/3cec58c7939e890a1ae5d714d4e8a669 to your computer and use it in GitHub Desktop.
Save fantactuka/3cec58c7939e890a1ae5d714d4e8a669 to your computer and use it in GitHub Desktop.
function $walk(
start: LexicalNode | null,
isBackward: boolean = false
): LexicalNode | null {
let node: LexicalNode | null = start;
if ($isElementNode(node) && !node.isEmpty()) {
return isBackward ? node.getLastChild() : node.getFirstChild();
}
let sibling: LexicalNode | null = null;
while (sibling === null && node !== null) {
sibling = isBackward ? node.getPreviousSibling() : node.getNextSibling();
node = sibling || node.getParent();
}
return node;
}
editor.getEditorState().read(() => {
let node: LexicalNode = startNode;
// while or do-while depending on whether start node needs to be processed too
do {
// break whenever found proper node
console.log(node);
} while (node = $walk(node))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment