panel-contextmenu.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import {
  2. memo,
  3. useEffect,
  4. useRef,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { useClickAway } from 'ahooks'
  8. import ShortcutsName from './shortcuts-name'
  9. import { useStore } from './store'
  10. import {
  11. useDSL,
  12. useNodesInteractions,
  13. usePanelInteractions,
  14. useWorkflowStartRun,
  15. } from './hooks'
  16. import AddBlock from './operator/add-block'
  17. import { useOperator } from './operator/hooks'
  18. import cn from '@/utils/classnames'
  19. const PanelContextmenu = () => {
  20. const { t } = useTranslation()
  21. const ref = useRef(null)
  22. const panelMenu = useStore(s => s.panelMenu)
  23. const clipboardElements = useStore(s => s.clipboardElements)
  24. const setShowImportDSLModal = useStore(s => s.setShowImportDSLModal)
  25. const { handleNodesPaste } = useNodesInteractions()
  26. const { handlePaneContextmenuCancel, handleNodeContextmenuCancel } = usePanelInteractions()
  27. const { handleStartWorkflowRun } = useWorkflowStartRun()
  28. const { handleAddNote } = useOperator()
  29. const { exportCheck } = useDSL()
  30. useEffect(() => {
  31. if (panelMenu)
  32. handleNodeContextmenuCancel()
  33. }, [panelMenu, handleNodeContextmenuCancel])
  34. useClickAway(() => {
  35. handlePaneContextmenuCancel()
  36. }, ref)
  37. const renderTrigger = () => {
  38. return (
  39. <div
  40. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  41. >
  42. {t('workflow.common.addBlock')}
  43. </div>
  44. )
  45. }
  46. if (!panelMenu)
  47. return null
  48. return (
  49. <div
  50. className='absolute w-[200px] rounded-lg border-[0.5px] border-gray-200 bg-white shadow-xl z-[9]'
  51. style={{
  52. left: panelMenu.left,
  53. top: panelMenu.top,
  54. }}
  55. ref={ref}
  56. >
  57. <div className='p-1'>
  58. <AddBlock
  59. renderTrigger={renderTrigger}
  60. offset={{
  61. mainAxis: -36,
  62. crossAxis: -4,
  63. }}
  64. />
  65. <div
  66. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  67. onClick={(e) => {
  68. e.stopPropagation()
  69. handleAddNote()
  70. handlePaneContextmenuCancel()
  71. }}
  72. >
  73. {t('workflow.nodes.note.addNote')}
  74. </div>
  75. <div
  76. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  77. onClick={() => {
  78. handleStartWorkflowRun()
  79. handlePaneContextmenuCancel()
  80. }}
  81. >
  82. {t('workflow.common.run')}
  83. <ShortcutsName keys={['alt', 'r']} />
  84. </div>
  85. </div>
  86. <div className='h-[1px] bg-gray-100'></div>
  87. <div className='p-1'>
  88. <div
  89. className={cn(
  90. 'flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer',
  91. !clipboardElements.length ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-50',
  92. )}
  93. onClick={() => {
  94. if (clipboardElements.length) {
  95. handleNodesPaste()
  96. handlePaneContextmenuCancel()
  97. }
  98. }}
  99. >
  100. {t('workflow.common.pasteHere')}
  101. <ShortcutsName keys={['ctrl', 'v']} />
  102. </div>
  103. </div>
  104. <div className='h-[1px] bg-gray-100'></div>
  105. <div className='p-1'>
  106. <div
  107. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  108. onClick={() => exportCheck()}
  109. >
  110. {t('app.export')}
  111. </div>
  112. <div
  113. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  114. onClick={() => setShowImportDSLModal(true)}
  115. >
  116. {t('workflow.common.importDSL')}
  117. </div>
  118. </div>
  119. </div>
  120. )
  121. }
  122. export default memo(PanelContextmenu)