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] Bug Fix: Insertion into inline ElementNode should not crash #6703

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 13 additions & 6 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ export class RangeSelection implements BaseSelection {
/**
* Attempts to "intelligently" insert an arbitrary list of Lexical nodes into the EditorState at the
* current Selection according to a set of heuristics that determine how surrounding nodes
* should be changed, replaced, or moved to accomodate the incoming ones.
* should be changed, replaced, or moved to accommodate the incoming ones.
*
* @param nodes - the nodes to insert
*/
Expand All @@ -1353,12 +1353,13 @@ export class RangeSelection implements BaseSelection {
}

const firstPoint = this.isBackward() ? this.focus : this.anchor;
const firstBlock = $getAncestor(firstPoint.getNode(), INTERNAL_$isBlock)!;
const firstNode = firstPoint.getNode();
const firstBlock = $getAncestor(firstNode, INTERNAL_$isBlock);

const last = nodes[nodes.length - 1]!;

// CASE 1: insert inside a code block
if ('__language' in firstBlock && $isElementNode(firstBlock)) {
if ($isElementNode(firstBlock) && '__language' in firstBlock) {
if ('__language' in nodes[0]) {
this.insertText(nodes[0].getTextContent());
} else {
Expand Down Expand Up @@ -1397,8 +1398,8 @@ export class RangeSelection implements BaseSelection {

const shouldInsert = !$isElementNode(firstBlock) || !firstBlock.isEmpty();
const insertedParagraph = shouldInsert ? this.insertParagraph() : null;
const lastToInsert = blocks[blocks.length - 1];
let firstToInsert = blocks[0];
const lastToInsert: LexicalNode | undefined = blocks[blocks.length - 1];
let firstToInsert: LexicalNode | undefined = blocks[0];
if (isMergeable(firstToInsert)) {
invariant(
$isElementNode(firstBlock),
Expand All @@ -1408,9 +1409,15 @@ export class RangeSelection implements BaseSelection {
firstToInsert = blocks[1];
}
if (firstToInsert) {
invariant(
firstBlock !== null,
'Expected node %s of type %s to have a block ElementNode ancestor',
firstNode.constructor.name,
firstNode.getType(),
);
insertRangeAfter(firstBlock, firstToInsert);
}
const lastInsertedBlock = $getAncestor(nodeToSelect, INTERNAL_$isBlock)!;
const lastInsertedBlock = $getAncestor(nodeToSelect, INTERNAL_$isBlock);

if (
insertedParagraph &&
Expand Down
2 changes: 1 addition & 1 deletion packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,7 @@ export function INTERNAL_$isBlock(
export function $getAncestor<NodeType extends LexicalNode = LexicalNode>(
node: LexicalNode,
predicate: (ancestor: LexicalNode) => ancestor is NodeType,
) {
): NodeType | null {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just being a bit more explicit rather than relying on inference, usually it's best practice for exported types to be explicit.

let parent = node;
while (parent !== null && parent.getParent() !== null && !predicate(parent)) {
parent = parent.getParentOrThrow();
Expand Down
Loading