@tobyt/expo-pdf-markup 0.2.2 → 0.3.0
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/CHANGELOG.md +16 -0
- package/README.md +85 -4
- package/build/ExpoPdfMarkup.types.d.ts +1 -1
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts +3 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +410 -4
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/pdfjs.d.ts +2 -0
- package/build/pdfjs.d.ts.map +1 -0
- package/build/pdfjs.js +6 -0
- package/build/pdfjs.js.map +1 -0
- package/build/pdfjs.web.d.ts +2 -0
- package/build/pdfjs.web.d.ts.map +1 -0
- package/build/pdfjs.web.js +2 -0
- package/build/pdfjs.web.js.map +1 -0
- package/build/web/annotationUtils.d.ts +37 -0
- package/build/web/annotationUtils.d.ts.map +1 -0
- package/build/web/annotationUtils.js +213 -0
- package/build/web/annotationUtils.js.map +1 -0
- package/build/web/types.d.ts +13 -0
- package/build/web/types.d.ts.map +1 -0
- package/build/web/types.js +2 -0
- package/build/web/types.js.map +1 -0
- package/ios/ExpoPdfMarkup.podspec +1 -1
- package/metro.d.ts +5 -0
- package/metro.js +90 -0
- package/package.json +8 -1
- package/src/ExpoPdfMarkup.types.ts +1 -1
- package/src/ExpoPdfMarkupView.web.tsx +512 -5
- package/src/index.ts +1 -0
- package/src/pdfjs.ts +5 -0
- package/src/pdfjs.web.ts +1 -0
- package/src/web/annotationUtils.ts +257 -0
- package/src/web/types.ts +12 -0
|
@@ -1,12 +1,519 @@
|
|
|
1
|
+
import * as pdfjs from 'pdfjs-dist';
|
|
2
|
+
import type { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
|
|
1
3
|
import * as React from 'react';
|
|
2
|
-
import { Text, View } from 'react-native';
|
|
3
4
|
|
|
4
|
-
import {
|
|
5
|
+
import type {
|
|
6
|
+
Annotation,
|
|
7
|
+
AnnotationMode,
|
|
8
|
+
AnnotationPoint,
|
|
9
|
+
ExpoPdfMarkupViewProps,
|
|
10
|
+
} from './ExpoPdfMarkup.types';
|
|
11
|
+
import {
|
|
12
|
+
canvasRectToPdfBounds,
|
|
13
|
+
canvasToPdf,
|
|
14
|
+
drawAnnotationsOnCanvas,
|
|
15
|
+
hitTestAnnotation,
|
|
16
|
+
newAnnotationId,
|
|
17
|
+
parseAnnotations,
|
|
18
|
+
serializeAnnotations,
|
|
19
|
+
} from './web/annotationUtils';
|
|
20
|
+
import type { PdfPageMeta } from './web/types';
|
|
21
|
+
|
|
22
|
+
// Configure pdfjs worker once at module level (consumers can override before mounting)
|
|
23
|
+
if (!pdfjs.GlobalWorkerOptions.workerSrc) {
|
|
24
|
+
// Default: served from public/ by withPdfMarkup() in metro.js.
|
|
25
|
+
// Override with setPdfJsWorkerSrc() if using a different bundler or CDN.
|
|
26
|
+
pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.mjs';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Allow consumers to override the pdfjs worker URL before mounting the view. */
|
|
30
|
+
export function setPdfJsWorkerSrc(url: string): void {
|
|
31
|
+
pdfjs.GlobalWorkerOptions.workerSrc = url;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// PageView
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
type PageViewProps = {
|
|
39
|
+
doc: PDFDocumentProxy;
|
|
40
|
+
pageIndex: number;
|
|
41
|
+
meta: PdfPageMeta;
|
|
42
|
+
annotations: Annotation[];
|
|
43
|
+
annotationMode: AnnotationMode;
|
|
44
|
+
annotationColor: string;
|
|
45
|
+
annotationLineWidth: number;
|
|
46
|
+
onAnnotationAdded: (annotation: Annotation) => void;
|
|
47
|
+
onAnnotationRemoved: (id: string) => void;
|
|
48
|
+
onTextInputRequested: (pageIndex: number, pdfPoint: AnnotationPoint) => void;
|
|
49
|
+
containerRef: (el: HTMLDivElement | null) => void;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function PageView({
|
|
53
|
+
doc,
|
|
54
|
+
pageIndex,
|
|
55
|
+
meta,
|
|
56
|
+
annotations,
|
|
57
|
+
annotationMode,
|
|
58
|
+
annotationColor,
|
|
59
|
+
annotationLineWidth,
|
|
60
|
+
onAnnotationAdded,
|
|
61
|
+
onAnnotationRemoved,
|
|
62
|
+
onTextInputRequested,
|
|
63
|
+
containerRef,
|
|
64
|
+
}: PageViewProps) {
|
|
65
|
+
const pdfCanvasRef = React.useRef<HTMLCanvasElement>(null);
|
|
66
|
+
const annotCanvasRef = React.useRef<HTMLCanvasElement>(null);
|
|
67
|
+
const pageRef = React.useRef<PDFPageProxy | null>(null);
|
|
68
|
+
const renderTaskRef = React.useRef<ReturnType<PDFPageProxy['render']> | null>(null);
|
|
69
|
+
const pointerDownRef = React.useRef(false);
|
|
70
|
+
const inkPointsRef = React.useRef<{ x: number; y: number }[]>([]);
|
|
71
|
+
const dragStartRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
72
|
+
const dragCurrentRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
73
|
+
|
|
74
|
+
const dpr = typeof window !== 'undefined' ? window.devicePixelRatio ?? 1 : 1;
|
|
75
|
+
const cssWidth = meta.canvasWidth / dpr;
|
|
76
|
+
const cssHeight = meta.canvasHeight / dpr;
|
|
77
|
+
|
|
78
|
+
// Render PDF page onto pdfCanvas
|
|
79
|
+
React.useEffect(() => {
|
|
80
|
+
let cancelled = false;
|
|
81
|
+
async function render() {
|
|
82
|
+
if (!pdfCanvasRef.current) return;
|
|
83
|
+
renderTaskRef.current?.cancel();
|
|
84
|
+
renderTaskRef.current = null;
|
|
85
|
+
const page = await doc.getPage(pageIndex + 1);
|
|
86
|
+
if (cancelled) {
|
|
87
|
+
page.cleanup();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
pageRef.current = page;
|
|
91
|
+
const canvas = pdfCanvasRef.current;
|
|
92
|
+
if (!canvas) return;
|
|
93
|
+
canvas.width = meta.canvasWidth;
|
|
94
|
+
canvas.height = meta.canvasHeight;
|
|
95
|
+
const ctx = canvas.getContext('2d');
|
|
96
|
+
if (!ctx) return;
|
|
97
|
+
const viewport = page.getViewport({ scale: meta.scale * dpr });
|
|
98
|
+
const task = page.render({ canvasContext: ctx, viewport });
|
|
99
|
+
renderTaskRef.current = task;
|
|
100
|
+
try {
|
|
101
|
+
await task.promise;
|
|
102
|
+
} catch {
|
|
103
|
+
// Render cancelled — ignore
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
render();
|
|
107
|
+
return () => {
|
|
108
|
+
cancelled = true;
|
|
109
|
+
renderTaskRef.current?.cancel();
|
|
110
|
+
};
|
|
111
|
+
}, [doc, pageIndex, meta, dpr]);
|
|
112
|
+
|
|
113
|
+
// Draw annotations on annotCanvas
|
|
114
|
+
const redrawAnnotations = React.useCallback(
|
|
115
|
+
(liveInkPoints?: { x: number; y: number }[], dragRect?: DOMRect) => {
|
|
116
|
+
const canvas = annotCanvasRef.current;
|
|
117
|
+
if (!canvas) return;
|
|
118
|
+
canvas.width = meta.canvasWidth;
|
|
119
|
+
canvas.height = meta.canvasHeight;
|
|
120
|
+
const ctx = canvas.getContext('2d');
|
|
121
|
+
if (!ctx) return;
|
|
122
|
+
ctx.scale(dpr, dpr);
|
|
123
|
+
drawAnnotationsOnCanvas(ctx, annotations, pageIndex, meta);
|
|
124
|
+
// Live ink preview
|
|
125
|
+
if (liveInkPoints && liveInkPoints.length > 1) {
|
|
126
|
+
ctx.save();
|
|
127
|
+
ctx.strokeStyle = annotationColor;
|
|
128
|
+
ctx.lineWidth = annotationLineWidth * meta.scale;
|
|
129
|
+
ctx.lineCap = 'round';
|
|
130
|
+
ctx.lineJoin = 'round';
|
|
131
|
+
ctx.beginPath();
|
|
132
|
+
for (let i = 0; i < liveInkPoints.length; i++) {
|
|
133
|
+
const pt = liveInkPoints[i];
|
|
134
|
+
if (i === 0) ctx.moveTo(pt.x, pt.y);
|
|
135
|
+
else ctx.lineTo(pt.x, pt.y);
|
|
136
|
+
}
|
|
137
|
+
ctx.stroke();
|
|
138
|
+
ctx.restore();
|
|
139
|
+
}
|
|
140
|
+
// Live drag rect preview (highlight/underline)
|
|
141
|
+
if (dragRect) {
|
|
142
|
+
ctx.save();
|
|
143
|
+
ctx.strokeStyle = annotationColor;
|
|
144
|
+
ctx.lineWidth = 1;
|
|
145
|
+
ctx.setLineDash([4, 4]);
|
|
146
|
+
ctx.strokeRect(dragRect.x, dragRect.y, dragRect.width, dragRect.height);
|
|
147
|
+
ctx.restore();
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
[annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth]
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
React.useEffect(() => {
|
|
154
|
+
redrawAnnotations();
|
|
155
|
+
}, [redrawAnnotations]);
|
|
156
|
+
|
|
157
|
+
function getCanvasPoint(e: React.PointerEvent<HTMLCanvasElement>): { x: number; y: number } {
|
|
158
|
+
const rect = (e.currentTarget as HTMLCanvasElement).getBoundingClientRect();
|
|
159
|
+
return { x: e.clientX - rect.left, y: e.clientY - rect.top };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function handlePointerDown(e: React.PointerEvent<HTMLCanvasElement>) {
|
|
163
|
+
if (annotationMode === 'none') return;
|
|
164
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
165
|
+
const pt = getCanvasPoint(e);
|
|
166
|
+
pointerDownRef.current = true;
|
|
167
|
+
if (annotationMode === 'ink') {
|
|
168
|
+
inkPointsRef.current = [pt];
|
|
169
|
+
} else if (annotationMode === 'highlight' || annotationMode === 'underline') {
|
|
170
|
+
dragStartRef.current = pt;
|
|
171
|
+
dragCurrentRef.current = pt;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function handlePointerMove(e: React.PointerEvent<HTMLCanvasElement>) {
|
|
176
|
+
if (!pointerDownRef.current) return;
|
|
177
|
+
const pt = getCanvasPoint(e);
|
|
178
|
+
if (annotationMode === 'ink') {
|
|
179
|
+
inkPointsRef.current.push(pt);
|
|
180
|
+
redrawAnnotations(inkPointsRef.current);
|
|
181
|
+
} else if (annotationMode === 'highlight' || annotationMode === 'underline') {
|
|
182
|
+
dragCurrentRef.current = pt;
|
|
183
|
+
const start = dragStartRef.current!;
|
|
184
|
+
const minX = Math.min(start.x, pt.x);
|
|
185
|
+
const minY = Math.min(start.y, pt.y);
|
|
186
|
+
const w = Math.abs(pt.x - start.x);
|
|
187
|
+
const h = Math.abs(pt.y - start.y);
|
|
188
|
+
redrawAnnotations(undefined, new DOMRect(minX, minY, w, h));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function handlePointerUp(e: React.PointerEvent<HTMLCanvasElement>) {
|
|
193
|
+
if (!pointerDownRef.current && annotationMode !== 'text' && annotationMode !== 'eraser') return;
|
|
194
|
+
pointerDownRef.current = false;
|
|
195
|
+
const pt = getCanvasPoint(e);
|
|
196
|
+
const { scale, pdfHeight } = meta;
|
|
197
|
+
|
|
198
|
+
if (annotationMode === 'ink') {
|
|
199
|
+
const cssPoints = inkPointsRef.current;
|
|
200
|
+
inkPointsRef.current = [];
|
|
201
|
+
if (cssPoints.length === 0) return;
|
|
202
|
+
const pdfPoints = cssPoints.map((p) => canvasToPdf(p.x, p.y, scale, pdfHeight));
|
|
203
|
+
onAnnotationAdded({
|
|
204
|
+
id: newAnnotationId(),
|
|
205
|
+
type: 'ink',
|
|
206
|
+
page: pageIndex,
|
|
207
|
+
color: annotationColor,
|
|
208
|
+
lineWidth: annotationLineWidth,
|
|
209
|
+
paths: [pdfPoints],
|
|
210
|
+
createdAt: Date.now(),
|
|
211
|
+
});
|
|
212
|
+
} else if (annotationMode === 'highlight' || annotationMode === 'underline') {
|
|
213
|
+
const start = dragStartRef.current;
|
|
214
|
+
dragStartRef.current = null;
|
|
215
|
+
dragCurrentRef.current = null;
|
|
216
|
+
if (!start) return;
|
|
217
|
+
const minX = Math.min(start.x, pt.x);
|
|
218
|
+
const minY = Math.min(start.y, pt.y);
|
|
219
|
+
const maxX = Math.max(start.x, pt.x);
|
|
220
|
+
const maxY = Math.max(start.y, pt.y);
|
|
221
|
+
if (maxX - minX < 2 || maxY - minY < 2) return;
|
|
222
|
+
const bounds = canvasRectToPdfBounds(minX, minY, maxX, maxY, scale, pdfHeight);
|
|
223
|
+
if (annotationMode === 'highlight') {
|
|
224
|
+
onAnnotationAdded({
|
|
225
|
+
id: newAnnotationId(),
|
|
226
|
+
type: 'highlight',
|
|
227
|
+
page: pageIndex,
|
|
228
|
+
color: annotationColor,
|
|
229
|
+
alpha: 0.5,
|
|
230
|
+
bounds,
|
|
231
|
+
createdAt: Date.now(),
|
|
232
|
+
});
|
|
233
|
+
} else {
|
|
234
|
+
onAnnotationAdded({
|
|
235
|
+
id: newAnnotationId(),
|
|
236
|
+
type: 'underline',
|
|
237
|
+
page: pageIndex,
|
|
238
|
+
color: annotationColor,
|
|
239
|
+
bounds,
|
|
240
|
+
createdAt: Date.now(),
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
} else if (annotationMode === 'text') {
|
|
244
|
+
const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
|
|
245
|
+
onTextInputRequested(pageIndex, pdfPoint);
|
|
246
|
+
} else if (annotationMode === 'eraser') {
|
|
247
|
+
const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
|
|
248
|
+
const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
|
|
249
|
+
if (hit) onAnnotationRemoved(hit.id);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const pointerEvents = annotationMode === 'none' ? 'none' : 'auto';
|
|
254
|
+
|
|
255
|
+
return (
|
|
256
|
+
<div
|
|
257
|
+
ref={containerRef}
|
|
258
|
+
style={{ position: 'relative', width: cssWidth, height: cssHeight, margin: '0 auto' }}
|
|
259
|
+
>
|
|
260
|
+
<canvas
|
|
261
|
+
ref={pdfCanvasRef}
|
|
262
|
+
style={{
|
|
263
|
+
position: 'absolute',
|
|
264
|
+
top: 0,
|
|
265
|
+
left: 0,
|
|
266
|
+
width: cssWidth,
|
|
267
|
+
height: cssHeight,
|
|
268
|
+
pointerEvents: 'none',
|
|
269
|
+
}}
|
|
270
|
+
/>
|
|
271
|
+
<canvas
|
|
272
|
+
ref={annotCanvasRef}
|
|
273
|
+
style={{
|
|
274
|
+
position: 'absolute',
|
|
275
|
+
top: 0,
|
|
276
|
+
left: 0,
|
|
277
|
+
width: cssWidth,
|
|
278
|
+
height: cssHeight,
|
|
279
|
+
cursor: 'crosshair',
|
|
280
|
+
pointerEvents,
|
|
281
|
+
}}
|
|
282
|
+
onPointerDown={handlePointerDown}
|
|
283
|
+
onPointerMove={handlePointerMove}
|
|
284
|
+
onPointerUp={handlePointerUp}
|
|
285
|
+
/>
|
|
286
|
+
</div>
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ---------------------------------------------------------------------------
|
|
291
|
+
// Main component
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
5
293
|
|
|
6
294
|
export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
295
|
+
const {
|
|
296
|
+
source,
|
|
297
|
+
page: propPage,
|
|
298
|
+
backgroundColor = '#f0f0f0',
|
|
299
|
+
annotations: annotationsProp,
|
|
300
|
+
annotationMode = 'none',
|
|
301
|
+
annotationColor = '#FF0000',
|
|
302
|
+
annotationLineWidth = 2,
|
|
303
|
+
onPageChanged,
|
|
304
|
+
onLoadComplete,
|
|
305
|
+
onError,
|
|
306
|
+
onAnnotationsChanged,
|
|
307
|
+
style,
|
|
308
|
+
} = props;
|
|
309
|
+
|
|
310
|
+
const scrollContainerRef = React.useRef<HTMLDivElement>(null);
|
|
311
|
+
const [containerWidth, setContainerWidth] = React.useState(0);
|
|
312
|
+
const [doc, setDoc] = React.useState<PDFDocumentProxy | null>(null);
|
|
313
|
+
const [pageMetas, setPageMetas] = React.useState<PdfPageMeta[]>([]);
|
|
314
|
+
const [annotations, setAnnotations] = React.useState<Annotation[]>([]);
|
|
315
|
+
const lastAnnotationsJsonRef = React.useRef<string | undefined>(undefined);
|
|
316
|
+
const pageContainerRefs = React.useRef<(HTMLDivElement | null)[]>([]);
|
|
317
|
+
const docRef = React.useRef<PDFDocumentProxy | null>(null);
|
|
318
|
+
const loadingSourceRef = React.useRef<string | null>(null);
|
|
319
|
+
|
|
320
|
+
// ResizeObserver → containerWidth
|
|
321
|
+
React.useEffect(() => {
|
|
322
|
+
const el = scrollContainerRef.current;
|
|
323
|
+
if (!el) return;
|
|
324
|
+
const ro = new ResizeObserver((entries) => {
|
|
325
|
+
const width = entries[0]?.contentRect.width ?? 0;
|
|
326
|
+
setContainerWidth(width);
|
|
327
|
+
});
|
|
328
|
+
ro.observe(el);
|
|
329
|
+
return () => ro.disconnect();
|
|
330
|
+
}, []);
|
|
331
|
+
|
|
332
|
+
// Load PDF when source or containerWidth changes
|
|
333
|
+
React.useEffect(() => {
|
|
334
|
+
if (!source || containerWidth <= 0) return;
|
|
335
|
+
let cancelled = false;
|
|
336
|
+
loadingSourceRef.current = source;
|
|
337
|
+
|
|
338
|
+
async function load() {
|
|
339
|
+
try {
|
|
340
|
+
const task = pdfjs.getDocument(source);
|
|
341
|
+
const loadedDoc = await task.promise;
|
|
342
|
+
if (cancelled || loadingSourceRef.current !== source) {
|
|
343
|
+
loadedDoc.destroy();
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const metas: PdfPageMeta[] = [];
|
|
347
|
+
const dpr = window.devicePixelRatio ?? 1;
|
|
348
|
+
for (let i = 1; i <= loadedDoc.numPages; i++) {
|
|
349
|
+
const pg = await loadedDoc.getPage(i);
|
|
350
|
+
if (cancelled) {
|
|
351
|
+
pg.cleanup();
|
|
352
|
+
loadedDoc.destroy();
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const viewport = pg.getViewport({ scale: 1 });
|
|
356
|
+
const scale = containerWidth / viewport.width;
|
|
357
|
+
const cssHeight = viewport.height * scale;
|
|
358
|
+
metas.push({
|
|
359
|
+
pdfWidth: viewport.width,
|
|
360
|
+
pdfHeight: viewport.height,
|
|
361
|
+
scale,
|
|
362
|
+
canvasWidth: Math.round(containerWidth * dpr),
|
|
363
|
+
canvasHeight: Math.round(cssHeight * dpr),
|
|
364
|
+
});
|
|
365
|
+
pg.cleanup();
|
|
366
|
+
}
|
|
367
|
+
docRef.current?.destroy();
|
|
368
|
+
docRef.current = loadedDoc;
|
|
369
|
+
setDoc(loadedDoc);
|
|
370
|
+
setPageMetas(metas);
|
|
371
|
+
pageContainerRefs.current = new Array(loadedDoc.numPages).fill(null);
|
|
372
|
+
onLoadComplete?.({ nativeEvent: { pageCount: loadedDoc.numPages } });
|
|
373
|
+
} catch (err) {
|
|
374
|
+
if (!cancelled) {
|
|
375
|
+
onError?.({
|
|
376
|
+
nativeEvent: { message: err instanceof Error ? err.message : String(err) },
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
load();
|
|
382
|
+
return () => {
|
|
383
|
+
cancelled = true;
|
|
384
|
+
};
|
|
385
|
+
}, [source, containerWidth]); // intentionally omit callbacks — they should not re-trigger a PDF load
|
|
386
|
+
|
|
387
|
+
// Sync annotations prop → state (guard round-trips)
|
|
388
|
+
React.useEffect(() => {
|
|
389
|
+
if (annotationsProp === lastAnnotationsJsonRef.current) return;
|
|
390
|
+
lastAnnotationsJsonRef.current = annotationsProp;
|
|
391
|
+
const parsed = parseAnnotations(annotationsProp);
|
|
392
|
+
setAnnotations(parsed.annotations);
|
|
393
|
+
}, [annotationsProp]);
|
|
394
|
+
|
|
395
|
+
// Scroll to propPage
|
|
396
|
+
React.useEffect(() => {
|
|
397
|
+
if (propPage == null || !doc) return;
|
|
398
|
+
const el = pageContainerRefs.current[propPage];
|
|
399
|
+
el?.scrollIntoView({ behavior: 'instant' as ScrollBehavior });
|
|
400
|
+
}, [propPage, doc]);
|
|
401
|
+
|
|
402
|
+
// IntersectionObserver → onPageChanged
|
|
403
|
+
React.useEffect(() => {
|
|
404
|
+
if (!doc || pageMetas.length === 0) return;
|
|
405
|
+
const els = pageContainerRefs.current.filter(Boolean) as HTMLDivElement[];
|
|
406
|
+
if (els.length === 0) return;
|
|
407
|
+
let lastReported = -1;
|
|
408
|
+
const io = new IntersectionObserver(
|
|
409
|
+
(entries) => {
|
|
410
|
+
let topPage = -1;
|
|
411
|
+
let topY = Infinity;
|
|
412
|
+
for (const entry of entries) {
|
|
413
|
+
if (entry.isIntersecting) {
|
|
414
|
+
const idx = els.indexOf(entry.target as HTMLDivElement);
|
|
415
|
+
if (idx !== -1 && entry.boundingClientRect.top < topY) {
|
|
416
|
+
topY = entry.boundingClientRect.top;
|
|
417
|
+
topPage = idx;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (topPage !== -1 && topPage !== lastReported) {
|
|
422
|
+
lastReported = topPage;
|
|
423
|
+
onPageChanged?.({ nativeEvent: { page: topPage, pageCount: doc.numPages } });
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
{ root: scrollContainerRef.current, threshold: 0.1 }
|
|
427
|
+
);
|
|
428
|
+
for (const el of els) io.observe(el);
|
|
429
|
+
return () => io.disconnect();
|
|
430
|
+
}, [doc, pageMetas, onPageChanged]);
|
|
431
|
+
|
|
432
|
+
const handleAnnotationAdded = React.useCallback(
|
|
433
|
+
(annotation: Annotation) => {
|
|
434
|
+
setAnnotations((prev) => {
|
|
435
|
+
const next = [...prev, annotation];
|
|
436
|
+
const json = serializeAnnotations({ version: 1, annotations: next });
|
|
437
|
+
lastAnnotationsJsonRef.current = json;
|
|
438
|
+
onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
|
|
439
|
+
return next;
|
|
440
|
+
});
|
|
441
|
+
},
|
|
442
|
+
[onAnnotationsChanged]
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
const handleAnnotationRemoved = React.useCallback(
|
|
446
|
+
(id: string) => {
|
|
447
|
+
setAnnotations((prev) => {
|
|
448
|
+
const next = prev.filter((a) => a.id !== id);
|
|
449
|
+
const json = serializeAnnotations({ version: 1, annotations: next });
|
|
450
|
+
lastAnnotationsJsonRef.current = json;
|
|
451
|
+
onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
|
|
452
|
+
return next;
|
|
453
|
+
});
|
|
454
|
+
},
|
|
455
|
+
[onAnnotationsChanged]
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
const handleTextInputRequested = React.useCallback(
|
|
459
|
+
(pageIndex: number, pdfPoint: AnnotationPoint) => {
|
|
460
|
+
const text = window.prompt('Enter annotation text:');
|
|
461
|
+
if (!text) return;
|
|
462
|
+
const fontSize = 16;
|
|
463
|
+
const estimatedWidth = text.length * fontSize * 0.6;
|
|
464
|
+
const bounds = {
|
|
465
|
+
x: pdfPoint.x,
|
|
466
|
+
y: pdfPoint.y,
|
|
467
|
+
width: estimatedWidth,
|
|
468
|
+
height: fontSize * 1.5,
|
|
469
|
+
};
|
|
470
|
+
handleAnnotationAdded({
|
|
471
|
+
id: newAnnotationId(),
|
|
472
|
+
type: 'text',
|
|
473
|
+
page: pageIndex,
|
|
474
|
+
color: annotationColor,
|
|
475
|
+
bounds,
|
|
476
|
+
contents: text,
|
|
477
|
+
fontSize,
|
|
478
|
+
createdAt: Date.now(),
|
|
479
|
+
});
|
|
480
|
+
},
|
|
481
|
+
[annotationColor, handleAnnotationAdded]
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
const styleObj = style as React.CSSProperties | undefined;
|
|
485
|
+
|
|
7
486
|
return (
|
|
8
|
-
<
|
|
9
|
-
|
|
10
|
-
|
|
487
|
+
<div
|
|
488
|
+
ref={scrollContainerRef}
|
|
489
|
+
style={{
|
|
490
|
+
overflowY: 'scroll',
|
|
491
|
+
backgroundColor,
|
|
492
|
+
height: '100%',
|
|
493
|
+
...styleObj,
|
|
494
|
+
}}
|
|
495
|
+
>
|
|
496
|
+
{doc &&
|
|
497
|
+
pageMetas.map((meta, i) => (
|
|
498
|
+
<React.Fragment key={i}>
|
|
499
|
+
<div style={{ height: 8 }} aria-hidden />
|
|
500
|
+
<PageView
|
|
501
|
+
doc={doc}
|
|
502
|
+
pageIndex={i}
|
|
503
|
+
meta={meta}
|
|
504
|
+
annotations={annotations}
|
|
505
|
+
annotationMode={annotationMode}
|
|
506
|
+
annotationColor={annotationColor}
|
|
507
|
+
annotationLineWidth={annotationLineWidth}
|
|
508
|
+
onAnnotationAdded={handleAnnotationAdded}
|
|
509
|
+
onAnnotationRemoved={handleAnnotationRemoved}
|
|
510
|
+
onTextInputRequested={handleTextInputRequested}
|
|
511
|
+
containerRef={(el) => {
|
|
512
|
+
pageContainerRefs.current[i] = el;
|
|
513
|
+
}}
|
|
514
|
+
/>
|
|
515
|
+
</React.Fragment>
|
|
516
|
+
))}
|
|
517
|
+
</div>
|
|
11
518
|
);
|
|
12
519
|
}
|
package/src/index.ts
CHANGED
package/src/pdfjs.ts
ADDED
package/src/pdfjs.web.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { setPdfJsWorkerSrc } from './ExpoPdfMarkupView.web';
|