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 insertNodes bugs #5325

Merged
merged 4 commits into from
Dec 2, 2023
Merged
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
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()!;
GermanJablo marked this conversation as resolved.
Show resolved Hide resolved
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
Loading