From 8c06a2866f5fefd2ee048332fdc7d9ea57c3bd50 Mon Sep 17 00:00:00 2001 From: Siddhartha Varma Date: Sat, 27 Nov 2021 21:56:15 +0530 Subject: [PATCH] Disable some menu buttons when no item selected (#393) * feat(menu): disable buttons when items not selected * cut and copy options not shown when item isn't selected * added cut option on ContextMenu * Show buttons but disabled Co-authored-by: Steve Ruiz --- .../components/ContextMenu/ContextMenu.tsx | 7 +++++ .../src/components/TopPanel/Menu/Menu.tsx | 26 ++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/tldraw/src/components/ContextMenu/ContextMenu.tsx b/packages/tldraw/src/components/ContextMenu/ContextMenu.tsx index 75096053e..6dbdd0580 100644 --- a/packages/tldraw/src/components/ContextMenu/ContextMenu.tsx +++ b/packages/tldraw/src/components/ContextMenu/ContextMenu.tsx @@ -93,6 +93,10 @@ export const ContextMenu = ({ onBlur, children }: ContextMenuProps): JSX.Element app.copyJson() }, [app]) + const handleCut = React.useCallback(() => { + app.cut() + }, [app]) + const handleCopy = React.useCallback(() => { app.copy() }, [app]) @@ -177,6 +181,9 @@ export const ContextMenu = ({ onBlur, children }: ContextMenuProps): JSX.Element /> )} + + Cut + Copy diff --git a/packages/tldraw/src/components/TopPanel/Menu/Menu.tsx b/packages/tldraw/src/components/TopPanel/Menu/Menu.tsx index b98da152a..10b680a6c 100644 --- a/packages/tldraw/src/components/TopPanel/Menu/Menu.tsx +++ b/packages/tldraw/src/components/TopPanel/Menu/Menu.tsx @@ -15,14 +15,20 @@ import { useFileSystemHandlers } from '~hooks' import { HeartIcon } from '~components/Primitives/icons/HeartIcon' import { preventEvent } from '~components/preventEvent' import { DiscordIcon } from '~components/Primitives/icons' +import type { TDSnapshot } from '~types' interface MenuProps { showSponsorLink: boolean readOnly: boolean } +const numberOfSelectedIdsSelector = (s: TDSnapshot) => { + return s.document.pageStates[s.appState.currentPageId].selectedIds.length +} + export const Menu = React.memo(function Menu({ showSponsorLink, readOnly }: MenuProps) { const app = useTldrawApp() + const numberOfSelectedIds = app.useStore(numberOfSelectedIdsSelector) const { onNewProject, onOpenProject, onSaveProject, onSaveProjectAs } = useFileSystemHandlers() @@ -70,6 +76,8 @@ export const Menu = React.memo(function Menu({ showSponsorLink, readOnly }: Menu const showSignInOutMenu = app.callbacks.onSignIn || app.callbacks.onSignOut || showSponsorLink + const hasSelection = numberOfSelectedIds > 0 + return ( @@ -110,20 +118,30 @@ export const Menu = React.memo(function Menu({ showSponsorLink, readOnly }: Menu Redo - + Cut - + Copy Paste - + Copy as SVG - + Copy as JSON