From debeb20f68d8200296264f2e936b955d94cff05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Jablo=C3=B1ski?= <43938777+GermanJablo@users.noreply.github.com> Date: Sat, 20 Jul 2024 22:53:03 -0300 Subject: [PATCH 1/2] Fix HeadingNode.insertNewAfter bug --- packages/lexical-rich-text/src/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/lexical-rich-text/src/index.ts b/packages/lexical-rich-text/src/index.ts index ba984f1d5c8..9bbbc18d46b 100644 --- a/packages/lexical-rich-text/src/index.ts +++ b/packages/lexical-rich-text/src/index.ts @@ -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(); From d9ec244a28c676ad827bc900e7844d357c373cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Jablo=C3=B1ski?= <43938777+GermanJablo@users.noreply.github.com> Date: Sat, 20 Jul 2024 23:25:24 -0300 Subject: [PATCH 2/2] add unit tests --- .../__tests__/unit/LexicalHeadingNode.test.ts | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/lexical-rich-text/src/__tests__/unit/LexicalHeadingNode.test.ts b/packages/lexical-rich-text/src/__tests__/unit/LexicalHeadingNode.test.ts index 39b10047c4a..057999ba031 100644 --- a/packages/lexical-rich-text/src/__tests__/unit/LexicalHeadingNode.test.ts +++ b/packages/lexical-rich-text/src/__tests__/unit/LexicalHeadingNode.test.ts @@ -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(() => { @@ -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( + '

hello world

', + ); + 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( + '

hello world


', + ); + }); + + 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'); + root.append(headingNode.append(headingTextNode1, headingTextNode2)); + headingTextNode2.selectEnd(); + }); + expect(testEnv.outerHTML).toBe( + '

hello world

', + ); + 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( + '

hello world


', + ); + }); + test('$createHeadingNode()', async () => { const {editor} = testEnv; await editor.update(() => {