|
1 | 1 | import { FileImageOutlined } from '@ant-design/icons'; |
| 2 | +import { FlowDownloadFormat, FlowDownloadService } from '@flowgram.ai/export-plugin'; |
| 3 | +import { usePlayground, useService } from '@flowgram.ai/free-layout-editor'; |
2 | 4 | import type { MenuProps } from 'antd'; |
3 | | -import { Button, Dropdown, Tooltip } from 'antd'; |
| 5 | +import { App, Button, Dropdown, Tooltip } from 'antd'; |
| 6 | +import { useEffect, useState } from 'react'; |
4 | 7 |
|
5 | 8 | /** |
6 | 9 | * 导出图片组件 |
7 | 10 | * @returns |
8 | 11 | */ |
9 | 12 | const ExportImage: React.FC = () => { |
| 13 | + const { message } = App.useApp(); |
| 14 | + // 正在导出中 |
| 15 | + const [isExporting, setIsExporting] = useState(false); |
| 16 | + const playground = usePlayground(); |
| 17 | + const { readonly } = playground.config; |
| 18 | + const downloadService = useService(FlowDownloadService); |
| 19 | + |
| 20 | + // 监听导出进度 |
| 21 | + useEffect(() => { |
| 22 | + const subscription = downloadService.onDownloadingChange((v) => { |
| 23 | + setIsExporting(v); |
| 24 | + }); |
| 25 | + return () => subscription.dispose(); |
| 26 | + }, [downloadService, readonly]); |
| 27 | + |
| 28 | + // 处理下载导出 |
| 29 | + const handleDownload = async (format: FlowDownloadFormat) => { |
| 30 | + await downloadService.download({ format }); |
| 31 | + const formatOption = exportImageMenuItems?.find((option) => option?.key === format); |
| 32 | + if (formatOption) { |
| 33 | + message.success(`导出${formatOption.key}成功`); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
10 | 37 | // 导出图片菜单项 |
11 | 38 | const exportImageMenuItems: MenuProps['items'] = [ |
12 | 39 | { |
13 | | - key: 'current-view-png', |
| 40 | + key: 'png', |
14 | 41 | label: '导出为 PNG', |
15 | | - onClick: () => console.log('导出当前视图为 PNG'), |
| 42 | + onClick: () => handleDownload(FlowDownloadFormat.PNG), |
16 | 43 | }, |
17 | 44 | { |
18 | | - key: 'current-view-jpeg', |
| 45 | + key: 'jpeg', |
19 | 46 | label: '导出为 JPEG', |
20 | | - onClick: () => console.log('导出当前视图为 JPEG'), |
| 47 | + onClick: () => handleDownload(FlowDownloadFormat.JPEG), |
21 | 48 | }, |
22 | 49 | { |
23 | | - key: 'current-view-svg', |
| 50 | + key: 'svg', |
24 | 51 | label: '导出为 SVG', |
25 | | - onClick: () => console.log('导出当前视图为 SVG'), |
| 52 | + onClick: () => handleDownload(FlowDownloadFormat.SVG), |
| 53 | + }, |
| 54 | + { |
| 55 | + key: 'json', |
| 56 | + label: '导出为 JSON', |
| 57 | + onClick: () => handleDownload(FlowDownloadFormat.JSON), |
| 58 | + }, |
| 59 | + { |
| 60 | + key: 'yaml', |
| 61 | + label: '导出为 YAML', |
| 62 | + onClick: () => handleDownload(FlowDownloadFormat.YAML), |
26 | 63 | }, |
27 | 64 | ]; |
28 | 65 |
|
29 | 66 | return ( |
30 | | - <Tooltip title="导出图片" color="white"> |
| 67 | + <Tooltip title="导出" color="white"> |
31 | 68 | <Dropdown |
32 | 69 | menu={{ |
33 | 70 | items: exportImageMenuItems, |
34 | 71 | }} |
| 72 | + disabled={isExporting} |
35 | 73 | trigger={['click']} |
36 | 74 | placement="bottomRight" |
37 | 75 | > |
|
0 commit comments