diff --git a/packages/lexical-selection/src/__tests__/unit/LexicalSelection.test.tsx b/packages/lexical-selection/src/__tests__/unit/LexicalSelection.test.tsx index d48d1166297..d6a7b9a588a 100644 --- a/packages/lexical-selection/src/__tests__/unit/LexicalSelection.test.tsx +++ b/packages/lexical-selection/src/__tests__/unit/LexicalSelection.test.tsx @@ -1121,6 +1121,30 @@ describe('LexicalSelection tests', () => { }); }); + test('insert text one selected node element selection', async () => { + await ReactTestUtils.act(async () => { + await editor.update(() => { + const root = $getRoot(); + + const paragraph = root.getFirstChild(); + + const elementNode = $createTestElementNode(); + const text = $createTextNode('foo'); + + paragraph.append(elementNode); + elementNode.append(text); + + const selection = $createRangeSelection(); + selection.anchor.set(text.__key, 0, 'text'); + selection.focus.set(paragraph.__key, 1, 'element'); + + selection.insertText(''); + + expect(root.getTextContent()).toBe(''); + }); + }); + }); + test('getNodes resolves nested block nodes', async () => { await ReactTestUtils.act(async () => { await editor.update(() => { diff --git a/packages/lexical/src/LexicalSelection.ts b/packages/lexical/src/LexicalSelection.ts index ae3a68f1963..12067bad8e3 100644 --- a/packages/lexical/src/LexicalSelection.ts +++ b/packages/lexical/src/LexicalSelection.ts @@ -717,20 +717,26 @@ export class RangeSelection implements BaseSelection { insertText(text: string): void { const anchor = this.anchor; const focus = this.focus; - const isBefore = this.isCollapsed() || anchor.isBefore(focus); const format = this.format; const style = this.style; - if (isBefore && anchor.type === 'element') { - $transferStartingElementPointToTextPoint(anchor, focus, format, style); - } else if (!isBefore && focus.type === 'element') { - $transferStartingElementPointToTextPoint(focus, anchor, format, style); + let firstPoint = anchor; + let endPoint = focus; + if (!this.isCollapsed() && focus.isBefore(anchor)) { + firstPoint = focus; + endPoint = anchor; + } + if (firstPoint.type === 'element') { + $transferStartingElementPointToTextPoint( + firstPoint, + endPoint, + format, + style, + ); } + const startOffset = firstPoint.offset; + let endOffset = endPoint.offset; const selectedNodes = this.getNodes(); const selectedNodesLength = selectedNodes.length; - const firstPoint = isBefore ? anchor : focus; - const endPoint = isBefore ? focus : anchor; - const startOffset = firstPoint.offset; - const endOffset = endPoint.offset; let firstNode: TextNode = selectedNodes[0] as TextNode; if (!$isTextNode(firstNode)) { @@ -742,6 +748,11 @@ export class RangeSelection implements BaseSelection { const lastIndex = selectedNodesLength - 1; let lastNode = selectedNodes[lastIndex]; + if (selectedNodesLength === 1 && endPoint.type === 'element') { + endOffset = firstNodeTextLength; + endPoint.set(firstPoint.key, endOffset, 'text'); + } + if ( this.isCollapsed() && startOffset === firstNodeTextLength &&