Created
July 5, 2023 17:08
-
-
Save fantactuka/3cec58c7939e890a1ae5d714d4e8a669 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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