Skip to content

Commit

Permalink
Nit: Floating toolbar (#5038)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed Sep 22, 2023
1 parent a3f5252 commit 176b8cf
Showing 1 changed file with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
GridSelection,
KEY_ESCAPE_COMMAND,
LexicalEditor,
LexicalNode,
NodeSelection,
RangeSelection,
SELECTION_CHANGE_COMMAND,
Expand Down Expand Up @@ -270,41 +269,21 @@ 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 [linkParent, autoLinkParent] = getLinkElements(selection);
// We don't want this menu to open for auto links.
if (linkParent != null && autoLinkParent == null) {
setIsLink(true);
} else {
setIsLink(false);
}
}
}, []);
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(() => {
function updateToolbar() {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const node = getSelectedNode(selection);
const linkParent = $findMatchingParent(node, $isLinkNode);
const autoLinkParent = $findMatchingParent(node, $isAutoLinkNode);
// We don't want this menu to open for auto links.
if (linkParent !== null && autoLinkParent === null) {
setIsLink(true);
} else {
setIsLink(false);
}
}
}
}, []);
useEffect(() => {
return mergeRegister(
editor.registerUpdateListener(({editorState}) => {
editorState.read(() => {
Expand All @@ -322,14 +301,22 @@ function useFloatingLinkEditorToolbar(
),
editor.registerCommand(
CLICK_COMMAND,
(_payload, newEditor) => {
openLinkInNewTab(_payload);
(payload) => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const node = getSelectedNode(selection);
const linkNode = $findMatchingParent(node, $isLinkNode);
if ($isLinkNode(linkNode) && (payload.metaKey || payload.ctrlKey)) {
window.open(linkNode.getURL(), '_blank');
return true;
}
}
return false;
},
COMMAND_PRIORITY_CRITICAL,
COMMAND_PRIORITY_LOW,
),
);
}, [editor, updateToolbar, openLinkInNewTab]);
}, [editor]);

return createPortal(
<FloatingLinkEditor
Expand Down

2 comments on commit 176b8cf

@vercel
Copy link

@vercel vercel bot commented on 176b8cf 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-git-main-fbopensource.vercel.app
lexical.dev
lexicaljs.com
lexical-fbopensource.vercel.app
lexicaljs.org
www.lexical.dev

@vercel
Copy link

@vercel vercel bot commented on 176b8cf 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
playground.lexical.dev
lexical-playground.vercel.app

Please sign in to comment.