From a7764533482f645d372d219f16bc278a431feb29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=B2=9B=E9=BA=9F?= Date: Tue, 6 Aug 2024 00:11:17 +0800 Subject: [PATCH 1/2] fix: the problem where text inserted into a node with a leaf node would be truncated --- packages/lexical/src/LexicalSelection.ts | 6 +++++- packages/lexical/src/LexicalUtils.ts | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/lexical/src/LexicalSelection.ts b/packages/lexical/src/LexicalSelection.ts index 64fe30607f4..32c409af1c7 100644 --- a/packages/lexical/src/LexicalSelection.ts +++ b/packages/lexical/src/LexicalSelection.ts @@ -56,6 +56,7 @@ import { getElementByKeyOrThrow, getTextNodeOffset, INTERNAL_$isBlock, + INTERNAL_$isParentLeafElement, isSelectionCapturedInDecoratorInput, isSelectionWithinEditor, removeDOMBlockCursorElement, @@ -1220,7 +1221,10 @@ export class RangeSelection implements BaseSelection { } const firstPoint = this.isBackward() ? this.focus : this.anchor; - const firstBlock = $getAncestor(firstPoint.getNode(), INTERNAL_$isBlock)!; + const firstBlock = $getAncestor( + firstPoint.getNode(), + INTERNAL_$isParentLeafElement, + )!; const last = nodes[nodes.length - 1]!; diff --git a/packages/lexical/src/LexicalUtils.ts b/packages/lexical/src/LexicalUtils.ts index 9a4880b1dc4..c3ba87416a7 100644 --- a/packages/lexical/src/LexicalUtils.ts +++ b/packages/lexical/src/LexicalUtils.ts @@ -1684,6 +1684,26 @@ export function INTERNAL_$isBlock( return !node.isInline() && node.canBeEmpty() !== false && isLeafElement; } +export function INTERNAL_$isParentLeafElement( + node: LexicalNode, +): node is ElementNode | DecoratorNode { + if ($isRootNode(node) || ($isDecoratorNode(node) && !node.isInline())) { + return true; + } + if (!$isElementNode(node) || $isRootOrShadowRoot(node)) { + return false; + } + + const firstChild = node.getFirstChild(); + const isLeafElement = + firstChild === null || + $isLineBreakNode(firstChild) || + $isTextNode(firstChild) || + firstChild.isInline(); + + return isLeafElement; +} + export function $getAncestor( node: LexicalNode, predicate: (ancestor: LexicalNode) => ancestor is NodeType, From 2403fbfbbdd642e40c9572879903a55c498cc070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=B2=9B=E9=BA=9F?= Date: Tue, 6 Aug 2024 00:45:02 +0800 Subject: [PATCH 2/2] fix: $removeTextAndSplitBlock add isParentLeafElement --- packages/lexical/src/LexicalSelection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lexical/src/LexicalSelection.ts b/packages/lexical/src/LexicalSelection.ts index 32c409af1c7..1de7444a140 100644 --- a/packages/lexical/src/LexicalSelection.ts +++ b/packages/lexical/src/LexicalSelection.ts @@ -2750,7 +2750,7 @@ function $removeTextAndSplitBlock(selection: RangeSelection): number { let node = anchor.getNode(); let offset = anchor.offset; - while (!INTERNAL_$isBlock(node)) { + while (!INTERNAL_$isParentLeafElement(node)) { [node, offset] = $splitNodeAtPoint(node, offset); }