control.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import type { MouseEvent } from 'react'
  2. import {
  3. memo,
  4. useCallback,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import cn from 'classnames'
  8. import {
  9. RiCursorLine,
  10. RiFunctionAddLine,
  11. RiHand,
  12. RiStickyNoteAddLine,
  13. } from '@remixicon/react'
  14. import { useKeyPress } from 'ahooks'
  15. import {
  16. useNodesReadOnly,
  17. useSelectionInteractions,
  18. useWorkflow,
  19. } from '../hooks'
  20. import { isEventTargetInputArea } from '../utils'
  21. import { useStore } from '../store'
  22. import AddBlock from './add-block'
  23. import TipPopup from './tip-popup'
  24. import { useOperator } from './hooks'
  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. const addNote = (e: MouseEvent<HTMLDivElement>) => {
  73. if (getNodesReadOnly())
  74. return
  75. e.stopPropagation()
  76. handleAddNote()
  77. }
  78. return (
  79. <div className='flex items-center p-0.5 rounded-lg border-[0.5px] border-gray-100 bg-white shadow-lg text-gray-500'>
  80. <AddBlock />
  81. <TipPopup title={t('workflow.nodes.note.addNote')}>
  82. <div
  83. className={cn(
  84. 'flex items-center justify-center ml-[1px] w-8 h-8 rounded-lg hover:bg-black/5 hover:text-gray-700 cursor-pointer',
  85. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  86. )}
  87. onClick={addNote}
  88. >
  89. <RiStickyNoteAddLine className='w-4 h-4' />
  90. </div>
  91. </TipPopup>
  92. <div className='mx-[3px] w-[1px] h-3.5 bg-gray-200'></div>
  93. <TipPopup title={t('workflow.common.pointerMode')}>
  94. <div
  95. className={cn(
  96. 'flex items-center justify-center mr-[1px] w-8 h-8 rounded-lg cursor-pointer',
  97. controlMode === 'pointer' ? 'bg-primary-50 text-primary-600' : 'hover:bg-black/5 hover:text-gray-700',
  98. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  99. )}
  100. onClick={handleModePointer}
  101. >
  102. <RiCursorLine className='w-4 h-4' />
  103. </div>
  104. </TipPopup>
  105. <TipPopup title={t('workflow.common.handMode')}>
  106. <div
  107. className={cn(
  108. 'flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer',
  109. controlMode === 'hand' ? 'bg-primary-50 text-primary-600' : 'hover:bg-black/5 hover:text-gray-700',
  110. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  111. )}
  112. onClick={handleModeHand}
  113. >
  114. <RiHand className='w-4 h-4' />
  115. </div>
  116. </TipPopup>
  117. <div className='mx-[3px] w-[1px] h-3.5 bg-gray-200'></div>
  118. <TipPopup title={t('workflow.panel.organizeBlocks')}>
  119. <div
  120. className={cn(
  121. 'flex items-center justify-center w-8 h-8 rounded-lg hover:bg-black/5 hover:text-gray-700 cursor-pointer',
  122. `${nodesReadOnly && '!cursor-not-allowed opacity-50'}`,
  123. )}
  124. onClick={goLayout}
  125. >
  126. <RiFunctionAddLine className='w-4 h-4' />
  127. </div>
  128. </TipPopup>
  129. </div>
  130. )
  131. }
  132. export default memo(Control)