@react-pdf-kit/viewer 2.0.1-rc.3 → 2.1.0-beta.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/README.md +49 -84
- package/dist/components/RPController.js +73 -74
- package/dist/components/RPPages.js +544 -545
- package/dist/components/layout/toolbar/SearchTool.js +96 -93
- package/dist/contexts/InitialStateContext.js +6 -8
- package/dist/types/utils/hooks/useWatermark.d.ts +1 -1
- package/dist/types/utils/types.d.ts +1 -1
- package/dist/utils/highlight.js +36 -36
- package/dist/utils/hooks/useHighlight.js +40 -78
- package/dist/utils/hooks/useLicense.js +1 -1
- package/dist/utils/hooks/usePrint.js +93 -82
- package/dist/utils/hooks/useWatermark.js +39 -57
- package/dist/utils/injectPrintCSS.js +2 -1
- package/package.json +1 -1
|
@@ -1,93 +1,55 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
import { getHighlightOptionsWithDefaults as
|
|
1
|
+
import { useState as m, useCallback as r, useEffect as C } from "react";
|
|
2
|
+
import { getHighlightOptionsWithDefaults as H, findMatches as M } from "../highlight.js";
|
|
3
3
|
import "../charators.js";
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
}, N = (n) => n.flatMap((e) => e.rects ?? (e.rect ? [e.rect] : [])), O = (n) => {
|
|
8
|
-
const e = n.reduce((o, t) => {
|
|
9
|
-
const s = Math.round(t.bottom);
|
|
10
|
-
return o[s] || (o[s] = []), o[s].push(t), o;
|
|
11
|
-
}, {});
|
|
12
|
-
return Object.entries(e).sort((o, t) => Number(t[0]) - Number(o[0])).map(([o, t]) => x(t));
|
|
13
|
-
}, L = (n) => {
|
|
14
|
-
const e = {};
|
|
15
|
-
for (const c of n) {
|
|
16
|
-
const o = c.keyword, t = e[o] ?? [];
|
|
17
|
-
t.push(c), e[o] = t;
|
|
18
|
-
}
|
|
19
|
-
return e;
|
|
20
|
-
}, j = (n, e) => {
|
|
21
|
-
const c = n[0], o = N(n), t = o.length > 0 ? O(o) : c.rect ? [c.rect] : [];
|
|
22
|
-
return {
|
|
23
|
-
...c,
|
|
24
|
-
pageMatchIdx: e,
|
|
25
|
-
rect: t[0] ?? c.rect,
|
|
26
|
-
rects: t
|
|
27
|
-
};
|
|
28
|
-
}, v = (n) => {
|
|
29
|
-
const e = {};
|
|
30
|
-
for (const t of n) {
|
|
31
|
-
const s = e[t.page] ?? [];
|
|
32
|
-
s.push(t), e[t.page] = s;
|
|
33
|
-
}
|
|
34
|
-
let c = 0;
|
|
35
|
-
const o = [];
|
|
36
|
-
for (const t of Object.values(e)) {
|
|
37
|
-
const s = L(t);
|
|
38
|
-
for (const g of Object.values(s))
|
|
39
|
-
o.push(j(g, c++));
|
|
40
|
-
}
|
|
41
|
-
return o;
|
|
42
|
-
}, K = (n) => {
|
|
43
|
-
const [e, c] = f(void 0), [o, t] = f([]), s = p(async (r) => {
|
|
44
|
-
if (!r)
|
|
4
|
+
const k = (s) => {
|
|
5
|
+
const [n, c] = m(void 0), [f, l] = m([]), u = r(async (t) => {
|
|
6
|
+
if (!t)
|
|
45
7
|
return {};
|
|
46
|
-
const i =
|
|
47
|
-
for (let
|
|
48
|
-
const
|
|
49
|
-
|
|
8
|
+
const i = t.numPages, e = {};
|
|
9
|
+
for (let o = 1; o <= i; o++) {
|
|
10
|
+
const h = await t.getPage(o);
|
|
11
|
+
e[o.toString()] = await h.getTextContent();
|
|
50
12
|
}
|
|
51
|
-
return
|
|
52
|
-
}, []),
|
|
53
|
-
c(
|
|
54
|
-
}, []),
|
|
55
|
-
|
|
13
|
+
return e;
|
|
14
|
+
}, []), p = r(async (t) => {
|
|
15
|
+
c(t);
|
|
16
|
+
}, []), g = r(() => {
|
|
17
|
+
l([]), c(void 0);
|
|
56
18
|
}, []);
|
|
57
|
-
return
|
|
58
|
-
|
|
19
|
+
return C(() => {
|
|
20
|
+
s && u(s).then((t) => {
|
|
59
21
|
let i = [];
|
|
60
|
-
for (const { keyword:
|
|
61
|
-
if (!
|
|
22
|
+
for (const { keyword: e, highlightColor: o, options: h } of n) {
|
|
23
|
+
if (!e)
|
|
62
24
|
continue;
|
|
63
|
-
const
|
|
64
|
-
for (const
|
|
65
|
-
const
|
|
66
|
-
[
|
|
67
|
-
|
|
68
|
-
Number(
|
|
69
|
-
|
|
70
|
-
).map((
|
|
71
|
-
...
|
|
72
|
-
page: Number(
|
|
73
|
-
pageMatchIdx:
|
|
74
|
-
color:
|
|
75
|
-
keyword:
|
|
25
|
+
const y = H(h);
|
|
26
|
+
for (const a of Object.keys(t)) {
|
|
27
|
+
const b = M(
|
|
28
|
+
[e],
|
|
29
|
+
t[a],
|
|
30
|
+
Number(a) - 1,
|
|
31
|
+
y
|
|
32
|
+
).map((w, x) => ({
|
|
33
|
+
...w,
|
|
34
|
+
page: Number(a),
|
|
35
|
+
pageMatchIdx: x,
|
|
36
|
+
color: o,
|
|
37
|
+
keyword: e
|
|
76
38
|
}));
|
|
77
|
-
i = [...i, ...
|
|
39
|
+
i = [...i, ...b];
|
|
78
40
|
}
|
|
79
41
|
}
|
|
80
|
-
|
|
42
|
+
l(i);
|
|
81
43
|
}).catch(() => {
|
|
82
|
-
|
|
44
|
+
g();
|
|
83
45
|
});
|
|
84
|
-
}, [
|
|
85
|
-
highlight:
|
|
86
|
-
clear:
|
|
87
|
-
highlightMatches:
|
|
88
|
-
highlightKeywords:
|
|
46
|
+
}, [n, s, g, u]), {
|
|
47
|
+
highlight: p,
|
|
48
|
+
clear: g,
|
|
49
|
+
highlightMatches: f,
|
|
50
|
+
highlightKeywords: n
|
|
89
51
|
};
|
|
90
52
|
};
|
|
91
53
|
export {
|
|
92
|
-
|
|
54
|
+
k as useHighlight
|
|
93
55
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useState as E, useCallback as h, useEffect as A } from "react";
|
|
2
2
|
import { appConsole as l } from "../appConsole.js";
|
|
3
|
-
const b = /* @__PURE__ */ new Date("2026-02-
|
|
3
|
+
const b = /* @__PURE__ */ new Date("2026-02-25T12:07:51.705Z"), d = "Please visit https://www.react-pdf.dev/manage-license/ to generate a new license key.", s = {
|
|
4
4
|
invalidLicense: `You are currently using without a valid license. ${d}`,
|
|
5
5
|
mismatchedDomain: `Your license key is not valid for the current domain / IP. ${d}`,
|
|
6
6
|
expired: `Your license key has expired. ${d}`,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
import { useDocumentContext as
|
|
3
|
-
import { PixelsPerInch as
|
|
1
|
+
import { useState as g, useRef as N, useEffect as R, useCallback as P } from "react";
|
|
2
|
+
import { useDocumentContext as O } from "../../contexts/RPDocumentContext.js";
|
|
3
|
+
import { PixelsPerInch as X, AnnotationMode as q, RenderingCancelledException as B } from "pdfjs-dist";
|
|
4
4
|
import "react/jsx-runtime";
|
|
5
5
|
import "./useLoadPdf.js";
|
|
6
6
|
import "../types.js";
|
|
@@ -13,160 +13,171 @@ import "./usePdfProperties.js";
|
|
|
13
13
|
import "../convertPdfDate.js";
|
|
14
14
|
import "../formatFileSize.js";
|
|
15
15
|
const re = () => {
|
|
16
|
-
const { pdf:
|
|
16
|
+
const { pdf: l, pages: f } = O(), [D, E] = g({
|
|
17
17
|
loadedPages: 0,
|
|
18
18
|
totalPages: 0,
|
|
19
19
|
percentage: 0
|
|
20
|
-
}), [T,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}, [
|
|
24
|
-
const
|
|
20
|
+
}), [T, b] = g(!1), [A, y] = g(!1), [Z, L] = g(null), [F, w] = g(void 0), s = N(!1);
|
|
21
|
+
R(() => {
|
|
22
|
+
s.current;
|
|
23
|
+
}, [s]);
|
|
24
|
+
const C = P(() => {
|
|
25
25
|
document.documentElement.classList.remove("rp-print-html-printing"), document.body.classList.remove("rp-print-body-printing");
|
|
26
26
|
const e = document.querySelector(".rp-print-zone");
|
|
27
27
|
e && e.remove();
|
|
28
|
-
}, []),
|
|
29
|
-
|
|
28
|
+
}, []), a = P(() => {
|
|
29
|
+
s.current = !1, b(!1), y(!1), L(null), w(void 0), E({
|
|
30
30
|
loadedPages: 0,
|
|
31
31
|
totalPages: 0,
|
|
32
32
|
percentage: 0
|
|
33
33
|
});
|
|
34
|
-
}, []),
|
|
35
|
-
|
|
34
|
+
}, []), M = () => {
|
|
35
|
+
s.current = !0;
|
|
36
36
|
}, I = async ({
|
|
37
37
|
scratchCanvas: e,
|
|
38
38
|
pdfDocument: n,
|
|
39
39
|
pageNumber: t,
|
|
40
|
-
viewerPdfPage:
|
|
41
|
-
printResolution:
|
|
42
|
-
optionalContentConfigPromise:
|
|
43
|
-
printAnnotationStoragePromise:
|
|
40
|
+
viewerPdfPage: o,
|
|
41
|
+
printResolution: r,
|
|
42
|
+
optionalContentConfigPromise: i,
|
|
43
|
+
printAnnotationStoragePromise: u
|
|
44
44
|
}) => {
|
|
45
|
-
if (
|
|
46
|
-
|
|
45
|
+
if (s.current) {
|
|
46
|
+
a();
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
|
-
const
|
|
50
|
-
e.width = Math.floor(
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
const [
|
|
49
|
+
const c = r / X.PDF;
|
|
50
|
+
e.width = Math.floor(o.width * c), e.height = Math.floor(o.height * c);
|
|
51
|
+
const p = e.getContext("2d");
|
|
52
|
+
p.save(), p.fillStyle = "rgb(255, 255, 255)", p.fillRect(0, 0, e.width, e.height), p.restore();
|
|
53
|
+
const [m, h] = await Promise.all([
|
|
54
54
|
n.getPage(t),
|
|
55
|
-
|
|
55
|
+
u
|
|
56
56
|
]);
|
|
57
|
-
if (
|
|
58
|
-
|
|
57
|
+
if (s.current) {
|
|
58
|
+
a();
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
const v = {
|
|
62
62
|
canvas: e,
|
|
63
|
-
canvasContext:
|
|
64
|
-
transform: [
|
|
65
|
-
viewport:
|
|
63
|
+
canvasContext: p,
|
|
64
|
+
transform: [c, 0, 0, c, 0, 0],
|
|
65
|
+
viewport: m.getViewport({ scale: 1, rotation: o.rotation }),
|
|
66
66
|
intent: "print",
|
|
67
|
-
annotationMode:
|
|
68
|
-
optionalContentConfigPromise:
|
|
67
|
+
annotationMode: q.ENABLE_STORAGE,
|
|
68
|
+
optionalContentConfigPromise: i,
|
|
69
69
|
printAnnotationStorage: h
|
|
70
70
|
};
|
|
71
71
|
try {
|
|
72
|
-
return
|
|
72
|
+
return m.render(v).promise;
|
|
73
73
|
} catch (d) {
|
|
74
|
-
throw d instanceof
|
|
74
|
+
throw d instanceof B || console.error(d), d;
|
|
75
75
|
}
|
|
76
|
-
},
|
|
76
|
+
}, S = P(() => {
|
|
77
77
|
const e = window.print;
|
|
78
78
|
return new Promise((n) => {
|
|
79
|
-
const t = (
|
|
79
|
+
const t = (i) => {
|
|
80
80
|
setTimeout(() => {
|
|
81
|
-
e.call(window), setTimeout(() =>
|
|
81
|
+
e.call(window), setTimeout(() => i(), 20);
|
|
82
82
|
}, 0);
|
|
83
83
|
};
|
|
84
84
|
if (document.querySelector(".rp-print-zone")) {
|
|
85
85
|
t(n);
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
|
-
const
|
|
89
|
-
document.querySelector(".rp-print-zone") && (t(n),
|
|
88
|
+
const r = new MutationObserver(() => {
|
|
89
|
+
document.querySelector(".rp-print-zone") && (t(n), r.disconnect());
|
|
90
90
|
});
|
|
91
|
-
|
|
91
|
+
r.observe(document.body, { childList: !0, subtree: !0 });
|
|
92
92
|
});
|
|
93
|
-
}, []),
|
|
93
|
+
}, []), U = async (e) => {
|
|
94
94
|
const n = document.createElement("img"), t = document.createElement("div");
|
|
95
|
-
return t.classList.add("rp-print-page"),
|
|
95
|
+
return t.classList.add("rp-print-page"), await new Promise((o, r) => {
|
|
96
|
+
e.toBlob((i) => {
|
|
97
|
+
i ? (n.src = URL.createObjectURL(i), n.onload = () => {
|
|
98
|
+
URL.revokeObjectURL(n.src), o();
|
|
99
|
+
}, n.onerror = r) : r(new Error("Failed to create blob"));
|
|
100
|
+
});
|
|
101
|
+
}).catch(() => {
|
|
102
|
+
}), t.appendChild(n), t;
|
|
96
103
|
}, x = (e, n) => {
|
|
97
104
|
const t = document.createElement("div");
|
|
98
105
|
t.classList.add("rp-print-zone");
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
106
|
+
const o = document.createElement("style"), r = n.get(1);
|
|
107
|
+
if (r) {
|
|
108
|
+
const { width: i, height: u } = r == null ? void 0 : r.page.getViewport({ scale: 1 });
|
|
109
|
+
o.innerHTML = `@page { size: ${i}px ${u}px }`;
|
|
110
|
+
}
|
|
111
|
+
return t.appendChild(o), t.append(...e), t;
|
|
112
|
+
}, k = P(
|
|
102
113
|
async (e) => {
|
|
103
|
-
if (!
|
|
114
|
+
if (!l)
|
|
104
115
|
return;
|
|
105
|
-
|
|
116
|
+
a();
|
|
106
117
|
const n = e && "visibleDefaultProgress" in e ? e : void 0;
|
|
107
|
-
(n == null ? void 0 : n.visibleDefaultProgress) !== void 0 ?
|
|
108
|
-
const t = document.createElement("canvas"),
|
|
118
|
+
(n == null ? void 0 : n.visibleDefaultProgress) !== void 0 ? w(n.visibleDefaultProgress) : w(!0);
|
|
119
|
+
const t = document.createElement("canvas"), o = [];
|
|
109
120
|
try {
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
),
|
|
114
|
-
for (const [
|
|
115
|
-
if (
|
|
116
|
-
|
|
121
|
+
l.isPureXfa && console.warn("[rp] XFA Form is not supported at the moment.");
|
|
122
|
+
const i = l.getOptionalContentConfig({ intent: "print" }), u = Promise.resolve(
|
|
123
|
+
l.annotationStorage.print ?? void 0
|
|
124
|
+
), c = f.size;
|
|
125
|
+
for (const [m, h] of f) {
|
|
126
|
+
if (s.current) {
|
|
127
|
+
a();
|
|
117
128
|
return;
|
|
118
129
|
}
|
|
119
130
|
const v = h.page.getViewport({ scale: 1 });
|
|
120
131
|
if (await I({
|
|
121
132
|
scratchCanvas: t,
|
|
122
|
-
pdfDocument:
|
|
123
|
-
pageNumber:
|
|
133
|
+
pdfDocument: l,
|
|
134
|
+
pageNumber: m,
|
|
124
135
|
viewerPdfPage: v,
|
|
125
136
|
printResolution: 150,
|
|
126
|
-
optionalContentConfigPromise:
|
|
127
|
-
printAnnotationStoragePromise:
|
|
128
|
-
}),
|
|
129
|
-
|
|
137
|
+
optionalContentConfigPromise: i,
|
|
138
|
+
printAnnotationStoragePromise: u
|
|
139
|
+
}), s.current) {
|
|
140
|
+
a();
|
|
130
141
|
return;
|
|
131
142
|
}
|
|
132
|
-
const d =
|
|
133
|
-
|
|
134
|
-
const
|
|
143
|
+
const d = await U(t);
|
|
144
|
+
o.push(d);
|
|
145
|
+
const z = parseFloat((o.length / c * 100).toFixed(2));
|
|
135
146
|
E({
|
|
136
|
-
loadedPages:
|
|
137
|
-
totalPages:
|
|
138
|
-
percentage:
|
|
147
|
+
loadedPages: m,
|
|
148
|
+
totalPages: c,
|
|
149
|
+
percentage: z
|
|
139
150
|
});
|
|
140
151
|
}
|
|
141
|
-
if (
|
|
142
|
-
|
|
152
|
+
if (s.current) {
|
|
153
|
+
a();
|
|
143
154
|
return;
|
|
144
155
|
}
|
|
145
|
-
const
|
|
146
|
-
document.body.appendChild(
|
|
147
|
-
} catch (
|
|
148
|
-
|
|
156
|
+
const p = x(o, f);
|
|
157
|
+
document.body.appendChild(p), document.documentElement.classList.add("rp-print-html-printing"), document.body.classList.add("rp-print-body-printing"), b(!0), await S();
|
|
158
|
+
} catch (r) {
|
|
159
|
+
y(!0), r instanceof Error && L(r);
|
|
149
160
|
} finally {
|
|
150
161
|
t.height = 0, t.width = 0;
|
|
151
162
|
}
|
|
152
163
|
},
|
|
153
|
-
[
|
|
164
|
+
[l, f, s, a, x, S]
|
|
154
165
|
);
|
|
155
|
-
return
|
|
166
|
+
return R(() => {
|
|
156
167
|
const e = () => {
|
|
157
|
-
|
|
168
|
+
a(), C();
|
|
158
169
|
};
|
|
159
170
|
return window.addEventListener("afterprint", e, !0), () => {
|
|
160
171
|
window.removeEventListener("afterprint", e, !0);
|
|
161
172
|
};
|
|
162
|
-
}, [
|
|
163
|
-
print:
|
|
164
|
-
cancel:
|
|
165
|
-
progress:
|
|
173
|
+
}, [C, a]), {
|
|
174
|
+
print: k,
|
|
175
|
+
cancel: M,
|
|
176
|
+
progress: D,
|
|
166
177
|
isComplete: T,
|
|
167
178
|
isError: A,
|
|
168
179
|
error: Z,
|
|
169
|
-
showDefaultProgress:
|
|
180
|
+
showDefaultProgress: F
|
|
170
181
|
};
|
|
171
182
|
};
|
|
172
183
|
export {
|