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
22f91f0c
Commit
22f91f0c
authored
Sep 08, 2025
by
weiw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:修改pdf的预览方式
parent
643d1d7d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
90 deletions
+36
-90
src/components/FilePreviewModal/PdfPreview.tsx
+19
-77
src/components/FilePreviewModal/index.tsx
+17
-13
No files found.
src/components/FilePreviewModal/PdfPreview.tsx
View file @
22f91f0c
// src/components/FilePreviewModal/PdfPreview.tsx
// src/components/FilePreviewModal/PdfPreview.tsx
import
React
,
{
useState
}
from
'react'
import
React
from
'react'
import
{
Document
,
Page
,
pdfjs
}
from
'react-pdf'
// 设置 PDF.js worker
pdfjs
.
GlobalWorkerOptions
.
workerSrc
=
`//cdnjs.cloudflare.com/ajax/libs/pdf.js/
${
pdfjs
.
version
}
/pdf.worker.min.js`
interface
PdfPreviewProps
{
interface
PdfPreviewProps
{
src
:
string
src
:
string
...
@@ -12,85 +8,31 @@ interface PdfPreviewProps {
...
@@ -12,85 +8,31 @@ interface PdfPreviewProps {
onError
?:
(
error
:
any
)
=>
void
onError
?:
(
error
:
any
)
=>
void
}
}
export
const
PdfPreview
:
React
.
FC
<
PdfPreviewProps
>
=
({
src
,
className
=
''
,
onLoaded
,
onError
})
=>
{
export
const
PdfPreview
:
React
.
FC
<
PdfPreviewProps
>
=
({
const
[
numPages
,
setNumPages
]
=
useState
<
number
|
null
>
(
null
)
src
,
const
[
pageNumber
,
setPageNumber
]
=
useState
<
number
>
(
1
)
className
=
''
,
const
[
containerWidth
,
setContainerWidth
]
=
useState
<
number
>
(
800
)
onLoaded
,
onError
,
function
onDocumentLoadSuccess
({
numPages
}:
{
numPages
:
number
})
{
})
=>
{
setNumPages
(
numPages
)
const
handleLoad
=
()
=>
{
setPageNumber
(
1
)
onLoaded
?.()
onLoaded
?.()
}
}
function
changePage
(
offset
:
number
)
{
const
handleError
=
(
_e
:
React
.
SyntheticEvent
)
=>
{
setPageNumber
(
prevPageNumber
=>
prevPageNumber
+
offset
)
onError
?.(
new
Error
(
'PDF加载失败'
))
}
function
previousPage
()
{
changePage
(
-
1
)
}
function
nextPage
()
{
changePage
(
1
)
}
}
return
(
return
(
<
div
className=
{
`${className} flex flex-col h-full`
}
>
<
div
className=
{
`${className} h-full flex flex-col`
}
>
<
div
className=
"flex-grow overflow-auto flex items-center justify-center bg-gray-50 p-2"
>
<
div
className=
"flex-grow overflow-hidden bg-gray-100"
>
<
div
<
embed
className=
"w-full flex justify-center"
src=
{
src
}
ref=
{
(
el
)
=>
{
type=
"application/pdf"
if
(
el
)
{
className=
"w-full h-full border-0"
setContainerWidth
(
el
.
clientWidth
)
onLoad=
{
handleLoad
}
}
onError=
{
handleError
}
}
}
/>
>
<
Document
file=
{
src
}
onLoadSuccess=
{
onDocumentLoadSuccess
}
onLoadError=
{
onError
}
className=
"flex flex-col items-center"
>
<
Page
pageNumber=
{
pageNumber
}
width=
{
Math
.
min
(
containerWidth
-
32
,
800
)
}
// 减去padding
className=
"shadow-md"
/>
</
Document
>
</
div
>
</
div
>
</
div
>
{
numPages
!==
null
&&
numPages
>
1
&&
(
<
div
className=
"pdf-navigation flex justify-center items-center py-3 bg-white border-t"
>
<
button
type=
"button"
// 添加 type 属性
onClick=
{
previousPage
}
disabled=
{
pageNumber
<=
1
}
className=
"px-3 py-1 mx-1 bg-gray-200 rounded disabled:opacity-50 text-sm"
>
上一页
</
button
>
<
span
className=
"mx-3 text-sm"
>
第
{
' '
}
{
pageNumber
}
{
' '
}
页,共
{
' '
}
{
numPages
}
{
' '
}
页
</
span
>
<
button
type=
"button"
// 添加 type 属性
onClick=
{
nextPage
}
disabled=
{
pageNumber
>=
numPages
}
className=
"px-3 py-1 mx-1 bg-gray-200 rounded disabled:opacity-50 text-sm"
>
下一页
</
button
>
</
div
>
)
}
</
div
>
</
div
>
)
)
}
}
src/components/FilePreviewModal/index.tsx
View file @
22f91f0c
...
@@ -20,17 +20,22 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
...
@@ -20,17 +20,22 @@ export const FilePreviewModal: React.FC<FilePreviewModalProps> = ({ isOpen, onCl
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
doc
?.
documentAlias
)
{
if
(
doc
?.
documentAlias
)
{
const
name
=
doc
.
documentAlias
.
toLowerCase
()
const
name
=
doc
.
documentAlias
.
toLowerCase
()
if
(
name
.
endsWith
(
'.docx'
))
// 添加更严格的文件类型判断
setFileType
(
'docx'
)
if
(
name
.
endsWith
(
'.pdf'
))
{
else
if
(
name
.
endsWith
(
'.doc'
))
setFileType
(
'pdf'
)
setFileType
(
'doc'
)
}
else
if
(
name
.
endsWith
(
'.xlsx'
))
else
if
(
name
.
endsWith
(
'.docx'
))
{
setFileType
(
'xlsx'
)
setFileType
(
'docx'
)
else
if
(
name
.
endsWith
(
'.xls'
))
}
setFileType
(
'xls'
)
else
if
(
name
.
endsWith
(
'.doc'
))
{
else
if
(
name
.
endsWith
(
'.pdf'
))
setFileType
(
'doc'
)
setFileType
(
'pdf'
)
}
else
setFileType
(
''
)
else
if
(
name
.
endsWith
(
'.xlsx'
)
||
name
.
endsWith
(
'.xls'
))
{
setFileType
(
'xlsx'
)
}
else
{
setFileType
(
''
)
}
}
}
},
[
doc
])
},
[
doc
])
...
@@ -169,8 +174,7 @@ setFileType('pdf')
...
@@ -169,8 +174,7 @@ setFileType('pdf')
)
}
)
}
{
renderPreview
()
}
{
renderPreview
()
}
</
ModalBody
>
</
ModalBody
>
<
ModalFooter
className=
"flex justify-end gap-2"
>
<
ModalFooter
className=
"flex justify-end gap-2"
></
ModalFooter
>
</
ModalFooter
>
</
ModalContent
>
</
ModalContent
>
</
Modal
>
</
Modal
>
)
)
...
...
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