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] Bug Fix: spliting AutoLinkNode #6631

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions packages/lexical-playground/__tests__/e2e/AutoLinks.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,63 @@ test.describe('Auto Links', () => {
{ignoreClasses: true},
);
});

test('Can handle new lines in the middle of a URL for rich text', async ({
page,
isPlainText,
}) => {
test.skip(isPlainText);
await focusEditor(page);

await page.keyboard.type('Hello http://www.example.com test');
for (let i = 0; i < 8; i++) {
await page.keyboard.press('ArrowLeft');
}
await page.keyboard.press('Enter');

await assertHTML(
page,
html`
<p dir="ltr">
<span data-lexical-text="true">Hello http://www.example.</span>
</p>
<p dir="ltr">
<span data-lexical-text="true">com test</span>
</p>
`,
undefined,
{ignoreClasses: true},
);
});
});

test.describe('Auto Links - Plain Text', () => {
test('Can handle new lines in the middle of a URL for plain text', async ({
page,
isPlainText,
}) => {
initialize({isCollab: false, isPlainText: true, page});
test.skip(!isPlainText);
await focusEditor(page);

await page.keyboard.type('Hello http://www.example.com test');
for (let i = 0; i < 8; i++) {
await page.keyboard.press('ArrowLeft');
}
await page.keyboard.press('Enter');
await page.pause();

await assertHTML(
page,
html`
<p dir="ltr">
<span data-lexical-text="true">Hello http://www.example.</span>
<br />
<span data-lexical-text="true">com test</span>
</p>
`,
undefined,
{ignoreClasses: true},
);
});
});
51 changes: 39 additions & 12 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ export class RangeSelection implements BaseSelection {
if ('__language' in nodes[0]) {
this.insertText(nodes[0].getTextContent());
} else {
const index = $removeTextAndSplitBlock(this);
const [index] = $removeTextAndSplitBlock(this);
firstBlock.splice(index, 0, nodes);
last.selectEnd();
}
Expand All @@ -1349,9 +1349,19 @@ export class RangeSelection implements BaseSelection {
$isElementNode(firstBlock),
"Expected 'firstBlock' to be an ElementNode",
);
const index = $removeTextAndSplitBlock(this);
const [index, rightPart] = $removeTextAndSplitBlock(this);
firstBlock.splice(index, 0, nodes);
last.selectEnd();

if (rightPart) {
const rightPartParent = rightPart.getParentOrThrow();
last.insertAfter(rightPart);
if (!$isRootNode(rightPartParent.getParentOrThrow())) {
// Remove redundant paragraph
rightPartParent.getParentOrThrow().remove();
}
rightPart.selectStart();
}
return;
}

Expand Down Expand Up @@ -1418,15 +1428,24 @@ export class RangeSelection implements BaseSelection {
paragraph.select();
return paragraph;
}
const index = $removeTextAndSplitBlock(this);
const [index, rightPart] = $removeTextAndSplitBlock(this);
const block = $getAncestor(this.anchor.getNode(), INTERNAL_$isBlock)!;
invariant($isElementNode(block), 'Expected ancestor to be an ElementNode');
const firstToAppend = block.getChildAtIndex(index);
const nodesToInsert = firstToAppend
? [firstToAppend, ...firstToAppend.getNextSiblings()]
: [];
const newBlock = block.insertNewAfter(this, false) as ElementNode | null;

if (newBlock) {
if (rightPart) {
const rightPartParent = rightPart.getParentOrThrow();
newBlock.append(rightPart);
if (!$isRootNode(rightPartParent.getParentOrThrow())) {
// Remove redundant paragraph
rightPartParent.getParentOrThrow().remove();
}
}
newBlock.append(...nodesToInsert);
newBlock.selectStart();
return newBlock;
Expand All @@ -1442,6 +1461,7 @@ export class RangeSelection implements BaseSelection {
insertLineBreak(selectStart?: boolean): void {
const lineBreak = $createLineBreakNode();
this.insertNodes([lineBreak]);

// this is used in MacOS with the command 'ctrl-O' (openLineBreak)
if (selectStart) {
const parent = lineBreak.getParentOrThrow();
Expand Down Expand Up @@ -2827,7 +2847,9 @@ export function $getTextContent(): string {
return selection.getTextContent();
}

function $removeTextAndSplitBlock(selection: RangeSelection): number {
function $removeTextAndSplitBlock(
selection: RangeSelection,
): [number, LexicalNode | null] {
let selection_ = selection;
if (!selection.isCollapsed()) {
selection_.removeText();
Expand All @@ -2847,39 +2869,44 @@ function $removeTextAndSplitBlock(selection: RangeSelection): number {
const anchor = selection_.anchor;
let node = anchor.getNode();
let offset = anchor.offset;
let split;
let splitNodes: LexicalNode[] | undefined;

while (!INTERNAL_$isBlock(node)) {
[node, offset] = $splitNodeAtPoint(node, offset);
[node, offset, split] = $splitNodeAtPoint(node, offset);
if (split) {
splitNodes = split;
}
}

return offset;
return [offset, splitNodes ? splitNodes[1] : null];
}

function $splitNodeAtPoint(
node: LexicalNode,
offset: number,
): [parent: ElementNode, offset: number] {
): [parent: ElementNode, offset: number, split: LexicalNode[] | null] {
const parent = node.getParent();
if (!parent) {
const paragraph = $createParagraphNode();
$getRoot().append(paragraph);
paragraph.select();
return [$getRoot(), 0];
return [$getRoot(), 0, null];
}

if ($isTextNode(node)) {
const split = node.splitText(offset);
if (split.length === 0) {
return [parent, node.getIndexWithinParent()];
return [parent, node.getIndexWithinParent(), null];
}
const x = offset === 0 ? 0 : 1;
const index = split[0].getIndexWithinParent() + x;

return [parent, index];
return [parent, index, split];
}

if (!$isElementNode(node) || offset === 0) {
return [parent, node.getIndexWithinParent()];
return [parent, node.getIndexWithinParent(), null];
}

const firstToAppend = node.getChildAtIndex(offset);
Expand All @@ -2895,7 +2922,7 @@ function $splitNodeAtPoint(
newElement.append(firstToAppend, ...firstToAppend.getNextSiblings());
}
}
return [parent, node.getIndexWithinParent() + 1];
return [parent, node.getIndexWithinParent() + 1, null];
}

function $wrapInlineNodes(nodes: LexicalNode[]) {
Expand Down
Loading