@rzymek/react-pdf-highlighter 8.0.1-rc.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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/components/AreaHighlight.d.ts +8 -0
- package/dist/components/Highlight.d.ts +17 -0
- package/dist/components/HighlightLayer.d.ts +26 -0
- package/dist/components/MouseMonitor.d.ts +15 -0
- package/dist/components/MouseSelection.d.ts +10 -0
- package/dist/components/PdfHighlighter.d.ts +92 -0
- package/dist/components/PdfLoader.d.ts +32 -0
- package/dist/components/Popup.d.ts +8 -0
- package/dist/components/Tip.d.ts +20 -0
- package/dist/components/TipContainer.d.ts +13 -0
- package/dist/index.d.ts +7 -0
- package/dist/lib/coordinates.d.ts +8 -0
- package/dist/lib/get-area-as-png.d.ts +2 -0
- package/dist/lib/get-bounding-rect.d.ts +2 -0
- package/dist/lib/get-client-rects.d.ts +2 -0
- package/dist/lib/optimize-client-rects.d.ts +2 -0
- package/dist/lib/pdfjs-dom.d.ts +8 -0
- package/dist/node_modules/.pnpm/pdfjs-dist@4.4.168/node_modules/pdfjs-dist/web/pdf_viewer.js +8022 -0
- package/dist/node_modules/.pnpm/ts-debounce@4.0.0/node_modules/ts-debounce/dist/src/index.esm.js +38 -0
- package/dist/src/components/AreaHighlight.js +58 -0
- package/dist/src/components/Highlight.js +45 -0
- package/dist/src/components/HighlightLayer.js +47 -0
- package/dist/src/components/MouseMonitor.js +45 -0
- package/dist/src/components/MouseSelection.js +119 -0
- package/dist/src/components/PdfHighlighter.js +543 -0
- package/dist/src/components/PdfLoader.js +81 -0
- package/dist/src/components/Popup.js +42 -0
- package/dist/src/components/Tip.js +82 -0
- package/dist/src/components/TipContainer.js +62 -0
- package/dist/src/index.js +15 -0
- package/dist/src/lib/coordinates.js +50 -0
- package/dist/src/lib/get-area-as-png.js +31 -0
- package/dist/src/lib/get-bounding-rect.js +40 -0
- package/dist/src/lib/get-client-rects.js +39 -0
- package/dist/src/lib/optimize-client-rects.js +54 -0
- package/dist/src/lib/pdfjs-dom.js +64 -0
- package/dist/src/style/AreaHighlight.module.css.js +14 -0
- package/dist/src/style/Highlight.module.css.js +20 -0
- package/dist/src/style/MouseSelection.module.css.js +8 -0
- package/dist/src/style/PdfHighlighter.module.css.js +14 -0
- package/dist/src/style/Tip.module.css.js +11 -0
- package/dist/src/style/TipContainer.module.css.js +8 -0
- package/dist/style.css +2903 -0
- package/dist/types.d.ts +62 -0
- package/package.json +69 -0
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import React, { PureComponent } from "react";
|
|
6
|
+
import { createRoot } from "react-dom/client";
|
|
7
|
+
import { debounce as r } from "../../node_modules/.pnpm/ts-debounce@4.0.0/node_modules/ts-debounce/dist/src/index.esm.js";
|
|
8
|
+
import { scaledToViewport, viewportToScaled } from "../lib/coordinates.js";
|
|
9
|
+
import { getAreaAsPNG } from "../lib/get-area-as-png.js";
|
|
10
|
+
import { getBoundingRect } from "../lib/get-bounding-rect.js";
|
|
11
|
+
import { getClientRects } from "../lib/get-client-rects.js";
|
|
12
|
+
import { findOrCreateContainerLayer, getWindow, isHTMLElement, getPagesFromRange, getPageFromElement } from "../lib/pdfjs-dom.js";
|
|
13
|
+
import styles from "../style/PdfHighlighter.module.css.js";
|
|
14
|
+
import { HighlightLayer } from "./HighlightLayer.js";
|
|
15
|
+
import { MouseSelection } from "./MouseSelection.js";
|
|
16
|
+
import { TipContainer } from "./TipContainer.js";
|
|
17
|
+
const EMPTY_ID = "empty-id";
|
|
18
|
+
class PdfHighlighter extends PureComponent {
|
|
19
|
+
constructor(props) {
|
|
20
|
+
super(props);
|
|
21
|
+
__publicField(this, "state", {
|
|
22
|
+
ghostHighlight: null,
|
|
23
|
+
isCollapsed: true,
|
|
24
|
+
range: null,
|
|
25
|
+
scrolledToHighlightId: EMPTY_ID,
|
|
26
|
+
isAreaSelectionInProgress: false,
|
|
27
|
+
tip: null,
|
|
28
|
+
tipPosition: null,
|
|
29
|
+
tipChildren: null
|
|
30
|
+
});
|
|
31
|
+
__publicField(this, "viewer");
|
|
32
|
+
__publicField(this, "resizeObserver", null);
|
|
33
|
+
__publicField(this, "containerNode", null);
|
|
34
|
+
__publicField(this, "containerNodeRef");
|
|
35
|
+
__publicField(this, "highlightRoots", {});
|
|
36
|
+
__publicField(this, "unsubscribe", () => {
|
|
37
|
+
});
|
|
38
|
+
__publicField(this, "attachRef", (eventBus) => {
|
|
39
|
+
var _a;
|
|
40
|
+
const { resizeObserver: observer } = this;
|
|
41
|
+
this.containerNode = this.containerNodeRef.current;
|
|
42
|
+
this.unsubscribe();
|
|
43
|
+
if (this.containerNode) {
|
|
44
|
+
const { ownerDocument: doc } = this.containerNode;
|
|
45
|
+
eventBus.on("textlayerrendered", this.onTextLayerRendered);
|
|
46
|
+
eventBus.on("pagesinit", this.onDocumentReady);
|
|
47
|
+
doc.addEventListener("selectionchange", this.onSelectionChange);
|
|
48
|
+
doc.addEventListener("keydown", this.handleKeyDown);
|
|
49
|
+
(_a = doc.defaultView) == null ? void 0 : _a.addEventListener("resize", this.debouncedScaleValue);
|
|
50
|
+
if (observer) observer.observe(this.containerNode);
|
|
51
|
+
this.unsubscribe = () => {
|
|
52
|
+
var _a2;
|
|
53
|
+
eventBus.off("pagesinit", this.onDocumentReady);
|
|
54
|
+
eventBus.off("textlayerrendered", this.onTextLayerRendered);
|
|
55
|
+
doc.removeEventListener("selectionchange", this.onSelectionChange);
|
|
56
|
+
doc.removeEventListener("keydown", this.handleKeyDown);
|
|
57
|
+
(_a2 = doc.defaultView) == null ? void 0 : _a2.removeEventListener(
|
|
58
|
+
"resize",
|
|
59
|
+
this.debouncedScaleValue
|
|
60
|
+
);
|
|
61
|
+
if (observer) observer.disconnect();
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
__publicField(this, "hideTipAndSelection", () => {
|
|
66
|
+
this.setState({
|
|
67
|
+
tipPosition: null,
|
|
68
|
+
tipChildren: null
|
|
69
|
+
});
|
|
70
|
+
this.setState(
|
|
71
|
+
{ ghostHighlight: null, tip: null },
|
|
72
|
+
() => this.renderHighlightLayers()
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
__publicField(this, "renderTip", () => {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
const { tipPosition, tipChildren } = this.state;
|
|
78
|
+
if (!tipPosition) return null;
|
|
79
|
+
const { boundingRect, pageNumber } = tipPosition;
|
|
80
|
+
const page = {
|
|
81
|
+
node: (_a = this.viewer) == null ? void 0 : _a.getPageView(
|
|
82
|
+
(boundingRect.pageNumber || pageNumber) - 1
|
|
83
|
+
).div,
|
|
84
|
+
pageNumber: boundingRect.pageNumber || pageNumber
|
|
85
|
+
};
|
|
86
|
+
const pageBoundingClientRect = page.node.getBoundingClientRect();
|
|
87
|
+
const pageBoundingRect = {
|
|
88
|
+
bottom: pageBoundingClientRect.bottom,
|
|
89
|
+
height: pageBoundingClientRect.height,
|
|
90
|
+
left: pageBoundingClientRect.left,
|
|
91
|
+
right: pageBoundingClientRect.right,
|
|
92
|
+
top: pageBoundingClientRect.top,
|
|
93
|
+
width: pageBoundingClientRect.width,
|
|
94
|
+
x: pageBoundingClientRect.x,
|
|
95
|
+
y: pageBoundingClientRect.y,
|
|
96
|
+
pageNumber: page.pageNumber
|
|
97
|
+
};
|
|
98
|
+
return /* @__PURE__ */ jsx(
|
|
99
|
+
TipContainer,
|
|
100
|
+
{
|
|
101
|
+
scrollTop: ((_b = this.viewer) == null ? void 0 : _b.container.scrollTop) ?? 0,
|
|
102
|
+
pageBoundingRect,
|
|
103
|
+
style: {
|
|
104
|
+
left: page.node.offsetLeft + boundingRect.left + boundingRect.width / 2,
|
|
105
|
+
top: boundingRect.top + page.node.offsetTop,
|
|
106
|
+
bottom: boundingRect.top + page.node.offsetTop + boundingRect.height
|
|
107
|
+
},
|
|
108
|
+
children: tipChildren
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
__publicField(this, "onTextLayerRendered", () => {
|
|
113
|
+
this.renderHighlightLayers();
|
|
114
|
+
});
|
|
115
|
+
__publicField(this, "scrollTo", (highlight) => {
|
|
116
|
+
const { pageNumber, boundingRect, usePdfCoordinates } = highlight.position;
|
|
117
|
+
if (!this.viewer) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
this.viewer.container.removeEventListener("scroll", this.onScroll);
|
|
121
|
+
const pageViewport = this.viewer.getPageView(pageNumber - 1).viewport;
|
|
122
|
+
const scrollMargin = 10;
|
|
123
|
+
this.viewer.scrollPageIntoView({
|
|
124
|
+
pageNumber,
|
|
125
|
+
destArray: [
|
|
126
|
+
null,
|
|
127
|
+
{ name: "XYZ" },
|
|
128
|
+
...pageViewport.convertToPdfPoint(
|
|
129
|
+
0,
|
|
130
|
+
scaledToViewport(boundingRect, pageViewport, usePdfCoordinates).top - scrollMargin
|
|
131
|
+
),
|
|
132
|
+
0
|
|
133
|
+
]
|
|
134
|
+
});
|
|
135
|
+
this.setState(
|
|
136
|
+
{
|
|
137
|
+
scrolledToHighlightId: highlight.id
|
|
138
|
+
},
|
|
139
|
+
() => this.renderHighlightLayers()
|
|
140
|
+
);
|
|
141
|
+
setTimeout(() => {
|
|
142
|
+
var _a;
|
|
143
|
+
(_a = this.viewer) == null ? void 0 : _a.container.addEventListener("scroll", this.onScroll);
|
|
144
|
+
}, 100);
|
|
145
|
+
});
|
|
146
|
+
__publicField(this, "onDocumentReady", () => {
|
|
147
|
+
const { scrollRef } = this.props;
|
|
148
|
+
this.handleScaleValue();
|
|
149
|
+
scrollRef(this.scrollTo);
|
|
150
|
+
});
|
|
151
|
+
__publicField(this, "onSelectionChange", () => {
|
|
152
|
+
const container = this.containerNode;
|
|
153
|
+
if (!container) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const selection = getWindow(container).getSelection();
|
|
157
|
+
if (!selection) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
|
|
161
|
+
if (selection.isCollapsed) {
|
|
162
|
+
this.setState({ isCollapsed: true });
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (!range || !container || !container.contains(range.commonAncestorContainer)) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this.setState({
|
|
169
|
+
isCollapsed: false,
|
|
170
|
+
range
|
|
171
|
+
});
|
|
172
|
+
this.debouncedAfterSelection();
|
|
173
|
+
});
|
|
174
|
+
__publicField(this, "onScroll", () => {
|
|
175
|
+
var _a;
|
|
176
|
+
const { onScrollChange } = this.props;
|
|
177
|
+
onScrollChange();
|
|
178
|
+
this.setState(
|
|
179
|
+
{
|
|
180
|
+
scrolledToHighlightId: EMPTY_ID
|
|
181
|
+
},
|
|
182
|
+
() => this.renderHighlightLayers()
|
|
183
|
+
);
|
|
184
|
+
(_a = this.viewer) == null ? void 0 : _a.container.removeEventListener("scroll", this.onScroll);
|
|
185
|
+
});
|
|
186
|
+
__publicField(this, "onMouseDown", (event) => {
|
|
187
|
+
if (!(event.target instanceof Element) || !isHTMLElement(event.target)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (event.target.closest("#PdfHighlighter__tip-container")) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
this.hideTipAndSelection();
|
|
194
|
+
});
|
|
195
|
+
__publicField(this, "handleKeyDown", (event) => {
|
|
196
|
+
if (event.code === "Escape") {
|
|
197
|
+
this.hideTipAndSelection();
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
__publicField(this, "afterSelection", () => {
|
|
201
|
+
const { onSelectionFinished } = this.props;
|
|
202
|
+
const { isCollapsed, range } = this.state;
|
|
203
|
+
if (!range || isCollapsed) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const pages = getPagesFromRange(range);
|
|
207
|
+
if (!pages || pages.length === 0) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const rects = getClientRects(range, pages);
|
|
211
|
+
if (rects.length === 0) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const boundingRect = getBoundingRect(rects);
|
|
215
|
+
const viewportPosition = {
|
|
216
|
+
boundingRect,
|
|
217
|
+
rects,
|
|
218
|
+
pageNumber: pages[0].number
|
|
219
|
+
};
|
|
220
|
+
const content = {
|
|
221
|
+
text: range.toString()
|
|
222
|
+
};
|
|
223
|
+
const scaledPosition = this.viewportPositionToScaled(viewportPosition);
|
|
224
|
+
this.setTip(
|
|
225
|
+
viewportPosition,
|
|
226
|
+
onSelectionFinished(
|
|
227
|
+
scaledPosition,
|
|
228
|
+
content,
|
|
229
|
+
() => this.hideTipAndSelection(),
|
|
230
|
+
() => this.setState(
|
|
231
|
+
{
|
|
232
|
+
ghostHighlight: { position: scaledPosition }
|
|
233
|
+
},
|
|
234
|
+
() => this.renderHighlightLayers()
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
__publicField(this, "debouncedAfterSelection", r(this.afterSelection, 500));
|
|
240
|
+
__publicField(this, "handleScaleValue", () => {
|
|
241
|
+
if (this.viewer) {
|
|
242
|
+
this.viewer.currentScaleValue = this.props.pdfScaleValue;
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
__publicField(this, "debouncedScaleValue", r(this.handleScaleValue, 500));
|
|
246
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
247
|
+
this.resizeObserver = new ResizeObserver(this.debouncedScaleValue);
|
|
248
|
+
}
|
|
249
|
+
this.containerNodeRef = React.createRef();
|
|
250
|
+
}
|
|
251
|
+
componentDidMount() {
|
|
252
|
+
this.init();
|
|
253
|
+
}
|
|
254
|
+
componentDidUpdate(prevProps) {
|
|
255
|
+
if (prevProps.pdfDocument !== this.props.pdfDocument) {
|
|
256
|
+
this.init();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (prevProps.highlights !== this.props.highlights) {
|
|
260
|
+
this.renderHighlightLayers();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async init() {
|
|
264
|
+
const { pdfDocument, pdfViewerOptions } = this.props;
|
|
265
|
+
const pdfjs = await import("../../node_modules/.pnpm/pdfjs-dist@4.4.168/node_modules/pdfjs-dist/web/pdf_viewer.js");
|
|
266
|
+
const eventBus = new pdfjs.EventBus();
|
|
267
|
+
const linkService = new pdfjs.PDFLinkService({
|
|
268
|
+
eventBus,
|
|
269
|
+
externalLinkTarget: 2
|
|
270
|
+
});
|
|
271
|
+
if (!this.containerNodeRef.current) {
|
|
272
|
+
throw new Error("!");
|
|
273
|
+
}
|
|
274
|
+
this.viewer = this.viewer || new pdfjs.PDFViewer({
|
|
275
|
+
container: this.containerNodeRef.current,
|
|
276
|
+
eventBus,
|
|
277
|
+
// enhanceTextSelection: true, // deprecated. https://github.com/mozilla/pdf.js/issues/9943#issuecomment-409369485
|
|
278
|
+
textLayerMode: 2,
|
|
279
|
+
removePageBorders: true,
|
|
280
|
+
linkService,
|
|
281
|
+
...pdfViewerOptions
|
|
282
|
+
});
|
|
283
|
+
linkService.setDocument(pdfDocument);
|
|
284
|
+
linkService.setViewer(this.viewer);
|
|
285
|
+
this.viewer.setDocument(pdfDocument);
|
|
286
|
+
this.attachRef(eventBus);
|
|
287
|
+
}
|
|
288
|
+
componentWillUnmount() {
|
|
289
|
+
this.unsubscribe();
|
|
290
|
+
}
|
|
291
|
+
findOrCreateHighlightLayer(page) {
|
|
292
|
+
var _a;
|
|
293
|
+
const { textLayer } = ((_a = this.viewer) == null ? void 0 : _a.getPageView(page - 1)) || {};
|
|
294
|
+
if (!textLayer) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
return findOrCreateContainerLayer(
|
|
298
|
+
textLayer.div,
|
|
299
|
+
`PdfHighlighter__highlight-layer ${styles.highlightLayer}`,
|
|
300
|
+
".PdfHighlighter__highlight-layer"
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
groupHighlightsByPage(highlights) {
|
|
304
|
+
const { ghostHighlight } = this.state;
|
|
305
|
+
const allHighlights = [...highlights, ghostHighlight].filter(
|
|
306
|
+
Boolean
|
|
307
|
+
);
|
|
308
|
+
const pageNumbers = /* @__PURE__ */ new Set();
|
|
309
|
+
for (const highlight of allHighlights) {
|
|
310
|
+
pageNumbers.add(highlight.position.pageNumber);
|
|
311
|
+
for (const rect of highlight.position.rects) {
|
|
312
|
+
if (rect.pageNumber) {
|
|
313
|
+
pageNumbers.add(rect.pageNumber);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const groupedHighlights = {};
|
|
318
|
+
for (const pageNumber of pageNumbers) {
|
|
319
|
+
groupedHighlights[pageNumber] = groupedHighlights[pageNumber] || [];
|
|
320
|
+
for (const highlight of allHighlights) {
|
|
321
|
+
const pageSpecificHighlight = {
|
|
322
|
+
...highlight,
|
|
323
|
+
position: {
|
|
324
|
+
pageNumber,
|
|
325
|
+
boundingRect: highlight.position.boundingRect,
|
|
326
|
+
rects: [],
|
|
327
|
+
usePdfCoordinates: highlight.position.usePdfCoordinates
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
let anyRectsOnPage = false;
|
|
331
|
+
for (const rect of highlight.position.rects) {
|
|
332
|
+
if (pageNumber === (rect.pageNumber || highlight.position.pageNumber)) {
|
|
333
|
+
pageSpecificHighlight.position.rects.push(rect);
|
|
334
|
+
anyRectsOnPage = true;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (anyRectsOnPage || pageNumber === highlight.position.pageNumber) {
|
|
338
|
+
groupedHighlights[pageNumber].push(pageSpecificHighlight);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return groupedHighlights;
|
|
343
|
+
}
|
|
344
|
+
showTip(highlight, content) {
|
|
345
|
+
const { isCollapsed, ghostHighlight, isAreaSelectionInProgress } = this.state;
|
|
346
|
+
const highlightInProgress = !isCollapsed || ghostHighlight;
|
|
347
|
+
if (highlightInProgress || isAreaSelectionInProgress) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
this.setTip(highlight.position, content);
|
|
351
|
+
}
|
|
352
|
+
scaledPositionToViewport({
|
|
353
|
+
pageNumber,
|
|
354
|
+
boundingRect,
|
|
355
|
+
rects,
|
|
356
|
+
usePdfCoordinates
|
|
357
|
+
}) {
|
|
358
|
+
var _a;
|
|
359
|
+
const viewport = (_a = this.viewer) == null ? void 0 : _a.getPageView(pageNumber - 1).viewport;
|
|
360
|
+
if (!viewport) {
|
|
361
|
+
return emptyPosition;
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
boundingRect: scaledToViewport(boundingRect, viewport, usePdfCoordinates),
|
|
365
|
+
rects: (rects || []).map(
|
|
366
|
+
(rect) => scaledToViewport(rect, viewport, usePdfCoordinates)
|
|
367
|
+
),
|
|
368
|
+
pageNumber
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
viewportPositionToScaled({
|
|
372
|
+
pageNumber,
|
|
373
|
+
boundingRect,
|
|
374
|
+
rects
|
|
375
|
+
}) {
|
|
376
|
+
var _a;
|
|
377
|
+
const viewport = (_a = this.viewer) == null ? void 0 : _a.getPageView(pageNumber - 1).viewport;
|
|
378
|
+
if (!viewport) {
|
|
379
|
+
return emptyScaledPosition;
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
boundingRect: viewportToScaled(boundingRect, viewport),
|
|
383
|
+
rects: (rects || []).map((rect) => viewportToScaled(rect, viewport)),
|
|
384
|
+
pageNumber
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
screenshot(position, pageNumber) {
|
|
388
|
+
var _a;
|
|
389
|
+
const canvas = (_a = this.viewer) == null ? void 0 : _a.getPageView(pageNumber - 1).canvas;
|
|
390
|
+
if (!canvas) {
|
|
391
|
+
return "";
|
|
392
|
+
}
|
|
393
|
+
return getAreaAsPNG(canvas, position);
|
|
394
|
+
}
|
|
395
|
+
setTip(position, inner) {
|
|
396
|
+
this.setState({
|
|
397
|
+
tipPosition: position,
|
|
398
|
+
tipChildren: inner
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
toggleTextSelection(flag) {
|
|
402
|
+
var _a;
|
|
403
|
+
if (!((_a = this.viewer) == null ? void 0 : _a.viewer)) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
this.viewer.viewer.classList.toggle(styles.disableSelection, flag);
|
|
407
|
+
}
|
|
408
|
+
render() {
|
|
409
|
+
const { onSelectionFinished, enableAreaSelection } = this.props;
|
|
410
|
+
return /* @__PURE__ */ jsx("div", { onPointerDown: this.onMouseDown, children: /* @__PURE__ */ jsxs(
|
|
411
|
+
"div",
|
|
412
|
+
{
|
|
413
|
+
ref: this.containerNodeRef,
|
|
414
|
+
className: styles.container,
|
|
415
|
+
onContextMenu: (e) => e.preventDefault(),
|
|
416
|
+
children: [
|
|
417
|
+
/* @__PURE__ */ jsx("div", { className: "pdfViewer" }),
|
|
418
|
+
this.renderTip(),
|
|
419
|
+
typeof enableAreaSelection === "function" ? /* @__PURE__ */ jsx(
|
|
420
|
+
MouseSelection,
|
|
421
|
+
{
|
|
422
|
+
onDragStart: () => this.toggleTextSelection(true),
|
|
423
|
+
onDragEnd: () => this.toggleTextSelection(false),
|
|
424
|
+
onChange: (isVisible) => this.setState({ isAreaSelectionInProgress: isVisible }),
|
|
425
|
+
shouldStart: (event) => enableAreaSelection(event) && event.target instanceof Element && isHTMLElement(event.target) && Boolean(event.target.closest(".page")),
|
|
426
|
+
onSelection: (startTarget, boundingRect, resetSelection) => {
|
|
427
|
+
const page = getPageFromElement(startTarget);
|
|
428
|
+
if (!page) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const pageBoundingRect = {
|
|
432
|
+
...boundingRect,
|
|
433
|
+
top: boundingRect.top - page.node.offsetTop,
|
|
434
|
+
left: boundingRect.left - page.node.offsetLeft,
|
|
435
|
+
pageNumber: page.number
|
|
436
|
+
};
|
|
437
|
+
const viewportPosition = {
|
|
438
|
+
boundingRect: pageBoundingRect,
|
|
439
|
+
rects: [],
|
|
440
|
+
pageNumber: page.number
|
|
441
|
+
};
|
|
442
|
+
const scaledPosition = this.viewportPositionToScaled(viewportPosition);
|
|
443
|
+
const image = this.screenshot(
|
|
444
|
+
pageBoundingRect,
|
|
445
|
+
pageBoundingRect.pageNumber
|
|
446
|
+
);
|
|
447
|
+
this.setTip(
|
|
448
|
+
viewportPosition,
|
|
449
|
+
onSelectionFinished(
|
|
450
|
+
scaledPosition,
|
|
451
|
+
{ image },
|
|
452
|
+
() => this.hideTipAndSelection(),
|
|
453
|
+
() => {
|
|
454
|
+
this.setState(
|
|
455
|
+
{
|
|
456
|
+
ghostHighlight: {
|
|
457
|
+
position: scaledPosition,
|
|
458
|
+
content: { image }
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
() => {
|
|
462
|
+
resetSelection();
|
|
463
|
+
this.renderHighlightLayers();
|
|
464
|
+
}
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
)
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
) : null
|
|
472
|
+
]
|
|
473
|
+
}
|
|
474
|
+
) });
|
|
475
|
+
}
|
|
476
|
+
renderHighlightLayers() {
|
|
477
|
+
const { pdfDocument } = this.props;
|
|
478
|
+
for (let pageNumber = 1; pageNumber <= pdfDocument.numPages; pageNumber++) {
|
|
479
|
+
const highlightRoot = this.highlightRoots[pageNumber];
|
|
480
|
+
if (highlightRoot == null ? void 0 : highlightRoot.container.isConnected) {
|
|
481
|
+
this.renderHighlightLayer(highlightRoot.reactRoot, pageNumber);
|
|
482
|
+
} else {
|
|
483
|
+
const highlightLayer = this.findOrCreateHighlightLayer(pageNumber);
|
|
484
|
+
if (highlightLayer) {
|
|
485
|
+
const reactRoot = createRoot(highlightLayer);
|
|
486
|
+
this.highlightRoots[pageNumber] = {
|
|
487
|
+
reactRoot,
|
|
488
|
+
container: highlightLayer
|
|
489
|
+
};
|
|
490
|
+
this.renderHighlightLayer(reactRoot, pageNumber);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
renderHighlightLayer(root, pageNumber) {
|
|
496
|
+
const { highlightTransform, highlights } = this.props;
|
|
497
|
+
const { tip, scrolledToHighlightId } = this.state;
|
|
498
|
+
root.render(
|
|
499
|
+
/* @__PURE__ */ jsx(
|
|
500
|
+
HighlightLayer,
|
|
501
|
+
{
|
|
502
|
+
highlightsByPage: this.groupHighlightsByPage(highlights),
|
|
503
|
+
pageNumber: pageNumber.toString(),
|
|
504
|
+
scrolledToHighlightId,
|
|
505
|
+
highlightTransform,
|
|
506
|
+
tip,
|
|
507
|
+
scaledPositionToViewport: this.scaledPositionToViewport.bind(this),
|
|
508
|
+
hideTipAndSelection: this.hideTipAndSelection.bind(this),
|
|
509
|
+
viewer: this.viewer,
|
|
510
|
+
screenshot: this.screenshot.bind(this),
|
|
511
|
+
showTip: this.showTip.bind(this),
|
|
512
|
+
setTip: (tip2) => {
|
|
513
|
+
this.setState({ tip: tip2 });
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
)
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
__publicField(PdfHighlighter, "defaultProps", {
|
|
521
|
+
pdfScaleValue: "auto"
|
|
522
|
+
});
|
|
523
|
+
const emptyPosition = {
|
|
524
|
+
boundingRect: { top: 0, left: 0, height: 0, width: 0 },
|
|
525
|
+
rects: [],
|
|
526
|
+
pageNumber: 0
|
|
527
|
+
};
|
|
528
|
+
const emptyScaledPosition = {
|
|
529
|
+
boundingRect: {
|
|
530
|
+
height: 0,
|
|
531
|
+
pageNumber: 0,
|
|
532
|
+
width: 0,
|
|
533
|
+
x1: 0,
|
|
534
|
+
x2: 0,
|
|
535
|
+
y1: 0,
|
|
536
|
+
y2: 0
|
|
537
|
+
},
|
|
538
|
+
pageNumber: 0,
|
|
539
|
+
rects: []
|
|
540
|
+
};
|
|
541
|
+
export {
|
|
542
|
+
PdfHighlighter
|
|
543
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import { jsxs, Fragment, jsx } from "react/jsx-runtime";
|
|
5
|
+
import { GlobalWorkerOptions, getDocument } from "pdfjs-dist";
|
|
6
|
+
import React, { Component } from "react";
|
|
7
|
+
class PdfLoader extends Component {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
__publicField(this, "state", {
|
|
11
|
+
pdfDocument: null,
|
|
12
|
+
error: null
|
|
13
|
+
});
|
|
14
|
+
__publicField(this, "documentRef", React.createRef());
|
|
15
|
+
}
|
|
16
|
+
componentDidMount() {
|
|
17
|
+
this.load();
|
|
18
|
+
}
|
|
19
|
+
componentWillUnmount() {
|
|
20
|
+
const { pdfDocument: discardedDocument } = this.state;
|
|
21
|
+
if (discardedDocument) {
|
|
22
|
+
discardedDocument.destroy();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
componentDidUpdate({ url }) {
|
|
26
|
+
if (this.props.url !== url) {
|
|
27
|
+
this.load();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
componentDidCatch(error) {
|
|
31
|
+
const { onError } = this.props;
|
|
32
|
+
if (onError) {
|
|
33
|
+
onError(error);
|
|
34
|
+
}
|
|
35
|
+
this.setState({ pdfDocument: null, error });
|
|
36
|
+
}
|
|
37
|
+
load() {
|
|
38
|
+
const { ownerDocument = document } = this.documentRef.current || {};
|
|
39
|
+
const { url, cMapUrl, cMapPacked, workerSrc } = this.props;
|
|
40
|
+
const { pdfDocument: discardedDocument } = this.state;
|
|
41
|
+
this.setState({ pdfDocument: null, error: null });
|
|
42
|
+
if (typeof workerSrc === "string") {
|
|
43
|
+
GlobalWorkerOptions.workerSrc = workerSrc;
|
|
44
|
+
}
|
|
45
|
+
Promise.resolve().then(() => discardedDocument == null ? void 0 : discardedDocument.destroy()).then(() => {
|
|
46
|
+
if (!url) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const document2 = {
|
|
50
|
+
...this.props,
|
|
51
|
+
ownerDocument,
|
|
52
|
+
cMapUrl,
|
|
53
|
+
cMapPacked
|
|
54
|
+
};
|
|
55
|
+
return getDocument(document2).promise.then((pdfDocument) => {
|
|
56
|
+
this.setState({ pdfDocument });
|
|
57
|
+
});
|
|
58
|
+
}).catch((e) => this.componentDidCatch(e));
|
|
59
|
+
}
|
|
60
|
+
render() {
|
|
61
|
+
const { children, beforeLoad } = this.props;
|
|
62
|
+
const { pdfDocument, error } = this.state;
|
|
63
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
64
|
+
/* @__PURE__ */ jsx("span", { ref: this.documentRef }),
|
|
65
|
+
error ? this.renderError() : !pdfDocument || !children ? beforeLoad : children(pdfDocument)
|
|
66
|
+
] });
|
|
67
|
+
}
|
|
68
|
+
renderError() {
|
|
69
|
+
const { errorMessage } = this.props;
|
|
70
|
+
if (errorMessage) {
|
|
71
|
+
return React.cloneElement(errorMessage, { error: this.state.error });
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
__publicField(PdfLoader, "defaultProps", {
|
|
77
|
+
workerSrc: "https://unpkg.com/pdfjs-dist@4.4.168/build/pdf.worker.min.mjs"
|
|
78
|
+
});
|
|
79
|
+
export {
|
|
80
|
+
PdfLoader
|
|
81
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { MouseMonitor } from "./MouseMonitor.js";
|
|
4
|
+
function Popup({
|
|
5
|
+
onMouseOver,
|
|
6
|
+
popupContent,
|
|
7
|
+
onMouseOut,
|
|
8
|
+
children
|
|
9
|
+
}) {
|
|
10
|
+
const [mouseIn, setMouseIn] = useState(false);
|
|
11
|
+
return /* @__PURE__ */ jsx(
|
|
12
|
+
"div",
|
|
13
|
+
{
|
|
14
|
+
onMouseOver: () => {
|
|
15
|
+
setMouseIn(true);
|
|
16
|
+
onMouseOver(
|
|
17
|
+
/* @__PURE__ */ jsx(
|
|
18
|
+
MouseMonitor,
|
|
19
|
+
{
|
|
20
|
+
onMoveAway: () => {
|
|
21
|
+
if (mouseIn) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
onMouseOut();
|
|
25
|
+
},
|
|
26
|
+
paddingX: 60,
|
|
27
|
+
paddingY: 30,
|
|
28
|
+
children: popupContent
|
|
29
|
+
}
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
},
|
|
33
|
+
onMouseOut: () => {
|
|
34
|
+
setMouseIn(false);
|
|
35
|
+
},
|
|
36
|
+
children
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
Popup
|
|
42
|
+
};
|