| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  | import { useReactFlow } from 'reactflow' | 
					
						
							|  |  |  | import { useKeyPress } from 'ahooks' | 
					
						
							|  |  |  | import { useCallback } from 'react' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   getKeyboardKeyCodeBySystem, | 
					
						
							|  |  |  |   isEventTargetInputArea, | 
					
						
							|  |  |  | } from '../utils' | 
					
						
							|  |  |  | import { useWorkflowHistoryStore } from '../workflow-history-store' | 
					
						
							|  |  |  | import { useWorkflowStore } from '../store' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   useEdgesInteractions, | 
					
						
							|  |  |  |   useNodesInteractions, | 
					
						
							|  |  |  |   useNodesSyncDraft, | 
					
						
							|  |  |  |   useWorkflowMoveMode, | 
					
						
							|  |  |  |   useWorkflowOrganize, | 
					
						
							|  |  |  |   useWorkflowStartRun, | 
					
						
							|  |  |  | } from '.' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useShortcuts = (): void => { | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     handleNodesCopy, | 
					
						
							|  |  |  |     handleNodesPaste, | 
					
						
							|  |  |  |     handleNodesDuplicate, | 
					
						
							|  |  |  |     handleNodesDelete, | 
					
						
							|  |  |  |     handleHistoryBack, | 
					
						
							|  |  |  |     handleHistoryForward, | 
					
						
							|  |  |  |   } = useNodesInteractions() | 
					
						
							|  |  |  |   const { handleStartWorkflowRun } = useWorkflowStartRun() | 
					
						
							|  |  |  |   const { shortcutsEnabled: workflowHistoryShortcutsEnabled } = useWorkflowHistoryStore() | 
					
						
							|  |  |  |   const { handleSyncWorkflowDraft } = useNodesSyncDraft() | 
					
						
							|  |  |  |   const { handleEdgeDelete } = useEdgesInteractions() | 
					
						
							|  |  |  |   const workflowStore = useWorkflowStore() | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     handleModeHand, | 
					
						
							|  |  |  |     handleModePointer, | 
					
						
							|  |  |  |   } = useWorkflowMoveMode() | 
					
						
							|  |  |  |   const { handleLayout } = useWorkflowOrganize() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     zoomTo, | 
					
						
							| 
									
										
										
										
											2024-08-23 13:11:55 +08:00
										 |  |  |     getZoom, | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |     fitView, | 
					
						
							|  |  |  |   } = useReactFlow() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 13:11:55 +08:00
										 |  |  |   // Zoom out to a minimum of 0.5 for shortcut
 | 
					
						
							|  |  |  |   const constrainedZoomOut = () => { | 
					
						
							|  |  |  |     const currentZoom = getZoom() | 
					
						
							|  |  |  |     const newZoom = Math.max(currentZoom - 0.1, 0.5) | 
					
						
							|  |  |  |     zoomTo(newZoom) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Zoom in to a maximum of 1 for shortcut
 | 
					
						
							|  |  |  |   const constrainedZoomIn = () => { | 
					
						
							|  |  |  |     const currentZoom = getZoom() | 
					
						
							|  |  |  |     const newZoom = Math.min(currentZoom + 0.1, 1) | 
					
						
							|  |  |  |     zoomTo(newZoom) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |   const shouldHandleShortcut = useCallback((e: KeyboardEvent) => { | 
					
						
							|  |  |  |     const { showFeaturesPanel } = workflowStore.getState() | 
					
						
							|  |  |  |     return !showFeaturesPanel && !isEventTargetInputArea(e.target as HTMLElement) | 
					
						
							|  |  |  |   }, [workflowStore]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(['delete', 'backspace'], (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleNodesDelete() | 
					
						
							|  |  |  |       handleEdgeDelete() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.c`, (e) => { | 
					
						
							| 
									
										
										
										
											2024-09-13 12:20:56 +08:00
										 |  |  |     const { showDebugAndPreviewPanel } = workflowStore.getState() | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e) && !showDebugAndPreviewPanel) { | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleNodesCopy() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { exactMatch: true, useCapture: true }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.v`, (e) => { | 
					
						
							| 
									
										
										
										
											2024-09-13 12:20:56 +08:00
										 |  |  |     const { showDebugAndPreviewPanel } = workflowStore.getState() | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e) && !showDebugAndPreviewPanel) { | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleNodesPaste() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { exactMatch: true, useCapture: true }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.d`, (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleNodesDuplicate() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { exactMatch: true, useCapture: true }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('alt')}.r`, (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleStartWorkflowRun() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { exactMatch: true, useCapture: true }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.z`, (e) => { | 
					
						
							| 
									
										
										
										
											2024-09-13 12:20:56 +08:00
										 |  |  |     const { showDebugAndPreviewPanel } = workflowStore.getState() | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e) && !showDebugAndPreviewPanel) { | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       workflowHistoryShortcutsEnabled && handleHistoryBack() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { exactMatch: true, useCapture: true }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress( | 
					
						
							|  |  |  |     [`${getKeyboardKeyCodeBySystem('ctrl')}.y`, `${getKeyboardKeyCodeBySystem('ctrl')}.shift.z`], | 
					
						
							|  |  |  |     (e) => { | 
					
						
							|  |  |  |       if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |         e.preventDefault() | 
					
						
							|  |  |  |         workflowHistoryShortcutsEnabled && handleHistoryForward() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     { exactMatch: true, useCapture: true }, | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress('h', (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleModeHand() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { | 
					
						
							|  |  |  |     exactMatch: true, | 
					
						
							|  |  |  |     useCapture: true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress('v', (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleModePointer() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { | 
					
						
							|  |  |  |     exactMatch: true, | 
					
						
							|  |  |  |     useCapture: true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.o`, (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       handleLayout() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { exactMatch: true, useCapture: true }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.1`, (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       fitView() | 
					
						
							|  |  |  |       handleSyncWorkflowDraft() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { | 
					
						
							|  |  |  |     exactMatch: true, | 
					
						
							|  |  |  |     useCapture: true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress('shift.1', (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       zoomTo(1) | 
					
						
							|  |  |  |       handleSyncWorkflowDraft() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { | 
					
						
							|  |  |  |     exactMatch: true, | 
					
						
							|  |  |  |     useCapture: true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress('shift.5', (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       zoomTo(0.5) | 
					
						
							|  |  |  |       handleSyncWorkflowDraft() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { | 
					
						
							|  |  |  |     exactMatch: true, | 
					
						
							|  |  |  |     useCapture: true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.dash`, (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							| 
									
										
										
										
											2024-08-23 13:11:55 +08:00
										 |  |  |       constrainedZoomOut() | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |       handleSyncWorkflowDraft() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { | 
					
						
							|  |  |  |     exactMatch: true, | 
					
						
							|  |  |  |     useCapture: true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.equalsign`, (e) => { | 
					
						
							|  |  |  |     if (shouldHandleShortcut(e)) { | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							| 
									
										
										
										
											2024-08-23 13:11:55 +08:00
										 |  |  |       constrainedZoomIn() | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |       handleSyncWorkflowDraft() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, { | 
					
						
							|  |  |  |     exactMatch: true, | 
					
						
							|  |  |  |     useCapture: true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } |