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

Fix autolink styles on creation #6069

Merged
merged 2 commits into from
May 9, 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
51 changes: 51 additions & 0 deletions packages/lexical-playground/__tests__/e2e/AutoLinks.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,55 @@ test.describe('Auto Links', () => {
{ignoreClasses: true},
);
});

test('Can convert url-like text with styles into links', async ({
page,
isPlainText,
}) => {
test.skip(isPlainText);
await focusEditor(page);

//increase font size
await click(page, '.font-increment');
await click(page, '.font-increment');

await page.keyboard.type('Hellohttp://example.com and more');

await assertHTML(
page,
html`
<p dir="ltr">
<span style="font-size: 19px;" data-lexical-text="true">
Hellohttp://example.com and more
</span>
</p>
`,
undefined,
{ignoreClasses: true},
);

// Add space before link text
await moveToLineBeginning(page);
await moveRight(page, 5);
await page.keyboard.type(' ');

await assertHTML(
page,
html`
<p dir="ltr">
<span style="font-size: 19px;" data-lexical-text="true">Hello</span>
<a href="http://example.com" dir="ltr">
<span style="font-size: 19px;" data-lexical-text="true">
http://example.com
</span>
</a>
<span style="font-size: 19px;" data-lexical-text="true">
and more
</span>
</p>
`,
undefined,
{ignoreClasses: true},
);
});
});
10 changes: 6 additions & 4 deletions packages/lexical-react/src/LexicalAutoLinkPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function extractMatchingNodes(
];
}

function createAutoLinkNode(
function $createAutoLinkNode_(
ivailop7 marked this conversation as resolved.
Show resolved Hide resolved
nodes: TextNode[],
startIndex: number,
endIndex: number,
Expand All @@ -200,6 +200,7 @@ function createAutoLinkNode(
const textNode = $createTextNode(match.text);
textNode.setFormat(linkTextNode.getFormat());
textNode.setDetail(linkTextNode.getDetail());
textNode.setStyle(linkTextNode.getStyle());
linkNode.append(textNode);
linkTextNode.replace(linkNode);
return remainingTextNode;
Expand Down Expand Up @@ -240,6 +241,7 @@ function createAutoLinkNode(
const textNode = $createTextNode(firstLinkTextNode.getTextContent());
textNode.setFormat(firstLinkTextNode.getFormat());
textNode.setDetail(firstLinkTextNode.getDetail());
textNode.setStyle(firstLinkTextNode.getStyle());
linkNode.append(textNode, ...linkNodes);
// it does not preserve caret position if caret was at the first text node
// so we need to restore caret position
Expand All @@ -256,7 +258,7 @@ function createAutoLinkNode(
return undefined;
}

function handleLinkCreation(
function $handleLinkCreation(
nodes: TextNode[],
matchers: Array<LinkMatcher>,
onChange: ChangeHandler,
Expand Down Expand Up @@ -290,7 +292,7 @@ function handleLinkCreation(

const actualMatchStart = invalidMatchEnd + matchStart - matchingOffset;
const actualMatchEnd = invalidMatchEnd + matchEnd - matchingOffset;
const remainingTextNode = createAutoLinkNode(
const remainingTextNode = $createAutoLinkNode_(
matchingNodes,
actualMatchStart,
actualMatchEnd,
Expand Down Expand Up @@ -449,7 +451,7 @@ function useAutoLink(
!$isAutoLinkNode(previous))
) {
const textNodesToMatch = getTextNodesToMatch(textNode);
handleLinkCreation(textNodesToMatch, matchers, onChangeWrapped);
$handleLinkCreation(textNodesToMatch, matchers, onChangeWrapped);
}

handleBadNeighbors(textNode, matchers, onChangeWrapped);
Expand Down
Loading