Skip to content

Commit

Permalink
Edit Link modal fix (#5551)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarrySIV authored Jan 30, 2024
1 parent 0cb7f1e commit 3177f77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
7 changes: 3 additions & 4 deletions packages/lexical-playground/__tests__/e2e/Links.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,8 +1771,8 @@ test.describe('Links', () => {
await assertSelection(page, {
anchorOffset: 5,
anchorPath: [0, 1, 0, 0],
focusOffset: 6,
focusPath: [0, 0, 0],
focusOffset: 0,
focusPath: [0, 1],
});
} else {
await assertSelection(page, {
Expand Down Expand Up @@ -1815,7 +1815,7 @@ test.describe('Links', () => {
anchorOffset: 5,
anchorPath: [0, 1, 0, 0],
focusOffset: 0,
focusPath: [0, 1],
focusPath: [0, 1, 0, 0],
});
} else {
await assertSelection(page, {
Expand Down Expand Up @@ -1913,7 +1913,6 @@ test.describe('Links', () => {
await selectAll(page);

await click(page, '.link');
await click(page, '.link-confirm');

await assertHTML(
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {$findMatchingParent, mergeRegister} from '@lexical/utils';
import {
$getSelection,
$isLineBreakNode,
$isRangeSelection,
BaseSelection,
CLICK_COMMAND,
Expand Down Expand Up @@ -294,10 +295,30 @@ function useFloatingLinkEditorToolbar(
function updateToolbar() {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const node = getSelectedNode(selection);
const linkParent = $findMatchingParent(node, $isLinkNode);
const autoLinkParent = $findMatchingParent(node, $isAutoLinkNode);
if (linkParent !== null || autoLinkParent !== null) {
const focusNode = getSelectedNode(selection);
const focusLinkNode = $findMatchingParent(focusNode, $isLinkNode);
const focusAutoLinkNode = $findMatchingParent(
focusNode,
$isAutoLinkNode,
);
if (!(focusLinkNode || focusAutoLinkNode)) {
setIsLink(false);
return;
}
const badNode = selection.getNodes().find((node) => {
const linkNode = $findMatchingParent(node, $isLinkNode);
const autoLinkNode = $findMatchingParent(node, $isAutoLinkNode);
if (
!linkNode?.is(focusLinkNode) &&
!autoLinkNode?.is(focusAutoLinkNode) &&
!linkNode &&
!autoLinkNode &&
!$isLineBreakNode(node)
) {
return node;
}
});
if (!badNode) {
setIsLink(true);
} else {
setIsLink(false);
Expand Down

0 comments on commit 3177f77

Please sign in to comment.