Spaces:
Running
Running
CreoCot commited on
Commit ·
fe80f2b
1
Parent(s): ff04e40
Added pdf viewer component
Browse files
src/components/Viewer/PdfViewerComponent.module.css
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.container {
|
| 2 |
+
display: flex;
|
| 3 |
+
width: 30em; /* Hardcoded as requested */
|
| 4 |
+
height: 50em; /* Hardcoded as requested */
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
/* This class will be used for rendering individual highlight areas */
|
| 8 |
+
.highlightArea {
|
| 9 |
+
background-color: rgba(255, 255, 0, 0.4); /* Yellow with some transparency */
|
| 10 |
+
position: absolute; /* Crucial: Positions the highlight over the PDF content */
|
| 11 |
+
pointer-events: none; /* Allows mouse events to pass through to the PDF */
|
| 12 |
+
border-radius: 2px; /* Slightly rounded corners */
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
/* The 'brawl' class from your original CSS. It's not used for highlights in the new structure,
|
| 16 |
+
but kept as it was in your original file. */
|
| 17 |
+
.brawl {
|
| 18 |
+
background-color: #111111;
|
| 19 |
+
fill: #111111;
|
| 20 |
+
padding: 10px;
|
| 21 |
+
}
|
src/components/Viewer/PdfViewerComponent.tsx
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useEffect, useMemo } from "react";
|
| 2 |
+
import { SpecialZoomLevel, Viewer, Worker } from "@react-pdf-viewer/core";
|
| 3 |
+
import { pageNavigationPlugin } from "@react-pdf-viewer/page-navigation";
|
| 4 |
+
import { searchPlugin, SearchTargetPage } from "@react-pdf-viewer/search";
|
| 5 |
+
|
| 6 |
+
import "@react-pdf-viewer/search/lib/styles/index.css";
|
| 7 |
+
import "@react-pdf-viewer/core/lib/styles/index.css";
|
| 8 |
+
import "@react-pdf-viewer/page-navigation/lib/styles/index.css";
|
| 9 |
+
import "@react-pdf-viewer/zoom/lib/styles/index.css";
|
| 10 |
+
import "@react-pdf-viewer/highlight/lib/styles/index.css";
|
| 11 |
+
|
| 12 |
+
import styles from "./PdfViewerComponent.module.css";
|
| 13 |
+
|
| 14 |
+
interface PdfViewerComponentProps {
|
| 15 |
+
pdfUrl: string;
|
| 16 |
+
initialPage?: number;
|
| 17 |
+
textToHighlight?: string;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const PdfViewerComponent = (props: PdfViewerComponentProps) => {
|
| 21 |
+
const pageNavigationPluginInstance = pageNavigationPlugin();
|
| 22 |
+
const searchPluginInstance = searchPlugin();
|
| 23 |
+
|
| 24 |
+
const { pdfUrl, textToHighlight, initialPage = 0 } = props;
|
| 25 |
+
|
| 26 |
+
const handleSearch = () => {
|
| 27 |
+
if (!textToHighlight || textToHighlight.trim() === "") {
|
| 28 |
+
return;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
searchPluginInstance.setTargetPages(
|
| 32 |
+
(targetPage: SearchTargetPage) => targetPage.pageIndex === initialPage,
|
| 33 |
+
);
|
| 34 |
+
|
| 35 |
+
searchPluginInstance.highlight({
|
| 36 |
+
keyword: textToHighlight,
|
| 37 |
+
matchCase: false,
|
| 38 |
+
});
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
return (
|
| 42 |
+
<div className={styles.container}>
|
| 43 |
+
<Worker workerUrl="https://unpkg.com/pdfjs-dist@3.11.174/build/pdf.worker.min.js">
|
| 44 |
+
<Viewer
|
| 45 |
+
enableSmoothScroll={true}
|
| 46 |
+
fileUrl={pdfUrl}
|
| 47 |
+
plugins={[pageNavigationPluginInstance, searchPluginInstance]}
|
| 48 |
+
defaultScale={SpecialZoomLevel.PageFit}
|
| 49 |
+
initialPage={initialPage}
|
| 50 |
+
onDocumentLoad={handleSearch}
|
| 51 |
+
/>
|
| 52 |
+
</Worker>
|
| 53 |
+
</div>
|
| 54 |
+
);
|
| 55 |
+
};
|
| 56 |
+
export default PdfViewerComponent;
|