11import { useStore } from '@nanostores/react' ;
2- import { useAppSelector } from 'app/store/storeHooks' ;
2+ import { useAppDispatch , useAppSelector } from 'app/store/storeHooks' ;
33import { $activeScopes } from 'common/hooks/interactionScopes' ;
44import { useAssertSingleton } from 'common/hooks/useAssertSingleton' ;
5+ import { $canvasRightPanelTab } from 'features/controlLayers/components/CanvasRightPanel' ;
6+ import { imagesToDeleteSelected } from 'features/deleteImageModal/store/slice' ;
57import { useGalleryNavigation } from 'features/gallery/hooks/useGalleryNavigation' ;
68import { useGalleryPagination } from 'features/gallery/hooks/useGalleryPagination' ;
79import { selectListImagesQueryArgs } from 'features/gallery/store/gallerySelectors' ;
10+ import { selectActiveTab } from 'features/ui/store/uiSelectors' ;
811import { $isRightPanelOpen } from 'features/ui/store/uiSlice' ;
912import { computed } from 'nanostores' ;
13+ import { useCallback , useMemo } from 'react' ;
1014import { useHotkeys } from 'react-hotkeys-hook' ;
1115import { useListImagesQuery } from 'services/api/endpoints/images' ;
1216
@@ -26,10 +30,23 @@ const $upDownHotkeysEnabled = computed([$activeScopes, $isRightPanelOpen], (acti
2630export const useGalleryHotkeys = ( ) => {
2731 useAssertSingleton ( 'useGalleryHotkeys' ) ;
2832 const { goNext, goPrev, isNextEnabled, isPrevEnabled } = useGalleryPagination ( ) ;
33+ const dispatch = useAppDispatch ( ) ;
34+ const selection = useAppSelector ( ( s ) => s . gallery . selection ) ;
2935 const queryArgs = useAppSelector ( selectListImagesQueryArgs ) ;
3036 const queryResult = useListImagesQuery ( queryArgs ) ;
3137 const leftRightHotkeysEnabled = useStore ( $leftRightHotkeysEnabled ) ;
3238 const upDownHotkeysEnabled = useStore ( $upDownHotkeysEnabled ) ;
39+ const canvasRightPanelTab = useStore ( $canvasRightPanelTab ) ;
40+ const appTab = useAppSelector ( selectActiveTab ) ;
41+
42+ // When we are on the canvas tab, we need to disable the delete hotkey when the user is focused on the layers tab in
43+ // the right hand panel, because the same hotkey is used to delete layers.
44+ const isDeleteEnabledByTab = useMemo ( ( ) => {
45+ if ( appTab !== 'canvas' ) {
46+ return true ;
47+ }
48+ return canvasRightPanelTab === 'gallery' ;
49+ } , [ appTab , canvasRightPanelTab ] ) ;
3350
3451 const {
3552 handleLeftImage,
@@ -95,4 +112,16 @@ export const useGalleryHotkeys = () => {
95112 { preventDefault : true , enabled : upDownHotkeysEnabled } ,
96113 [ isOnLastRow , goNext , isNextEnabled , queryResult . isFetching , handleDownImage , upDownHotkeysEnabled ]
97114 ) ;
115+
116+ const handleDelete = useCallback ( ( ) => {
117+ if ( ! selection . length ) {
118+ return ;
119+ }
120+ dispatch ( imagesToDeleteSelected ( selection ) ) ;
121+ } , [ dispatch , selection ] ) ;
122+
123+ useHotkeys ( [ 'delete' , 'backspace' ] , handleDelete , { enabled : leftRightHotkeysEnabled && isDeleteEnabledByTab } , [
124+ leftRightHotkeysEnabled ,
125+ isDeleteEnabledByTab ,
126+ ] ) ;
98127} ;
0 commit comments