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

[lexical-react] Bug Fix: useCharacterLimit AutoLink infinite loop bug #6662 #6668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading