Skip to content

Commit

Permalink
Fix reconciler bug with handling of text content (facebook#3837)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Feb 7, 2023
1 parent d096645 commit ac62be0
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions packages/lexical/src/LexicalReconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,6 @@ function createNode(
const endIndex = childrenSize - 1;
const children = createChildrenArray(node, activeNextNodeMap);
createChildrenWithDirection(children, endIndex, node, dom);
if ($textContentRequiresDoubleLinebreakAtEnd(node)) {
subTreeTextContent += DOUBLE_LINE_BREAK;
// @ts-expect-error: internal field
dom.__lexicalTextContent = subTreeTextContent;
}
}
const format = node.__format;

Expand Down Expand Up @@ -252,13 +247,14 @@ function createChildrenWithDirection(
): void {
const previousSubTreeDirectionedTextContent = subTreeDirectionedTextContent;
subTreeDirectionedTextContent = '';
createChildren(children, 0, endIndex, dom, null);
createChildren(children, element, 0, endIndex, dom, null);
reconcileBlockDirection(element, dom);
subTreeDirectionedTextContent = previousSubTreeDirectionedTextContent;
}

function createChildren(
children: Array<NodeKey>,
element: ElementNode,
_startIndex: number,
endIndex: number,
dom: null | HTMLElement,
Expand All @@ -271,7 +267,9 @@ function createChildren(
for (; startIndex <= endIndex; ++startIndex) {
createNode(children[startIndex], dom, insertDOM);
}

if ($textContentRequiresDoubleLinebreakAtEnd(element)) {
subTreeTextContent += DOUBLE_LINE_BREAK;
}
// @ts-expect-error: internal field
dom.__lexicalTextContent = subTreeTextContent;
subTreeTextContent = previousSubTreeTextContent + subTreeTextContent;
Expand Down Expand Up @@ -454,7 +452,14 @@ function reconcileChildren(

if (prevChildrenSize === 0) {
if (nextChildrenSize !== 0) {
createChildren(nextChildren, 0, nextChildrenSize - 1, dom, null);
createChildren(
nextChildren,
nextElement,
0,
nextChildrenSize - 1,
dom,
null,
);
}
} else if (nextChildrenSize === 0) {
if (prevChildrenSize !== 0) {
Expand All @@ -475,6 +480,7 @@ function reconcileChildren(
}
} else {
reconcileNodeChildren(
nextElement,
prevChildren,
nextChildren,
prevChildrenSize,
Expand Down Expand Up @@ -662,6 +668,7 @@ function getNextSibling(element: HTMLElement): Node | null {
}

function reconcileNodeChildren(
nextElement: ElementNode,
prevChildren: Array<NodeKey>,
nextChildren: Array<NodeKey>,
prevChildrenLength: number,
Expand Down Expand Up @@ -736,7 +743,14 @@ function reconcileNodeChildren(
previousNode === undefined
? null
: activeEditor.getElementByKey(previousNode);
createChildren(nextChildren, nextIndex, nextEndIndex, dom, insertDOM);
createChildren(
nextChildren,
nextElement,
nextIndex,
nextEndIndex,
dom,
insertDOM,
);
} else if (removeOldChildren && !appendNewChildren) {
destroyChildren(prevChildren, prevIndex, prevEndIndex, dom);
}
Expand Down

0 comments on commit ac62be0

Please sign in to comment.