control.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import type { MouseEvent } from 'react'
  2. import {
  3. memo,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. RiCursorLine,
  8. RiFunctionAddLine,
  9. RiHand,
  10. RiStickyNoteAddLine,
  11. } from '@remixicon/react'
  12. import {
  13. useNodesReadOnly,
  14. useWorkflowMoveMode,
  15. useWorkflowOrganize,
  16. } from '../hooks'
  17. import {
  18. ControlMode,
  19. } from '../types'
  20. import { useStore } from '../store'
  21. import Divider from '../../base/divider'
  22. import AddBlock from './add-block'
  23. import TipPopup from './tip-popup'
  24. import { useOperator } from './hooks'
  25. import cn from '@/utils/classnames'
  26. import { useAppDetailContext } from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/layout-main'
  27. const Control = () => {
  28. const { detail, isCreate, isEdit, isOperation } = useAppDetailContext()
  29. const { t } = useTranslation()
  30. const controlMode = useStore(s => s.controlMode)
  31. const { handleModePointer, handleModeHand } = useWorkflowMoveMode()
  32. const { handleLayout } = useWorkflowOrganize()
  33. const { handleAddNote } = useOperator()
  34. const {
  35. nodesReadOnly,
  36. getNodesReadOnly,
  37. } = useNodesReadOnly()
  38. const addNote = (e: MouseEvent<HTMLDivElement>) => {
  39. if (getNodesReadOnly())
  40. return
  41. e.stopPropagation()
  42. handleAddNote()
  43. }
  44. return (
  45. <div className='flex items-center rounded-lg border-[0.5px] border-components-actionbar-border bg-components-actionbar-bg p-0.5 text-text-tertiary shadow-lg'>
  46. {
  47. isEdit && (<>
  48. <AddBlock />
  49. <TipPopup title={t('workflow.nodes.note.addNote')}>
  50. <div
  51. className={cn(
  52. 'ml-[1px] flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-state-base-hover hover:text-text-secondary',
  53. `${nodesReadOnly && 'cursor-not-allowed text-text-disabled hover:bg-transparent hover:text-text-disabled'}`,
  54. )}
  55. onClick={addNote}
  56. >
  57. <RiStickyNoteAddLine className='h-4 w-4' />
  58. </div>
  59. </TipPopup>
  60. <Divider type='vertical' className='mx-0.5 h-3.5' />
  61. </>)
  62. }
  63. <TipPopup title={t('workflow.common.pointerMode')} shortcuts={['v']}>
  64. <div
  65. className={cn(
  66. 'mr-[1px] flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg',
  67. controlMode === ControlMode.Pointer ? 'bg-state-accent-active text-text-accent' : 'hover:bg-state-base-hover hover:text-text-secondary',
  68. `${nodesReadOnly && 'cursor-not-allowed text-text-disabled hover:bg-transparent hover:text-text-disabled'}`,
  69. )}
  70. onClick={handleModePointer}
  71. >
  72. <RiCursorLine className='h-4 w-4' />
  73. </div>
  74. </TipPopup>
  75. <TipPopup title={t('workflow.common.handMode')} shortcuts={['h']}>
  76. <div
  77. className={cn(
  78. 'flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg',
  79. controlMode === ControlMode.Hand ? 'bg-state-accent-active text-text-accent' : 'hover:bg-state-base-hover hover:text-text-secondary',
  80. `${nodesReadOnly && 'cursor-not-allowed text-text-disabled hover:bg-transparent hover:text-text-disabled'}`,
  81. )}
  82. onClick={handleModeHand}
  83. >
  84. <RiHand className='h-4 w-4' />
  85. </div>
  86. </TipPopup>
  87. <Divider type='vertical' className='mx-0.5 h-3.5' />
  88. <TipPopup title={t('workflow.panel.organizeBlocks')} shortcuts={['ctrl', 'o']}>
  89. <div
  90. className={cn(
  91. 'flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-state-base-hover hover:text-text-secondary',
  92. `${nodesReadOnly && 'cursor-not-allowed text-text-disabled hover:bg-transparent hover:text-text-disabled'}`,
  93. )}
  94. onClick={handleLayout}
  95. >
  96. <RiFunctionAddLine className='h-4 w-4' />
  97. </div>
  98. </TipPopup>
  99. </div>
  100. )
  101. }
  102. export default memo(Control)