Skip to content

Commit

Permalink
Fix #6701 insertion into inline ElementNode
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Oct 4, 2024
1 parent 864a9ae commit 732f3dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
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 {
let parent = node;
while (parent !== null && parent.getParent() !== null && !predicate(parent)) {
parent = parent.getParentOrThrow();
Expand Down

0 comments on commit 732f3dd

Please sign in to comment.