|
1 |
| -import { createElement, ReactElement, useState } from "react"; |
| 1 | +import { createElement, ReactElement } from "react"; |
| 2 | +import { DocumentContext } from "../store"; |
2 | 3 | import { DocumentViewerContainerProps } from "../typings/DocumentViewerProps";
|
3 |
| -import { Document, Page, pdfjs } from "react-pdf"; |
4 |
| -import "react-pdf/dist/Page/AnnotationLayer.css"; |
5 |
| -import "react-pdf/dist/Page/TextLayer.css"; |
6 |
| - |
7 |
| -pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`; |
8 |
| -const options = { |
9 |
| - cMapUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/cmaps/`, |
10 |
| - standardFontDataUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/standard_fonts` |
11 |
| -}; |
| 4 | +import { useRendererSelector } from "../utils/useRendererSelector"; |
12 | 5 |
|
13 | 6 | export default function DocumentViewer(props: DocumentViewerContainerProps): ReactElement {
|
14 |
| - const [numberOfPages, setNumberOfPages] = useState<number>(1); |
15 |
| - const [currentPage, setCurrentPage] = useState<number>(1); |
16 |
| - const [pdfUrl, setPdfUrl] = useState<string | null>(null); |
17 |
| - |
18 |
| - // Load PDF when URL changes |
19 |
| - if (props.file.value?.uri && !pdfUrl) { |
20 |
| - setPdfUrl(props.file.value.uri); |
21 |
| - } |
22 |
| - |
23 |
| - if (!props.file.value?.uri) { |
24 |
| - return <div>No document selected</div>; |
25 |
| - } |
26 |
| - |
27 |
| - function onDocumentLoadSuccess({ numPages }: { numPages: number }): void { |
28 |
| - setNumberOfPages(numPages); |
29 |
| - } |
| 7 | + const { CurrentRenderer } = useRendererSelector(props); |
30 | 8 |
|
31 | 9 | return (
|
32 |
| - <div className="widget-document-viewer"> |
33 |
| - <div className="widget-document-viewer-controls"> |
34 |
| - <button onClick={() => setCurrentPage(prev => Math.max(prev - 1, 1))} disabled={currentPage <= 1}> |
35 |
| - Previous |
36 |
| - </button> |
37 |
| - <span> |
38 |
| - Page {currentPage} of {numberOfPages} |
39 |
| - </span> |
40 |
| - <button onClick={() => setCurrentPage(prev => Math.min(prev + 1, numberOfPages))}>Next</button> |
41 |
| - </div> |
42 |
| - <div className="widget-document-viewer-content"> |
43 |
| - <Document file={pdfUrl} options={options} onLoadSuccess={onDocumentLoadSuccess}> |
44 |
| - <Page pageNumber={currentPage} /> |
45 |
| - </Document> |
46 |
| - </div> |
47 |
| - </div> |
| 10 | + <DocumentContext.Provider value={props}> |
| 11 | + <div className="widget-document-viewer">{CurrentRenderer && <CurrentRenderer {...props} />}</div> |
| 12 | + </DocumentContext.Provider> |
48 | 13 | );
|
49 | 14 | }
|
0 commit comments