Skip to content

Commit

Permalink
feat: LexicalClickableLinkPlugin should support ctrl click mode #4565 (
Browse files Browse the repository at this point in the history
  • Loading branch information
sathishsridhar committed Sep 22, 2023
1 parent f7603cf commit a3f5252
Showing 1 changed file with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import {$findMatchingParent, mergeRegister} from '@lexical/utils';
import {
$getSelection,
$isRangeSelection,
CLICK_COMMAND,
COMMAND_PRIORITY_CRITICAL,
COMMAND_PRIORITY_HIGH,
COMMAND_PRIORITY_LOW,
GridSelection,
KEY_ESCAPE_COMMAND,
LexicalEditor,
LexicalNode,
NodeSelection,
RangeSelection,
SELECTION_CHANGE_COMMAND,
Expand Down Expand Up @@ -268,13 +270,19 @@ function useFloatingLinkEditorToolbar(
const [activeEditor, setActiveEditor] = useState(editor);
const [isLink, setIsLink] = useState(false);

const getLinkElements = (
selection: RangeSelection,
): Array<LexicalNode | null> => {
const node = getSelectedNode(selection);
const linkParent = $findMatchingParent(node, $isLinkNode);
const autoLinkParent = $findMatchingParent(node, $isAutoLinkNode);

return [linkParent, autoLinkParent];
};
const updateToolbar = useCallback(() => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const node = getSelectedNode(selection);
const linkParent = $findMatchingParent(node, $isLinkNode);
const autoLinkParent = $findMatchingParent(node, $isAutoLinkNode);

const [linkParent, autoLinkParent] = getLinkElements(selection);
// We don't want this menu to open for auto links.
if (linkParent != null && autoLinkParent == null) {
setIsLink(true);
Expand All @@ -283,7 +291,19 @@ function useFloatingLinkEditorToolbar(
}
}
}, []);

const openLinkInNewTab = useCallback((_payload: MouseEvent) => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const [linkParent, autoLinkParent] = getLinkElements(selection);
const url = linkParent?.__url || autoLinkParent?.__url;
if (
(linkParent != null || autoLinkParent != null) &&
(_payload?.metaKey || _payload?.ctrlKey)
) {
window.open(url, '_blank');
}
}
}, []);
useEffect(() => {
return mergeRegister(
editor.registerUpdateListener(({editorState}) => {
Expand All @@ -300,8 +320,16 @@ function useFloatingLinkEditorToolbar(
},
COMMAND_PRIORITY_CRITICAL,
),
editor.registerCommand(
CLICK_COMMAND,
(_payload, newEditor) => {
openLinkInNewTab(_payload);
return false;
},
COMMAND_PRIORITY_CRITICAL,
),
);
}, [editor, updateToolbar]);
}, [editor, updateToolbar, openLinkInNewTab]);

return createPortal(
<FloatingLinkEditor
Expand Down

2 comments on commit a3f5252

@vercel
Copy link

@vercel vercel bot commented on a3f5252 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical.dev
lexicaljs.com
lexical-git-main-fbopensource.vercel.app
lexicaljs.org
www.lexical.dev
lexical-fbopensource.vercel.app

@vercel
Copy link

@vercel vercel bot commented on a3f5252 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-git-main-fbopensource.vercel.app
lexical-playground-fbopensource.vercel.app
lexical-playground.vercel.app
playground.lexical.dev

Please sign in to comment.