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

Nit: Floating toolbar #5038

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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