Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into refactor-npm-package-…
Browse files Browse the repository at this point in the history
…process-gh-5869
  • Loading branch information
etrepum committed Apr 24, 2024
2 parents 1879576 + c82b2db commit 836fff6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParagraphNode>();

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(() => {
Expand Down
29 changes: 20 additions & 9 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 &&
Expand Down

0 comments on commit 836fff6

Please sign in to comment.