@@ -4,6 +4,8 @@ import "react-pdf/dist/Page/AnnotationLayer.css";
4
4
import "react-pdf/dist/Page/TextLayer.css" ;
5
5
import { DocumentViewerContainerProps } from "../typings/DocumentViewerProps" ;
6
6
import { DocRendererElement } from "./documentRenderer" ;
7
+ import { useZoomScale } from "../utils/useZoomScale" ;
8
+ import BaseViewer from "./BaseViewer" ;
7
9
pdfjs . GlobalWorkerOptions . workerSrc = `//unpkg.com/pdfjs-dist@${ pdfjs . version } /build/pdf.worker.min.mjs` ;
8
10
const options = {
9
11
cMapUrl : `https://unpkg.com/pdfjs-dist@${ pdfjs . version } /cmaps/` ,
@@ -13,28 +15,30 @@ const options = {
13
15
const PDFViewer : DocRendererElement = ( props : DocumentViewerContainerProps ) => {
14
16
const { file } = props ;
15
17
const [ numberOfPages , setNumberOfPages ] = useState < number > ( 1 ) ;
18
+ const { zoomLevel, zoomIn, zoomOut } = useZoomScale ( ) ;
16
19
const [ currentPage , setCurrentPage ] = useState < number > ( 1 ) ;
17
20
const [ pdfUrl , setPdfUrl ] = useState < string | null > ( null ) ;
18
21
19
- if ( ! file . value ?. uri ) {
20
- return < div > No document selected</ div > ;
21
- }
22
-
23
22
useEffect ( ( ) => {
24
23
if ( file . status === "available" && file . value . uri ) {
25
24
setPdfUrl ( file . value . uri ) ;
26
25
}
27
- } , [ file , file . status , file . value . uri ] ) ;
26
+ } , [ file , file . status , file . value ? .uri ] ) ;
28
27
29
28
function onDocumentLoadSuccess ( { numPages } : { numPages : number } ) : void {
30
29
setNumberOfPages ( numPages ) ;
31
30
}
32
31
32
+ if ( ! file . value ?. uri ) {
33
+ return < div > No document selected</ div > ;
34
+ }
35
+
33
36
return (
34
- < Fragment >
35
- < div className = "widget-document-viewer-controls" >
36
- < div className = "widget-document-viewer-controls-left" > { file . value ?. name } </ div >
37
- < div className = "widget-document-viewer-controls-icons" >
37
+ < BaseViewer
38
+ { ...props }
39
+ fileName = { file . value ?. name || "" }
40
+ CustomControl = {
41
+ < Fragment >
38
42
< div className = "widget-document-viewer-pagination" >
39
43
< button
40
44
onClick = { ( ) => setCurrentPage ( prev => Math . max ( prev - 1 , 1 ) ) }
@@ -51,19 +55,32 @@ const PDFViewer: DocRendererElement = (props: DocumentViewerContainerProps) => {
51
55
aria-label = { "Go to next page" }
52
56
> </ button >
53
57
</ div >
54
- </ div >
55
- </ div >
56
- < div className = "widget-document-viewer-content" >
57
- { pdfUrl && (
58
- < Document file = { pdfUrl } options = { options } onLoadSuccess = { onDocumentLoadSuccess } >
59
- < Page pageNumber = { currentPage } />
60
- </ Document >
61
- ) }
62
- </ div >
63
- </ Fragment >
58
+ < div className = "widget-document-viewer-zoom" >
59
+ < button
60
+ onClick = { zoomOut }
61
+ disabled = { zoomLevel <= 0.3 }
62
+ className = "icons icon-ZoomOut btn btn-icon-only"
63
+ aria-label = { "Go to previous page" }
64
+ > </ button >
65
+ < button
66
+ onClick = { zoomIn }
67
+ disabled = { zoomLevel >= 10 }
68
+ className = "icons icon-ZoomIn btn btn-icon-only"
69
+ aria-label = { "Go to previous page" }
70
+ > </ button >
71
+ </ div >
72
+ </ Fragment >
73
+ }
74
+ >
75
+ < Document file = { pdfUrl } options = { options } onLoadSuccess = { onDocumentLoadSuccess } >
76
+ < Page pageNumber = { currentPage } scale = { zoomLevel } />
77
+ </ Document >
78
+ </ BaseViewer >
64
79
) ;
65
80
} ;
66
81
67
- PDFViewer . contentTypes = [ "application/pdf" , "application/x-pdf" , "application/acrobat" , "text/pdf" ] ;
82
+ PDFViewer . contentTypes = [ "application/pdf" , "application/x-pdf" , "application/acrobat" , "text/pdf" , "text/html" ] ;
83
+
84
+ PDFViewer . fileTypes = [ "pdf" , "pdfx" ] ;
68
85
69
86
export default PDFViewer ;
0 commit comments