feat(chat-sdk/chatitem): 消息支持导出图表图片 (#1937)

This commit is contained in:
pisces
2024-12-23 09:05:56 +08:00
committed by GitHub
parent 5de5b0a5e2
commit 642d6a02e1
9 changed files with 274 additions and 124 deletions

View File

@@ -0,0 +1,3 @@
export * from './useMethodRegister';
export * from './useComposing';
export * from './useExportByEcharts';

View File

@@ -0,0 +1,41 @@
import { message } from 'antd';
import { ECharts } from 'echarts';
export interface ExportByEchartsProps {
instanceRef: React.MutableRefObject<ECharts | undefined>;
question: string;
options?: Parameters<ECharts['getConnectedDataURL']>[0];
}
export const useExportByEcharts = ({ instanceRef, question, options }: ExportByEchartsProps) => {
const handleSaveAsImage = () => {
if (instanceRef.current) {
return instanceRef.current.getConnectedDataURL({
type: 'png',
pixelRatio: 2,
backgroundColor: '#fff',
excludeComponents: ['toolbox'],
...options,
});
}
};
const downloadImage = (url: string) => {
const a = document.createElement('a');
a.href = url;
a.download = `${question}.png`;
a.click();
};
const downloadChartAsImage = () => {
const url = handleSaveAsImage();
if (url) {
downloadImage(url);
message.success('导出图片成功');
} else {
message.error('该条消息暂不支持导出图片');
}
};
return { downloadChartAsImage };
};

View File

@@ -0,0 +1,25 @@
import { useCallback, useRef } from 'react';
export const useMethodRegister = (fallback?: (...args: any[]) => any) => {
const methodStore = useRef<Map<string, (...args: any[]) => any>>(new Map());
const register = useCallback<(key: string, method: (...args: any[]) => any) => any>(
(key, method) => {
methodStore.current.set(key, method);
},
[methodStore]
);
const call = useCallback<(key: string, ...args: any[]) => any>(
(key, ...args) => {
const method = methodStore.current.get(key);
if (method) {
return method(...args);
}
return fallback?.(...args);
},
[methodStore]
);
return { register, call };
};