mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-20 06:34:55 +00:00
feat(chat-sdk/chatitem): 消息支持导出图表图片 (#1937)
This commit is contained in:
3
webapp/packages/chat-sdk/src/hooks/index.ts
Normal file
3
webapp/packages/chat-sdk/src/hooks/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './useMethodRegister';
|
||||
export * from './useComposing';
|
||||
export * from './useExportByEcharts';
|
||||
41
webapp/packages/chat-sdk/src/hooks/useExportByEcharts.ts
Normal file
41
webapp/packages/chat-sdk/src/hooks/useExportByEcharts.ts
Normal 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 };
|
||||
};
|
||||
25
webapp/packages/chat-sdk/src/hooks/useMethodRegister.ts
Normal file
25
webapp/packages/chat-sdk/src/hooks/useMethodRegister.ts
Normal 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 };
|
||||
};
|
||||
Reference in New Issue
Block a user