Skip to content

Commit

Permalink
Fix insertNodes bugs (#5325)
Browse files Browse the repository at this point in the history
Co-authored-by: Gerard Rovira <[email protected]>
Co-authored-by: EgonBolton <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2023
1 parent 2afcbbe commit 9836d54
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {$createLinkNode} from '@lexical/link';
import {$createHeadingNode} from '@lexical/rich-text';
import {$createHeadingNode, $isHeadingNode} from '@lexical/rich-text';
import {
$getSelectionStyleValueForProperty,
$patchStyleText,
Expand All @@ -21,6 +21,7 @@ import {
$getRoot,
$getSelection,
$insertNodes,
$isParagraphNode,
$isRangeSelection,
$setSelection,
RangeSelection,
Expand Down Expand Up @@ -2631,6 +2632,33 @@ describe('insertNodes', () => {
expect($getRoot().getTextContent()).toBe('footext');
});
});

it('last node is LineBreakNode', async () => {
const editor = createTestEditor();
const element = document.createElement('div');
editor.setRootElement(element);

await editor.update(() => {
// Empty text node to test empty text split
const paragraph = $createParagraphNode();
$getRoot().append(paragraph);
const selection = paragraph.select();
expect($isRangeSelection(selection)).toBeTruthy();

const newHeading = $createHeadingNode('h1').append(
$createTextNode('heading'),
);
selection.insertNodes([newHeading, $createLineBreakNode()]);
});
editor.getEditorState().read(() => {
expect(element.innerHTML).toBe(
'<h1 dir="ltr"><span data-lexical-text="true">heading</span></h1><p><br></p>',
);
const selectedNode = ($getSelection() as RangeSelection).anchor.getNode();
expect($isParagraphNode(selectedNode)).toBeTruthy();
expect($isHeadingNode(selectedNode.getPreviousSibling())).toBeTruthy();
});
});
});

describe('$patchStyleText', () => {
Expand Down
13 changes: 6 additions & 7 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1596,18 +1596,17 @@ export class RangeSelection implements BaseSelection {
const notInline = (node: LexicalNode) =>
($isElementNode(node) || $isDecoratorNode(node)) && !node.isInline();

const nodeToSelect = $isElementNode(last)
? last.getLastDescendant() || last
: last;
if (!nodes.some(notInline)) {
const index = removeTextAndSplitBlock(this);
firstBlock.splice(index, 0, nodes);
nodeToSelect.selectEnd();
last.selectEnd();
return;
}

// CASE 3: At least 1 element of the array is not inline
const blocks = $wrapInlineNodes(nodes).getChildren();
const blocksParent = $wrapInlineNodes(nodes);
const nodeToSelect = blocksParent.getLastDescendant()!;
const blocks = blocksParent.getChildren();
const isLI = (node: LexicalNode) =>
'__value' in node && '__checked' in node;
const isMergeable = (node: LexicalNode) =>
Expand All @@ -1632,7 +1631,7 @@ export class RangeSelection implements BaseSelection {

if (
insertedParagraph &&
$isElementNode(lastToInsert) &&
$isElementNode(lastInsertedBlock) &&
(isLI(insertedParagraph) || INTERNAL_$isBlock(lastToInsert))
) {
lastInsertedBlock.append(...insertedParagraph.getChildren());
Expand All @@ -1648,7 +1647,7 @@ export class RangeSelection implements BaseSelection {
const lastChild = $isElementNode(firstBlock)
? firstBlock.getLastChild()
: null;
if ($isLineBreakNode(lastChild) && lastToInsert !== firstBlock) {
if ($isLineBreakNode(lastChild) && lastInsertedBlock !== firstBlock) {
lastChild.remove();
}
}
Expand Down

2 comments on commit 9836d54

@vercel
Copy link

@vercel vercel bot commented on 9836d54 Dec 2, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-fbopensource.vercel.app
lexical-git-main-fbopensource.vercel.app
lexical.dev
lexicaljs.org
www.lexical.dev
lexicaljs.com

@vercel
Copy link

@vercel vercel bot commented on 9836d54 Dec 2, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-git-main-fbopensource.vercel.app
lexical-playground-fbopensource.vercel.app
playground.lexical.dev
lexical-playground.vercel.app

Please sign in to comment.