Commit eb51f0f7 by weiw

fix:解决点击pdf再点击docx预览会下载的bug

parent 22f91f0c
...@@ -16,11 +16,14 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl ...@@ -16,11 +16,14 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const [fileType, setFileType] = useState<string>('') const [fileType, setFileType] = useState<string>('')
// 确定文件类型 // 确定文件类型 - 改进版本
useEffect(() => { useEffect(() => {
// 重置状态
setFileType('')
if (doc?.documentAlias) { if (doc?.documentAlias) {
const name = doc.documentAlias.toLowerCase() const name = doc.documentAlias.toLowerCase().trim()
// 添加更严格的文件类型判断
if (name.endsWith('.pdf')) { if (name.endsWith('.pdf')) {
setFileType('pdf') setFileType('pdf')
} }
...@@ -30,14 +33,24 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl ...@@ -30,14 +33,24 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
else if (name.endsWith('.doc')) { else if (name.endsWith('.doc')) {
setFileType('doc') setFileType('doc')
} }
else if (name.endsWith('.xlsx') || name.endsWith('.xls')) { else if (name.endsWith('.xlsx')) {
setFileType('xlsx') setFileType('xlsx')
} }
else if (name.endsWith('.xls')) {
setFileType('xls')
}
else { else {
setFileType('') setFileType('')
} }
} }
}, [doc]) }, [doc, isOpen]) // 添加 isOpen 依赖确保每次打开时重新判断
// 当文件类型或URL变化时重置loading状态
useEffect(() => {
if (fileType && docUrl) {
setLoading(true)
}
}, [fileType, docUrl])
const handleDownload = () => { const handleDownload = () => {
if (docUrl) { if (docUrl) {
...@@ -74,6 +87,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl ...@@ -74,6 +87,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
return ( return (
<div className="h-[70vh] max-h-[70vh] overflow-auto"> <div className="h-[70vh] max-h-[70vh] overflow-auto">
<DocxPreview <DocxPreview
key={docUrl} // 添加key确保组件重新挂载
src={docUrl} src={docUrl}
className="w-full min-h-full" className="w-full min-h-full"
onRendered={handleDocumentRendered} onRendered={handleDocumentRendered}
...@@ -103,6 +117,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl ...@@ -103,6 +117,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
return ( return (
<div className="h-[70vh] max-h-[70vh] overflow-auto"> <div className="h-[70vh] max-h-[70vh] overflow-auto">
<ExcelPreview <ExcelPreview
key={docUrl} // 添加key确保组件重新挂载
src={docUrl} src={docUrl}
options={{ xls: true }} options={{ xls: true }}
className="w-full min-h-full" className="w-full min-h-full"
...@@ -116,6 +131,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl ...@@ -116,6 +131,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
return ( return (
<div className="h-[70vh] max-h-[70vh] overflow-auto"> <div className="h-[70vh] max-h-[70vh] overflow-auto">
<ExcelPreview <ExcelPreview
key={docUrl} // 添加key确保组件重新挂载
src={docUrl} src={docUrl}
className="w-full min-h-full" className="w-full min-h-full"
onRendered={handleDocumentRendered} onRendered={handleDocumentRendered}
...@@ -128,6 +144,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl ...@@ -128,6 +144,7 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
return ( return (
<div className="h-[70vh] max-h-[70vh] overflow-auto"> <div className="h-[70vh] max-h-[70vh] overflow-auto">
<PdfPreview <PdfPreview
key={docUrl} // 添加key确保组件重新挂载
src={docUrl} src={docUrl}
className="w-full min-h-full" className="w-full min-h-full"
onLoaded={handleDocumentRendered} onLoaded={handleDocumentRendered}
...@@ -150,10 +167,17 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl ...@@ -150,10 +167,17 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
} }
} }
// 模态框关闭时重置状态
const handleClose = () => {
setLoading(false)
setFileType('')
onClose()
}
return ( return (
<Modal <Modal
isOpen={isOpen} isOpen={isOpen}
onClose={onClose} onClose={handleClose} // 使用改进的关闭函数
size="3xl" size="3xl"
classNames={{ classNames={{
base: 'max-h-[90vh] max-w-[50vw]', base: 'max-h-[90vh] max-w-[50vw]',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment