panel-contextmenu.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {
  2. memo,
  3. useRef,
  4. } from 'react'
  5. import cn from 'classnames'
  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. useNodesInteractions,
  12. usePanelInteractions,
  13. useWorkflowStartRun,
  14. } from './hooks'
  15. import AddBlock from './operator/add-block'
  16. import { useOperator } from './operator/hooks'
  17. import { exportAppConfig } from '@/service/apps'
  18. import { useToastContext } from '@/app/components/base/toast'
  19. import { useStore as useAppStore } from '@/app/components/app/store'
  20. const PanelContextmenu = () => {
  21. const { t } = useTranslation()
  22. const { notify } = useToastContext()
  23. const ref = useRef(null)
  24. const panelMenu = useStore(s => s.panelMenu)
  25. const clipboardElements = useStore(s => s.clipboardElements)
  26. const appDetail = useAppStore(s => s.appDetail)
  27. const { handleNodesPaste } = useNodesInteractions()
  28. const { handlePaneContextmenuCancel } = usePanelInteractions()
  29. const { handleStartWorkflowRun } = useWorkflowStartRun()
  30. const { handleAddNote } = useOperator()
  31. useClickAway(() => {
  32. handlePaneContextmenuCancel()
  33. }, ref)
  34. const onExport = async () => {
  35. if (!appDetail)
  36. return
  37. try {
  38. const { data } = await exportAppConfig(appDetail.id)
  39. const a = document.createElement('a')
  40. const file = new Blob([data], { type: 'application/yaml' })
  41. a.href = URL.createObjectURL(file)
  42. a.download = `${appDetail.name}.yml`
  43. a.click()
  44. }
  45. catch (e) {
  46. notify({ type: 'error', message: t('app.exportFailed') })
  47. }
  48. }
  49. const renderTrigger = () => {
  50. return (
  51. <div
  52. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  53. >
  54. {t('workflow.common.addBlock')}
  55. </div>
  56. )
  57. }
  58. if (!panelMenu)
  59. return null
  60. return (
  61. <div
  62. className='absolute w-[200px] rounded-lg border-[0.5px] border-gray-200 bg-white shadow-xl z-[9]'
  63. style={{
  64. left: panelMenu.left,
  65. top: panelMenu.top,
  66. }}
  67. ref={ref}
  68. >
  69. <div className='p-1'>
  70. <AddBlock
  71. renderTrigger={renderTrigger}
  72. offset={{
  73. mainAxis: -36,
  74. crossAxis: -4,
  75. }}
  76. />
  77. <div
  78. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  79. onClick={(e) => {
  80. e.stopPropagation()
  81. handleAddNote()
  82. handlePaneContextmenuCancel()
  83. }}
  84. >
  85. {t('workflow.nodes.note.addNote')}
  86. </div>
  87. <div
  88. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  89. onClick={() => {
  90. handleStartWorkflowRun()
  91. handlePaneContextmenuCancel()
  92. }}
  93. >
  94. {t('workflow.common.run')}
  95. <ShortcutsName keys={['alt', 'r']} />
  96. </div>
  97. </div>
  98. <div className='h-[1px] bg-gray-100'></div>
  99. <div className='p-1'>
  100. <div
  101. className={cn(
  102. 'flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer',
  103. !clipboardElements.length ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-50',
  104. )}
  105. onClick={() => {
  106. if (clipboardElements.length) {
  107. handleNodesPaste()
  108. handlePaneContextmenuCancel()
  109. }
  110. }}
  111. >
  112. {t('workflow.common.pasteHere')}
  113. <ShortcutsName keys={['ctrl', 'v']} />
  114. </div>
  115. </div>
  116. <div className='h-[1px] bg-gray-100'></div>
  117. <div className='p-1'>
  118. <div
  119. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  120. onClick={() => onExport()}
  121. >
  122. {t('app.export')}
  123. </div>
  124. </div>
  125. </div>
  126. )
  127. }
  128. export default memo(PanelContextmenu)