[lexical] Bug Fix: Refactor RootNode.__cachedText computation for coherency#8099
[lexical] Bug Fix: Refactor RootNode.__cachedText computation for coherency#8099etrepum merged 4 commits intofacebook:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| ); | ||
| rootNode.append(barParagraphNode, bazParagraphNode); | ||
| }, | ||
| {discrete: true}, |
There was a problem hiding this comment.
This just makes the stack trace nicer when the invariant fails, doesn't change the nature of the test
| expect(root.__cachedText).toBe( | ||
| ElementNode.prototype.getTextContent.call(root), | ||
| ); |
There was a problem hiding this comment.
This is a cleaner way to do the rootCopy thing, assuming RootNode doesn't do any meaningful changes to its implementation of getTextContent
| export interface LexicalPrivateDOM { | ||
| __lexicalTextContent?: string | undefined | null; | ||
| __lexicalLineBreak?: HTMLBRElement | HTMLImageElement | undefined | null; | ||
| __lexicalDirTextContent?: string | undefined | null; |
| let subTreeTextContent = ''; | ||
| let subTreeTextFormat: number | null = null; | ||
| let subTreeTextStyle: string | null = null; | ||
| let editorTextContent = ''; |
There was a problem hiding this comment.
This is entirely redundant, subTreeTextContent does the same thing
|
|
||
| function $createChildren( | ||
| children: Array<NodeKey>, | ||
| element: ElementNode & LexicalPrivateDOM, |
There was a problem hiding this comment.
This was a mistake, LexicalPrivateDOM is for DOM and not Lexical nodes
| if ($isElementNode(prevNode)) { | ||
| invariant( | ||
| $isElementNode(nextNode), | ||
| 'Node with key %s changed from ElementNode to !ElementNode', | ||
| key, | ||
| ); |
There was a problem hiding this comment.
Since the key must refer to the same exact class of element an invariant makes more sense
| return triggerCommandListeners(editor, command, payload); | ||
| } | ||
|
|
||
| export function $textContentRequiresDoubleLinebreakAtEnd( |
There was a problem hiding this comment.
All use cases of this function were inlined because the isLastChild condition is known at all call sites
Description
The way that the reconciler computes
RootNode.__cachedTextwas inconsistent with the way thatElementNode.getTextContent()works. There were some cache coherency issues with the way ElementNode cached text was handled.This refactors the logic such that:
subTreeTextContentandeditorTextContentwere computed independently for no obvious reason)DOUBLE_LINE_BREAKcondition is handled when reconciling children of a node, rather than trying to append it to the cache of the ElementNode itselfCloses #8096
Test plan
All existing tests pass, and new unit tests pass.