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

fix: the problem where text inserted into a node with a leaf node wo… #6493

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
getElementByKeyOrThrow,
getTextNodeOffset,
INTERNAL_$isBlock,
INTERNAL_$isParentLeafElement,
isSelectionCapturedInDecoratorInput,
isSelectionWithinEditor,
removeDOMBlockCursorElement,
Expand Down Expand Up @@ -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]!;

Expand Down Expand Up @@ -2744,7 +2748,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);
}

Expand Down
20 changes: 20 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,26 @@ export function INTERNAL_$isBlock(
return !node.isInline() && node.canBeEmpty() !== false && isLeafElement;
}

export function INTERNAL_$isParentLeafElement(
node: LexicalNode,
): node is ElementNode | DecoratorNode<unknown> {
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<NodeType extends LexicalNode = LexicalNode>(
node: LexicalNode,
predicate: (ancestor: LexicalNode) => ancestor is NodeType,
Expand Down
Loading