@react-pdf-kit/viewer 2.1.0-beta.1 → 2.1.0-beta.3
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 +84 -49
- package/dist/assets/style.css +1 -1
- package/dist/components/layout/toolbar/DocumentDialog.js +36 -28
- package/dist/main.js +106 -104
- package/dist/types/main.d.ts +2 -0
- package/dist/utils/highlight.js +36 -36
- package/dist/utils/hooks/useHighlight.js +78 -40
- package/dist/utils/hooks/useLicense.js +1 -1
- package/dist/utils/hooks/usePrint.js +69 -76
- package/dist/utils/injectPrintCSS.js +4 -3
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useState as g, useRef as
|
|
2
|
-
import { useDocumentContext as
|
|
3
|
-
import { PixelsPerInch as
|
|
1
|
+
import { useState as g, useRef as k, useEffect as D, useCallback as P } from "react";
|
|
2
|
+
import { useDocumentContext as q } from "../../contexts/RPDocumentContext.js";
|
|
3
|
+
import { PixelsPerInch as V, AnnotationMode as O, RenderingCancelledException as U } from "pdfjs-dist";
|
|
4
4
|
import "react/jsx-runtime";
|
|
5
5
|
import "./useLoadPdf.js";
|
|
6
6
|
import "../types.js";
|
|
@@ -13,118 +13,111 @@ import "./usePdfProperties.js";
|
|
|
13
13
|
import "../convertPdfDate.js";
|
|
14
14
|
import "../formatFileSize.js";
|
|
15
15
|
const re = () => {
|
|
16
|
-
const { pdf: l, pages: f } =
|
|
16
|
+
const { pdf: l, pages: f } = q(), [R, E] = g({
|
|
17
17
|
loadedPages: 0,
|
|
18
18
|
totalPages: 0,
|
|
19
19
|
percentage: 0
|
|
20
|
-
}), [T,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}, [
|
|
24
|
-
const
|
|
20
|
+
}), [T, y] = g(!1), [A, b] = g(!1), [Z, C] = g(null), [M, w] = g(void 0), i = k(!1);
|
|
21
|
+
D(() => {
|
|
22
|
+
i.current;
|
|
23
|
+
}, [i]);
|
|
24
|
+
const S = 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
|
+
}, []), s = P(() => {
|
|
29
|
+
i.current = !1, y(!1), b(!1), C(null), w(void 0), E({
|
|
30
30
|
loadedPages: 0,
|
|
31
31
|
totalPages: 0,
|
|
32
32
|
percentage: 0
|
|
33
33
|
});
|
|
34
|
-
}, []),
|
|
35
|
-
|
|
34
|
+
}, []), F = () => {
|
|
35
|
+
i.current = !0;
|
|
36
36
|
}, I = async ({
|
|
37
37
|
scratchCanvas: e,
|
|
38
38
|
pdfDocument: n,
|
|
39
39
|
pageNumber: t,
|
|
40
|
-
viewerPdfPage:
|
|
41
|
-
printResolution:
|
|
42
|
-
optionalContentConfigPromise:
|
|
40
|
+
viewerPdfPage: r,
|
|
41
|
+
printResolution: o,
|
|
42
|
+
optionalContentConfigPromise: c,
|
|
43
43
|
printAnnotationStoragePromise: u
|
|
44
44
|
}) => {
|
|
45
|
-
if (
|
|
46
|
-
|
|
45
|
+
if (i.current) {
|
|
46
|
+
s();
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
|
-
const
|
|
50
|
-
e.width = Math.floor(
|
|
49
|
+
const a = o / V.PDF;
|
|
50
|
+
e.width = Math.floor(r.width * a), e.height = Math.floor(r.height * a);
|
|
51
51
|
const p = e.getContext("2d");
|
|
52
52
|
p.save(), p.fillStyle = "rgb(255, 255, 255)", p.fillRect(0, 0, e.width, e.height), p.restore();
|
|
53
53
|
const [m, h] = await Promise.all([
|
|
54
54
|
n.getPage(t),
|
|
55
55
|
u
|
|
56
56
|
]);
|
|
57
|
-
if (
|
|
58
|
-
|
|
57
|
+
if (i.current) {
|
|
58
|
+
s();
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
const v = {
|
|
62
62
|
canvas: e,
|
|
63
63
|
canvasContext: p,
|
|
64
|
-
transform: [
|
|
65
|
-
viewport: m.getViewport({ scale: 1, rotation:
|
|
64
|
+
transform: [a, 0, 0, a, 0, 0],
|
|
65
|
+
viewport: m.getViewport({ scale: 1, rotation: r.rotation }),
|
|
66
66
|
intent: "print",
|
|
67
|
-
annotationMode:
|
|
68
|
-
optionalContentConfigPromise:
|
|
67
|
+
annotationMode: O.ENABLE_STORAGE,
|
|
68
|
+
optionalContentConfigPromise: c,
|
|
69
69
|
printAnnotationStorage: h
|
|
70
70
|
};
|
|
71
71
|
try {
|
|
72
72
|
return m.render(v).promise;
|
|
73
73
|
} catch (d) {
|
|
74
|
-
throw d instanceof
|
|
74
|
+
throw d instanceof U || console.error(d), d;
|
|
75
75
|
}
|
|
76
|
-
},
|
|
76
|
+
}, L = P(() => {
|
|
77
77
|
const e = window.print;
|
|
78
78
|
return new Promise((n) => {
|
|
79
|
-
const t = (
|
|
79
|
+
const t = (c) => {
|
|
80
80
|
setTimeout(() => {
|
|
81
|
-
e.call(window), setTimeout(() =>
|
|
81
|
+
e.call(window), setTimeout(() => c(), 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 o = new MutationObserver(() => {
|
|
89
|
+
document.querySelector(".rp-print-zone") && (t(n), o.disconnect());
|
|
90
90
|
});
|
|
91
|
-
|
|
91
|
+
o.observe(document.body, { childList: !0, subtree: !0 });
|
|
92
92
|
});
|
|
93
|
-
}, []),
|
|
93
|
+
}, []), z = (e) => {
|
|
94
94
|
const n = document.createElement("img"), t = document.createElement("div");
|
|
95
|
-
return t.classList.add("rp-print-page"),
|
|
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;
|
|
95
|
+
return t.classList.add("rp-print-page"), n.src = e.toDataURL(), t.appendChild(n), t;
|
|
103
96
|
}, x = (e, n) => {
|
|
104
97
|
const t = document.createElement("div");
|
|
105
98
|
t.classList.add("rp-print-zone");
|
|
106
|
-
const
|
|
107
|
-
if (
|
|
108
|
-
const
|
|
109
|
-
|
|
99
|
+
const r = document.createElement("style"), o = n.get(1);
|
|
100
|
+
if (o) {
|
|
101
|
+
const c = o.page.getViewport({ scale: 1 }), { width: u, height: a } = c;
|
|
102
|
+
r.innerHTML = `@page { size: ${u}pt ${a}pt; margin: 0; }`;
|
|
110
103
|
}
|
|
111
|
-
return t.appendChild(
|
|
112
|
-
},
|
|
104
|
+
return t.appendChild(r), t.append(...e), t;
|
|
105
|
+
}, N = P(
|
|
113
106
|
async (e) => {
|
|
114
107
|
if (!l)
|
|
115
108
|
return;
|
|
116
|
-
|
|
109
|
+
s();
|
|
117
110
|
const n = e && "visibleDefaultProgress" in e ? e : void 0;
|
|
118
111
|
(n == null ? void 0 : n.visibleDefaultProgress) !== void 0 ? w(n.visibleDefaultProgress) : w(!0);
|
|
119
|
-
const t = document.createElement("canvas"),
|
|
112
|
+
const t = document.createElement("canvas"), r = [];
|
|
120
113
|
try {
|
|
121
114
|
l.isPureXfa && console.warn("[rp] XFA Form is not supported at the moment.");
|
|
122
|
-
const
|
|
115
|
+
const c = l.getOptionalContentConfig({ intent: "print" }), u = Promise.resolve(
|
|
123
116
|
l.annotationStorage.print ?? void 0
|
|
124
|
-
),
|
|
117
|
+
), a = f.size;
|
|
125
118
|
for (const [m, h] of f) {
|
|
126
|
-
if (
|
|
127
|
-
|
|
119
|
+
if (i.current) {
|
|
120
|
+
s();
|
|
128
121
|
return;
|
|
129
122
|
}
|
|
130
123
|
const v = h.page.getViewport({ scale: 1 });
|
|
@@ -134,50 +127,50 @@ const re = () => {
|
|
|
134
127
|
pageNumber: m,
|
|
135
128
|
viewerPdfPage: v,
|
|
136
129
|
printResolution: 150,
|
|
137
|
-
optionalContentConfigPromise:
|
|
130
|
+
optionalContentConfigPromise: c,
|
|
138
131
|
printAnnotationStoragePromise: u
|
|
139
|
-
}),
|
|
140
|
-
|
|
132
|
+
}), i.current) {
|
|
133
|
+
s();
|
|
141
134
|
return;
|
|
142
135
|
}
|
|
143
|
-
const d =
|
|
144
|
-
|
|
145
|
-
const
|
|
136
|
+
const d = z(t);
|
|
137
|
+
r.push(d);
|
|
138
|
+
const X = parseFloat((r.length / a * 100).toFixed(2));
|
|
146
139
|
E({
|
|
147
140
|
loadedPages: m,
|
|
148
|
-
totalPages:
|
|
149
|
-
percentage:
|
|
141
|
+
totalPages: a,
|
|
142
|
+
percentage: X
|
|
150
143
|
});
|
|
151
144
|
}
|
|
152
|
-
if (
|
|
153
|
-
|
|
145
|
+
if (i.current) {
|
|
146
|
+
s();
|
|
154
147
|
return;
|
|
155
148
|
}
|
|
156
|
-
const p = x(
|
|
157
|
-
document.body.appendChild(p), document.documentElement.classList.add("rp-print-html-printing"), document.body.classList.add("rp-print-body-printing"),
|
|
158
|
-
} catch (
|
|
159
|
-
|
|
149
|
+
const p = x(r, f);
|
|
150
|
+
document.body.appendChild(p), document.documentElement.classList.add("rp-print-html-printing"), document.body.classList.add("rp-print-body-printing"), y(!0), await L();
|
|
151
|
+
} catch (o) {
|
|
152
|
+
b(!0), o instanceof Error && C(o);
|
|
160
153
|
} finally {
|
|
161
154
|
t.height = 0, t.width = 0;
|
|
162
155
|
}
|
|
163
156
|
},
|
|
164
|
-
[l, f,
|
|
157
|
+
[l, f, i, s, x, L]
|
|
165
158
|
);
|
|
166
|
-
return
|
|
159
|
+
return D(() => {
|
|
167
160
|
const e = () => {
|
|
168
|
-
|
|
161
|
+
s(), S();
|
|
169
162
|
};
|
|
170
163
|
return window.addEventListener("afterprint", e, !0), () => {
|
|
171
164
|
window.removeEventListener("afterprint", e, !0);
|
|
172
165
|
};
|
|
173
|
-
}, [
|
|
174
|
-
print:
|
|
175
|
-
cancel:
|
|
176
|
-
progress:
|
|
166
|
+
}, [S, s]), {
|
|
167
|
+
print: N,
|
|
168
|
+
cancel: F,
|
|
169
|
+
progress: R,
|
|
177
170
|
isComplete: T,
|
|
178
171
|
isError: A,
|
|
179
172
|
error: Z,
|
|
180
|
-
showDefaultProgress:
|
|
173
|
+
showDefaultProgress: M
|
|
181
174
|
};
|
|
182
175
|
};
|
|
183
176
|
export {
|
|
@@ -3,17 +3,18 @@ function e() {
|
|
|
3
3
|
return;
|
|
4
4
|
const t = document.createElement("style");
|
|
5
5
|
t.id = "rp-print-style", t.textContent = `
|
|
6
|
+
@page { margin: 0; }
|
|
7
|
+
|
|
6
8
|
.rp-print-zone { display: none; }
|
|
7
9
|
|
|
8
10
|
@media print {
|
|
9
|
-
@page { margin: 0; }
|
|
10
11
|
.rp-print-html-printing { height: 100%; }
|
|
11
|
-
.rp-print-body-printing { background:
|
|
12
|
+
.rp-print-body-printing { background: rgb(0 0 0 / 0) none; height: 100% !important; margin: 0 !important; display: block !important; }
|
|
12
13
|
.rp-print-body-printing * { display: none; }
|
|
13
14
|
.rp-print-zone { display: block; height: 100%; }
|
|
14
15
|
.rp-print-page { display: flex; align-items: center; flex-direction: column; justify-content: center; height: 100%; width: 100%; page-break-after: always; page-break-inside: avoid; }
|
|
15
16
|
.rp-print-page:last-child { page-break-after: auto; }
|
|
16
|
-
.rp-print-page img, .rp-print-page canvas { display: block; max-
|
|
17
|
+
.rp-print-page img, .rp-print-page canvas { display: block; max-width: 100%; max-height: 100%; direction: ltr; }
|
|
17
18
|
}
|
|
18
19
|
`, document.head.appendChild(t);
|
|
19
20
|
}
|