pdf-tsx 0.12.0 → 0.14.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.
@@ -3,91 +3,136 @@ import { useScrollTracking as t } from "../hooks/useScrollTracking.js";
3
3
  import { useKeyboardShortcuts as n } from "../hooks/useKeyboardShortcuts.js";
4
4
  import { useTextLayer as r } from "../hooks/useTextLayer.js";
5
5
  import { isSafeUrl as i } from "../utils/url.js";
6
- import { useAnnotationLayer as a } from "../hooks/useAnnotationLayer.js";
7
- import { usePDFLoader as ee } from "../hooks/usePDFLoader.js";
8
- import { usePDFRenderer as te } from "../hooks/usePDFRenderer.js";
9
- import { useOverlayRects as ne } from "../hooks/useOverlayRects.js";
10
- import { computeFit as o } from "../utils/pdfFit.js";
11
- import { ThemeProvider as re } from "../context/ThemeContext.js";
12
- import { LabelsProvider as ie } from "../context/LabelsContext.js";
6
+ import { useAnnotationLayer as ee } from "../hooks/useAnnotationLayer.js";
7
+ import { usePDFLoader as te } from "../hooks/usePDFLoader.js";
8
+ import { usePDFRenderer as ne } from "../hooks/usePDFRenderer.js";
9
+ import { useOverlayRects as re } from "../hooks/useOverlayRects.js";
10
+ import { computeFit as a } from "../utils/pdfFit.js";
11
+ import { ThemeProvider as ie } from "../context/ThemeContext.js";
12
+ import { LabelsProvider as ae } from "../context/LabelsContext.js";
13
+ import { useLabels as o } from "../hooks/useLabels.js";
13
14
  import s from "./PDFViewer.module.js";
14
15
  import { useCallback as c, useEffect as l, useMemo as u, useRef as d, useState as f } from "react";
15
- import * as ae from "pdfjs-dist";
16
- import { Fragment as oe, jsx as p, jsxs as m } from "react/jsx-runtime";
16
+ import * as oe from "pdfjs-dist";
17
+ import { Fragment as se, jsx as p, jsxs as m } from "react/jsx-runtime";
17
18
  //#region src/components/PDFViewer.tsx
18
- var h = 2, g = ({ file: g, workerSrc: _, sidebar: se, toolbar: v, onChangeFile: y, themeOverrides: ce, defaultTheme: le, defaultFit: b, language: ue, className: x, style: de }) => {
19
+ var h = 2;
20
+ function ce({ hasRead: e }) {
21
+ let t = o();
22
+ return /* @__PURE__ */ m("div", {
23
+ className: `${s.requireReadBanner} ${e ? s.requireReadDone : ""}`,
24
+ children: [e ? /* @__PURE__ */ p("svg", {
25
+ width: "16",
26
+ height: "16",
27
+ viewBox: "0 0 16 16",
28
+ fill: "none",
29
+ "aria-hidden": "true",
30
+ children: /* @__PURE__ */ p("path", {
31
+ d: "M3 8l3.5 3.5L13 4.5",
32
+ stroke: "currentColor",
33
+ strokeWidth: "2",
34
+ strokeLinecap: "round",
35
+ strokeLinejoin: "round"
36
+ })
37
+ }) : /* @__PURE__ */ p("svg", {
38
+ width: "16",
39
+ height: "16",
40
+ viewBox: "0 0 16 16",
41
+ fill: "none",
42
+ "aria-hidden": "true",
43
+ children: /* @__PURE__ */ p("path", {
44
+ d: "M8 3v10M4 9l4 4 4-4",
45
+ stroke: "currentColor",
46
+ strokeWidth: "2",
47
+ strokeLinecap: "round",
48
+ strokeLinejoin: "round"
49
+ })
50
+ }), /* @__PURE__ */ p("span", { children: e ? t.requireReadDone : t.requireReadPending })]
51
+ });
52
+ }
53
+ var g = ({ file: o, workerSrc: g, sidebar: le, toolbar: _, onChangeFile: v, themeOverrides: ue, defaultTheme: de, defaultFit: y, language: fe, className: b, style: pe, requireRead: x, onDocumentRead: S }) => {
19
54
  l(() => {
20
- ae.GlobalWorkerOptions.workerSrc = _;
21
- }, [_]);
22
- let [S, C] = f(1), [w, T] = f(0), [E, D] = f(null), [O, k] = f([]), [A, fe] = f(!1), [j, M] = f(b ?? null), [N, P] = f(null), F = d(null), I = d([]), L = d([]), R = d(w);
55
+ oe.GlobalWorkerOptions.workerSrc = g;
56
+ }, [g]);
57
+ let [C, w] = f(1), [T, E] = f(0), [D, O] = f(null), [k, me] = f([]), [A, he] = f(!1), [j, M] = f(y ?? null), [N, P] = f(null), [F, I] = f(!1), L = d(!1), R = d(null), z = d([]), B = d([]), V = d(T);
23
58
  l(() => {
24
- R.current = w;
25
- }, [w]);
26
- let [z, B] = f(null);
27
- z !== g && (B(g), D(null));
28
- let { pdf: V, numPages: H, loading: pe } = ee(g);
59
+ V.current = T;
60
+ }, [T]);
61
+ let [ge, _e] = f(null);
62
+ ge !== o && (_e(o), O(null), I(!1), L.current = !1);
63
+ let { pdf: H, numPages: U, loading: ve } = te(o);
29
64
  l(() => {
30
- V && (I.current = Array(V.numPages).fill(null), L.current = Array(V.numPages).fill(null));
31
- }, [V]), l(() => {
32
- !V || !b || !F.current || o(b, V, F.current, R.current).then(C);
33
- }, [V, b]);
34
- let U = d(j);
65
+ H && (z.current = Array(H.numPages).fill(null), B.current = Array(H.numPages).fill(null));
66
+ }, [H]), l(() => {
67
+ !H || !y || !R.current || a(y, H, R.current, V.current).then(w);
68
+ }, [H, y]);
69
+ let W = d(j);
35
70
  l(() => {
36
- U.current = j;
71
+ W.current = j;
37
72
  }, [j]), l(() => {
38
- if (!V || !F.current) return;
39
- let e = F.current, t = new ResizeObserver(() => {
40
- U.current && o(U.current, V, e, R.current).then(C);
73
+ if (!H || !R.current) return;
74
+ let e = R.current, t = new ResizeObserver(() => {
75
+ W.current && a(W.current, H, e, V.current).then(w);
41
76
  });
42
77
  return t.observe(e), () => t.disconnect();
43
- }, [V]);
44
- let { currentPage: W, goToPage: G } = t({
45
- numPages: H,
46
- containerRef: F,
47
- pageRefs: I
48
- }), K = u(() => {
49
- if (H === 0) return [];
50
- let e = /* @__PURE__ */ new Set(), t = Math.max(W - h, 1), n = Math.min(W + h, H);
78
+ }, [H]);
79
+ let { currentPage: G, goToPage: K } = t({
80
+ numPages: U,
81
+ containerRef: R,
82
+ pageRefs: z
83
+ });
84
+ l(() => {
85
+ U === 0 || G < U || (x && !F && I(!0), L.current || (L.current = !0, S?.()));
86
+ }, [
87
+ G,
88
+ U,
89
+ x,
90
+ F,
91
+ S
92
+ ]);
93
+ let q = u(() => {
94
+ if (U === 0) return [];
95
+ let e = /* @__PURE__ */ new Set(), t = Math.max(G - h, 1), n = Math.min(G + h, U);
51
96
  for (let r = t; r <= n; r++) e.add(r);
52
- E && e.add(E.page);
53
- for (let t of O) e.add(t.page);
97
+ D && e.add(D.page);
98
+ for (let t of k) e.add(t.page);
54
99
  return Array.from(e).sort((e, t) => e - t);
55
100
  }, [
56
- W,
57
- H,
58
- E,
59
- O
60
- ]), { pageSizes: me, renderedPages: q } = te({
61
- pdf: V,
62
- zoom: S,
63
- rotation: w,
64
- visiblePages: K,
65
- canvasRefs: L
66
- }), J = u(() => {
67
- let e = new Set(K);
68
- for (let t of q) e.add(t);
101
+ G,
102
+ U,
103
+ D,
104
+ k
105
+ ]), { pageSizes: ye, renderedPages: J } = ne({
106
+ pdf: H,
107
+ zoom: C,
108
+ rotation: T,
109
+ visiblePages: q,
110
+ canvasRefs: B
111
+ }), Y = u(() => {
112
+ let e = new Set(q);
113
+ for (let t of J) e.add(t);
69
114
  return Array.from(e).sort((e, t) => e - t);
70
- }, [K, q]);
115
+ }, [q, J]);
71
116
  n({
72
- pdf: V,
73
- currentPage: W,
74
- goToPage: G,
75
- setZoom: C,
76
- setRotation: T,
117
+ pdf: H,
118
+ currentPage: G,
119
+ goToPage: K,
120
+ setZoom: w,
121
+ setRotation: E,
77
122
  setFitMode: M
78
123
  });
79
- let he = r({
80
- pdf: V,
81
- zoom: S,
82
- rotation: w,
83
- mountedPages: J,
84
- canvasRefs: L
85
- }), { linkOverlays: ge, noteOverlays: _e } = a({
86
- pdf: V,
87
- zoom: S,
88
- rotation: w,
89
- mountedPages: J,
90
- goToPage: G
124
+ let be = r({
125
+ pdf: H,
126
+ zoom: C,
127
+ rotation: T,
128
+ mountedPages: Y,
129
+ canvasRefs: B
130
+ }), { linkOverlays: xe, noteOverlays: Se } = ee({
131
+ pdf: H,
132
+ zoom: C,
133
+ rotation: T,
134
+ mountedPages: Y,
135
+ goToPage: K
91
136
  });
92
137
  l(() => {
93
138
  if (!N) return;
@@ -98,16 +143,16 @@ var h = 2, g = ({ file: g, workerSrc: _, sidebar: se, toolbar: v, onChangeFile:
98
143
  document.removeEventListener("click", e), document.removeEventListener("keydown", t);
99
144
  };
100
145
  }, [N]);
101
- let { overlayRect: Y, searchOverlays: ve } = ne({
102
- pdf: V,
103
- highlight: E,
104
- searchMatches: O,
105
- zoom: S,
106
- rotation: w
107
- }), ye = c((e, t) => {
146
+ let { overlayRect: X, searchOverlays: Ce } = re({
147
+ pdf: H,
148
+ highlight: D,
149
+ searchMatches: k,
150
+ zoom: C,
151
+ rotation: T
152
+ }), we = c((e, t) => {
108
153
  (!e.url || !i(e.url)) && (t.preventDefault(), e.url || e._handleClick());
109
- }, []), X = d(null), Z = c((e, t) => {
110
- X.current && clearTimeout(X.current);
154
+ }, []), Z = d(null), Te = c((e, t) => {
155
+ Z.current && clearTimeout(Z.current);
111
156
  let n = t.clientY > window.innerHeight * .6;
112
157
  P({
113
158
  x: Math.min(t.clientX, window.innerWidth - 304),
@@ -117,196 +162,203 @@ var h = 2, g = ({ file: g, workerSrc: _, sidebar: se, toolbar: v, onChangeFile:
117
162
  title: e.title
118
163
  });
119
164
  }, []), Q = c(() => {
120
- X.current = setTimeout(() => P(null), 200);
165
+ Z.current = setTimeout(() => P(null), 200);
121
166
  }, []), $ = c((e) => {
122
- D({
167
+ O({
123
168
  page: e.page,
124
169
  pdfRect: e.pdfRect
125
- }), G(e.page);
126
- }, [D, G]), be = u(() => ({
127
- pdf: V,
128
- numPages: H,
129
- zoom: S,
130
- setZoom: C,
131
- rotation: w,
132
- setRotation: T,
133
- currentPage: W,
134
- goToPage: G,
135
- containerRef: F,
136
- canvasRefs: L,
137
- file: g,
138
- highlight: E,
139
- setHighlight: D,
140
- searchMatches: O,
141
- setSearchMatches: k,
142
- onChangeFile: y,
170
+ }), K(e.page);
171
+ }, [O, K]), Ee = u(() => ({
172
+ pdf: H,
173
+ numPages: U,
174
+ zoom: C,
175
+ setZoom: w,
176
+ rotation: T,
177
+ setRotation: E,
178
+ currentPage: G,
179
+ goToPage: K,
180
+ containerRef: R,
181
+ canvasRefs: B,
182
+ file: o,
183
+ highlight: D,
184
+ setHighlight: O,
185
+ searchMatches: k,
186
+ setSearchMatches: me,
187
+ onChangeFile: v,
143
188
  sidebarCollapsed: A,
144
- setSidebarCollapsed: fe,
189
+ setSidebarCollapsed: he,
145
190
  fitMode: j,
146
- setFitMode: M
191
+ setFitMode: M,
192
+ navigationLocked: !!x && !F
147
193
  }), [
148
- V,
149
194
  H,
150
- S,
151
- w,
152
- W,
195
+ U,
196
+ C,
197
+ T,
153
198
  G,
154
- g,
155
- E,
156
- O,
157
- y,
199
+ K,
200
+ o,
201
+ D,
202
+ k,
203
+ v,
158
204
  A,
159
- j
205
+ j,
206
+ x,
207
+ F
160
208
  ]);
161
- return /* @__PURE__ */ p(re, {
162
- themeOverrides: ce,
163
- defaultTheme: le,
164
- children: /* @__PURE__ */ p(ie, {
165
- language: ue,
209
+ return /* @__PURE__ */ p(ie, {
210
+ themeOverrides: ue,
211
+ defaultTheme: de,
212
+ children: /* @__PURE__ */ p(ae, {
213
+ language: fe,
166
214
  children: /* @__PURE__ */ m(e.Provider, {
167
- value: be,
215
+ value: Ee,
168
216
  children: [/* @__PURE__ */ m("div", {
169
- className: `${s.appLayout}${x ? ` ${x}` : ""}`,
170
- style: de,
171
- children: [v && /* @__PURE__ */ p("div", {
217
+ className: `${s.appLayout}${b ? ` ${b}` : ""}`,
218
+ style: pe,
219
+ children: [_ && /* @__PURE__ */ p("div", {
172
220
  className: s.toolbar,
173
- children: v
221
+ children: _
174
222
  }), /* @__PURE__ */ m("div", {
175
223
  className: s.body,
176
- children: [se, /* @__PURE__ */ m("div", {
224
+ children: [le, /* @__PURE__ */ m("div", {
177
225
  className: s.scrollContainer,
178
- ref: F,
179
- children: [pe && /* @__PURE__ */ p("div", { className: s.documentLoading }), Array.from({ length: H }, (e, t) => {
180
- let n = q.has(t + 1), r = me[t], a = J.includes(t + 1);
181
- return /* @__PURE__ */ m("div", {
182
- className: s.pageWrapper,
183
- ref: (e) => {
184
- I.current[t] = e;
185
- },
186
- style: r ? {
187
- width: r.width,
188
- height: r.height
189
- } : void 0,
190
- children: [
191
- r && !n && /* @__PURE__ */ p("div", {
192
- className: s.pageSkeleton,
193
- style: {
194
- width: r.width,
195
- height: r.height
196
- }
197
- }),
198
- a && /* @__PURE__ */ m(oe, { children: [/* @__PURE__ */ p("canvas", {
199
- className: s.pageCanvas,
200
- ref: (e) => {
201
- L.current[t] = e;
202
- },
203
- style: { display: n ? "block" : "none" }
204
- }), /* @__PURE__ */ p("div", {
205
- className: `textLayer ${s.textLayer}`,
206
- ref: (e) => {
207
- he.current[t] = e;
208
- }
209
- })] }),
210
- ge.filter((e) => e.page === t + 1).map((e, t) => /* @__PURE__ */ p("a", {
211
- className: s.linkOverlay,
212
- href: e.url && i(e.url) ? e.url : "#",
213
- target: "_blank",
214
- rel: "noopener noreferrer",
215
- style: {
216
- left: e.x,
217
- top: e.y,
218
- width: e.width,
219
- height: e.height
220
- },
221
- onClick: (t) => ye(e, t)
222
- }, t)),
223
- _e.filter((e) => e.page === t + 1).map((e, t) => e.subtype === "Text" ? /* @__PURE__ */ p("div", {
224
- className: s.noteIconOverlay,
225
- style: {
226
- left: e.x,
227
- top: e.y,
228
- width: Math.max(e.width, 26),
229
- height: Math.max(e.height, 26)
230
- },
231
- onMouseEnter: (t) => Z(e, t),
232
- onMouseLeave: Q,
233
- onClick: () => $(e),
234
- children: /* @__PURE__ */ m("svg", {
235
- width: "22",
236
- height: "22",
237
- viewBox: "0 0 12 12",
238
- fill: "none",
239
- "aria-hidden": "true",
240
- children: [
241
- /* @__PURE__ */ p("rect", {
242
- x: "0.5",
243
- y: "0.5",
244
- width: "9",
245
- height: "8",
246
- rx: "1",
247
- fill: "rgba(255,210,0,0.9)",
248
- stroke: "rgba(160,120,0,0.7)",
249
- strokeWidth: "1"
250
- }),
251
- /* @__PURE__ */ p("path", {
252
- d: "M9.5 5.5v3l2.5-3z",
253
- fill: "rgba(255,210,0,0.9)",
254
- stroke: "rgba(160,120,0,0.7)",
255
- strokeWidth: "1"
256
- }),
257
- /* @__PURE__ */ p("line", {
258
- x1: "2",
259
- y1: "3",
260
- x2: "8",
261
- y2: "3",
262
- stroke: "rgba(90,60,0,0.8)",
263
- strokeWidth: "1",
264
- strokeLinecap: "round"
265
- }),
266
- /* @__PURE__ */ p("line", {
267
- x1: "2",
268
- y1: "5.5",
269
- x2: "7",
270
- y2: "5.5",
271
- stroke: "rgba(90,60,0,0.8)",
272
- strokeWidth: "1",
273
- strokeLinecap: "round"
274
- })
275
- ]
226
+ ref: R,
227
+ children: [
228
+ ve && /* @__PURE__ */ p("div", { className: s.documentLoading }),
229
+ Array.from({ length: U }, (e, t) => {
230
+ let n = J.has(t + 1), r = ye[t], ee = Y.includes(t + 1);
231
+ return /* @__PURE__ */ m("div", {
232
+ className: s.pageWrapper,
233
+ ref: (e) => {
234
+ z.current[t] = e;
235
+ },
236
+ style: r ? {
237
+ width: r.width,
238
+ height: r.height
239
+ } : void 0,
240
+ children: [
241
+ r && !n && /* @__PURE__ */ p("div", {
242
+ className: s.pageSkeleton,
243
+ style: {
244
+ width: r.width,
245
+ height: r.height
246
+ }
247
+ }),
248
+ ee && /* @__PURE__ */ m(se, { children: [/* @__PURE__ */ p("canvas", {
249
+ className: s.pageCanvas,
250
+ ref: (e) => {
251
+ B.current[t] = e;
252
+ },
253
+ style: { display: n ? "block" : "none" }
254
+ }), /* @__PURE__ */ p("div", {
255
+ className: `textLayer ${s.textLayer}`,
256
+ ref: (e) => {
257
+ be.current[t] = e;
258
+ }
259
+ })] }),
260
+ xe.filter((e) => e.page === t + 1).map((e, t) => /* @__PURE__ */ p("a", {
261
+ className: s.linkOverlay,
262
+ href: e.url && i(e.url) ? e.url : "#",
263
+ target: "_blank",
264
+ rel: "noopener noreferrer",
265
+ style: {
266
+ left: e.x,
267
+ top: e.y,
268
+ width: e.width,
269
+ height: e.height
270
+ },
271
+ onClick: (t) => we(e, t)
272
+ }, t)),
273
+ Se.filter((e) => e.page === t + 1).map((e, t) => e.subtype === "Text" ? /* @__PURE__ */ p("div", {
274
+ className: s.noteIconOverlay,
275
+ style: {
276
+ left: e.x,
277
+ top: e.y,
278
+ width: Math.max(e.width, 26),
279
+ height: Math.max(e.height, 26)
280
+ },
281
+ onMouseEnter: (t) => Te(e, t),
282
+ onMouseLeave: Q,
283
+ onClick: () => $(e),
284
+ children: /* @__PURE__ */ m("svg", {
285
+ width: "22",
286
+ height: "22",
287
+ viewBox: "0 0 12 12",
288
+ fill: "none",
289
+ "aria-hidden": "true",
290
+ children: [
291
+ /* @__PURE__ */ p("rect", {
292
+ x: "0.5",
293
+ y: "0.5",
294
+ width: "9",
295
+ height: "8",
296
+ rx: "1",
297
+ fill: "rgba(255,210,0,0.9)",
298
+ stroke: "rgba(160,120,0,0.7)",
299
+ strokeWidth: "1"
300
+ }),
301
+ /* @__PURE__ */ p("path", {
302
+ d: "M9.5 5.5v3l2.5-3z",
303
+ fill: "rgba(255,210,0,0.9)",
304
+ stroke: "rgba(160,120,0,0.7)",
305
+ strokeWidth: "1"
306
+ }),
307
+ /* @__PURE__ */ p("line", {
308
+ x1: "2",
309
+ y1: "3",
310
+ x2: "8",
311
+ y2: "3",
312
+ stroke: "rgba(90,60,0,0.8)",
313
+ strokeWidth: "1",
314
+ strokeLinecap: "round"
315
+ }),
316
+ /* @__PURE__ */ p("line", {
317
+ x1: "2",
318
+ y1: "5.5",
319
+ x2: "7",
320
+ y2: "5.5",
321
+ stroke: "rgba(90,60,0,0.8)",
322
+ strokeWidth: "1",
323
+ strokeLinecap: "round"
324
+ })
325
+ ]
326
+ })
327
+ }, t) : /* @__PURE__ */ p("div", {
328
+ className: s.noteOverlay,
329
+ style: {
330
+ left: e.x,
331
+ top: e.y,
332
+ width: e.width,
333
+ height: e.height
334
+ },
335
+ onMouseEnter: (t) => Te(e, t),
336
+ onMouseLeave: Q,
337
+ onClick: () => $(e)
338
+ }, t)),
339
+ Ce.filter((e) => e.page === t + 1).map((e, t) => /* @__PURE__ */ p("div", {
340
+ className: s.searchMatchOverlay,
341
+ style: {
342
+ left: e.x,
343
+ top: e.y,
344
+ width: e.width,
345
+ height: e.height
346
+ }
347
+ }, t)),
348
+ D && X?.page === t + 1 && /* @__PURE__ */ p("div", {
349
+ className: s.highlightOverlay,
350
+ style: {
351
+ left: X.x,
352
+ top: X.y,
353
+ width: X.width,
354
+ height: X.height
355
+ }
276
356
  })
277
- }, t) : /* @__PURE__ */ p("div", {
278
- className: s.noteOverlay,
279
- style: {
280
- left: e.x,
281
- top: e.y,
282
- width: e.width,
283
- height: e.height
284
- },
285
- onMouseEnter: (t) => Z(e, t),
286
- onMouseLeave: Q,
287
- onClick: () => $(e)
288
- }, t)),
289
- ve.filter((e) => e.page === t + 1).map((e, t) => /* @__PURE__ */ p("div", {
290
- className: s.searchMatchOverlay,
291
- style: {
292
- left: e.x,
293
- top: e.y,
294
- width: e.width,
295
- height: e.height
296
- }
297
- }, t)),
298
- E && Y?.page === t + 1 && /* @__PURE__ */ p("div", {
299
- className: s.highlightOverlay,
300
- style: {
301
- left: Y.x,
302
- top: Y.y,
303
- width: Y.width,
304
- height: Y.height
305
- }
306
- })
307
- ]
308
- }, `${t}-${w}`);
309
- })]
357
+ ]
358
+ }, `${t}-${T}`);
359
+ }),
360
+ x && /* @__PURE__ */ p(ce, { hasRead: F })
361
+ ]
310
362
  })]
311
363
  })]
312
364
  }), N && /* @__PURE__ */ m("div", {
@@ -319,7 +371,7 @@ var h = 2, g = ({ file: g, workerSrc: _, sidebar: se, toolbar: v, onChangeFile:
319
371
  top: N.y
320
372
  },
321
373
  onMouseEnter: () => {
322
- X.current && clearTimeout(X.current);
374
+ Z.current && clearTimeout(Z.current);
323
375
  },
324
376
  onMouseLeave: Q,
325
377
  onClick: (e) => e.stopPropagation(),