feat(@vben/common-ui): 优化文档预览和下载功能

This commit is contained in:
Kven 2025-05-06 12:28:05 +08:00
parent a08474d32c
commit 33d8241ce4
2 changed files with 46 additions and 9 deletions

View File

@ -7,8 +7,8 @@ import VueOfficeDocx from '@vue-office/docx';
import '@vue-office/docx/lib/index.css';
const url = ref('');
const docx = ref<any>('/docx/027c6b7c-fea6-4964-839b-27857c4d3181.docx');
// const url = ref('');
const docx = ref<any>(`/docx/027c6b7c-fea6-4964-839b-27857c4d3181.docx`);
const pptStyle = ref({
height: 'calc(100vh - 100px)',
width: '100%',
@ -29,7 +29,11 @@ const [Drawer, drawerApi] = useVbenDrawer({
},
onOpenChange(isOpen: boolean) {
if (isOpen) {
url.value = drawerApi.getData<Record<string, any>>();
const data = drawerApi.getData<Record<string, any>>();
if (data) {
docx.value = `/docx/${data}`; // docx
}
// url.value = drawerApi.getData<Record<string, any>>();
}
},
});

View File

@ -67,9 +67,12 @@ const [PreviewDrawer, previewDrawerApi] = useVbenDrawer({
connectedComponent: WordPreview,
// placement: 'left',
});
function openPreviewDrawer(placement: DrawerPlacement = 'right') {
previewDrawerApi.setState({ placement }).open();
function openPreviewDrawer(
placement: DrawerPlacement = 'right',
filename?: string,
) {
const fileData = filename.value;
previewDrawerApi.setState({ placement }).setData(fileData).open();
}
const startFetching = async () => {
@ -111,7 +114,29 @@ const startFetching = async () => {
content: value.value || '',
},
);
fetchResult.value = '/static/9e1c421e-991c-411f-8176-6350a97e70f3.docx';
const { answer } = res;
// .docx URL
function isDocxURL(str: string): boolean {
return str.endsWith('.docx');
}
// 使 /static/
function extractDocxFilename(url: string): null | string {
const match = url.match(/\/static\/([a-f0-9-]+\.docx)$/i);
if (match) {
return match[1]; // 9e1c421e-991c-411f-8176-6350a97e70f3.docx
}
return null;
}
const filename = ref('');
if (answer && isDocxURL(answer)) {
filename.value = extractDocxFilename(answer);
}
fetchResult.value = `/static/${filename.value}`;
resultItems.value.push({
key: resultItems.value.length + 1,
role: 'ai',
@ -123,7 +148,7 @@ const startFetching = async () => {
size: 'nomarl',
type: 'primary',
onClick: () => {
openPreviewDrawer('right');
openPreviewDrawer('right', filename);
},
},
'文档预览',
@ -136,7 +161,15 @@ const startFetching = async () => {
style: {
marginLeft: '10px',
},
onClick: () => {},
onClick: () => {
// <a>
const link = document.createElement('a');
link.href = answer; //
link.download = filename; //
document.body.append(link); // <a>
link.click(); //
link.remove(); // <a>
},
},
'文档下载',
),