Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: traverse backwards with $dfsIterator #7094

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nigelgutzmann
Copy link

Description

The $dfsIterator allows you to pass in a startNode and an endNode, and then will progress from the start to the end.

However, if you pass in an endNode that exists in the tree BEFORE the startNode, the iterator will progress forward from the startNode until it traverses the entire remainder of the tree and never hit the endNode.

This change allows the iterator to detect this condition and traverse the tree backwards.

The use-case for this is to traverse backwards from a starting point, towards the beginning of the editor, until you meet some condition. For example:

let previousTextNode = null;
for (const node of $dfsIterator(startNode, $getRootNode())) {
    if ($isTextNode(node)) {
        previousTextNode = node;
        break
    }
}

The alternative is to call $dfs().reverse(), but that is slow for editors with many nodes (lots of overhead).

Copy link

vercel bot commented Jan 25, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 25, 2025 0:22am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 25, 2025 0:22am

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 25, 2025
Copy link

github-actions bot commented Jan 25, 2025

size-limit report 📦

Path Size
lexical - cjs 29.07 KB (0%)
lexical - esm 28.86 KB (0%)
@lexical/rich-text - cjs 38.1 KB (+0.15% 🔺)
@lexical/rich-text - esm 30.96 KB (+0.16% 🔺)
@lexical/plain-text - cjs 36.61 KB (+0.17% 🔺)
@lexical/plain-text - esm 28.22 KB (0%)
@lexical/react - cjs 39.89 KB (+0.11% 🔺)
@lexical/react - esm 32.33 KB (+0.18% 🔺)

@etrepum
Copy link
Collaborator

etrepum commented Jan 25, 2025

I’m not sure it makes sense for the direction to be determined by the two nodes alone here, could be a bit confusing. I have been working on a much more robust traversal API that’s not quite ready that does offer both directions in #7046

@nigelgutzmann
Copy link
Author

@etrepum thanks for taking a look at my PR, I appreciate it!

I took a look at #7046, but I don't fully understand it. It is a new concept, so it might take me some time to get the idea fully.

Naively, I don't understand how that PR will change the $dfsIterator API. You mentioned that it could be confusing for the direction to be determined by the two nodes alone, but the API of $dfsIterator is the same on your branch - it still just accepts the two nodes.

Or is there going to be some other API entirely for a backwards depth-first search, using the NodeCaret from your PR?

@nigelgutzmann
Copy link
Author

Perhaps I should have marked this PR as a bug fix. It seems wrong to me that you can supply an endNode, but the iterator will travel further and further from the endNode, never hit it, and then stop at the end of the tree.

@etrepum
Copy link
Collaborator

etrepum commented Jan 25, 2025

That’s right, the PR doesn’t change the existing API at all, it provides new more powerful abstractions. $dfsIterator is always strictly depth first left to right, there aren’t any existing full traversal APIs that go right to left ($getNextRightPreorderNode is as close as it gets IIRC).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants