@semiont/react-ui 0.5.25 → 0.5.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/PdfAnnotationCanvas.client-6ZSF6ZTJ.js +677 -0
- package/dist/PdfAnnotationCanvas.client-6ZSF6ZTJ.js.map +1 -0
- package/dist/PdfAnnotationCanvas.client-JCJMXWWJ.css +180 -0
- package/dist/PdfAnnotationCanvas.client-JCJMXWWJ.css.map +1 -0
- package/dist/{ar-CULRHLNG.js → ar-YTUUGK2O.js} +13 -1
- package/dist/ar-YTUUGK2O.js.map +1 -0
- package/dist/{bn-HYR7CEL2.js → bn-ASGNGPMD.js} +13 -1
- package/dist/bn-ASGNGPMD.js.map +1 -0
- package/dist/{chunk-TG2XQEC4.js → chunk-BTUG3VJJ.js} +18 -298
- package/dist/chunk-BTUG3VJJ.js.map +1 -0
- package/dist/{chunk-NH6L2GIL.js → chunk-L3WKRWP3.js} +13 -1
- package/dist/chunk-L3WKRWP3.js.map +1 -0
- package/dist/chunk-OBQOZCJE.js +287 -0
- package/dist/chunk-OBQOZCJE.js.map +1 -0
- package/dist/{cs-YBRWY7NH.js → cs-WMYVAOZY.js} +13 -1
- package/dist/cs-WMYVAOZY.js.map +1 -0
- package/dist/{da-PHDEOBV3.js → da-FSHHN4D6.js} +13 -1
- package/dist/da-FSHHN4D6.js.map +1 -0
- package/dist/{de-O4UCCVLJ.js → de-FJFO3TPT.js} +13 -1
- package/dist/de-FJFO3TPT.js.map +1 -0
- package/dist/{el-MVGO6WZV.js → el-XOH4QIAG.js} +13 -1
- package/dist/el-XOH4QIAG.js.map +1 -0
- package/dist/{en-2KQILFKQ.js → en-3FCBW3YD.js} +4 -2
- package/dist/{es-USSLXQY7.js → es-HDX4QI7E.js} +13 -1
- package/dist/es-HDX4QI7E.js.map +1 -0
- package/dist/{fa-WMAREFWY.js → fa-DJ6WTFTJ.js} +13 -1
- package/dist/fa-DJ6WTFTJ.js.map +1 -0
- package/dist/{fi-B3NBNOTS.js → fi-4S7H7W7U.js} +13 -1
- package/dist/fi-4S7H7W7U.js.map +1 -0
- package/dist/{fr-TXYK5T5W.js → fr-6UJSYHXC.js} +13 -1
- package/dist/fr-6UJSYHXC.js.map +1 -0
- package/dist/{he-JCJACW5D.js → he-ZZJOHJX2.js} +13 -1
- package/dist/he-ZZJOHJX2.js.map +1 -0
- package/dist/{hi-VOQLZNJU.js → hi-HZJTAW7U.js} +13 -1
- package/dist/hi-HZJTAW7U.js.map +1 -0
- package/dist/{id-BFN5RZNZ.js → id-E3I6ILEM.js} +13 -1
- package/dist/id-E3I6ILEM.js.map +1 -0
- package/dist/index.css +140 -58
- package/dist/index.css.map +1 -1
- package/dist/index.js +67 -55
- package/dist/index.js.map +1 -1
- package/dist/{it-TWWDHKKM.js → it-ULAPAD7U.js} +13 -1
- package/dist/it-ULAPAD7U.js.map +1 -0
- package/dist/{ja-YRQLEBFM.js → ja-SAHZQ4CZ.js} +13 -1
- package/dist/ja-SAHZQ4CZ.js.map +1 -0
- package/dist/{ko-PG6B3CRB.js → ko-DFU2ADQ4.js} +13 -1
- package/dist/ko-DFU2ADQ4.js.map +1 -0
- package/dist/{ms-7EZECVKM.js → ms-NWL76XNT.js} +13 -1
- package/dist/ms-NWL76XNT.js.map +1 -0
- package/dist/{nl-HP753YAP.js → nl-ATCM76WN.js} +13 -1
- package/dist/nl-ATCM76WN.js.map +1 -0
- package/dist/{no-JUD5WX3N.js → no-UIAQ5RJF.js} +13 -1
- package/dist/no-UIAQ5RJF.js.map +1 -0
- package/dist/{pl-RVUL4CBM.js → pl-NOECQDXD.js} +13 -1
- package/dist/pl-NOECQDXD.js.map +1 -0
- package/dist/{pt-RPOVD4QM.js → pt-SVWIIUPC.js} +13 -1
- package/dist/pt-SVWIIUPC.js.map +1 -0
- package/dist/{ro-DMYWQTRD.js → ro-NR2DRTZG.js} +13 -1
- package/dist/ro-NR2DRTZG.js.map +1 -0
- package/dist/{sv-DMZ4FGXD.js → sv-STVJ37JP.js} +13 -1
- package/dist/sv-STVJ37JP.js.map +1 -0
- package/dist/test-utils.js +5 -3
- package/dist/test-utils.js.map +1 -1
- package/dist/{th-4HVBJBQ2.js → th-UWQR4K54.js} +13 -1
- package/dist/th-UWQR4K54.js.map +1 -0
- package/dist/{tr-6BDPU5ML.js → tr-WNXZSD27.js} +13 -1
- package/dist/tr-WNXZSD27.js.map +1 -0
- package/dist/{uk-TBZBT2TL.js → uk-SJRMB47Y.js} +13 -1
- package/dist/uk-SJRMB47Y.js.map +1 -0
- package/dist/{vi-UMKZKZL3.js → vi-TMRQFES2.js} +13 -1
- package/dist/vi-TMRQFES2.js.map +1 -0
- package/dist/{zh-YBJWJJYB.js → zh-QOL3LYEM.js} +13 -1
- package/dist/zh-QOL3LYEM.js.map +1 -0
- package/package.json +4 -4
- package/src/components/StatusDisplay.css +35 -35
- package/src/components/annotation/annotations.css +1 -1
- package/src/components/modals/SearchModal.css +5 -5
- package/src/components/pdf-annotation/PdfAnnotationCanvas.css +143 -6
- package/src/components/pdf-annotation/PdfAnnotationCanvas.tsx +622 -231
- package/src/components/pdf-annotation/__tests__/PdfAnnotationCanvas.test.tsx +761 -2
- package/src/components/pdf-annotation/__tests__/estimate-slot-height.test.ts +53 -0
- package/src/components/pdf-annotation/estimate-slot-height.ts +31 -0
- package/src/components/resource/__tests__/browse-renderers.dispatch.test.tsx +33 -0
- package/src/components/resource/annotate-renderers.tsx +10 -1
- package/src/components/resource/browse-renderers.tsx +10 -1
- package/src/features/auth/auth.css +10 -10
- package/src/features/resource-viewer/__tests__/ResourceViewerPage.test.tsx +26 -0
- package/src/features/resource-viewer/components/ResourceViewerPage.tsx +1 -1
- package/src/styles/core/inputs.css +1 -1
- package/src/styles/features/compose.css +1 -1
- package/src/styles/features/resource-viewer.css +1 -1
- package/src/styles/features/static-pages.css +11 -11
- package/src/styles/motivations/motivation-comment.css +2 -2
- package/src/styles/motivations/motivation-reference.css +2 -2
- package/src/styles/patterns/panels-base.css +4 -4
- package/src/styles/utilities/motion.css +15 -15
- package/src/styles/variables.css +29 -1
- package/translations/ar.json +10 -0
- package/translations/bn.json +10 -0
- package/translations/cs.json +10 -0
- package/translations/da.json +10 -0
- package/translations/de.json +10 -0
- package/translations/el.json +10 -0
- package/translations/en.json +10 -0
- package/translations/es.json +10 -0
- package/translations/fa.json +10 -0
- package/translations/fi.json +10 -0
- package/translations/fr.json +10 -0
- package/translations/he.json +10 -0
- package/translations/hi.json +10 -0
- package/translations/id.json +10 -0
- package/translations/it.json +10 -0
- package/translations/ja.json +10 -0
- package/translations/ko.json +10 -0
- package/translations/ms.json +10 -0
- package/translations/nl.json +10 -0
- package/translations/no.json +10 -0
- package/translations/pl.json +10 -0
- package/translations/pt.json +10 -0
- package/translations/ro.json +10 -0
- package/translations/sv.json +10 -0
- package/translations/th.json +10 -0
- package/translations/tr.json +10 -0
- package/translations/uk.json +10 -0
- package/translations/vi.json +10 -0
- package/translations/zh.json +10 -0
- package/dist/PdfAnnotationCanvas.client-EHQWF6BR.js +0 -462
- package/dist/PdfAnnotationCanvas.client-EHQWF6BR.js.map +0 -1
- package/dist/PdfAnnotationCanvas.client-SU5KNT7G.css +0 -98
- package/dist/PdfAnnotationCanvas.client-SU5KNT7G.css.map +0 -1
- package/dist/ar-CULRHLNG.js.map +0 -1
- package/dist/bn-HYR7CEL2.js.map +0 -1
- package/dist/chunk-NH6L2GIL.js.map +0 -1
- package/dist/chunk-TG2XQEC4.js.map +0 -1
- package/dist/cs-YBRWY7NH.js.map +0 -1
- package/dist/da-PHDEOBV3.js.map +0 -1
- package/dist/de-O4UCCVLJ.js.map +0 -1
- package/dist/el-MVGO6WZV.js.map +0 -1
- package/dist/es-USSLXQY7.js.map +0 -1
- package/dist/fa-WMAREFWY.js.map +0 -1
- package/dist/fi-B3NBNOTS.js.map +0 -1
- package/dist/fr-TXYK5T5W.js.map +0 -1
- package/dist/he-JCJACW5D.js.map +0 -1
- package/dist/hi-VOQLZNJU.js.map +0 -1
- package/dist/id-BFN5RZNZ.js.map +0 -1
- package/dist/it-TWWDHKKM.js.map +0 -1
- package/dist/ja-YRQLEBFM.js.map +0 -1
- package/dist/ko-PG6B3CRB.js.map +0 -1
- package/dist/ms-7EZECVKM.js.map +0 -1
- package/dist/nl-HP753YAP.js.map +0 -1
- package/dist/no-JUD5WX3N.js.map +0 -1
- package/dist/pl-RVUL4CBM.js.map +0 -1
- package/dist/pt-RPOVD4QM.js.map +0 -1
- package/dist/ro-DMYWQTRD.js.map +0 -1
- package/dist/sv-DMZ4FGXD.js.map +0 -1
- package/dist/th-4HVBJBQ2.js.map +0 -1
- package/dist/tr-6BDPU5ML.js.map +0 -1
- package/dist/uk-TBZBT2TL.js.map +0 -1
- package/dist/vi-UMKZKZL3.js.map +0 -1
- package/dist/zh-YBJWJJYB.js.map +0 -1
- /package/dist/{en-2KQILFKQ.js.map → en-3FCBW3YD.js.map} +0 -0
|
@@ -6,6 +6,8 @@ import { resourceId as toResourceId } from '@semiont/core';
|
|
|
6
6
|
import { toViewportAnchorRect } from '../../lib/anchor-rect';
|
|
7
7
|
import { createFragmentSelector, anchorRuns, isTextRun, textUnder } from '@semiont/core';
|
|
8
8
|
import { rectsForPage } from './rects-for-page';
|
|
9
|
+
import { estimateSlotHeight } from './estimate-slot-height';
|
|
10
|
+
import { useTranslations } from '../../contexts/TranslationContext';
|
|
9
11
|
import { createHoverHandlers, type SemiontSession } from '@semiont/sdk';
|
|
10
12
|
import type { SelectionMotivation } from '../annotation/AnnotateToolbar';
|
|
11
13
|
import {
|
|
@@ -22,6 +24,55 @@ import './PdfAnnotationCanvas.css';
|
|
|
22
24
|
|
|
23
25
|
export type DrawingMode = 'rectangle' | 'circle' | 'polygon' | null;
|
|
24
26
|
|
|
27
|
+
/**
|
|
28
|
+
* How the document's pages are laid out.
|
|
29
|
+
*
|
|
30
|
+
* `paged` is one page with Previous/Next. `scroll` is a virtualized column:
|
|
31
|
+
* every page gets a slot so the scrollbar tells the truth about the
|
|
32
|
+
* document's length, but only the pages near the viewport are mounted.
|
|
33
|
+
*
|
|
34
|
+
* Declared explicitly rather than inferred from `drawingMode`, which does NOT
|
|
35
|
+
* distinguish the modes — AnnotateView passes `drawingMode={null}` whenever no
|
|
36
|
+
* motivation is selected, so keying layout on it would flip a reader between
|
|
37
|
+
* scrolling and paged views as they picked up and put down a tool.
|
|
38
|
+
* See .plans/PDF-CONTINUOUS-SCROLL.md D3.
|
|
39
|
+
*/
|
|
40
|
+
export type PageLayout = 'paged' | 'scroll';
|
|
41
|
+
|
|
42
|
+
/** How far outside the viewport a page starts loading, and stays loaded. */
|
|
43
|
+
const PRELOAD_MARGIN = '100% 0px';
|
|
44
|
+
|
|
45
|
+
/** Width of one page rectangle in the strip, in CSS px. Height follows the
|
|
46
|
+
* document's own aspect ratio, so the strip looks like the document — wide
|
|
47
|
+
* enough to carry a three-digit page number, which is what makes the strip
|
|
48
|
+
* useful rather than merely positional on a long document. */
|
|
49
|
+
const STRIP_PAGE_WIDTH = 30;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The axis pages advance along.
|
|
53
|
+
*
|
|
54
|
+
* ONE source for two consumers: the column scrolls along it, and the page
|
|
55
|
+
* strip runs parallel to it. A strip that ran across the scroll direction
|
|
56
|
+
* would read as a control for a movement the document does not make.
|
|
57
|
+
*
|
|
58
|
+
* Constant today because scrolling is vertical. The horizontal-scrolling
|
|
59
|
+
* setting turns this into a value read from preferences — both consumers
|
|
60
|
+
* follow automatically, which is the point of routing them through one name.
|
|
61
|
+
*/
|
|
62
|
+
const SCROLL_AXIS: 'vertical' | 'horizontal' = 'vertical';
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Scroll an element into view where the environment supports it.
|
|
66
|
+
*
|
|
67
|
+
* Same posture as this file's guarded observers: jsdom implements no layout
|
|
68
|
+
* and therefore no `scrollIntoView`, and a viewer that throws in a test
|
|
69
|
+
* harness (or an exotic embedding host) is worse than one that simply does
|
|
70
|
+
* not scroll.
|
|
71
|
+
*/
|
|
72
|
+
function scrollElementIntoView(el: Element | null | undefined, options: ScrollIntoViewOptions): void {
|
|
73
|
+
if (typeof el?.scrollIntoView === 'function') el.scrollIntoView(options);
|
|
74
|
+
}
|
|
75
|
+
|
|
25
76
|
/**
|
|
26
77
|
* Get color for annotation based on motivation
|
|
27
78
|
*/
|
|
@@ -44,134 +95,70 @@ function getMotivationColor(motivation: SelectionMotivation | null): { stroke: s
|
|
|
44
95
|
}
|
|
45
96
|
}
|
|
46
97
|
|
|
47
|
-
interface
|
|
48
|
-
|
|
49
|
-
|
|
98
|
+
interface PdfPageViewProps {
|
|
99
|
+
doc: PDFDocumentProxy;
|
|
100
|
+
pageNumber: number;
|
|
101
|
+
/** Raster scale. Display-only: the overlay's geometry never reads it. */
|
|
102
|
+
scale: number;
|
|
50
103
|
resourceUri: string;
|
|
51
|
-
existingAnnotations
|
|
104
|
+
existingAnnotations: Annotation[];
|
|
52
105
|
drawingMode: DrawingMode;
|
|
53
106
|
selectedMotivation?: SelectionMotivation | null;
|
|
54
107
|
session?: SemiontSession | null | undefined;
|
|
55
108
|
hoveredAnnotationId?: string | null;
|
|
56
109
|
selectedAnnotationId?: string | null;
|
|
57
|
-
hoverDelayMs
|
|
110
|
+
hoverDelayMs: number;
|
|
111
|
+
/** The document-wide server map, fetched once per resource by the parent. */
|
|
112
|
+
fetchResourceAnchored: () => Promise<AnchoredText | null>;
|
|
58
113
|
}
|
|
59
114
|
|
|
60
115
|
/**
|
|
61
|
-
*
|
|
116
|
+
* One rendered page: its raster, its text map, its annotation overlay, and
|
|
117
|
+
* the drag that draws on it.
|
|
62
118
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
119
|
+
* Everything a page needs lives HERE rather than in the parent, which is what
|
|
120
|
+
* makes a scrolling column possible: mounting a page loads it, and unmounting
|
|
121
|
+
* releases it. The raster is a data-URL string, so the last reference going
|
|
122
|
+
* away IS the memory release — no eviction bookkeeping, no object-URL revoke.
|
|
123
|
+
* The drag lives here too because it needs this page's display dimensions and
|
|
124
|
+
* this page's text; a drag can no more span pages than a rectangle can.
|
|
66
125
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
126
|
+
function PdfPageView({
|
|
127
|
+
doc,
|
|
128
|
+
pageNumber,
|
|
129
|
+
scale,
|
|
69
130
|
resourceUri,
|
|
70
|
-
existingAnnotations
|
|
131
|
+
existingAnnotations,
|
|
71
132
|
drawingMode,
|
|
72
133
|
selectedMotivation,
|
|
73
134
|
session,
|
|
74
135
|
hoveredAnnotationId,
|
|
75
136
|
selectedAnnotationId,
|
|
76
|
-
hoverDelayMs
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const [pdfDoc, setPdfDoc] = useState<PDFDocumentProxy | null>(null);
|
|
80
|
-
const [numPages, setNumPages] = useState<number>(0);
|
|
81
|
-
const [pageNumber, setPageNumber] = useState(1);
|
|
137
|
+
hoverDelayMs,
|
|
138
|
+
fetchResourceAnchored,
|
|
139
|
+
}: PdfPageViewProps) {
|
|
82
140
|
const [pageImageUrl, setPageImageUrl] = useState<string | null>(null);
|
|
83
|
-
const [isLoading, setIsLoading] = useState(true);
|
|
84
|
-
const [error, setError] = useState<string | null>(null);
|
|
85
141
|
const [pageDimensions, setPageDimensions] = useState<{ width: number; height: number } | null>(null);
|
|
86
142
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* for the browser to read.
|
|
143
|
+
* This page's text and per-run geometry, read once when the page loads so a
|
|
144
|
+
* drag can be quoted without a round trip. Null while loading; `items` is
|
|
145
|
+
* empty on a scanned page, which has no text layer for the browser to read.
|
|
91
146
|
*/
|
|
92
147
|
const [pageAnchored, setPageAnchored] = useState<AnchoredText | null>(null);
|
|
93
148
|
const [displayDimensions, setDisplayDimensions] = useState<{ width: number; height: number } | null>(null);
|
|
94
|
-
|
|
149
|
+
/** Translation KEY, not text: `t` is a new closure each render, so keeping
|
|
150
|
+
* it out of the load effect's deps is what stops a reload loop. */
|
|
151
|
+
const [errorKey, setErrorKey] = useState<string | null>(null);
|
|
152
|
+
const t = useTranslations('PdfViewer');
|
|
95
153
|
|
|
96
|
-
// Drawing state
|
|
97
154
|
const [isDrawing, setIsDrawing] = useState(false);
|
|
98
155
|
const [selection, setSelection] = useState<CanvasRectangle | null>(null);
|
|
99
156
|
|
|
100
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
101
157
|
const imageRef = useRef<HTMLImageElement>(null);
|
|
102
158
|
|
|
103
|
-
// Load PDF document on mount
|
|
104
159
|
useEffect(() => {
|
|
105
160
|
let cancelled = false;
|
|
106
161
|
|
|
107
|
-
async function loadPdf() {
|
|
108
|
-
try {
|
|
109
|
-
setIsLoading(true);
|
|
110
|
-
setError(null);
|
|
111
|
-
|
|
112
|
-
const doc = await loadPdfDocument(pdfUrl);
|
|
113
|
-
|
|
114
|
-
if (cancelled) return;
|
|
115
|
-
|
|
116
|
-
setPdfDoc(doc);
|
|
117
|
-
setNumPages(doc.numPages);
|
|
118
|
-
setIsLoading(false);
|
|
119
|
-
} catch (err) {
|
|
120
|
-
if (cancelled) return;
|
|
121
|
-
|
|
122
|
-
console.error('Error loading PDF:', err);
|
|
123
|
-
setError('Failed to load PDF');
|
|
124
|
-
setIsLoading(false);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
loadPdf();
|
|
129
|
-
|
|
130
|
-
return () => {
|
|
131
|
-
cancelled = true;
|
|
132
|
-
};
|
|
133
|
-
}, [pdfUrl]);
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* The server-derived map, fetched once per document rather than once per
|
|
137
|
-
* page-load effect (PERSIST-ANCHORS P4). The map is WHOLE-RESOURCE — one
|
|
138
|
-
* artifact covering every page — so re-reading it from the per-page effect
|
|
139
|
-
* meant a full refetch and re-decode on every page turn; on a 400-page scan
|
|
140
|
-
* that is one decode per interaction instead of one per document.
|
|
141
|
-
*
|
|
142
|
-
* The cache holds the in-flight promise so concurrent page loads share one
|
|
143
|
-
* fetch. Answers cache — including "no map" and a stored decline, which are
|
|
144
|
-
* definitive — but a transport failure clears the entry, so the next page
|
|
145
|
-
* load retries instead of pinning the whole document to geometry-only.
|
|
146
|
-
*/
|
|
147
|
-
const resourceAnchoredRef = useRef<{ uri: string; outcome: Promise<AnchoredText | null> } | null>(null);
|
|
148
|
-
const fetchResourceAnchored = useCallback((): Promise<AnchoredText | null> => {
|
|
149
|
-
if (!session) return Promise.resolve(null); // no session yet — don't cache its absence
|
|
150
|
-
const cached = resourceAnchoredRef.current;
|
|
151
|
-
if (cached && cached.uri === resourceUri) return cached.outcome;
|
|
152
|
-
|
|
153
|
-
const uri = resourceUri;
|
|
154
|
-
const outcome = session.client.browse.resourceAnchoredText(toResourceId(uri)).then(
|
|
155
|
-
(served) => (served && !('declined' in served) ? served : null),
|
|
156
|
-
() => {
|
|
157
|
-
if (resourceAnchoredRef.current?.uri === uri) resourceAnchoredRef.current = null;
|
|
158
|
-
return null;
|
|
159
|
-
},
|
|
160
|
-
);
|
|
161
|
-
resourceAnchoredRef.current = { uri, outcome };
|
|
162
|
-
return outcome;
|
|
163
|
-
}, [session, resourceUri]);
|
|
164
|
-
|
|
165
|
-
// Load current page when page number changes
|
|
166
|
-
useEffect(() => {
|
|
167
|
-
if (!pdfDoc) return;
|
|
168
|
-
|
|
169
|
-
let cancelled = false;
|
|
170
|
-
const doc = pdfDoc;
|
|
171
|
-
|
|
172
|
-
// Never quote the previous page's text under this page's rectangles.
|
|
173
|
-
setPageAnchored(null);
|
|
174
|
-
|
|
175
162
|
/**
|
|
176
163
|
* The map a rectangle on this page quotes from, or null when there is
|
|
177
164
|
* none to be had.
|
|
@@ -194,7 +181,7 @@ export function PdfAnnotationCanvas({
|
|
|
194
181
|
// No runs means a scanned page: the characters exist only as pixels
|
|
195
182
|
// and pdf.js has nothing to give. The server derived a map at ingest,
|
|
196
183
|
// so ask for it rather than leaving the annotation anonymous.
|
|
197
|
-
// Whole-resource — served once per document
|
|
184
|
+
// Whole-resource — served once per document by the parent's cache —
|
|
198
185
|
// and `textUnder` filters by page: the same shape the native branch
|
|
199
186
|
// produces, so nothing downstream branches.
|
|
200
187
|
//
|
|
@@ -204,8 +191,7 @@ export function PdfAnnotationCanvas({
|
|
|
204
191
|
// this existed. The served record is the full extraction outcome
|
|
205
192
|
// (PERSIST-ANCHORS D1); a stored decline means extraction ran and
|
|
206
193
|
// found nothing to anchor — for this canvas the same degradation as
|
|
207
|
-
// no map at all.
|
|
208
|
-
// provenance this canvas does not read.
|
|
194
|
+
// no map at all.
|
|
209
195
|
return await fetchResourceAnchored();
|
|
210
196
|
} catch {
|
|
211
197
|
return null;
|
|
@@ -215,15 +201,11 @@ export function PdfAnnotationCanvas({
|
|
|
215
201
|
async function loadPage() {
|
|
216
202
|
try {
|
|
217
203
|
const page = await doc.getPage(pageNumber);
|
|
218
|
-
|
|
219
204
|
if (cancelled) return;
|
|
220
205
|
|
|
221
206
|
// Get page dimensions (at scale 1.0)
|
|
222
207
|
const viewport = page.getViewport({ scale: 1.0 });
|
|
223
|
-
setPageDimensions({
|
|
224
|
-
width: viewport.width,
|
|
225
|
-
height: viewport.height
|
|
226
|
-
});
|
|
208
|
+
setPageDimensions({ width: viewport.width, height: viewport.height });
|
|
227
209
|
|
|
228
210
|
// Anchoring and rendering are independent, and only one of them the
|
|
229
211
|
// reader is waiting on. Sequencing them put a network round-trip in
|
|
@@ -245,7 +227,7 @@ export function PdfAnnotationCanvas({
|
|
|
245
227
|
if (cancelled) return;
|
|
246
228
|
|
|
247
229
|
console.error('Error loading page:', err);
|
|
248
|
-
|
|
230
|
+
setErrorKey('pageLoadFailed');
|
|
249
231
|
}
|
|
250
232
|
}
|
|
251
233
|
|
|
@@ -254,7 +236,7 @@ export function PdfAnnotationCanvas({
|
|
|
254
236
|
return () => {
|
|
255
237
|
cancelled = true;
|
|
256
238
|
};
|
|
257
|
-
}, [
|
|
239
|
+
}, [doc, pageNumber, scale, fetchResourceAnchored]);
|
|
258
240
|
|
|
259
241
|
// Update display dimensions on resize
|
|
260
242
|
useEffect(() => {
|
|
@@ -438,8 +420,8 @@ export function PdfAnnotationCanvas({
|
|
|
438
420
|
// It will be cleared when drawingMode changes or user starts new selection
|
|
439
421
|
}, [isDrawing, selection, pageNumber, pageDimensions, displayDimensions, selectedMotivation, existingAnnotations, session, resourceUri, pageAnchored]);
|
|
440
422
|
|
|
441
|
-
// Every FragmentSelector rect on
|
|
442
|
-
//
|
|
423
|
+
// Every FragmentSelector rect on this page — one per line for a multi-line
|
|
424
|
+
// (multi-selector) annotation, exactly one for a manual annotation.
|
|
443
425
|
const pageRects = rectsForPage(existingAnnotations, pageNumber);
|
|
444
426
|
|
|
445
427
|
// Hover handlers with currentHover guard and dwell delay
|
|
@@ -451,150 +433,559 @@ export function PdfAnnotationCanvas({
|
|
|
451
433
|
// Calculate motivation color
|
|
452
434
|
const { stroke, fill } = getMotivationColor(selectedMotivation ?? null);
|
|
453
435
|
|
|
454
|
-
if (
|
|
455
|
-
return <div className="semiont-pdf-annotation-canvas__error">{
|
|
436
|
+
if (errorKey) {
|
|
437
|
+
return <div className="semiont-pdf-annotation-canvas__error">{t(errorKey)}</div>;
|
|
456
438
|
}
|
|
457
439
|
|
|
458
440
|
return (
|
|
459
|
-
<div
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
draggable={false}
|
|
485
|
-
style={{ pointerEvents: 'none' }}
|
|
486
|
-
onLoad={() => {
|
|
487
|
-
// Use double RAF to ensure layout is complete even in onLoad
|
|
441
|
+
<div
|
|
442
|
+
className="semiont-pdf-annotation-canvas__container"
|
|
443
|
+
onMouseDown={handleMouseDown}
|
|
444
|
+
onMouseMove={handleMouseMove}
|
|
445
|
+
onMouseUp={handleMouseUp}
|
|
446
|
+
onMouseLeave={() => {
|
|
447
|
+
if (isDrawing) {
|
|
448
|
+
setIsDrawing(false);
|
|
449
|
+
setSelection(null);
|
|
450
|
+
}
|
|
451
|
+
}}
|
|
452
|
+
data-drawing-mode={drawingMode || 'none'}
|
|
453
|
+
>
|
|
454
|
+
{/* PDF page rendered as image */}
|
|
455
|
+
{pageImageUrl && (
|
|
456
|
+
<img
|
|
457
|
+
ref={imageRef}
|
|
458
|
+
src={pageImageUrl}
|
|
459
|
+
alt={`PDF page ${pageNumber}`}
|
|
460
|
+
className="semiont-pdf-annotation-canvas__image"
|
|
461
|
+
draggable={false}
|
|
462
|
+
style={{ pointerEvents: 'none' }}
|
|
463
|
+
onLoad={() => {
|
|
464
|
+
// Use double RAF to ensure layout is complete even in onLoad
|
|
465
|
+
requestAnimationFrame(() => {
|
|
488
466
|
requestAnimationFrame(() => {
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
}
|
|
496
|
-
});
|
|
467
|
+
if (imageRef.current) {
|
|
468
|
+
setDisplayDimensions({
|
|
469
|
+
width: imageRef.current.clientWidth,
|
|
470
|
+
height: imageRef.current.clientHeight
|
|
471
|
+
});
|
|
472
|
+
}
|
|
497
473
|
});
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
474
|
+
});
|
|
475
|
+
}}
|
|
476
|
+
/>
|
|
477
|
+
)}
|
|
478
|
+
|
|
479
|
+
{/* SVG overlay for annotations */}
|
|
480
|
+
{displayDimensions && pageDimensions && (
|
|
481
|
+
<div className="semiont-pdf-annotation-canvas__overlay-container">
|
|
482
|
+
<div className="semiont-pdf-annotation-canvas__overlay">
|
|
483
|
+
<svg
|
|
484
|
+
className="semiont-pdf-annotation-canvas__svg"
|
|
485
|
+
width={displayDimensions.width}
|
|
486
|
+
height={displayDimensions.height}
|
|
487
|
+
>
|
|
488
|
+
{/* Render existing annotations for this page */}
|
|
489
|
+
{pageRects.map(r => {
|
|
490
|
+
const rect = pdfToCanvasCoordinates(r.coord, pageDimensions.height, 1.0);
|
|
491
|
+
|
|
492
|
+
// Scale to display coordinates
|
|
493
|
+
const scaleX = displayDimensions.width / pageDimensions.width;
|
|
494
|
+
const scaleY = displayDimensions.height / pageDimensions.height;
|
|
495
|
+
|
|
496
|
+
const isHovered = r.annId === hoveredAnnotationId;
|
|
497
|
+
const isSelected = r.annId === selectedAnnotationId;
|
|
498
|
+
|
|
499
|
+
// Colour by the annotation's own motivation (not the toolbar's).
|
|
500
|
+
const annMotivation = r.annotation.motivation as SelectionMotivation | null;
|
|
501
|
+
const { stroke: annStroke, fill: annFill } = getMotivationColor(annMotivation);
|
|
502
|
+
|
|
503
|
+
return (
|
|
504
|
+
<rect
|
|
505
|
+
key={`${r.annId}:${r.selectorIndex}`}
|
|
506
|
+
x={rect.x * scaleX}
|
|
507
|
+
y={rect.y * scaleY}
|
|
508
|
+
width={rect.width * scaleX}
|
|
509
|
+
height={rect.height * scaleY}
|
|
510
|
+
stroke={annStroke}
|
|
511
|
+
strokeWidth={isSelected ? 4 : isHovered ? 3 : 2}
|
|
512
|
+
fill={annFill}
|
|
513
|
+
style={{
|
|
514
|
+
pointerEvents: 'auto',
|
|
515
|
+
cursor: 'pointer',
|
|
516
|
+
opacity: isSelected ? 1 : isHovered ? 0.9 : 0.7
|
|
517
|
+
}}
|
|
518
|
+
onClick={(e) => session?.client.browse.click(r.annId, r.annotation.motivation, e.currentTarget.getBoundingClientRect())}
|
|
519
|
+
onMouseEnter={() => handleMouseEnter(r.annId)}
|
|
520
|
+
onMouseLeave={handleMouseLeave}
|
|
521
|
+
/>
|
|
522
|
+
);
|
|
523
|
+
})}
|
|
524
|
+
|
|
525
|
+
{/* Render current selection while drawing or awaiting save */}
|
|
526
|
+
{selection && (() => {
|
|
527
|
+
const rectX = Math.min(selection.startX, selection.endX);
|
|
528
|
+
const rectY = Math.min(selection.startY, selection.endY);
|
|
529
|
+
const rectWidth = Math.abs(selection.endX - selection.startX);
|
|
530
|
+
const rectHeight = Math.abs(selection.endY - selection.startY);
|
|
531
|
+
|
|
532
|
+
// PDF only supports rectangle shapes (FragmentSelector with viewrect)
|
|
533
|
+
// Circle/polygon are disabled in the UI for PDF media types
|
|
534
|
+
return (
|
|
535
|
+
<rect
|
|
536
|
+
x={rectX}
|
|
537
|
+
y={rectY}
|
|
538
|
+
width={rectWidth}
|
|
539
|
+
height={rectHeight}
|
|
540
|
+
stroke={stroke}
|
|
541
|
+
strokeWidth={2}
|
|
542
|
+
strokeDasharray="5,5"
|
|
543
|
+
fill={fill}
|
|
544
|
+
pointerEvents="none"
|
|
545
|
+
/>
|
|
546
|
+
);
|
|
547
|
+
})()}
|
|
548
|
+
</svg>
|
|
549
|
+
</div>
|
|
550
|
+
</div>
|
|
551
|
+
)}
|
|
552
|
+
</div>
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
interface PdfAnnotationCanvasProps {
|
|
557
|
+
pdfUrl: string;
|
|
558
|
+
/** The '@id' of the annotated resource — stamped as `source` on mark:requested (multi-viewer routing). */
|
|
559
|
+
resourceUri: string;
|
|
560
|
+
existingAnnotations?: Annotation[];
|
|
561
|
+
drawingMode: DrawingMode;
|
|
562
|
+
selectedMotivation?: SelectionMotivation | null;
|
|
563
|
+
session?: SemiontSession | null | undefined;
|
|
564
|
+
hoveredAnnotationId?: string | null;
|
|
565
|
+
selectedAnnotationId?: string | null;
|
|
566
|
+
hoverDelayMs?: number;
|
|
567
|
+
/** `paged` (default) or the virtualized `scroll` column. See `PageLayout`. */
|
|
568
|
+
pageLayout?: PageLayout;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* PDF annotation canvas with page navigation and rectangle drawing
|
|
573
|
+
*
|
|
574
|
+
* @emits browse:click - Annotation clicked on PDF. Payload: { annotationId: string, motivation: Motivation }
|
|
575
|
+
* @emits mark:requested - New annotation drawn on PDF. Payload: { selector: [FragmentSelector, TextQuoteSelector?], motivation: SelectionMotivation } — the quote is the text under the rectangle, omitted when the page has no text layer
|
|
576
|
+
* @emits beckon:hover - Annotation hovered or unhovered. Payload: { annotationId: string | null }
|
|
577
|
+
*/
|
|
578
|
+
export function PdfAnnotationCanvas({
|
|
579
|
+
pdfUrl,
|
|
580
|
+
resourceUri,
|
|
581
|
+
existingAnnotations = [],
|
|
582
|
+
drawingMode,
|
|
583
|
+
selectedMotivation,
|
|
584
|
+
session,
|
|
585
|
+
hoveredAnnotationId,
|
|
586
|
+
selectedAnnotationId,
|
|
587
|
+
hoverDelayMs = 150,
|
|
588
|
+
pageLayout = 'paged'
|
|
589
|
+
}: PdfAnnotationCanvasProps) {
|
|
590
|
+
// PDF state
|
|
591
|
+
const [pdfDoc, setPdfDoc] = useState<PDFDocumentProxy | null>(null);
|
|
592
|
+
const [numPages, setNumPages] = useState<number>(0);
|
|
593
|
+
const [pageNumber, setPageNumber] = useState(1);
|
|
594
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
595
|
+
const [errorKey, setErrorKey] = useState<string | null>(null);
|
|
596
|
+
const t = useTranslations('PdfViewer');
|
|
597
|
+
/**
|
|
598
|
+
* Page 1's shape, for reserving space in slots that have not mounted yet:
|
|
599
|
+
* its aspect ratio and its raster width. NOT its raster height — the image
|
|
600
|
+
* renders under `max-width: 100%; height: auto`, so its displayed height
|
|
601
|
+
* depends on the column's width, and reserving raster pixels made the
|
|
602
|
+
* column's height lurch on every mount (S1b).
|
|
603
|
+
* See .plans/PDF-CONTINUOUS-SCROLL.md D4 + S1b.
|
|
604
|
+
*/
|
|
605
|
+
const [pageShape, setPageShape] = useState<{ aspect: number; rasterWidth: number } | null>(null);
|
|
606
|
+
/** Measured inner width of the column — the other half of the reservation. */
|
|
607
|
+
const [columnWidth, setColumnWidth] = useState<number | null>(null);
|
|
608
|
+
const columnRef = useRef<HTMLDivElement>(null);
|
|
609
|
+
const [scale] = useState(1.5); // Fixed scale for better quality
|
|
610
|
+
|
|
611
|
+
/** Pages currently intersecting the viewport (plus the preload margin). */
|
|
612
|
+
const [visiblePages, setVisiblePages] = useState<Set<number>>(new Set());
|
|
613
|
+
/** Pages genuinely in view — observed with NO preload margin, so it answers
|
|
614
|
+
* "what is the reader looking at" rather than "what should be loaded".
|
|
615
|
+
* Drives the indicator and the rail; never the mount window. */
|
|
616
|
+
const [onscreenPages, setOnscreenPages] = useState<Set<number>>(new Set());
|
|
617
|
+
const slotRefs = useRef(new Map<number, HTMLElement>());
|
|
618
|
+
const observerRef = useRef<IntersectionObserver | null>(null);
|
|
619
|
+
const onscreenRef = useRef<IntersectionObserver | null>(null);
|
|
620
|
+
|
|
621
|
+
// Load PDF document on mount
|
|
622
|
+
useEffect(() => {
|
|
623
|
+
let cancelled = false;
|
|
624
|
+
|
|
625
|
+
async function loadPdf() {
|
|
626
|
+
try {
|
|
627
|
+
setIsLoading(true);
|
|
628
|
+
setErrorKey(null);
|
|
629
|
+
|
|
630
|
+
const doc = await loadPdfDocument(pdfUrl);
|
|
501
631
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
632
|
+
if (cancelled) return;
|
|
633
|
+
|
|
634
|
+
setPdfDoc(doc);
|
|
635
|
+
setNumPages(doc.numPages);
|
|
636
|
+
setIsLoading(false);
|
|
637
|
+
|
|
638
|
+
// One extra getPage, for slot sizing (D4). Failure is not fatal:
|
|
639
|
+
// unsized slots still scroll, just less faithfully.
|
|
640
|
+
try {
|
|
641
|
+
const first = await doc.getPage(1);
|
|
642
|
+
if (cancelled) return;
|
|
643
|
+
const natural = first.getViewport({ scale: 1.0 });
|
|
644
|
+
setPageShape({
|
|
645
|
+
aspect: natural.height / natural.width,
|
|
646
|
+
rasterWidth: first.getViewport({ scale }).width,
|
|
647
|
+
});
|
|
648
|
+
} catch {
|
|
649
|
+
/* leave unsized — an unsized slot is stable; a mis-sized one moves */
|
|
650
|
+
}
|
|
651
|
+
} catch (err) {
|
|
652
|
+
if (cancelled) return;
|
|
653
|
+
|
|
654
|
+
console.error('Error loading PDF:', err);
|
|
655
|
+
setErrorKey('loadFailed');
|
|
656
|
+
setIsLoading(false);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
loadPdf();
|
|
661
|
+
|
|
662
|
+
return () => {
|
|
663
|
+
cancelled = true;
|
|
664
|
+
};
|
|
665
|
+
}, [pdfUrl, scale]);
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* The server-derived map, fetched once per document rather than once per
|
|
669
|
+
* page (PERSIST-ANCHORS P4). The map is WHOLE-RESOURCE — one artifact
|
|
670
|
+
* covering every page — so re-reading it per page meant a full refetch and
|
|
671
|
+
* re-decode on every page turn; on a 400-page scan that is one decode per
|
|
672
|
+
* interaction instead of one per document. Living on the parent is also
|
|
673
|
+
* what lets a scrolling column mount many pages against a single fetch.
|
|
674
|
+
*
|
|
675
|
+
* The cache holds the in-flight promise so concurrent page loads share one
|
|
676
|
+
* fetch. Answers cache — including "no map" and a stored decline, which are
|
|
677
|
+
* definitive — but a transport failure clears the entry, so the next page
|
|
678
|
+
* load retries instead of pinning the whole document to geometry-only.
|
|
679
|
+
*/
|
|
680
|
+
const resourceAnchoredRef = useRef<{ uri: string; outcome: Promise<AnchoredText | null> } | null>(null);
|
|
681
|
+
const fetchResourceAnchored = useCallback((): Promise<AnchoredText | null> => {
|
|
682
|
+
if (!session) return Promise.resolve(null); // no session yet — don't cache its absence
|
|
683
|
+
const cached = resourceAnchoredRef.current;
|
|
684
|
+
if (cached && cached.uri === resourceUri) return cached.outcome;
|
|
685
|
+
|
|
686
|
+
const uri = resourceUri;
|
|
687
|
+
const outcome = session.client.browse.resourceAnchoredText(toResourceId(uri)).then(
|
|
688
|
+
(served) => (served && !('declined' in served) ? served : null),
|
|
689
|
+
() => {
|
|
690
|
+
if (resourceAnchoredRef.current?.uri === uri) resourceAnchoredRef.current = null;
|
|
691
|
+
return null;
|
|
692
|
+
},
|
|
693
|
+
);
|
|
694
|
+
resourceAnchoredRef.current = { uri, outcome };
|
|
695
|
+
return outcome;
|
|
696
|
+
}, [session, resourceUri]);
|
|
697
|
+
|
|
698
|
+
// The column's width decides every page's displayed height, so the
|
|
699
|
+
// reservation cannot be computed without it.
|
|
700
|
+
useEffect(() => {
|
|
701
|
+
if (pageLayout !== 'scroll') return;
|
|
702
|
+
const measure = () => {
|
|
703
|
+
if (columnRef.current) setColumnWidth(columnRef.current.clientWidth);
|
|
704
|
+
};
|
|
705
|
+
measure();
|
|
706
|
+
let observer: ResizeObserver | null = null;
|
|
707
|
+
try {
|
|
708
|
+
observer = new ResizeObserver(measure);
|
|
709
|
+
if (columnRef.current) observer.observe(columnRef.current);
|
|
710
|
+
} catch {
|
|
711
|
+
window.addEventListener('resize', measure);
|
|
712
|
+
}
|
|
713
|
+
return () => {
|
|
714
|
+
if (observer) observer.disconnect();
|
|
715
|
+
else window.removeEventListener('resize', measure);
|
|
716
|
+
};
|
|
717
|
+
}, [pageLayout]);
|
|
718
|
+
|
|
719
|
+
const slotHeight = estimateSlotHeight(columnWidth, pageShape?.rasterWidth ?? null, pageShape?.aspect ?? null)
|
|
720
|
+
// Before the column has been measured there is still a sane reservation:
|
|
721
|
+
// the raster's own height. Wrong in a narrow column, but every slot is
|
|
722
|
+
// wrong by the SAME amount, so the column is at least internally
|
|
723
|
+
// consistent until the first measurement lands.
|
|
724
|
+
?? (pageShape ? Math.round(pageShape.rasterWidth * pageShape.aspect) : null);
|
|
725
|
+
|
|
726
|
+
// The mount window. Slots report their own visibility; a page is mounted
|
|
727
|
+
// while its slot intersects (widened by PRELOAD_MARGIN so the next page is
|
|
728
|
+
// ready before it is reached), and unmounting is what frees its raster.
|
|
729
|
+
useEffect(() => {
|
|
730
|
+
if (pageLayout !== 'scroll' || numPages === 0) return;
|
|
731
|
+
|
|
732
|
+
let observer: IntersectionObserver | null = null;
|
|
733
|
+
try {
|
|
734
|
+
observer = new IntersectionObserver(
|
|
735
|
+
(entries) => {
|
|
736
|
+
setVisiblePages((prev) => {
|
|
737
|
+
const next = new Set(prev);
|
|
738
|
+
for (const entry of entries) {
|
|
739
|
+
const page = Number((entry.target as HTMLElement).dataset.page);
|
|
740
|
+
if (!page) continue;
|
|
741
|
+
if (entry.isIntersecting) next.add(page);
|
|
742
|
+
else next.delete(page);
|
|
743
|
+
}
|
|
744
|
+
return next;
|
|
745
|
+
});
|
|
746
|
+
},
|
|
747
|
+
{ rootMargin: PRELOAD_MARGIN },
|
|
748
|
+
);
|
|
749
|
+
} catch {
|
|
750
|
+
// No IntersectionObserver: mount every page rather than none. Correct,
|
|
751
|
+
// simply not virtualized — the same posture as the ResizeObserver
|
|
752
|
+
// fallback in PdfPageView.
|
|
753
|
+
setVisiblePages(new Set(Array.from({ length: numPages }, (_, i) => i + 1)));
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
observerRef.current = observer;
|
|
758
|
+
for (const el of slotRefs.current.values()) observer.observe(el);
|
|
759
|
+
|
|
760
|
+
// A SECOND observer, with no preload margin, answers a different question:
|
|
761
|
+
// which pages the reader can actually see. Two observers rather than one
|
|
762
|
+
// set doing both jobs, because widening the mount window must never move
|
|
763
|
+
// the page indicator.
|
|
764
|
+
let onscreen: IntersectionObserver | null = null;
|
|
765
|
+
try {
|
|
766
|
+
onscreen = new IntersectionObserver((entries) => {
|
|
767
|
+
setOnscreenPages((prev) => {
|
|
768
|
+
const next = new Set(prev);
|
|
769
|
+
for (const entry of entries) {
|
|
770
|
+
const page = Number((entry.target as HTMLElement).dataset.page);
|
|
771
|
+
if (!page) continue;
|
|
772
|
+
if (entry.isIntersecting) next.add(page);
|
|
773
|
+
else next.delete(page);
|
|
774
|
+
}
|
|
775
|
+
return next;
|
|
776
|
+
});
|
|
777
|
+
});
|
|
778
|
+
onscreenRef.current = onscreen;
|
|
779
|
+
for (const el of slotRefs.current.values()) onscreen.observe(el);
|
|
780
|
+
} catch {
|
|
781
|
+
// Without an observer there is no notion of "on screen"; fall back to
|
|
782
|
+
// page 1 so the indicator names something rather than nothing.
|
|
783
|
+
setOnscreenPages(new Set([1]));
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
return () => {
|
|
787
|
+
observer.disconnect();
|
|
788
|
+
observerRef.current = null;
|
|
789
|
+
onscreen?.disconnect();
|
|
790
|
+
onscreenRef.current = null;
|
|
791
|
+
};
|
|
792
|
+
}, [pageLayout, numPages]);
|
|
793
|
+
|
|
794
|
+
const registerSlot = useCallback((page: number) => (el: HTMLDivElement | null) => {
|
|
795
|
+
const previous = slotRefs.current.get(page);
|
|
796
|
+
if (previous) {
|
|
797
|
+
observerRef.current?.unobserve(previous);
|
|
798
|
+
onscreenRef.current?.unobserve(previous);
|
|
799
|
+
}
|
|
800
|
+
if (el) {
|
|
801
|
+
slotRefs.current.set(page, el);
|
|
802
|
+
observerRef.current?.observe(el);
|
|
803
|
+
onscreenRef.current?.observe(el);
|
|
804
|
+
} else {
|
|
805
|
+
slotRefs.current.delete(page);
|
|
806
|
+
}
|
|
807
|
+
}, []);
|
|
808
|
+
|
|
809
|
+
/** Keeps the current page's rectangle in view as the reader scrolls. */
|
|
810
|
+
const currentTickRef = useRef<HTMLButtonElement>(null);
|
|
811
|
+
|
|
812
|
+
const scrollToPage = useCallback((page: number) => {
|
|
813
|
+
scrollElementIntoView(slotRefs.current.get(page), { block: 'start' });
|
|
814
|
+
}, []);
|
|
815
|
+
|
|
816
|
+
// In the column the reader decides which page they are on by scrolling, so
|
|
817
|
+
// the indicator reports rather than controls: the topmost page actually ON
|
|
818
|
+
// SCREEN. NOT `visiblePages` — that set is deliberately widened by
|
|
819
|
+
// PRELOAD_MARGIN to mount pages before they are reached, so reading it here
|
|
820
|
+
// would name a page still a viewport away.
|
|
821
|
+
const currentPage = pageLayout === 'scroll'
|
|
822
|
+
? (onscreenPages.size > 0 ? Math.min(...onscreenPages) : 1)
|
|
823
|
+
: pageNumber;
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Left/Right step pages (S1a).
|
|
827
|
+
*
|
|
828
|
+
* Bound to the window rather than a focusable wrapper so it works without
|
|
829
|
+
* the reader hunting for the viewer's focus — but that reach is exactly why
|
|
830
|
+
* the guards matter more than the feature: a viewer that eats arrow keys
|
|
831
|
+
* while someone is editing an annotation body, or mid-drag, is worse than
|
|
832
|
+
* one with no shortcut at all.
|
|
833
|
+
*/
|
|
834
|
+
useEffect(() => {
|
|
835
|
+
if (numPages === 0) return;
|
|
836
|
+
const BACK = ['ArrowLeft', 'PageUp'];
|
|
837
|
+
const FORWARD = ['ArrowRight', 'PageDown'];
|
|
838
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
839
|
+
// PageUp/PageDown jump a page; Left/Right do too, because they have no
|
|
840
|
+
// native meaning in a vertical layout so taking them costs nothing.
|
|
841
|
+
//
|
|
842
|
+
// Up/Down are deliberately NOT here. They are the scroller's fine
|
|
843
|
+
// movement, and a page can be taller than the viewport — taking them
|
|
844
|
+
// would leave no keyboard way to reach the bottom of one. This is the
|
|
845
|
+
// split Preview, Chrome's PDF viewer and Acrobat all use.
|
|
846
|
+
if (!BACK.includes(e.key) && !FORWARD.includes(e.key)) return;
|
|
847
|
+
if (e.metaKey || e.ctrlKey || e.altKey) return; // browser/OS navigation
|
|
848
|
+
const el = e.target as HTMLElement | null;
|
|
849
|
+
const tag = el?.tagName;
|
|
850
|
+
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || el?.isContentEditable) return;
|
|
851
|
+
|
|
852
|
+
const next = FORWARD.includes(e.key) ? currentPage + 1 : currentPage - 1;
|
|
853
|
+
if (next < 1 || next > numPages) return;
|
|
854
|
+
e.preventDefault();
|
|
855
|
+
if (pageLayout === 'scroll') scrollToPage(next);
|
|
856
|
+
else setPageNumber(next);
|
|
857
|
+
};
|
|
858
|
+
window.addEventListener('keydown', onKeyDown);
|
|
859
|
+
return () => window.removeEventListener('keydown', onKeyDown);
|
|
860
|
+
}, [numPages, currentPage, pageLayout, scrollToPage]);
|
|
861
|
+
|
|
862
|
+
const stripRef = useRef<HTMLDivElement>(null);
|
|
863
|
+
|
|
864
|
+
useEffect(() => {
|
|
865
|
+
// `nearest` so following the reader never yanks the strip around; it
|
|
866
|
+
// scrolls only when the current rectangle would otherwise leave view.
|
|
867
|
+
scrollElementIntoView(currentTickRef.current, { block: 'nearest', inline: 'nearest' });
|
|
868
|
+
|
|
869
|
+
// Move focus with the current page, but ONLY when the strip already owns
|
|
870
|
+
// focus. Otherwise a clicked rectangle keeps focus while the current-page
|
|
871
|
+
// marker moves on, and the focus ring — which appears as soon as the
|
|
872
|
+
// reader touches an arrow key and the browser switches to keyboard
|
|
873
|
+
// modality — sits on a page that is no longer current: two rectangles
|
|
874
|
+
// claiming to be "here". Guarding on ownership means scrolling with the
|
|
875
|
+
// mouse never snatches focus from whatever the reader was doing.
|
|
876
|
+
const active = typeof document !== 'undefined' ? document.activeElement : null;
|
|
877
|
+
if (active && stripRef.current?.contains(active) && active !== currentTickRef.current) {
|
|
878
|
+
currentTickRef.current?.focus();
|
|
879
|
+
}
|
|
880
|
+
}, [currentPage]);
|
|
881
|
+
|
|
882
|
+
const pageProps = {
|
|
883
|
+
scale,
|
|
884
|
+
resourceUri,
|
|
885
|
+
existingAnnotations,
|
|
886
|
+
drawingMode,
|
|
887
|
+
selectedMotivation,
|
|
888
|
+
session,
|
|
889
|
+
hoveredAnnotationId,
|
|
890
|
+
selectedAnnotationId,
|
|
891
|
+
hoverDelayMs,
|
|
892
|
+
fetchResourceAnchored,
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
if (errorKey) {
|
|
896
|
+
return <div className="semiont-pdf-annotation-canvas__error">{t(errorKey)}</div>;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
return (
|
|
900
|
+
<div className="semiont-pdf-annotation-canvas">
|
|
901
|
+
{isLoading && <div className="semiont-pdf-annotation-canvas__loading">{t('loading')}</div>}
|
|
902
|
+
|
|
903
|
+
{pdfDoc && pageLayout === 'scroll' ? (
|
|
904
|
+
<div className="semiont-pdf-annotation-canvas__viewport" data-axis={SCROLL_AXIS}>
|
|
905
|
+
{numPages > 0 && pageShape && (
|
|
906
|
+
<div
|
|
907
|
+
className="semiont-pdf-annotation-canvas__strip"
|
|
908
|
+
data-axis={SCROLL_AXIS}
|
|
909
|
+
data-scroller="true"
|
|
910
|
+
aria-orientation={SCROLL_AXIS}
|
|
911
|
+
ref={stripRef}
|
|
912
|
+
>
|
|
913
|
+
{Array.from({ length: numPages }, (_, i) => i + 1).map((page) => (
|
|
914
|
+
<button
|
|
915
|
+
key={page}
|
|
916
|
+
type="button"
|
|
917
|
+
data-page={page}
|
|
918
|
+
className="semiont-pdf-annotation-canvas__strip-page"
|
|
919
|
+
style={{ height: `${Math.round(STRIP_PAGE_WIDTH * pageShape.aspect)}px` }}
|
|
920
|
+
aria-label={t('pageOf', { page, total: numPages })}
|
|
921
|
+
tabIndex={page === currentPage ? 0 : -1}
|
|
922
|
+
{...(page === currentPage ? { 'aria-current': 'page' as const } : {})}
|
|
923
|
+
ref={page === currentPage ? currentTickRef : undefined}
|
|
924
|
+
onClick={() => scrollToPage(page)}
|
|
510
925
|
>
|
|
511
|
-
{
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
// Scale to display coordinates
|
|
516
|
-
const scaleX = displayDimensions.width / pageDimensions.width;
|
|
517
|
-
const scaleY = displayDimensions.height / pageDimensions.height;
|
|
518
|
-
|
|
519
|
-
const isHovered = r.annId === hoveredAnnotationId;
|
|
520
|
-
const isSelected = r.annId === selectedAnnotationId;
|
|
521
|
-
|
|
522
|
-
// Colour by the annotation's own motivation (not the toolbar's).
|
|
523
|
-
const annMotivation = r.annotation.motivation as SelectionMotivation | null;
|
|
524
|
-
const { stroke: annStroke, fill: annFill } = getMotivationColor(annMotivation);
|
|
525
|
-
|
|
526
|
-
return (
|
|
527
|
-
<rect
|
|
528
|
-
key={`${r.annId}:${r.selectorIndex}`}
|
|
529
|
-
x={rect.x * scaleX}
|
|
530
|
-
y={rect.y * scaleY}
|
|
531
|
-
width={rect.width * scaleX}
|
|
532
|
-
height={rect.height * scaleY}
|
|
533
|
-
stroke={annStroke}
|
|
534
|
-
strokeWidth={isSelected ? 4 : isHovered ? 3 : 2}
|
|
535
|
-
fill={annFill}
|
|
536
|
-
style={{
|
|
537
|
-
pointerEvents: 'auto',
|
|
538
|
-
cursor: 'pointer',
|
|
539
|
-
opacity: isSelected ? 1 : isHovered ? 0.9 : 0.7
|
|
540
|
-
}}
|
|
541
|
-
onClick={(e) => session?.client.browse.click(r.annId, r.annotation.motivation, e.currentTarget.getBoundingClientRect())}
|
|
542
|
-
onMouseEnter={() => handleMouseEnter(r.annId)}
|
|
543
|
-
onMouseLeave={handleMouseLeave}
|
|
544
|
-
/>
|
|
545
|
-
);
|
|
546
|
-
})}
|
|
547
|
-
|
|
548
|
-
{/* Render current selection while drawing or awaiting save */}
|
|
549
|
-
{selection && (() => {
|
|
550
|
-
const rectX = Math.min(selection.startX, selection.endX);
|
|
551
|
-
const rectY = Math.min(selection.startY, selection.endY);
|
|
552
|
-
const rectWidth = Math.abs(selection.endX - selection.startX);
|
|
553
|
-
const rectHeight = Math.abs(selection.endY - selection.startY);
|
|
554
|
-
|
|
555
|
-
// PDF only supports rectangle shapes (FragmentSelector with viewrect)
|
|
556
|
-
// Circle/polygon are disabled in the UI for PDF media types
|
|
557
|
-
return (
|
|
558
|
-
<rect
|
|
559
|
-
x={rectX}
|
|
560
|
-
y={rectY}
|
|
561
|
-
width={rectWidth}
|
|
562
|
-
height={rectHeight}
|
|
563
|
-
stroke={stroke}
|
|
564
|
-
strokeWidth={2}
|
|
565
|
-
strokeDasharray="5,5"
|
|
566
|
-
fill={fill}
|
|
567
|
-
pointerEvents="none"
|
|
568
|
-
/>
|
|
569
|
-
);
|
|
570
|
-
})()}
|
|
571
|
-
</svg>
|
|
572
|
-
</div>
|
|
926
|
+
{page}
|
|
927
|
+
</button>
|
|
928
|
+
))}
|
|
573
929
|
</div>
|
|
574
930
|
)}
|
|
575
|
-
|
|
931
|
+
<div className="semiont-pdf-annotation-canvas__column" data-axis={SCROLL_AXIS} ref={columnRef}>
|
|
932
|
+
{Array.from({ length: numPages }, (_, i) => i + 1).map((page) => (
|
|
933
|
+
<div
|
|
934
|
+
key={page}
|
|
935
|
+
ref={registerSlot(page)}
|
|
936
|
+
data-page={page}
|
|
937
|
+
className="semiont-pdf-annotation-canvas__slot"
|
|
938
|
+
// min-height, and applied whether or not the page is mounted:
|
|
939
|
+
// releasing it on mount is what made the scrollbar jump. `min`
|
|
940
|
+
// rather than a fixed height so a page that renders slightly
|
|
941
|
+
// taller expands instead of clipping.
|
|
942
|
+
style={slotHeight ? { minHeight: slotHeight } : undefined}
|
|
943
|
+
>
|
|
944
|
+
{visiblePages.has(page) && (
|
|
945
|
+
<PdfPageView doc={pdfDoc} pageNumber={page} {...pageProps} />
|
|
946
|
+
)}
|
|
947
|
+
</div>
|
|
948
|
+
))}
|
|
949
|
+
</div>
|
|
950
|
+
</div>
|
|
951
|
+
) : (
|
|
952
|
+
pdfDoc && !isLoading && (
|
|
953
|
+
// `key` is load-bearing: without it React reuses this instance
|
|
954
|
+
// across page changes, so the previous page's raster, text map and
|
|
955
|
+
// load error survive into the next page — a stale overlay, a quote
|
|
956
|
+
// taken from the wrong page, and an error that never clears. Each
|
|
957
|
+
// page is a different thing; mounting it as one resets all of it.
|
|
958
|
+
<PdfPageView key={pageNumber} doc={pdfDoc} pageNumber={pageNumber} {...pageProps} />
|
|
959
|
+
)
|
|
960
|
+
)}
|
|
576
961
|
|
|
577
962
|
{/* Page navigation controls */}
|
|
578
963
|
{numPages > 0 && (
|
|
579
|
-
<
|
|
964
|
+
<nav className="semiont-pdf-annotation-canvas__controls" aria-label={t('pagination')}>
|
|
580
965
|
<button
|
|
581
|
-
disabled={
|
|
582
|
-
onClick={() => setPageNumber(pageNumber - 1)}
|
|
966
|
+
disabled={currentPage <= 1}
|
|
967
|
+
onClick={() => (pageLayout === 'scroll' ? scrollToPage(currentPage - 1) : setPageNumber(pageNumber - 1))}
|
|
583
968
|
className="semiont-pdf-annotation-canvas__button"
|
|
584
969
|
>
|
|
585
|
-
|
|
970
|
+
{t('previous')}
|
|
586
971
|
</button>
|
|
587
|
-
|
|
588
|
-
|
|
972
|
+
{/*
|
|
973
|
+
aria-live: the page number is the only feedback a page change gives,
|
|
974
|
+
and in the scrolling column it changes without any control being
|
|
975
|
+
activated at all — so a screen-reader user who scrolls would
|
|
976
|
+
otherwise get silence.
|
|
977
|
+
*/}
|
|
978
|
+
<span className="semiont-pdf-annotation-canvas__page-info" aria-live="polite">
|
|
979
|
+
{t('pageOf', { page: currentPage, total: numPages })}
|
|
589
980
|
</span>
|
|
590
981
|
<button
|
|
591
|
-
disabled={
|
|
592
|
-
onClick={() => setPageNumber(pageNumber + 1)}
|
|
982
|
+
disabled={currentPage >= numPages}
|
|
983
|
+
onClick={() => (pageLayout === 'scroll' ? scrollToPage(currentPage + 1) : setPageNumber(pageNumber + 1))}
|
|
593
984
|
className="semiont-pdf-annotation-canvas__button"
|
|
594
985
|
>
|
|
595
|
-
|
|
986
|
+
{t('next')}
|
|
596
987
|
</button>
|
|
597
|
-
</
|
|
988
|
+
</nav>
|
|
598
989
|
)}
|
|
599
990
|
</div>
|
|
600
991
|
);
|