@@ -20,6 +20,8 @@ import {
2020} from '@mongodb-js/compass-components' ;
2121import AddCollection from './icons/add-collection' ;
2222import { useOpenWorkspace } from '@mongodb-js/compass-workspaces/provider' ;
23+ import { useApplicationMenu } from '@mongodb-js/compass-electron-menu' ;
24+ import { dualSourceHandlerDebounce } from '../utils/utils' ;
2325
2426const breadcrumbsStyles = css ( {
2527 padding : `${ spacing [ 300 ] } px ${ spacing [ 400 ] } px` ,
@@ -55,6 +57,7 @@ export const DiagramEditorToolbar: React.FunctionComponent<{
5557 diagramName ?: string ;
5658 hasUndo : boolean ;
5759 hasRedo : boolean ;
60+ diagramEditorHasFocus ?: boolean ;
5861 isInRelationshipDrawingMode : boolean ;
5962 onUndoClick : ( ) => void ;
6063 onRedoClick : ( ) => void ;
@@ -67,6 +70,7 @@ export const DiagramEditorToolbar: React.FunctionComponent<{
6770 hasUndo,
6871 onUndoClick,
6972 hasRedo,
73+ diagramEditorHasFocus,
7074 onRedoClick,
7175 onExportClick,
7276 onRelationshipDrawingToggle,
@@ -87,19 +91,41 @@ export const DiagramEditorToolbar: React.FunctionComponent<{
8791 [ diagramName , openDataModelingWorkspace ]
8892 ) ;
8993
90- // TODO(COMPASS-9976): Integrate with application menu
94+ // Use dualSourceHandlerDebounce to avoid handling the same keypresses
95+ // coming through useHotkeys and the application menu.
96+ const [ undoHotkey , undoAppMenu ] = useMemo (
97+ ( ) => dualSourceHandlerDebounce ( onUndoClick ) ,
98+ [ onUndoClick ]
99+ ) ;
100+ const [ redoHotkey , redoAppMenu ] = useMemo (
101+ ( ) => dualSourceHandlerDebounce ( onRedoClick ) ,
102+ [ onRedoClick ]
103+ ) ;
104+
91105 // macOS: Cmd+Shift+Z = Redo, Cmd+Z = Undo
92106 // Windows/Linux: Ctrl+Z = Undo, Ctrl+Y = Redo
93- useHotkeys ( 'mod+z' , onUndoClick , { enabled : step === 'EDITING' } , [
94- onUndoClick ,
107+ useHotkeys ( 'mod+z' , undoHotkey , { enabled : step === 'EDITING' } , [
108+ undoHotkey ,
95109 ] ) ;
96- useHotkeys ( 'mod+shift+z' , onRedoClick , { enabled : step === 'EDITING' } , [
97- onRedoClick ,
110+ useHotkeys ( 'mod+shift+z' , redoHotkey , { enabled : step === 'EDITING' } , [
111+ redoHotkey ,
98112 ] ) ;
99- useHotkeys ( 'mod+y' , onRedoClick , { enabled : step === 'EDITING' } , [
100- onRedoClick ,
113+ useHotkeys ( 'mod+y' , redoHotkey , { enabled : step === 'EDITING' } , [
114+ redoHotkey ,
101115 ] ) ;
102116
117+ // Take over the undo/redo functionality in the application menu
118+ // if either no element is focused or a child of the data modeling editor
119+ // view is focused.
120+ useApplicationMenu ( {
121+ roles : diagramEditorHasFocus
122+ ? {
123+ undo : undoAppMenu ,
124+ redo : redoAppMenu ,
125+ }
126+ : { } ,
127+ } ) ;
128+
103129 if ( step !== 'EDITING' ) {
104130 return null ;
105131 }
0 commit comments