|
1 | 1 | "use client";
|
2 | 2 |
|
3 |
| -import { CanvasLayer, Page, Pages, Root, TextLayer } from "@anaralabs/lector"; |
4 |
| -import React from "react"; |
| 3 | +import { |
| 4 | + CanvasLayer, |
| 5 | + Page, |
| 6 | + Pages, |
| 7 | + Root, |
| 8 | + TextLayer, |
| 9 | + AnnotationHighlightLayer, |
| 10 | + type Annotation, |
| 11 | + SelectionTooltip, |
| 12 | + useAnnotations, |
| 13 | + useSelectionDimensions, |
| 14 | + usePdfJump, |
| 15 | +} from "@anaralabs/lector"; |
| 16 | +import React, { useCallback, useEffect, useState } from "react"; |
5 | 17 | import "pdfjs-dist/web/pdf_viewer.css";
|
6 | 18 |
|
7 | 19 | import { GlobalWorkerOptions } from "pdfjs-dist";
|
8 | 20 | import ZoomMenu from "./zoom-menu";
|
9 | 21 | import DocumentMenu from "./document-menu";
|
10 | 22 | import { PageNavigation } from "./page-navigation";
|
| 23 | +import { SelectionTooltipContent, TooltipContent, TooltipContentProps } from "./annotationts"; |
11 | 24 |
|
12 | 25 | const fileUrl = "/pdf/pathways.pdf";
|
| 26 | +const STORAGE_KEY = "pdf-annotations"; |
13 | 27 |
|
14 | 28 | GlobalWorkerOptions.workerSrc = new URL(
|
15 | 29 | "pdfjs-dist/build/pdf.worker.mjs",
|
16 | 30 | import.meta.url
|
17 | 31 | ).toString();
|
18 | 32 |
|
19 |
| -export const AnaraViewer = () => { |
| 33 | +interface PDFContentProps { |
| 34 | + onAnnotationsChange: (annotations: Annotation[]) => void; |
| 35 | + initialAnnotations?: Annotation[]; |
| 36 | + focusedAnnotationId?: string; |
| 37 | + onAnnotationClick: (annotation: Annotation | null) => void; |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +const PDFContent = ({ |
| 42 | + onAnnotationsChange, |
| 43 | + focusedAnnotationId, |
| 44 | + onAnnotationClick, |
| 45 | +}: PDFContentProps) => { |
| 46 | + const { addAnnotation, annotations } = useAnnotations(); |
| 47 | + const { getSelection } = useSelectionDimensions(); |
| 48 | + const { jumpToHighlightRects } = usePdfJump(); |
| 49 | + |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + onAnnotationsChange(annotations); |
| 53 | + }, [annotations, onAnnotationsChange]); |
| 54 | + |
| 55 | + const handleCreateAnnotation = useCallback(() => { |
| 56 | + const selection = getSelection(); |
| 57 | + if (!selection || !selection.highlights.length) return; |
| 58 | + |
| 59 | + const newAnnotation = { |
| 60 | + pageNumber: selection.highlights[0].pageNumber, |
| 61 | + highlights: selection.highlights, |
| 62 | + color: "rgba(255, 255, 0, 0.3)", |
| 63 | + }; |
| 64 | + |
| 65 | + addAnnotation(newAnnotation); |
| 66 | + |
| 67 | + window.getSelection()?.removeAllRanges(); |
| 68 | + }, [addAnnotation, getSelection]); |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + if (annotations.length === 0) return; |
| 72 | + |
| 73 | + const lastAnnotation = annotations[annotations.length - 1]; |
| 74 | + const isNewAnnotation = Date.now() - new Date(lastAnnotation.createdAt).getTime() < 1000; |
| 75 | + |
| 76 | + if (isNewAnnotation) { |
| 77 | + onAnnotationClick(lastAnnotation); |
| 78 | + } |
| 79 | + }, [annotations, onAnnotationClick]); |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + if (!focusedAnnotationId) return; |
| 83 | + |
| 84 | + const annotation = annotations.find(a => a.id === focusedAnnotationId); |
| 85 | + if (!annotation || !annotation.highlights.length) return; |
| 86 | + |
| 87 | + jumpToHighlightRects( |
| 88 | + annotation.highlights, |
| 89 | + "pixels", |
| 90 | + "center", |
| 91 | + -50 |
| 92 | + ); |
| 93 | + }, [focusedAnnotationId, annotations, jumpToHighlightRects]); |
| 94 | + |
| 95 | + const handlePagesClick = useCallback((e: React.MouseEvent) => { |
| 96 | + const target = e.target as HTMLElement; |
| 97 | + |
| 98 | + if (target.closest('[role="tooltip"]')) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + const clickedHighlight = target.closest('[data-highlight-id]'); |
| 103 | + |
| 104 | + // If we clicked on a highlight, let the AnnotationHighlightLayer handle it |
| 105 | + if (clickedHighlight) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (focusedAnnotationId) { |
| 110 | + onAnnotationClick(null); |
| 111 | + } |
| 112 | + }, [focusedAnnotationId, onAnnotationClick]); |
| 113 | + |
| 114 | + const renderTooltipContent = useCallback(({ annotation, onClose }: TooltipContentProps) => { |
| 115 | + return <TooltipContent annotation={annotation} onClose={onClose} />; |
| 116 | + }, []); |
| 117 | + |
20 | 118 | return (
|
21 |
| - <Root |
22 |
| - className="border overflow-hidden flex flex-col w-full h-[600px] rounded-lg" |
23 |
| - source={fileUrl} |
24 |
| - isZoomFitWidth={true} |
25 |
| - loader={<div className="w-full"></div>} |
| 119 | + <Pages |
| 120 | + className="dark:invert-[94%] dark:hue-rotate-180 dark:brightness-[80%] dark:contrast-[228%] dark:bg-gray-100" |
| 121 | + onClick={handlePagesClick} |
26 | 122 | >
|
27 |
| - <div className="p-1 relative flex justify-between border-b"> |
28 |
| - <ZoomMenu /> |
29 |
| - <PageNavigation /> |
30 |
| - <DocumentMenu documentUrl={fileUrl} /> |
31 |
| - </div> |
32 |
| - <Pages className="dark:invert-[94%] dark:hue-rotate-180 dark:brightness-[80%] dark:contrast-[228%] dark:bg-gray-100"> |
33 |
| - <Page> |
34 |
| - <CanvasLayer /> |
35 |
| - <TextLayer /> |
36 |
| - </Page> |
37 |
| - </Pages> |
38 |
| - </Root> |
| 123 | + <Page> |
| 124 | + <CanvasLayer /> |
| 125 | + <TextLayer /> |
| 126 | + <AnnotationHighlightLayer |
| 127 | + focusedAnnotationId={focusedAnnotationId} |
| 128 | + onAnnotationClick={onAnnotationClick} |
| 129 | + renderTooltipContent={renderTooltipContent} |
| 130 | + /> |
| 131 | + <SelectionTooltip> |
| 132 | + <SelectionTooltipContent onHighlight={handleCreateAnnotation} /> |
| 133 | + </SelectionTooltip> |
| 134 | + </Page> |
| 135 | + </Pages> |
| 136 | + ); |
| 137 | +}; |
| 138 | + |
| 139 | +export const AnaraViewer = () => { |
| 140 | + const [savedAnnotations, setSavedAnnotations] = React.useState<Annotation[]>([]); |
| 141 | + const [focusedAnnotationId, setFocusedAnnotationId] = useState<string>(); |
| 142 | + |
| 143 | + const handleAnnotationsChange = useCallback((annotations: Annotation[]) => { |
| 144 | + try { |
| 145 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(annotations)); |
| 146 | + setSavedAnnotations(annotations); |
| 147 | + } catch (error) { |
| 148 | + console.error('Error saving annotations:', error); |
| 149 | + } |
| 150 | + }, []); |
| 151 | + |
| 152 | + const handleAnnotationClick = useCallback((annotation: Annotation | null) => { |
| 153 | + setFocusedAnnotationId(annotation?.id); |
| 154 | + }, []); |
| 155 | + |
| 156 | + return ( |
| 157 | + <div className="flex flex-col gap-4"> |
| 158 | + <Root |
| 159 | + className="border overflow-hidden flex flex-col w-full h-[600px] rounded-lg" |
| 160 | + source={fileUrl} |
| 161 | + isZoomFitWidth={true} |
| 162 | + loader={<div className="w-full"></div>} |
| 163 | + > |
| 164 | + <div className="p-1 relative flex justify-between border-b"> |
| 165 | + <ZoomMenu /> |
| 166 | + <PageNavigation /> |
| 167 | + <DocumentMenu documentUrl={fileUrl} /> |
| 168 | + </div> |
| 169 | + <PDFContent |
| 170 | + initialAnnotations={savedAnnotations} |
| 171 | + onAnnotationsChange={handleAnnotationsChange} |
| 172 | + focusedAnnotationId={focusedAnnotationId} |
| 173 | + onAnnotationClick={handleAnnotationClick} |
| 174 | + /> |
| 175 | + </Root> |
| 176 | + </div> |
39 | 177 | );
|
40 | 178 | };
|
0 commit comments