use-shortcuts.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { useReactFlow } from 'reactflow'
  2. import { useKeyPress } from 'ahooks'
  3. import { useCallback } from 'react'
  4. import {
  5. getKeyboardKeyCodeBySystem,
  6. isEventTargetInputArea,
  7. } from '../utils'
  8. import { useWorkflowHistoryStore } from '../workflow-history-store'
  9. import { useWorkflowStore } from '../store'
  10. import {
  11. useEdgesInteractions,
  12. useNodesInteractions,
  13. useNodesSyncDraft,
  14. useWorkflowMoveMode,
  15. useWorkflowOrganize,
  16. useWorkflowStartRun,
  17. } from '.'
  18. export const useShortcuts = (): void => {
  19. const {
  20. handleNodesCopy,
  21. handleNodesPaste,
  22. handleNodesDuplicate,
  23. handleNodesDelete,
  24. handleHistoryBack,
  25. handleHistoryForward,
  26. } = useNodesInteractions()
  27. const { handleStartWorkflowRun } = useWorkflowStartRun()
  28. const { shortcutsEnabled: workflowHistoryShortcutsEnabled } = useWorkflowHistoryStore()
  29. const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  30. const { handleEdgeDelete } = useEdgesInteractions()
  31. const workflowStore = useWorkflowStore()
  32. const {
  33. handleModeHand,
  34. handleModePointer,
  35. } = useWorkflowMoveMode()
  36. const { handleLayout } = useWorkflowOrganize()
  37. const {
  38. zoomTo,
  39. getZoom,
  40. fitView,
  41. } = useReactFlow()
  42. // Zoom out to a minimum of 0.5 for shortcut
  43. const constrainedZoomOut = () => {
  44. const currentZoom = getZoom()
  45. const newZoom = Math.max(currentZoom - 0.1, 0.5)
  46. zoomTo(newZoom)
  47. }
  48. // Zoom in to a maximum of 1 for shortcut
  49. const constrainedZoomIn = () => {
  50. const currentZoom = getZoom()
  51. const newZoom = Math.min(currentZoom + 0.1, 1)
  52. zoomTo(newZoom)
  53. }
  54. const shouldHandleShortcut = useCallback((e: KeyboardEvent) => {
  55. const { showFeaturesPanel } = workflowStore.getState()
  56. return !showFeaturesPanel && !isEventTargetInputArea(e.target as HTMLElement)
  57. }, [workflowStore])
  58. useKeyPress(['delete', 'backspace'], (e) => {
  59. if (shouldHandleShortcut(e)) {
  60. e.preventDefault()
  61. handleNodesDelete()
  62. handleEdgeDelete()
  63. }
  64. })
  65. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.c`, (e) => {
  66. const { showDebugAndPreviewPanel, showInputsPanel } = workflowStore.getState()
  67. if (shouldHandleShortcut(e) && !showDebugAndPreviewPanel && !showInputsPanel) {
  68. e.preventDefault()
  69. handleNodesCopy()
  70. }
  71. }, { exactMatch: true, useCapture: true })
  72. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.v`, (e) => {
  73. if (shouldHandleShortcut(e)) {
  74. e.preventDefault()
  75. handleNodesPaste()
  76. }
  77. }, { exactMatch: true, useCapture: true })
  78. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.d`, (e) => {
  79. if (shouldHandleShortcut(e)) {
  80. e.preventDefault()
  81. handleNodesDuplicate()
  82. }
  83. }, { exactMatch: true, useCapture: true })
  84. useKeyPress(`${getKeyboardKeyCodeBySystem('alt')}.r`, (e) => {
  85. if (shouldHandleShortcut(e)) {
  86. e.preventDefault()
  87. handleStartWorkflowRun()
  88. }
  89. }, { exactMatch: true, useCapture: true })
  90. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.z`, (e) => {
  91. if (shouldHandleShortcut(e)) {
  92. e.preventDefault()
  93. workflowHistoryShortcutsEnabled && handleHistoryBack()
  94. }
  95. }, { exactMatch: true, useCapture: true })
  96. useKeyPress(
  97. [`${getKeyboardKeyCodeBySystem('ctrl')}.y`, `${getKeyboardKeyCodeBySystem('ctrl')}.shift.z`],
  98. (e) => {
  99. if (shouldHandleShortcut(e)) {
  100. e.preventDefault()
  101. workflowHistoryShortcutsEnabled && handleHistoryForward()
  102. }
  103. },
  104. { exactMatch: true, useCapture: true },
  105. )
  106. useKeyPress('h', (e) => {
  107. if (shouldHandleShortcut(e)) {
  108. e.preventDefault()
  109. handleModeHand()
  110. }
  111. }, {
  112. exactMatch: true,
  113. useCapture: true,
  114. })
  115. useKeyPress('v', (e) => {
  116. if (shouldHandleShortcut(e)) {
  117. e.preventDefault()
  118. handleModePointer()
  119. }
  120. }, {
  121. exactMatch: true,
  122. useCapture: true,
  123. })
  124. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.o`, (e) => {
  125. if (shouldHandleShortcut(e)) {
  126. e.preventDefault()
  127. handleLayout()
  128. }
  129. }, { exactMatch: true, useCapture: true })
  130. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.1`, (e) => {
  131. if (shouldHandleShortcut(e)) {
  132. e.preventDefault()
  133. fitView()
  134. handleSyncWorkflowDraft()
  135. }
  136. }, {
  137. exactMatch: true,
  138. useCapture: true,
  139. })
  140. useKeyPress('shift.1', (e) => {
  141. if (shouldHandleShortcut(e)) {
  142. e.preventDefault()
  143. zoomTo(1)
  144. handleSyncWorkflowDraft()
  145. }
  146. }, {
  147. exactMatch: true,
  148. useCapture: true,
  149. })
  150. useKeyPress('shift.5', (e) => {
  151. if (shouldHandleShortcut(e)) {
  152. e.preventDefault()
  153. zoomTo(0.5)
  154. handleSyncWorkflowDraft()
  155. }
  156. }, {
  157. exactMatch: true,
  158. useCapture: true,
  159. })
  160. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.dash`, (e) => {
  161. if (shouldHandleShortcut(e)) {
  162. e.preventDefault()
  163. constrainedZoomOut()
  164. handleSyncWorkflowDraft()
  165. }
  166. }, {
  167. exactMatch: true,
  168. useCapture: true,
  169. })
  170. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.equalsign`, (e) => {
  171. if (shouldHandleShortcut(e)) {
  172. e.preventDefault()
  173. constrainedZoomIn()
  174. handleSyncWorkflowDraft()
  175. }
  176. }, {
  177. exactMatch: true,
  178. useCapture: true,
  179. })
  180. }