Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sdream-ai-fe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
侯明涛
sdream-ai-fe
Commits
eb51f0f7
Commit
eb51f0f7
authored
Sep 08, 2025
by
weiw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:解决点击pdf再点击docx预览会下载的bug
parent
22f91f0c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
6 deletions
+30
-6
src/components/FilePreviewModal/index.tsx
+30
-6
No files found.
src/components/FilePreviewModal/index.tsx
View file @
eb51f0f7
...
@@ -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]'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment