use-shortcuts.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. zoomIn,
  39. zoomOut,
  40. zoomTo,
  41. fitView,
  42. } = useReactFlow()
  43. const shouldHandleShortcut = useCallback((e: KeyboardEvent) => {
  44. const { showFeaturesPanel } = workflowStore.getState()
  45. return !showFeaturesPanel && !isEventTargetInputArea(e.target as HTMLElement)
  46. }, [workflowStore])
  47. useKeyPress(['delete', 'backspace'], (e) => {
  48. if (shouldHandleShortcut(e)) {
  49. e.preventDefault()
  50. handleNodesDelete()
  51. handleEdgeDelete()
  52. }
  53. })
  54. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.c`, (e) => {
  55. if (shouldHandleShortcut(e)) {
  56. e.preventDefault()
  57. handleNodesCopy()
  58. }
  59. }, { exactMatch: true, useCapture: true })
  60. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.v`, (e) => {
  61. if (shouldHandleShortcut(e)) {
  62. e.preventDefault()
  63. handleNodesPaste()
  64. }
  65. }, { exactMatch: true, useCapture: true })
  66. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.d`, (e) => {
  67. if (shouldHandleShortcut(e)) {
  68. e.preventDefault()
  69. handleNodesDuplicate()
  70. }
  71. }, { exactMatch: true, useCapture: true })
  72. useKeyPress(`${getKeyboardKeyCodeBySystem('alt')}.r`, (e) => {
  73. if (shouldHandleShortcut(e)) {
  74. e.preventDefault()
  75. handleStartWorkflowRun()
  76. }
  77. }, { exactMatch: true, useCapture: true })
  78. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.z`, (e) => {
  79. if (shouldHandleShortcut(e)) {
  80. e.preventDefault()
  81. workflowHistoryShortcutsEnabled && handleHistoryBack()
  82. }
  83. }, { exactMatch: true, useCapture: true })
  84. useKeyPress(
  85. [`${getKeyboardKeyCodeBySystem('ctrl')}.y`, `${getKeyboardKeyCodeBySystem('ctrl')}.shift.z`],
  86. (e) => {
  87. if (shouldHandleShortcut(e)) {
  88. e.preventDefault()
  89. workflowHistoryShortcutsEnabled && handleHistoryForward()
  90. }
  91. },
  92. { exactMatch: true, useCapture: true },
  93. )
  94. useKeyPress('h', (e) => {
  95. if (shouldHandleShortcut(e)) {
  96. e.preventDefault()
  97. handleModeHand()
  98. }
  99. }, {
  100. exactMatch: true,
  101. useCapture: true,
  102. })
  103. useKeyPress('v', (e) => {
  104. if (shouldHandleShortcut(e)) {
  105. e.preventDefault()
  106. handleModePointer()
  107. }
  108. }, {
  109. exactMatch: true,
  110. useCapture: true,
  111. })
  112. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.o`, (e) => {
  113. if (shouldHandleShortcut(e)) {
  114. e.preventDefault()
  115. handleLayout()
  116. }
  117. }, { exactMatch: true, useCapture: true })
  118. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.1`, (e) => {
  119. if (shouldHandleShortcut(e)) {
  120. e.preventDefault()
  121. fitView()
  122. handleSyncWorkflowDraft()
  123. }
  124. }, {
  125. exactMatch: true,
  126. useCapture: true,
  127. })
  128. useKeyPress('shift.1', (e) => {
  129. if (shouldHandleShortcut(e)) {
  130. e.preventDefault()
  131. zoomTo(1)
  132. handleSyncWorkflowDraft()
  133. }
  134. }, {
  135. exactMatch: true,
  136. useCapture: true,
  137. })
  138. useKeyPress('shift.5', (e) => {
  139. if (shouldHandleShortcut(e)) {
  140. e.preventDefault()
  141. zoomTo(0.5)
  142. handleSyncWorkflowDraft()
  143. }
  144. }, {
  145. exactMatch: true,
  146. useCapture: true,
  147. })
  148. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.dash`, (e) => {
  149. if (shouldHandleShortcut(e)) {
  150. e.preventDefault()
  151. zoomOut()
  152. handleSyncWorkflowDraft()
  153. }
  154. }, {
  155. exactMatch: true,
  156. useCapture: true,
  157. })
  158. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.equalsign`, (e) => {
  159. if (shouldHandleShortcut(e)) {
  160. e.preventDefault()
  161. zoomIn()
  162. handleSyncWorkflowDraft()
  163. }
  164. }, {
  165. exactMatch: true,
  166. useCapture: true,
  167. })
  168. }