panel-contextmenu.tsx 3.7 KB

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