control.tsx 3.8 KB

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