Skip to content

Better tab handling - #3770

Merged
trueadm merged 6 commits into
meta:mainfrom
abelsj60:betterTabHandling
Feb 1, 2023
Merged

trueadm merged 6 commits into
meta:mainfrom
abelsj60:betterTabHandling

Conversation

@abelsj60

@abelsj60 abelsj60 commented Jan 24, 2023 •

Copy link
Copy Markdown
Contributor

This pull request fixes a problem with the use of the hidden tab character ('\t').

In some scenarios, new characters can't be inserted when it's present.

Here's a brief description of the new functionality from a new comment in $updateSelectedTextFromDOM:

  // The newly typed character may be missing from the DOM's textContent
  // when some types of tokenized nodes include a tab. For instance,
  // a LinedCodeNode <code><div><codeToken></div></code> will
  // result in the following misbehavior:

  // a. /tconst --> type 'd' at offset 1 --> /tconst
  //  - Missing 'd'
  // b. /tconst --> type 'd' at offset 3 --> /tcodnst
  //    --> type 'd' at offset 3 --> /tcodnst
  //  - Missing second 'd'

  // In these cases, we can fix the problem by manually inserting the
  // newly typed character where we know it should have been.

Here's an edited version of the details I left on Discord:

I'm trying to figure out a tab character problem in a nested LexicalNode.

Say I have a TextNode, a code highlight, that contains a "\t" special character.

  • It's on its own like this due to Prisma tokenization: LexicalNode<\t>LexicalNode...
  • The \t is a hidden character, it's length is 0 unless stringified.
  • When the user types a character, the input event fires on Root and Lexical intercepts the request to decide if it should handle the insert.
  • If it takes over, Lexical uses the value of event.data via $getNearestNodeFromDOMNode to insert the text. If it doesn't take over, Lexical uses the value of editor._window.getSelection() (via $getDOMSelection) to check if there's a mismatch with the current node (apparently, called a "backing node").
    If there's a mismatch, an update occurs. If there isn't, it doesn't.
  • Unlike event.data, the value of getSelection is the full string of the browser's AnchorNode (which Lexical semantically mirrors internally).

This is all fine and dandy.

The problem is that, if there is a tab character at the start of the .getSelection value, one of three things will happen:

  1. The new character is not included in the DOM text if the browser's AnchorNode is at offset 0 (AKA, one index after \t.
  2. A new character will only be included one time if the browser's AnchorNode is at a point greater than offset 0, e.g., \tcondst, but never \tconddst.
  3. A new character can't be added before the \t.

Of course, the value of event.data is correct in all three cases.

In paragraphs, Lexical generally seems to use CSS instead of tab characters, so this problem doesn't seem to manifest. Further, I see different behavior than what you might see with the official CodeNode as my node places code in lines (divs). As a result, Lexical's algorithms behave differently.

This LinedCodeNode-related pull request is sibling to:

#3769
#3695
#3731
#3695
#3693

@vercel

vercel Bot commented Jan 24, 2023 •

Copy link
Copy Markdown

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

Name Status Preview Comments Updated
lexical ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 1, 2023 at 8:00AM (UTC)
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 1, 2023 at 8:00AM (UTC)

@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 24, 2023
@abelsj60

abelsj60 commented Jan 28, 2023 •

Copy link
Copy Markdown
Contributor Author

@acywatson This is the sibling pull request I mentioned in #3769. It also contains e2es I don't understand.

The first run had two failures that succeed on the second run.

The second run had two new claimed failures:

e2e-collab-linux
e2e-collab-windows

On the third run, only e2e-collab-linux purported to fail.

I can continue poking at it, but I'm really not sure what to make of this. I noticed that in GitHub actions, most current pull requests have at least one failing (or cautionary) test.

I feel as if there is a piece of this picture I don't understand, and I don't know how to proceed as a result.

Basically, I'm confused.

Can you help?

-j

Comment thread packages/lexical/src/LexicalUtils.ts Outdated

// In these cases, we can fix the problem by manually inserting the
// newly typed character where we know it should have been.
const hasTabCharacter = textContent.includes('\t');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't tab always at the start?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't tab always at the start?

Oh! Well, I wasn't sure.

I think I probably used .includes because I know it will always find the tab while .startsWith or text[0]...may not?

Your approach is smarter. I'm happy to try it. I just always worry about being too smart with code. Every time I get smart with code, I regret it. Well, I regret it in life, too. But, I don't usually have to worry about that.

@abelsj60 abelsj60 Jan 31, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what it is.

If you have a tab at the start of a line, move the anchor point to offset 0, then type, you'll get text before the tab. The tab basically gets pushed right or left. I guess I figured using .includes was more resilient to all these unknown scenarios.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! We definitely want to gate this to only code highlight nodes then, otherwise we're adding overhead to all other text inputs, which is every keypress.

Comment thread packages/lexical/src/LexicalUtils.ts Outdated
let textContent = getAnchorTextFromDOM(anchorNode);
const node = $getNearestNodeFromDOMNode(anchorNode);
if (textContent !== null && $isTextNode(node)) {
// The newly typed character may be missing from the DOM's textContent

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's some way we can contain this heuristic to only code highlight nodes? Maybe if we introduce a new method on TextNode called canContainTabs(): boolean which is false by default and then implement it on CodeHighlightNode to return true. Then we can only validate this use case here and avoid applying it too all text insertions?

@abelsj60 abelsj60 Jan 31, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's some way we can contain this heuristic to only code highlight nodes? Maybe if we introduce a new method on TextNode called canContainTabs(): boolean which is false by default and then implement it on CodeHighlightNode to return true. Then we can only validate this use case here and avoid applying it too all text insertions?

I like what you're saying. These helper methods are a good idea.

I would've been afraid to be so specific with core on my own, though.

To be clear, this problem comes up when you contain highlight nodes in lines/divs, which is what I'm doing. I'm pretty committed to offering this LinedCodeNode up once done, which it nearly is. Maybe others will use it, too.

You don't really see this problem arise right now in the official code package because it doesn't contain code in lines. In essence, Lexical has side stepped it.

I'm using my own HighlightNode (LinedCodeTextNode), which has some specialized behavior over the official one. I'd be happy to add canContainTabs() to mine and target this code to check for it.

Offhand, that...seems like it'd work — and ward against unexpected bugs.

Would that be enough?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire code node right now sucks (what we have). It should have been lined to begin with, with a div around each line to prevent wrapping and keep line numbers consistent. So anything you've done in this area would be advantageous and I'd love to bring it back into core.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire code node right now sucks (what we have). It should have been lined to begin with, with a div around each line to prevent wrapping and keep line numbers consistent. So anything you've done in this area would be advantageous and I'd love to bring it back into core.

That's motivating, thanks. I'll shift canContainTabs into place and pull it into this PR.

Comment thread packages/lexical/src/LexicalUtils.ts Outdated
// newly typed character where we know it should have been.
const hasTabCharacter = textContent.includes('\t');

if (data && hasTabCharacter) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to define data, as what if the value is "0"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just suddenly realized what you meant.

I'll add data.length > 0 to the condition. I think that's what you mean? I'll push the change after the tests finish. Last time I pushed a commit in the middle of running tests, GitHub swallowed it.

@trueadm
trueadm merged commit 64e410f into meta:main Feb 1, 2023
@trueadm trueadm mentioned this pull request Feb 3, 2023
@zurfyx zurfyx mentioned this pull request May 1, 2023
2 tasks done

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 55015613 Deployed Feb 1, 2023 by vercel[bot]
Preview – lexical — 55015613 Deployed Feb 1, 2023 by vercel[bot]
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