control.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import type { MouseEvent } from 'react'
  2. import {
  3. memo,
  4. useCallback,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import {
  8. RiCursorLine,
  9. RiFunctionAddLine,
  10. RiHand,
  11. RiStickyNoteAddLine,
  12. } from '@remixicon/react'
  13. import { useKeyPress } from 'ahooks'
  14. import {
  15. useNodesReadOnly,
  16. useSelectionInteractions,
  17. useWorkflow,
  18. } from '../hooks'
  19. import { getKeyboardKeyCodeBySystem, isEventTargetInputArea } from '../utils'
  20. import { useStore } from '../store'
  21. import AddBlock from './add-block'
  22. import TipPopup from './tip-popup'
  23. import { useOperator } from './hooks'
  24. import cn from '@/utils/classnames'
  25. const Control = () => {
  26. const { t } = useTranslation()
  27. const controlMode = useStore(s => s.controlMode)
  28. const setControlMode = useStore(s => s.setControlMode)
  29. const { handleLayout } = useWorkflow()
  30. const { handleAddNote } = useOperator()
  31. const {
  32. nodesReadOnly,
  33. getNodesReadOnly,
  34. } = useNodesReadOnly()
  35. const { handleSelectionCancel } = useSelectionInteractions()
  36. const handleModePointer = useCallback(() => {
  37. if (getNodesReadOnly())
  38. return
  39. setControlMode('pointer')
  40. }, [getNodesReadOnly, setControlMode])
  41. const handleModeHand = useCallback(() => {
  42. if (getNodesReadOnly())
  43. return
  44. setControlMode('hand')
  45. handleSelectionCancel()
  46. }, [getNodesReadOnly, setControlMode, handleSelectionCancel])
  47. useKeyPress('h', (e) => {
  48. if (getNodesReadOnly())
  49. return
  50. if (isEventTargetInputArea(e.target as HTMLElement))
  51. return
  52. e.preventDefault()
  53. handleModeHand()
  54. }, {
  55. exactMatch: true,
  56. useCapture: true,
  57. })
  58. useKeyPress('v', (e) => {
  59. if (isEventTargetInputArea(e.target as HTMLElement))
  60. return
  61. e.preventDefault()
  62. handleModePointer()
  63. }, {
  64. exactMatch: true,
  65. useCapture: true,
  66. })
  67. const goLayout = () => {
  68. if (getNodesReadOnly())
  69. return
  70. handleLayout()
  71. }
  72. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.o`, (e) => {
  73. e.preventDefault()
  74. goLayout()
  75. }, { exactMatch: true, useCapture: true })
  76. const addNote = (e: MouseEvent<HTMLDivElement>) => {
  77. if (getNodesReadOnly())
  78. return
  79. e.stopPropagation()
  80. handleAddNote()
  81. }
  82. return (
  83. <div className='flex items-center p-0.5 rounded-lg border-[0.5px] border-gray-100 bg-white shadow-lg text-gray-500'>
  84. <AddBlock />
  85. <TipPopup title={t('workflow.nodes.note.addNote')}>
  86. <div
  87. className={cn(
  88. 'flex items-center justify-center ml-[1px] w-8 h-8 rounded-lg hover:bg-black/5 hover:text-gray-700 cursor-pointer',
  89. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  90. )}
  91. onClick={addNote}
  92. >
  93. <RiStickyNoteAddLine className='w-4 h-4' />
  94. </div>
  95. </TipPopup>
  96. <div className='mx-[3px] w-[1px] h-3.5 bg-gray-200'></div>
  97. <TipPopup title={t('workflow.common.pointerMode')} shortcuts={['v']}>
  98. <div
  99. className={cn(
  100. 'flex items-center justify-center mr-[1px] w-8 h-8 rounded-lg cursor-pointer',
  101. controlMode === 'pointer' ? 'bg-primary-50 text-primary-600' : 'hover:bg-black/5 hover:text-gray-700',
  102. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  103. )}
  104. onClick={handleModePointer}
  105. >
  106. <RiCursorLine className='w-4 h-4' />
  107. </div>
  108. </TipPopup>
  109. <TipPopup title={t('workflow.common.handMode')} shortcuts={['h']}>
  110. <div
  111. className={cn(
  112. 'flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer',
  113. controlMode === 'hand' ? 'bg-primary-50 text-primary-600' : 'hover:bg-black/5 hover:text-gray-700',
  114. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  115. )}
  116. onClick={handleModeHand}
  117. >
  118. <RiHand className='w-4 h-4' />
  119. </div>
  120. </TipPopup>
  121. <div className='mx-[3px] w-[1px] h-3.5 bg-gray-200'></div>
  122. <TipPopup title={t('workflow.panel.organizeBlocks')} shortcuts={['ctrl', 'o']}>
  123. <div
  124. className={cn(
  125. 'flex items-center justify-center w-8 h-8 rounded-lg hover:bg-black/5 hover:text-gray-700 cursor-pointer',
  126. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  127. )}
  128. onClick={goLayout}
  129. >
  130. <RiFunctionAddLine className='w-4 h-4' />
  131. </div>
  132. </TipPopup>
  133. </div>
  134. )
  135. }
  136. export default memo(Control)