Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

use-shortcuts.ts 5.5KB

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