Skip to content

Commit

Permalink
[lexical-react] Bug Fix: useCharacterLimit AutoLink infinite loop bug f…
Browse files Browse the repository at this point in the history
  • Loading branch information
redstar08 authored and liuhongxin committed Sep 25, 2024
1 parent 25a3121 commit 6d926a3
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/lexical-react/src/shared/useCharacterLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import type {LexicalEditor, LexicalNode} from 'lexical';

import {$isAutoLinkNode, $isLinkNode} from '@lexical/link';
import {
$createOverflowNode,
$isOverflowNode,
Expand Down Expand Up @@ -200,13 +201,17 @@ function $wrapOverflowedNodes(offset: number): void {
}
} else if (previousLength < offset) {
const descendant = node.getFirstDescendant();
const descendantParent =
descendant !== null ? descendant.getParent() : null;
const descendantLength =
descendant !== null ? descendant.getTextContentSize() : 0;
const previousPlusDescendantLength = previousLength + descendantLength;
// For simple text we can redimension the overflow into a smaller and more accurate
// container
const firstDescendantIsSimpleText =
$isTextNode(descendant) && descendant.isSimpleText();
$isTextNode(descendant) &&
descendant.isSimpleText() &&
!($isAutoLinkNode(descendantParent) || $isLinkNode(descendantParent));
const firstDescendantDoesNotOverflow =
previousPlusDescendantLength <= offset;

Expand All @@ -217,14 +222,21 @@ function $wrapOverflowedNodes(offset: number): void {
} else if ($isLeafNode(node)) {
const previousAccumulatedLength = accumulatedLength;
accumulatedLength += node.getTextContentSize();
const parentNode = node.getParent();

if (accumulatedLength > offset && !$isOverflowNode(node.getParent())) {
if (
accumulatedLength > offset &&
!$isOverflowNode(parentNode) &&
!$isOverflowNode(parentNode ? parentNode.getParent() : null)
) {
const previousSelection = $getSelection();
let overflowNode;

// For simple text we can improve the limit accuracy by splitting the TextNode
// on the split point
if (
if ($isAutoLinkNode(parentNode) || $isLinkNode(parentNode)) {
overflowNode = $wrapNode(parentNode);
} else if (
previousAccumulatedLength < offset &&
$isTextNode(node) &&
node.isSimpleText()
Expand Down

0 comments on commit 6d926a3

Please sign in to comment.