Skip to content

Commit

Permalink
Fix insertNodes insert position at start of inline ElementNode (#5110)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed Oct 11, 2023
1 parent 85fb867 commit bde5c5d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
$patchStyleText,
} from '@lexical/selection';
import {
$createLineBreakNode,
$createParagraphNode,
$createTextNode,
$getNodeByKey,
Expand Down Expand Up @@ -2372,6 +2373,30 @@ describe('LexicalSelectionHelpers tests', () => {
);
});
});

test('can insert a linebreak node before an inline element node', async () => {
const editor = createTestEditor();
const element = document.createElement('div');
editor.setRootElement(element);

await editor.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
const link = $createLinkNode('https://lexical.dev/');
paragraph.append(link);
const text = $createTextNode('Lexical');
link.append(text);
text.select(0, 0);

$insertNodes([$createLineBreakNode()]);
});

// TODO #5109 ElementNode should have a way to control when other nodes can be inserted inside
expect(element.innerHTML).toBe(
'<p><a href="https://lexical.dev/" dir="ltr"><br><span data-lexical-text="true">Lexical</span></a></p>',
);
});
});

describe('can insert block element nodes correctly', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,7 @@ export class RangeSelection implements BaseSelection {
);
}
didReplaceOrMerge = false;
if ($isElementNode(target) && !target.isInline()) {
if ($isElementNode(target) && (i === 0 || !target.isInline())) {
lastNode = node;
if ($isDecoratorNode(node) && !node.isInline()) {
if (nodes.length === 1 && target.canBeEmpty() && target.isEmpty()) {
Expand Down

2 comments on commit bde5c5d

@vercel
Copy link

@vercel vercel bot commented on bde5c5d Oct 11, 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-git-main-fbopensource.vercel.app
lexicaljs.org
lexical.dev
lexical-fbopensource.vercel.app
lexicaljs.com
www.lexical.dev

@vercel
Copy link

@vercel vercel bot commented on bde5c5d Oct 11, 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.vercel.app
lexical-playground-fbopensource.vercel.app
lexical-playground-git-main-fbopensource.vercel.app
playground.lexical.dev

Please sign in to comment.