use-shortcuts.ts 4.9 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. if (shouldHandleShortcut(e)) {
  67. e.preventDefault()
  68. handleNodesCopy()
  69. }
  70. }, { exactMatch: true, useCapture: true })
  71. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.v`, (e) => {
  72. if (shouldHandleShortcut(e)) {
  73. e.preventDefault()
  74. handleNodesPaste()
  75. }
  76. }, { exactMatch: true, useCapture: true })
  77. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.d`, (e) => {
  78. if (shouldHandleShortcut(e)) {
  79. e.preventDefault()
  80. handleNodesDuplicate()
  81. }
  82. }, { exactMatch: true, useCapture: true })
  83. useKeyPress(`${getKeyboardKeyCodeBySystem('alt')}.r`, (e) => {
  84. if (shouldHandleShortcut(e)) {
  85. e.preventDefault()
  86. handleStartWorkflowRun()
  87. }
  88. }, { exactMatch: true, useCapture: true })
  89. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.z`, (e) => {
  90. if (shouldHandleShortcut(e)) {
  91. e.preventDefault()
  92. workflowHistoryShortcutsEnabled && handleHistoryBack()
  93. }
  94. }, { exactMatch: true, useCapture: true })
  95. useKeyPress(
  96. [`${getKeyboardKeyCodeBySystem('ctrl')}.y`, `${getKeyboardKeyCodeBySystem('ctrl')}.shift.z`],
  97. (e) => {
  98. if (shouldHandleShortcut(e)) {
  99. e.preventDefault()
  100. workflowHistoryShortcutsEnabled && handleHistoryForward()
  101. }
  102. },
  103. { exactMatch: true, useCapture: true },
  104. )
  105. useKeyPress('h', (e) => {
  106. if (shouldHandleShortcut(e)) {
  107. e.preventDefault()
  108. handleModeHand()
  109. }
  110. }, {
  111. exactMatch: true,
  112. useCapture: true,
  113. })
  114. useKeyPress('v', (e) => {
  115. if (shouldHandleShortcut(e)) {
  116. e.preventDefault()
  117. handleModePointer()
  118. }
  119. }, {
  120. exactMatch: true,
  121. useCapture: true,
  122. })
  123. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.o`, (e) => {
  124. if (shouldHandleShortcut(e)) {
  125. e.preventDefault()
  126. handleLayout()
  127. }
  128. }, { exactMatch: true, useCapture: true })
  129. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.1`, (e) => {
  130. if (shouldHandleShortcut(e)) {
  131. e.preventDefault()
  132. fitView()
  133. handleSyncWorkflowDraft()
  134. }
  135. }, {
  136. exactMatch: true,
  137. useCapture: true,
  138. })
  139. useKeyPress('shift.1', (e) => {
  140. if (shouldHandleShortcut(e)) {
  141. e.preventDefault()
  142. zoomTo(1)
  143. handleSyncWorkflowDraft()
  144. }
  145. }, {
  146. exactMatch: true,
  147. useCapture: true,
  148. })
  149. useKeyPress('shift.5', (e) => {
  150. if (shouldHandleShortcut(e)) {
  151. e.preventDefault()
  152. zoomTo(0.5)
  153. handleSyncWorkflowDraft()
  154. }
  155. }, {
  156. exactMatch: true,
  157. useCapture: true,
  158. })
  159. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.dash`, (e) => {
  160. if (shouldHandleShortcut(e)) {
  161. e.preventDefault()
  162. constrainedZoomOut()
  163. handleSyncWorkflowDraft()
  164. }
  165. }, {
  166. exactMatch: true,
  167. useCapture: true,
  168. })
  169. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.equalsign`, (e) => {
  170. if (shouldHandleShortcut(e)) {
  171. e.preventDefault()
  172. constrainedZoomIn()
  173. handleSyncWorkflowDraft()
  174. }
  175. }, {
  176. exactMatch: true,
  177. useCapture: true,
  178. })
  179. }