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-rich-text] Bug Fix: HeadingNode.insertNewAfter #6435

Merged
merged 6 commits into from
Jul 24, 2024
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 @@ -84,7 +84,7 @@ describe('LexicalHeadingNode tests', () => {
});
});

test('HeadingNode.insertNewAfter()', async () => {
test('HeadingNode.insertNewAfter() empty', async () => {
const {editor} = testEnv;
let headingNode: HeadingNode;
await editor.update(() => {
Expand All @@ -106,6 +106,56 @@ describe('LexicalHeadingNode tests', () => {
);
});

test('HeadingNode.insertNewAfter() middle', async () => {
const {editor} = testEnv;
let headingNode: HeadingNode;
await editor.update(() => {
const root = $getRoot();
headingNode = new HeadingNode('h1');
const headingTextNode = $createTextNode('hello world');
root.append(headingNode.append(headingTextNode));
headingTextNode.select(5, 5);
});
expect(testEnv.outerHTML).toBe(
'<div contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><h1 dir="ltr"><span data-lexical-text="true">hello world</span></h1></div>',
);
await editor.update(() => {
const selection = $getSelection() as RangeSelection;
const result = headingNode.insertNewAfter(selection);
expect(result).toBeInstanceOf(HeadingNode);
expect(result.getDirection()).toEqual(headingNode.getDirection());
});
expect(testEnv.outerHTML).toBe(
'<div contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><h1 dir="ltr"><span data-lexical-text="true">hello world</span></h1><h1><br></h1></div>',
);
});

test('HeadingNode.insertNewAfter() end', async () => {
const {editor} = testEnv;
let headingNode: HeadingNode;
await editor.update(() => {
const root = $getRoot();
headingNode = new HeadingNode('h1');
const headingTextNode1 = $createTextNode('hello');
const headingTextNode2 = $createTextNode(' world');
headingTextNode2.setFormat('bold');
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

root.append(headingNode.append(headingTextNode1, headingTextNode2));
headingTextNode2.selectEnd();
});
expect(testEnv.outerHTML).toBe(
'<div contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><h1 dir="ltr"><span data-lexical-text="true">hello</span><strong data-lexical-text="true"> world</strong></h1></div>',
);
await editor.update(() => {
const selection = $getSelection() as RangeSelection;
const result = headingNode.insertNewAfter(selection);
expect(result).toBeInstanceOf(ParagraphNode);
expect(result.getDirection()).toEqual(headingNode.getDirection());
});
expect(testEnv.outerHTML).toBe(
'<div contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><h1 dir="ltr"><span data-lexical-text="true">hello</span><strong data-lexical-text="true"> world</strong></h1><p><br></p></div>',
);
});

test('$createHeadingNode()', async () => {
const {editor} = testEnv;
await editor.update(() => {
Expand Down
8 changes: 7 additions & 1 deletion packages/lexical-rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,14 @@ export class HeadingNode extends ElementNode {
restoreSelection = true,
): ParagraphNode | HeadingNode {
const anchorOffet = selection ? selection.anchor.offset : 0;
const lastDesc = this.getLastDescendant();
const isAtEnd =
!lastDesc ||
(selection &&
selection.anchor.key === lastDesc.getKey() &&
anchorOffet === lastDesc.getTextContentSize());
const newElement =
anchorOffet === this.getTextContentSize() || !selection
isAtEnd || !selection
? $createParagraphNode()
: $createHeadingNode(this.getTag());
const direction = this.getDirection();
Expand Down
Loading