@arbidocs/react 0.5.109 → 0.5.111

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.
Files changed (85) hide show
  1. package/dist/PdfViewerImpl-QUSDLNNM.cjs +380 -0
  2. package/dist/PdfViewerImpl-QUSDLNNM.cjs.map +1 -0
  3. package/dist/PdfViewerImpl-TEBYF2QI.js +378 -0
  4. package/dist/PdfViewerImpl-TEBYF2QI.js.map +1 -0
  5. package/dist/{PdfViewer.d-bQH9yxFg.d.cts → PdfViewerImpl.d-B28hLEuj.d.cts} +8 -1
  6. package/dist/{PdfViewer.d-bQH9yxFg.d.ts → PdfViewerImpl.d-B28hLEuj.d.ts} +8 -1
  7. package/dist/annotations/freeze/index.d.cts +1 -1
  8. package/dist/annotations/freeze/index.d.ts +1 -1
  9. package/dist/artifacts/index.cjs +13 -13
  10. package/dist/artifacts/index.js +3 -3
  11. package/dist/calls/livekit/index.cjs +48 -48
  12. package/dist/calls/livekit/index.js +3 -3
  13. package/dist/{chunk-W2FATOTX.cjs → chunk-3JAEX57E.cjs} +4 -4
  14. package/dist/{chunk-W2FATOTX.cjs.map → chunk-3JAEX57E.cjs.map} +1 -1
  15. package/dist/{chunk-H5M7FXLB.js → chunk-4FL3X3LA.js} +3 -3
  16. package/dist/{chunk-H5M7FXLB.js.map → chunk-4FL3X3LA.js.map} +1 -1
  17. package/dist/{chunk-HD5H2PPP.cjs → chunk-5TRUFWKT.cjs} +66 -66
  18. package/dist/{chunk-HD5H2PPP.cjs.map → chunk-5TRUFWKT.cjs.map} +1 -1
  19. package/dist/chunk-6QNBSXCE.js +271 -0
  20. package/dist/chunk-6QNBSXCE.js.map +1 -0
  21. package/dist/{chunk-QF2BW6HZ.cjs → chunk-DI5KSVKY.cjs} +38 -38
  22. package/dist/{chunk-QF2BW6HZ.cjs.map → chunk-DI5KSVKY.cjs.map} +1 -1
  23. package/dist/{chunk-DQALOQPI.js → chunk-DZKRL5H3.js} +4 -4
  24. package/dist/{chunk-DQALOQPI.js.map → chunk-DZKRL5H3.js.map} +1 -1
  25. package/dist/{chunk-RVUGSM65.cjs → chunk-GKGAPU6T.cjs} +44 -44
  26. package/dist/{chunk-RVUGSM65.cjs.map → chunk-GKGAPU6T.cjs.map} +1 -1
  27. package/dist/chunk-L2IPPTVC.js +20 -0
  28. package/dist/chunk-L2IPPTVC.js.map +1 -0
  29. package/dist/{chunk-FZJMA32S.js → chunk-LSU4UPS5.js} +4 -4
  30. package/dist/{chunk-FZJMA32S.js.map → chunk-LSU4UPS5.js.map} +1 -1
  31. package/dist/{chunk-7LIMHY56.cjs → chunk-O32T7XRD.cjs} +3 -3
  32. package/dist/{chunk-7LIMHY56.cjs.map → chunk-O32T7XRD.cjs.map} +1 -1
  33. package/dist/chunk-PWGSIM3F.cjs +291 -0
  34. package/dist/chunk-PWGSIM3F.cjs.map +1 -0
  35. package/dist/{chunk-CPQ3CU4B.js → chunk-QMY2JKBI.js} +3 -3
  36. package/dist/{chunk-CPQ3CU4B.js.map → chunk-QMY2JKBI.js.map} +1 -1
  37. package/dist/chunk-QP3WQSAA.cjs +23 -0
  38. package/dist/chunk-QP3WQSAA.cjs.map +1 -0
  39. package/dist/chunk-SSXFVWCT.js +46 -0
  40. package/dist/chunk-SSXFVWCT.js.map +1 -0
  41. package/dist/chunk-T5FZICXN.cjs +50 -0
  42. package/dist/chunk-T5FZICXN.cjs.map +1 -0
  43. package/dist/{chunk-P6WLV5UY.js → chunk-XWPLOFDN.js} +3 -3
  44. package/dist/{chunk-P6WLV5UY.js.map → chunk-XWPLOFDN.js.map} +1 -1
  45. package/dist/{chunk-5HDKL6XB.cjs → chunk-YMB6XUR2.cjs} +57 -57
  46. package/dist/{chunk-5HDKL6XB.cjs.map → chunk-YMB6XUR2.cjs.map} +1 -1
  47. package/dist/{chunk-6H3O63TQ.js → chunk-ZM6N7YZH.js} +3 -3
  48. package/dist/{chunk-6H3O63TQ.js.map → chunk-ZM6N7YZH.js.map} +1 -1
  49. package/dist/document-tools/core.cjs +98 -98
  50. package/dist/document-tools/core.d.cts +2 -2
  51. package/dist/document-tools/core.d.ts +2 -2
  52. package/dist/document-tools/core.js +3 -3
  53. package/dist/document-tools/index.cjs +100 -100
  54. package/dist/document-tools/index.d.cts +1 -1
  55. package/dist/document-tools/index.d.ts +1 -1
  56. package/dist/document-tools/index.js +4 -4
  57. package/dist/index.cjs +1426 -1341
  58. package/dist/index.cjs.map +1 -1
  59. package/dist/{index.d-BMhTH98a.d.ts → index.d-CYXcTKYt.d.ts} +1 -0
  60. package/dist/{index.d-BMhTH98a.d.cts → index.d-LArLnpI8.d.cts} +1 -0
  61. package/dist/index.d.cts +39 -8
  62. package/dist/index.d.ts +39 -8
  63. package/dist/index.js +104 -33
  64. package/dist/index.js.map +1 -1
  65. package/dist/lazy-chunk.d-B6YDLMKI.d.cts +31 -0
  66. package/dist/lazy-chunk.d-B6YDLMKI.d.ts +31 -0
  67. package/dist/{query-keys.d-ChgpJLPF.d.cts → query-keys.d-B1Am9tIF.d.cts} +1 -1
  68. package/dist/{query-keys.d-ChgpJLPF.d.ts → query-keys.d-B1Am9tIF.d.ts} +1 -1
  69. package/dist/ui/index.cjs +13 -0
  70. package/dist/ui/index.d.cts +2 -1
  71. package/dist/ui/index.d.ts +2 -1
  72. package/dist/ui/index.js +1 -0
  73. package/dist/viewers/PdfViewer.cjs +17 -0
  74. package/dist/viewers/PdfViewer.cjs.map +1 -0
  75. package/dist/viewers/PdfViewer.d.cts +9 -0
  76. package/dist/viewers/PdfViewer.d.ts +9 -0
  77. package/dist/viewers/PdfViewer.js +4 -0
  78. package/dist/viewers/PdfViewer.js.map +1 -0
  79. package/dist/viewers/index.cjs +79 -653
  80. package/dist/viewers/index.cjs.map +1 -1
  81. package/dist/viewers/index.d.cts +3 -1
  82. package/dist/viewers/index.d.ts +3 -1
  83. package/dist/viewers/index.js +6 -639
  84. package/dist/viewers/index.js.map +1 -1
  85. package/package.json +11 -3
@@ -1,644 +1,11 @@
1
- import { installPdfjsPolyfills } from '../chunk-PTOP7OHG.js';
1
+ export { PdfViewer, pdfViewerChunk } from '../chunk-L2IPPTVC.js';
2
+ export { INITIAL_LAZY_LOAD_BUFFER, LAZY_LOAD_BUFFER_AFTER, LAZY_LOAD_BUFFER_BEFORE, MAX_ZOOM, MIN_ZOOM, PAGE_SELECTOR, anchorDelta, anchoredScroll, clampZoom, cycleZoom, doubleTapZoom, pinchZoom, renderDpr, usePageIntersectionObserver, usePinchZoom, useScrollAnchor, zoomLabel } from '../chunk-6QNBSXCE.js';
2
3
  export { installPdfjsPolyfills } from '../chunk-PTOP7OHG.js';
3
4
  export { AudioViewer, ImageViewer, VideoViewer } from '../chunk-HDCRPPBW.js';
4
- import { useEffect, useRef, useCallback, useLayoutEffect, useState, useMemo } from 'react';
5
- import { Document, Page, pdfjs } from 'react-pdf';
6
- import 'react-pdf/dist/Page/AnnotationLayer.css';
7
- import 'react-pdf/dist/Page/TextLayer.css';
8
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
+ import '../chunk-SSXFVWCT.js';
6
+ import { useRef, useState, useEffect, useLayoutEffect, useMemo } from 'react';
7
+ import { jsxs, jsx } from 'react/jsx-runtime';
9
8
 
10
- var PAGE_SELECTOR = '[role="article"][data-page-number]';
11
- function anchorDelta(previousHeight, nextHeight, pageTop, containerTop) {
12
- const delta = nextHeight - previousHeight;
13
- if (delta === 0) return 0;
14
- return pageTop + previousHeight <= containerTop + 1 ? delta : 0;
15
- }
16
- function measureVisiblePages(pages, viewTop, viewBottom) {
17
- const visibility = /* @__PURE__ */ new Map();
18
- let page = null;
19
- let bestPx = 0;
20
- let score = 0;
21
- const viewHeight = viewBottom - viewTop;
22
- for (const p of pages) {
23
- const height = p.bottom - p.top;
24
- const px = Math.min(p.bottom, viewBottom) - Math.max(p.top, viewTop);
25
- if (px <= 0 || height <= 0) continue;
26
- visibility.set(p.n, px / height);
27
- if (px > bestPx) {
28
- bestPx = px;
29
- page = p.n;
30
- score = px / Math.min(height, viewHeight);
31
- }
32
- }
33
- return { page, score, visibility };
34
- }
35
- function useScrollAnchor(containerRef, key) {
36
- useEffect(() => {
37
- const container = containerRef.current;
38
- if (!container || typeof ResizeObserver === "undefined") return;
39
- container.style.overflowAnchor = "none";
40
- const heights = /* @__PURE__ */ new Map();
41
- const observer = new ResizeObserver((entries) => {
42
- let shift = 0;
43
- const containerTop = container.getBoundingClientRect().top;
44
- for (const entry of entries) {
45
- const el = entry.target;
46
- const height = el.getBoundingClientRect().height;
47
- const previous = heights.get(el);
48
- heights.set(el, height);
49
- if (previous === void 0) continue;
50
- shift += anchorDelta(previous, height, el.getBoundingClientRect().top, containerTop);
51
- }
52
- if (shift !== 0 && !container.dataset.zooming) container.scrollTop += shift;
53
- });
54
- container.querySelectorAll(PAGE_SELECTOR).forEach((el) => {
55
- heights.set(el, el.getBoundingClientRect().height);
56
- observer.observe(el);
57
- });
58
- return () => observer.disconnect();
59
- }, [containerRef, key]);
60
- }
61
-
62
- // src/viewers/usePageIntersectionObserver.ts
63
- var PAGE_UPDATE_DEBOUNCE_MS = 10;
64
- var MIN_VISIBILITY_THRESHOLD = 0.15;
65
- var INTERSECTION_THRESHOLDS = [0, 0.25, 0.5, 0.75, 1];
66
- function usePageIntersectionObserver({
67
- containerRef,
68
- scrollCompleteRef,
69
- isEnabled,
70
- getPageElements,
71
- getPageNumber,
72
- onPageChange,
73
- onVisibilityUpdate,
74
- observeKey
75
- }) {
76
- const latest = useRef({ getPageElements, getPageNumber, onPageChange, onVisibilityUpdate });
77
- latest.current = { getPageElements, getPageNumber, onPageChange, onVisibilityUpdate };
78
- useEffect(() => {
79
- if (!isEnabled || !containerRef.current) return;
80
- const container = containerRef.current;
81
- let lastReportedPage = null;
82
- let updateTimeout = null;
83
- let frame = 0;
84
- const evaluate = () => {
85
- const root = container.getBoundingClientRect();
86
- const pages = [];
87
- for (const el of latest.current.getPageElements()) {
88
- const n = latest.current.getPageNumber(el);
89
- if (n <= 0) continue;
90
- const r = el.getBoundingClientRect();
91
- pages.push({ n, top: r.top, bottom: r.bottom });
92
- }
93
- const { page, score, visibility } = measureVisiblePages(pages, root.top, root.bottom);
94
- if (scrollCompleteRef.current && page !== null && page !== lastReportedPage && score > MIN_VISIBILITY_THRESHOLD) {
95
- lastReportedPage = page;
96
- latest.current.onPageChange(page);
97
- }
98
- latest.current.onVisibilityUpdate?.(visibility);
99
- };
100
- const observer = new IntersectionObserver(
101
- () => {
102
- if (updateTimeout) clearTimeout(updateTimeout);
103
- updateTimeout = setTimeout(evaluate, PAGE_UPDATE_DEBOUNCE_MS);
104
- },
105
- { root: container, threshold: INTERSECTION_THRESHOLDS }
106
- );
107
- const onScroll = () => {
108
- if (!frame) {
109
- frame = requestAnimationFrame(() => {
110
- frame = 0;
111
- evaluate();
112
- });
113
- }
114
- };
115
- container.addEventListener("scroll", onScroll, { passive: true });
116
- latest.current.getPageElements().forEach((element) => observer.observe(element));
117
- return () => {
118
- observer.disconnect();
119
- container.removeEventListener("scroll", onScroll);
120
- if (updateTimeout) clearTimeout(updateTimeout);
121
- if (frame) cancelAnimationFrame(frame);
122
- };
123
- }, [containerRef, scrollCompleteRef, isEnabled, observeKey]);
124
- }
125
- var MIN_ZOOM = 0.5;
126
- var MAX_ZOOM = 4;
127
- var DOUBLE_TAP_ZOOM = 2;
128
- var MAX_CANVAS_PIXELS = 8e6;
129
- var MAX_RENDER_DPR = 2;
130
- function clampZoom(zoom) {
131
- if (!Number.isFinite(zoom)) return 1;
132
- return Math.round(Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, zoom)) * 100) / 100;
133
- }
134
- var ZOOM_PRESETS = [1.5, 2, 3];
135
- function cycleZoom(zoom) {
136
- return ZOOM_PRESETS.find((preset) => preset > zoom + 0.05) ?? 1;
137
- }
138
- function doubleTapZoom(zoom) {
139
- return zoom > 1.05 ? 1 : DOUBLE_TAP_ZOOM;
140
- }
141
- function zoomLabel(zoom) {
142
- return `${Math.round(zoom * 100)}%`;
143
- }
144
- function pinchZoom(startZoom, startDistance, distance2) {
145
- if (startDistance <= 0) return startZoom;
146
- return clampZoom(startZoom * distance2 / startDistance);
147
- }
148
- function anchoredScroll(scroll, focal, oldZoom, newZoom) {
149
- if (oldZoom <= 0) return scroll;
150
- return (scroll + focal) * newZoom / oldZoom - focal;
151
- }
152
- function renderDpr(cssWidth, cssHeight, devicePixelRatio, maxPixels = MAX_CANVAS_PIXELS) {
153
- const wanted = Math.min(Math.max(devicePixelRatio || 1, 1), MAX_RENDER_DPR);
154
- const area = cssWidth * cssHeight;
155
- if (area <= 0) return wanted;
156
- const fits = Math.sqrt(maxPixels / area);
157
- return Math.max(1, Math.min(wanted, fits));
158
- }
159
- function fitPageWidth(contentWidth, pageBorder) {
160
- if (!Number.isFinite(contentWidth)) return 0;
161
- return Math.max(0, Math.floor(contentWidth) - pageBorder);
162
- }
163
- function contentBoxWidth(el) {
164
- const style = getComputedStyle(el);
165
- const padding = (parseFloat(style.paddingLeft) || 0) + (parseFloat(style.paddingRight) || 0);
166
- return Math.max(0, el.clientWidth - padding);
167
- }
168
- var distance = (a, b) => Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY);
169
- var INTERACTIVE = 'button, a, input, textarea, select, [role="button"]';
170
- var DOUBLE_TAP_MS = 300;
171
- var DOUBLE_TAP_SLOP_PX = 30;
172
- function usePinchZoom({ containerRef, zoom, onZoomChange }) {
173
- const zoomRef = useRef(zoom);
174
- const onChangeRef = useRef(onZoomChange);
175
- onChangeRef.current = onZoomChange;
176
- const focalRef = useRef(null);
177
- const appliedZoomRef = useRef(zoom);
178
- const request = useCallback((next, focal) => {
179
- const clamped = clampZoom(next);
180
- if (clamped === zoomRef.current) return;
181
- focalRef.current = focal;
182
- zoomRef.current = clamped;
183
- onChangeRef.current?.(clamped);
184
- }, []);
185
- useLayoutEffect(() => {
186
- zoomRef.current = zoom;
187
- const container = containerRef.current;
188
- const previous = appliedZoomRef.current;
189
- appliedZoomRef.current = zoom;
190
- if (!container || previous === zoom) return;
191
- const rect = container.getBoundingClientRect();
192
- const focal = focalRef.current ?? { x: rect.width / 2, y: rect.height / 2 };
193
- focalRef.current = null;
194
- container.dataset.zooming = "true";
195
- container.scrollLeft = anchoredScroll(container.scrollLeft, focal.x, previous, zoom);
196
- container.scrollTop = anchoredScroll(container.scrollTop, focal.y, previous, zoom);
197
- requestAnimationFrame(
198
- () => requestAnimationFrame(() => {
199
- delete container.dataset.zooming;
200
- })
201
- );
202
- }, [zoom, containerRef]);
203
- useEffect(() => {
204
- const el = containerRef.current;
205
- if (!el) return;
206
- let pinch = null;
207
- let lastTap = null;
208
- let frame = 0;
209
- let pending = null;
210
- const focalOf = (a, b) => {
211
- const rect = el.getBoundingClientRect();
212
- return {
213
- x: (a.clientX + b.clientX) / 2 - rect.left,
214
- y: (a.clientY + b.clientY) / 2 - rect.top
215
- };
216
- };
217
- const flush = () => {
218
- frame = 0;
219
- if (pending) request(pending.zoom, pending.focal);
220
- pending = null;
221
- };
222
- const onStart = (e) => {
223
- if (e.touches.length === 2) {
224
- pinch = { startDistance: distance(e.touches[0], e.touches[1]), startZoom: zoomRef.current };
225
- lastTap = null;
226
- }
227
- };
228
- const onMove = (e) => {
229
- if (!pinch || e.touches.length !== 2) return;
230
- e.preventDefault();
231
- pending = {
232
- zoom: pinchZoom(pinch.startZoom, pinch.startDistance, distance(e.touches[0], e.touches[1])),
233
- focal: focalOf(e.touches[0], e.touches[1])
234
- };
235
- if (!frame) frame = requestAnimationFrame(flush);
236
- };
237
- const onEnd = (e) => {
238
- if (pinch && e.touches.length < 2) {
239
- pinch = null;
240
- return;
241
- }
242
- if (e.touches.length !== 0 || e.changedTouches.length !== 1) return;
243
- const t = e.changedTouches[0];
244
- if (e.target?.closest?.(INTERACTIVE)) return;
245
- const now = e.timeStamp;
246
- if (lastTap && now - lastTap.time < DOUBLE_TAP_MS && Math.hypot(t.clientX - lastTap.x, t.clientY - lastTap.y) < DOUBLE_TAP_SLOP_PX) {
247
- e.preventDefault();
248
- const rect = el.getBoundingClientRect();
249
- request(doubleTapZoom(zoomRef.current), {
250
- x: t.clientX - rect.left,
251
- y: t.clientY - rect.top
252
- });
253
- lastTap = null;
254
- } else {
255
- lastTap = { time: now, x: t.clientX, y: t.clientY };
256
- }
257
- };
258
- el.addEventListener("touchstart", onStart, { passive: true });
259
- el.addEventListener("touchmove", onMove, { passive: false });
260
- el.addEventListener("touchend", onEnd, { passive: false });
261
- el.addEventListener("touchcancel", onEnd, { passive: false });
262
- return () => {
263
- el.removeEventListener("touchstart", onStart);
264
- el.removeEventListener("touchmove", onMove);
265
- el.removeEventListener("touchend", onEnd);
266
- el.removeEventListener("touchcancel", onEnd);
267
- if (frame) cancelAnimationFrame(frame);
268
- };
269
- }, [containerRef, request]);
270
- }
271
-
272
- // src/viewers/pdf-viewer-constants.ts
273
- var LAZY_LOAD_BUFFER_BEFORE = 2;
274
- var LAZY_LOAD_BUFFER_AFTER = 5;
275
- var INITIAL_LAZY_LOAD_BUFFER = { before: 0, after: 1 };
276
- var DEFAULT_ASSETS = {
277
- workerSrc: "/pdf.worker.min.mjs",
278
- cMapUrl: "/cmaps/",
279
- standardFontDataUrl: "/standard_fonts/",
280
- wasmUrl: "/wasm/"
281
- };
282
- function ensurePdfWorker(workerSrc) {
283
- installPdfjsPolyfills();
284
- if (pdfjs.GlobalWorkerOptions.workerSrc !== workerSrc) {
285
- pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
286
- }
287
- }
288
- var DEFAULT_PAGE_ASPECT_RATIO = 1.414;
289
- var PAGE_BORDER_PX = 2;
290
- var RERENDER_DEBOUNCE_MS = 250;
291
- var ZOOMED_BUFFER_THRESHOLD = 1.5;
292
- function sameSet(a, b) {
293
- if (a.size !== b.size) return false;
294
- for (const v of a) if (!b.has(v)) return false;
295
- return true;
296
- }
297
- var TEXT_LAYER_BUFFER = 1;
298
- var ratioOf = (page) => {
299
- const { width, height } = page.getViewport({ scale: 1 });
300
- return width > 0 && height > 0 ? height / width : DEFAULT_PAGE_ASPECT_RATIO;
301
- };
302
- function PdfViewer({
303
- file,
304
- currentPage,
305
- onLoadSuccess,
306
- scrollTrigger,
307
- zoom = 1,
308
- onZoomChange,
309
- onPageChange,
310
- disableFontFace = false,
311
- assets,
312
- renderPageOverlay
313
- }) {
314
- const workerSrc = assets?.workerSrc ?? DEFAULT_ASSETS.workerSrc;
315
- const cMapUrl = assets?.cMapUrl ?? DEFAULT_ASSETS.cMapUrl;
316
- const standardFontDataUrl = assets?.standardFontDataUrl ?? DEFAULT_ASSETS.standardFontDataUrl;
317
- const wasmUrl = assets?.wasmUrl ?? DEFAULT_ASSETS.wasmUrl;
318
- ensurePdfWorker(workerSrc);
319
- const [numPages, setNumPages] = useState(0);
320
- const [containerWidth, setContainerWidth] = useState(0);
321
- const [defaultRatio, setDefaultRatio] = useState(DEFAULT_PAGE_ASPECT_RATIO);
322
- const [pageRatios, setPageRatios] = useState(/* @__PURE__ */ new Map());
323
- const [pageWidthsPt, setPageWidthsPt] = useState(/* @__PURE__ */ new Map());
324
- const [loadError, setLoadError] = useState(null);
325
- const [documentKey, setDocumentKey] = useState(() => `pdf-${Date.now()}`);
326
- const initialPage = currentPage || 1;
327
- const [renderedPages, setRenderedPages] = useState(
328
- /* @__PURE__ */ new Set([
329
- Math.max(1, initialPage - INITIAL_LAZY_LOAD_BUFFER.before),
330
- initialPage,
331
- initialPage + INITIAL_LAZY_LOAD_BUFFER.after
332
- ])
333
- );
334
- const [visiblePages, setVisiblePages] = useState(/* @__PURE__ */ new Set([initialPage]));
335
- const loadLifetime = useMemo(() => ({ file, active: true }), [file]);
336
- useEffect(() => {
337
- loadLifetime.active = true;
338
- return () => {
339
- loadLifetime.active = false;
340
- };
341
- }, [loadLifetime]);
342
- useEffect(() => {
343
- setDocumentKey(`pdf-${Date.now()}`);
344
- setNumPages(0);
345
- setLoadError(null);
346
- }, [file]);
347
- const containerRef = useRef(null);
348
- const pageRefs = useRef(/* @__PURE__ */ new Map());
349
- const onDocumentLoadSuccess = useCallback(
350
- (pdf) => {
351
- if (!loadLifetime.active) return;
352
- const n = pdf.numPages;
353
- void pdf.getPage?.(1).then((page) => {
354
- if (loadLifetime.active) setDefaultRatio(ratioOf(page));
355
- }).catch(() => {
356
- });
357
- setNumPages(n);
358
- setLoadError(null);
359
- try {
360
- onLoadSuccess?.(n);
361
- } catch (error) {
362
- console.error("Error in onLoadSuccess callback:", error);
363
- }
364
- },
365
- [onLoadSuccess, loadLifetime]
366
- );
367
- useEffect(() => {
368
- if (numPages === 0) return;
369
- let cancelled = false;
370
- const expand = () => {
371
- if (cancelled || !loadLifetime.active) return;
372
- setRenderedPages((prev) => {
373
- if (prev.size === 0) return prev;
374
- const newSet = new Set(prev);
375
- const anchor = currentPage || 1;
376
- for (let i = Math.max(1, anchor - LAZY_LOAD_BUFFER_BEFORE); i <= Math.min(numPages, anchor + LAZY_LOAD_BUFFER_AFTER); i++) {
377
- newSet.add(i);
378
- }
379
- return newSet;
380
- });
381
- };
382
- if (typeof requestIdleCallback !== "undefined") {
383
- const handle2 = requestIdleCallback(expand, { timeout: 500 });
384
- return () => {
385
- cancelled = true;
386
- if (typeof cancelIdleCallback !== "undefined") cancelIdleCallback(handle2);
387
- };
388
- }
389
- const handle = setTimeout(expand, 500);
390
- return () => {
391
- cancelled = true;
392
- clearTimeout(handle);
393
- };
394
- }, [numPages, currentPage, loadLifetime]);
395
- const onDocumentLoadError = useCallback((error) => {
396
- console.error("PDF loading error:", error);
397
- setLoadError(error);
398
- setNumPages(0);
399
- }, []);
400
- const onPassword = useCallback((updatePassword, reason) => {
401
- const message = reason === 2 ? "Incorrect password. Enter the password to unlock this PDF:" : "This PDF is password-protected. Enter the password to view it:";
402
- const password = window.prompt(message);
403
- if (password === null) {
404
- setLoadError(new Error("Password required to open this PDF"));
405
- setNumPages(0);
406
- return;
407
- }
408
- updatePassword(password);
409
- }, []);
410
- useEffect(() => {
411
- const container = containerRef.current;
412
- if (!container) return;
413
- const setFromContentWidth = (contentWidth) => setContainerWidth(fitPageWidth(contentWidth, PAGE_BORDER_PX));
414
- setFromContentWidth(contentBoxWidth(container));
415
- const resizeObserver = new ResizeObserver((entries) => {
416
- const entry = entries[entries.length - 1];
417
- const box = entry?.contentBoxSize?.[0];
418
- setFromContentWidth(box ? box.inlineSize : entry?.contentRect.width ?? 0);
419
- });
420
- resizeObserver.observe(container);
421
- return () => {
422
- resizeObserver.disconnect();
423
- };
424
- }, []);
425
- const scrollCompleteRef = useRef(false);
426
- useEffect(() => {
427
- if (numPages === 0) return;
428
- if (scrollCompleteRef.current) return;
429
- if (currentPage) {
430
- setRenderedPages((prev) => {
431
- const newSet = new Set(prev);
432
- for (let i = Math.max(1, currentPage - LAZY_LOAD_BUFFER_BEFORE); i <= Math.min(numPages, currentPage + LAZY_LOAD_BUFFER_AFTER); i++) {
433
- newSet.add(i);
434
- }
435
- return newSet;
436
- });
437
- requestAnimationFrame(() => {
438
- const pageElement = pageRefs.current.get(currentPage);
439
- if (pageElement) {
440
- pageElement.scrollIntoView({ behavior: "auto", block: "start" });
441
- }
442
- setTimeout(() => {
443
- scrollCompleteRef.current = true;
444
- }, 100);
445
- });
446
- }
447
- }, [numPages]);
448
- const currentPageRef = useRef(currentPage);
449
- currentPageRef.current = currentPage;
450
- useEffect(() => {
451
- const currentPage2 = currentPageRef.current;
452
- if (!scrollTrigger || numPages === 0 || !currentPage2) return;
453
- scrollCompleteRef.current = false;
454
- setRenderedPages((prev) => {
455
- const newSet = new Set(prev);
456
- for (let i = Math.max(1, currentPage2 - LAZY_LOAD_BUFFER_BEFORE); i <= Math.min(numPages, currentPage2 + LAZY_LOAD_BUFFER_AFTER); i++) {
457
- newSet.add(i);
458
- }
459
- return newSet;
460
- });
461
- requestAnimationFrame(() => {
462
- requestAnimationFrame(() => {
463
- const pageElement = pageRefs.current.get(currentPage2);
464
- if (pageElement) {
465
- pageElement.scrollIntoView({ behavior: "instant", block: "start" });
466
- }
467
- scrollCompleteRef.current = true;
468
- });
469
- });
470
- }, [scrollTrigger, numPages]);
471
- const handleVisibilityUpdate = useCallback(
472
- (visibilityMap) => {
473
- const visiblePageNums = Array.from(visibilityMap.entries()).filter(([, ratio]) => ratio > 0).map(([pageNum]) => pageNum);
474
- if (visiblePageNums.length > 0) {
475
- const minVisible = Math.min(...visiblePageNums);
476
- const maxVisible = Math.max(...visiblePageNums);
477
- const zoomed = zoom > ZOOMED_BUFFER_THRESHOLD;
478
- const before = zoomed ? 1 : LAZY_LOAD_BUFFER_BEFORE;
479
- const after = zoomed ? 2 : LAZY_LOAD_BUFFER_AFTER;
480
- const pagesToRender = /* @__PURE__ */ new Set();
481
- for (let i = Math.max(1, minVisible - before); i <= Math.min(numPages, maxVisible + after); i++) {
482
- pagesToRender.add(i);
483
- }
484
- setRenderedPages((prev) => sameSet(prev, pagesToRender) ? prev : pagesToRender);
485
- const nearViewport = /* @__PURE__ */ new Set();
486
- for (let i = Math.max(1, minVisible - TEXT_LAYER_BUFFER); i <= Math.min(numPages, maxVisible + TEXT_LAYER_BUFFER); i++) {
487
- nearViewport.add(i);
488
- }
489
- setVisiblePages((prev) => sameSet(prev, nearViewport) ? prev : nearViewport);
490
- }
491
- },
492
- [numPages, zoom]
493
- );
494
- const documentOptions = useMemo(
495
- () => ({
496
- standardFontDataUrl,
497
- cMapUrl,
498
- cMapPacked: true,
499
- wasmUrl,
500
- ...disableFontFace && { disableFontFace: true }
501
- }),
502
- [standardFontDataUrl, cMapUrl, wasmUrl, disableFontFace]
503
- );
504
- const handlePageChange = useCallback(
505
- (pageNumber) => onPageChange?.(pageNumber),
506
- [onPageChange]
507
- );
508
- usePageIntersectionObserver({
509
- containerRef,
510
- scrollCompleteRef,
511
- isEnabled: numPages > 0,
512
- getPageElements: () => Array.from(pageRefs.current.values()),
513
- getPageNumber: (element) => parseInt(element.getAttribute("data-page-number") ?? "0"),
514
- onPageChange: handlePageChange,
515
- onVisibilityUpdate: handleVisibilityUpdate,
516
- observeKey: `${numPages}:${containerWidth > 0}`
517
- });
518
- useScrollAnchor(containerRef, `${numPages}:${containerWidth > 0}`);
519
- usePinchZoom({ containerRef, zoom, onZoomChange });
520
- const [renderZoom, setRenderZoom] = useState(zoom);
521
- useEffect(() => {
522
- if (zoom === renderZoom) return;
523
- const timer = setTimeout(() => setRenderZoom(zoom), RERENDER_DEBOUNCE_MS);
524
- return () => clearTimeout(timer);
525
- }, [zoom, renderZoom]);
526
- return /* @__PURE__ */ jsx(
527
- "div",
528
- {
529
- ref: containerRef,
530
- className: "w-full h-full overflow-auto bg-muted flex flex-col items-start p-4 pb-20",
531
- style: { touchAction: "pan-x pan-y", scrollbarGutter: "stable" },
532
- role: "region",
533
- "aria-label": "PDF document viewer",
534
- "data-testid": "pdf-viewer",
535
- children: /* @__PURE__ */ jsx(
536
- Document,
537
- {
538
- className: "flex w-max min-w-full flex-col",
539
- suspense: false,
540
- file,
541
- onLoadSuccess: onDocumentLoadSuccess,
542
- onLoadError: onDocumentLoadError,
543
- onPassword,
544
- options: documentOptions,
545
- loading: /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center h-full", children: /* @__PURE__ */ jsx("div", { className: "text-sm text-muted-foreground", children: "Loading PDF..." }) }),
546
- error: /* @__PURE__ */ jsx("div", { className: "flex flex-col items-center justify-center h-full gap-4 p-8", children: /* @__PURE__ */ jsx("div", { className: "text-destructive text-center", children: loadError?.message === "Cannot read properties of null (reading 'sendWithPromise')" ? /* @__PURE__ */ jsxs(Fragment, { children: [
547
- /* @__PURE__ */ jsx("p", { className: "font-semibold mb-2", children: "PDF viewer initialization failed" }),
548
- /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: "Please refresh the page to retry" })
549
- ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
550
- /* @__PURE__ */ jsx("p", { className: "font-semibold mb-2", children: "Failed to load PDF" }),
551
- loadError && /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: loadError.message })
552
- ] }) }) }),
553
- children: containerWidth > 0 && Array.from(new Array(numPages), (_, index) => {
554
- const pageNumber = index + 1;
555
- const shouldRender = renderedPages.has(pageNumber);
556
- const ratio = pageRatios.get(pageNumber) ?? defaultRatio;
557
- const pageWidth = containerWidth * zoom;
558
- const pageHeight = pageWidth * ratio;
559
- const renderWidth = containerWidth * renderZoom;
560
- const scale = zoom / renderZoom;
561
- const shouldRenderTextLayer = visiblePages.has(pageNumber);
562
- return /* @__PURE__ */ jsxs(
563
- "div",
564
- {
565
- ref: (el) => {
566
- if (el) {
567
- pageRefs.current.set(pageNumber, el);
568
- } else {
569
- pageRefs.current.delete(pageNumber);
570
- }
571
- },
572
- "data-page-number": pageNumber,
573
- className: "relative mx-auto mb-6 shrink-0 shadow-xl bg-white border border-border",
574
- style: { width: pageWidth + PAGE_BORDER_PX, height: pageHeight + PAGE_BORDER_PX },
575
- role: "article",
576
- "aria-label": `Page ${pageNumber} of ${numPages}`,
577
- children: [
578
- shouldRender ? /* @__PURE__ */ jsx(
579
- "div",
580
- {
581
- "data-testid": `pdf-page-scaler-${pageNumber}`,
582
- style: scale === 1 ? void 0 : { transform: `scale(${scale})`, transformOrigin: "0 0" },
583
- children: /* @__PURE__ */ jsx(
584
- Page,
585
- {
586
- pageNumber,
587
- width: renderWidth,
588
- renderTextLayer: shouldRenderTextLayer,
589
- renderAnnotationLayer: false,
590
- onLoadSuccess: (page) => {
591
- setPageWidthsPt(
592
- (prev) => prev.get(pageNumber) === page.originalWidth ? prev : new Map(prev).set(pageNumber, page.originalWidth)
593
- );
594
- const ratio2 = page.originalHeight / page.originalWidth;
595
- setPageRatios(
596
- (prev) => Math.abs((prev.get(pageNumber) ?? defaultRatio) - ratio2) < 1e-3 ? prev : new Map(prev).set(pageNumber, ratio2)
597
- );
598
- },
599
- devicePixelRatio: renderDpr(
600
- renderWidth,
601
- renderWidth * ratio,
602
- window.devicePixelRatio || 1
603
- ),
604
- loading: /* @__PURE__ */ jsx(
605
- "div",
606
- {
607
- className: "flex items-center justify-center bg-muted",
608
- style: { width: renderWidth, height: renderWidth * ratio },
609
- children: /* @__PURE__ */ jsxs("div", { className: "text-sm text-muted-foreground", children: [
610
- "Loading page ",
611
- pageNumber,
612
- "..."
613
- ] })
614
- }
615
- )
616
- }
617
- )
618
- }
619
- ) : /* @__PURE__ */ jsx(
620
- "div",
621
- {
622
- className: "flex items-center justify-center bg-muted",
623
- style: { width: pageWidth, height: pageHeight },
624
- children: /* @__PURE__ */ jsxs("div", { className: "text-sm text-muted-foreground", children: [
625
- "Page ",
626
- pageNumber
627
- ] })
628
- }
629
- ),
630
- renderPageOverlay?.({ pageNumber, pageWidthPt: pageWidthsPt.get(pageNumber) })
631
- ]
632
- },
633
- `page_${pageNumber}`
634
- );
635
- })
636
- },
637
- documentKey
638
- )
639
- }
640
- );
641
- }
642
9
  function measureContentWidth(container) {
643
10
  let pageWidth = 0;
644
11
  container.querySelectorAll("section.docx").forEach((section) => {
@@ -900,6 +267,6 @@ function computeSelectionRuns(pages, selectedChunks) {
900
267
  return runs;
901
268
  }
902
269
 
903
- export { DocxViewer, INITIAL_LAZY_LOAD_BUFFER, LAZY_LOAD_BUFFER_AFTER, LAZY_LOAD_BUFFER_BEFORE, MAX_ZOOM, MIN_ZOOM, PAGE_SELECTOR, PdfViewer, XlsxViewer, anchorDelta, anchoredScroll, clampZoom, computeSelectionRuns, cycleZoom, doubleTapZoom, pinchZoom, renderDpr, usePageIntersectionObserver, usePinchZoom, useProcessedChunks, useScrollAnchor, zoomLabel };
270
+ export { DocxViewer, XlsxViewer, computeSelectionRuns, useProcessedChunks };
904
271
  //# sourceMappingURL=index.js.map
905
272
  //# sourceMappingURL=index.js.map