@olenbetong/synergi-react 5.0.0 → 5.1.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/es/components/Checkbox/index.js +1 -9
- package/es/components/CodeEditor/CodeEditor.css +6 -6
- package/es/components/FilePond.js +3 -12
- package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
- package/es/components/ImageEditor/useImageHistory.js +13 -1
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +1 -1
- package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
- package/es/components/Portal/index.js +20 -20
- package/es/components/SplitContainer/index.js +3 -1
- package/es/components/Toolbar/Toolbar.css +137 -137
- package/es/components/Toolbar/index.js +6 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
- package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
- package/es/elements-ds/DescriptionInput.d.ts +16 -0
- package/es/elements-ds/DescriptionInput.js +41 -0
- package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
- package/es/elements-ds/DuplicateElementDamage.js +57 -0
- package/es/elements-ds/ElementLookup.d.ts +23 -0
- package/es/elements-ds/ElementLookup.js +35 -0
- package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
- package/es/elements-ds/ElementLookupDialog.js +84 -0
- package/es/elements-ds/FormFields.d.ts +43 -0
- package/es/elements-ds/FormFields.js +67 -0
- package/es/elements-ds/HIDScanner.d.ts +7 -0
- package/es/elements-ds/HIDScanner.js +36 -0
- package/es/elements-ds/ManualForm.d.ts +7 -0
- package/es/elements-ds/ManualForm.js +70 -0
- package/es/elements-ds/ProjectSelect.d.ts +28 -0
- package/es/elements-ds/ProjectSelect.js +45 -0
- package/es/elements-ds/elements-ds.css +157 -0
- package/es/elements-ds/index.d.ts +12 -0
- package/es/elements-ds/index.js +14 -0
- package/es/useTranslation/index.js +5 -3
- package/package.json +15 -3
- package/src/chrome/AppShell/AppShell.test.tsx +48 -0
- package/src/chrome/AppShell/index.tsx +29 -29
- package/src/chrome/openReport.test.ts +50 -0
- package/src/chrome/openReport.ts +34 -34
- package/src/colorScheme.test.ts +188 -0
- package/src/colorScheme.ts +125 -125
- package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
- package/src/components/Checkbox/index.tsx +1 -10
- package/src/components/CodeEditor/CodeEditor.css +6 -6
- package/src/components/CodeEditor/CodeEditor.tsx +173 -173
- package/src/components/CodeEditor/index.ts +1 -1
- package/src/components/ColorCard/ColorCard.test.tsx +34 -0
- package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
- package/src/components/DateNavigator/index.tsx +186 -186
- package/src/components/FilePond.browser.test.tsx +389 -0
- package/src/components/FilePond.tsx +303 -312
- package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
- package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
- package/src/components/ImageEditor/ImageEditor.tsx +348 -348
- package/src/components/ImageEditor/index.ts +9 -9
- package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
- package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
- package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
- package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
- package/src/components/ImageEditor/useActiveTool.ts +21 -21
- package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
- package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
- package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
- package/src/components/ImageEditor/useImageHistory.ts +206 -197
- package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
- package/src/components/ImageEditor/useOpenCV.ts +184 -184
- package/src/components/LinkedCardList/index.scss +95 -95
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +1 -0
- package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
- package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
- package/src/components/PageBanner/index.tsx +80 -80
- package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
- package/src/components/PdfViewer/PdfViewer.tsx +158 -158
- package/src/components/Portal/Portal.test.tsx +106 -0
- package/src/components/Portal/index.tsx +22 -25
- package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
- package/src/components/Sidebar/Sidebar.test.tsx +123 -0
- package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
- package/src/components/SplitContainer/index.tsx +335 -333
- package/src/components/Toolbar/Toolbar.css +137 -137
- package/src/components/Toolbar/Toolbar.test.tsx +216 -0
- package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
- package/src/components/Toolbar/index.tsx +270 -265
- package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
- package/src/designsystemet-web-types.d.ts +24 -24
- package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
- package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
- package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
- package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
- package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
- package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
- package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
- package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
- package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
- package/src/elements/ElementLookup.tsx +123 -123
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
- package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
- package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
- package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
- package/src/elements/ElementLookupDialog/index.tsx +226 -226
- package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
- package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
- package/src/elements/ProjectSelect.tsx +130 -130
- package/src/elements/index.ts +15 -15
- package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
- package/src/elements-ds/DescriptionInput.tsx +114 -0
- package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
- package/src/elements-ds/ElementLookup.tsx +114 -0
- package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
- package/src/elements-ds/ElementLookupDialog.tsx +196 -0
- package/src/elements-ds/FormFields.tsx +176 -0
- package/src/elements-ds/HIDScanner.tsx +53 -0
- package/src/elements-ds/ManualForm.tsx +129 -0
- package/src/elements-ds/ProjectSelect.tsx +120 -0
- package/src/elements-ds/elements-ds.css +157 -0
- package/src/elements-ds/index.ts +16 -0
- package/src/filter/index.ts +34 -34
- package/src/i18n.test.ts +31 -0
- package/src/i18n.ts +13 -13
- package/src/mui/MuiThemeProvider.tsx +135 -135
- package/src/test/fakeCamera.ts +98 -0
- package/src/test/fakeDataApi.ts +90 -0
- package/src/test/setup.browser.ts +21 -0
- package/src/test/setup.ts +23 -23
- package/src/useTranslation/index.ts +5 -3
- package/src/useTranslation/useTranslation.test.ts +128 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
- package/es/test/setup.d.ts +0 -6
- package/es/test/setup.js +0 -6
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { PDFDocument, rgb } from "pdf-lib";
|
|
3
|
+
import workerUrl from "pdfjs-dist/build/pdf.worker.min.mjs?url";
|
|
4
|
+
import { pdfjs } from "react-pdf";
|
|
5
|
+
import { userEvent } from "vitest/browser";
|
|
6
|
+
import { beforeAll, describe, expect, it } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { PdfViewer } from "./PdfViewer";
|
|
9
|
+
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
/*
|
|
12
|
+
* On a page the platform serves pdf.js's worker next to pdfjs-dist; here there is no import
|
|
13
|
+
* map, so point pdf.js at the worker the way the platform would.
|
|
14
|
+
*/
|
|
15
|
+
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/** A PDF of `pages` A4 pages, each with a black block in its top-left corner. */
|
|
19
|
+
async function createPdf(pages: number) {
|
|
20
|
+
let pdf = await PDFDocument.create();
|
|
21
|
+
for (let i = 0; i < pages; i++) {
|
|
22
|
+
let page = pdf.addPage([595, 842]);
|
|
23
|
+
page.drawRectangle({ x: 0, y: 742, width: 100, height: 100, color: rgb(0, 0, 0) });
|
|
24
|
+
}
|
|
25
|
+
return new Blob([(await pdf.save()) as BlobPart], { type: "application/pdf" });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function canvases(container: HTMLElement) {
|
|
29
|
+
return [...container.querySelectorAll("canvas")];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe("PdfViewer", () => {
|
|
33
|
+
it("draws every page of the document", async () => {
|
|
34
|
+
let file = await createPdf(3);
|
|
35
|
+
let { container } = render(
|
|
36
|
+
<div style={{ width: 400, height: 600 }}>
|
|
37
|
+
<PdfViewer file={file} />
|
|
38
|
+
</div>,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
await expect.poll(() => canvases(container).length, { timeout: 10_000 }).toBe(3);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("fits the whole page in the viewer, then zooms with Ctrl", async () => {
|
|
45
|
+
let file = await createPdf(1);
|
|
46
|
+
let { container } = render(
|
|
47
|
+
<div style={{ width: 600, height: 400 }}>
|
|
48
|
+
<PdfViewer file={file} />
|
|
49
|
+
</div>,
|
|
50
|
+
);
|
|
51
|
+
let width = () => Math.round(canvases(container)[0]?.getBoundingClientRect().width ?? 0);
|
|
52
|
+
|
|
53
|
+
// An A4 page in a 600 x 400 box is as tall as the box: 400 x 595 / 842 = 283 px wide.
|
|
54
|
+
await expect.poll(width, { timeout: 10_000 }).toBe(283);
|
|
55
|
+
|
|
56
|
+
await userEvent.keyboard("{Control>}={/Control}");
|
|
57
|
+
await expect.poll(width).toBe(311);
|
|
58
|
+
|
|
59
|
+
await userEvent.keyboard("{Control>}-{/Control}");
|
|
60
|
+
await expect.poll(width).toBe(283);
|
|
61
|
+
|
|
62
|
+
await userEvent.keyboard("{Control>}={/Control}{Control>}={/Control}{Control>}0{/Control}");
|
|
63
|
+
await expect.poll(width).toBe(283);
|
|
64
|
+
|
|
65
|
+
// Ctrl + wheel zooms the document, not the page.
|
|
66
|
+
let wheel = new WheelEvent("wheel", { deltaY: -500, ctrlKey: true, bubbles: true, cancelable: true });
|
|
67
|
+
container.querySelector(".pdfViewerContainer")!.dispatchEvent(wheel);
|
|
68
|
+
expect(wheel.defaultPrevented).toBe(true);
|
|
69
|
+
await expect.poll(width).toBeGreaterThan(283);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("shows why a document cannot be read", async () => {
|
|
73
|
+
let file = new Blob(["this is not a pdf"], { type: "application/pdf" });
|
|
74
|
+
render(<PdfViewer file={file} />);
|
|
75
|
+
|
|
76
|
+
expect(await screen.findByText(/Error loading PDF/, {}, { timeout: 10_000 })).toBeInTheDocument();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
-
import { Document, Page, pdfjs } from "react-pdf";
|
|
3
|
-
import "./PdfViewer.css";
|
|
4
|
-
|
|
5
|
-
/*
|
|
6
|
-
* pdf.js parses in a Web Worker that must be loaded from a URL on the page's origin. On the
|
|
7
|
-
* 2026 templates pdfjs-dist comes from the platform bundle through the import map, and the
|
|
8
|
-
* platform serves the worker next to it - so resolve the specifier the way the browser does
|
|
9
|
-
* and step up one level. This makes the viewer platform-only: off the platform (a legacy
|
|
10
|
-
* template bundling this package) the specifier does not resolve, pdf.js logs a warning and
|
|
11
|
-
* parses on the main thread. A `new URL(..., import.meta.url)` fallback would make Vite emit
|
|
12
|
-
* the 1.2 MB worker into every consumer's build output without anything ever serving it.
|
|
13
|
-
*/
|
|
14
|
-
function platformWorkerUrl(): string | undefined {
|
|
15
|
-
try {
|
|
16
|
-
return new URL("../pdf.worker.min.js", import.meta.resolve("pdfjs-dist")).toString();
|
|
17
|
-
} catch {
|
|
18
|
-
console.warn("PdfViewer: pdfjs-dist is not on the page's import map; pdf.js will parse on the main thread.");
|
|
19
|
-
return undefined;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
let workerUrl = platformWorkerUrl();
|
|
24
|
-
if (workerUrl) {
|
|
25
|
-
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
type PdfViewerProps = { url?: string; file?: string | Blob | ArrayBuffer };
|
|
29
|
-
|
|
30
|
-
export function PdfViewer({ url, file }: PdfViewerProps) {
|
|
31
|
-
let [numPages, setNumPages] = useState(0);
|
|
32
|
-
let [error, setError] = useState<string | null>(null);
|
|
33
|
-
let containerRef = useRef<HTMLDivElement | null>(null);
|
|
34
|
-
let [containerWidth, setContainerWidth] = useState<number>(0);
|
|
35
|
-
let [containerHeight, setContainerHeight] = useState<number>(0);
|
|
36
|
-
let [scale, setScale] = useState<number>(1);
|
|
37
|
-
let [naturalPageSize, setNaturalPageSize] = useState<{
|
|
38
|
-
w: number;
|
|
39
|
-
h: number;
|
|
40
|
-
} | null>(null);
|
|
41
|
-
|
|
42
|
-
const pages = useMemo(() => Array.from({ length: numPages }, (_, i) => i + 1), [numPages]);
|
|
43
|
-
|
|
44
|
-
// Observe container size to support fit-to-page calculations
|
|
45
|
-
useEffect(() => {
|
|
46
|
-
let el = containerRef.current;
|
|
47
|
-
if (!el) return;
|
|
48
|
-
let ro = new ResizeObserver((entries) => {
|
|
49
|
-
let cr = entries[0].contentRect;
|
|
50
|
-
setContainerWidth(Math.max(1, Math.floor(cr.width)));
|
|
51
|
-
setContainerHeight(Math.max(1, Math.floor(cr.height)));
|
|
52
|
-
});
|
|
53
|
-
ro.observe(el);
|
|
54
|
-
setContainerWidth(Math.max(1, Math.floor(el.clientWidth)));
|
|
55
|
-
setContainerHeight(Math.max(1, Math.floor(el.clientHeight)));
|
|
56
|
-
return () => ro.disconnect();
|
|
57
|
-
}, []);
|
|
58
|
-
|
|
59
|
-
// Intercept browser zoom gestures and zoom the PDF instead
|
|
60
|
-
useEffect(() => {
|
|
61
|
-
let el = containerRef.current;
|
|
62
|
-
if (!el) return;
|
|
63
|
-
|
|
64
|
-
const clamp = (v: number, min: number, max: number) => Math.min(max, Math.max(min, v));
|
|
65
|
-
const zoomBy = (delta: number) => {
|
|
66
|
-
// Smooth exponential zoom for trackpads/mice
|
|
67
|
-
const factor = Math.exp(delta * 0.001);
|
|
68
|
-
setScale((s) => clamp(s * factor, 0.25, 5));
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const onWheel = (e: WheelEvent) => {
|
|
72
|
-
if (e.ctrlKey) {
|
|
73
|
-
// ctrl+wheel is usually browser zoom; override for PDF zoom
|
|
74
|
-
e.preventDefault();
|
|
75
|
-
zoomBy(-e.deltaY);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const onKeyDown = (e: KeyboardEvent) => {
|
|
80
|
-
if (!e.ctrlKey) return;
|
|
81
|
-
const key = e.key;
|
|
82
|
-
if (key === "+" || key === "=" || key === "Add") {
|
|
83
|
-
e.preventDefault();
|
|
84
|
-
setScale((s) => clamp(s * 1.1, 0.25, 5));
|
|
85
|
-
} else if (key === "-" || key === "_" || key === "Subtract") {
|
|
86
|
-
e.preventDefault();
|
|
87
|
-
setScale((s) => clamp(s / 1.1, 0.25, 5));
|
|
88
|
-
} else if (key === "0") {
|
|
89
|
-
e.preventDefault();
|
|
90
|
-
setScale(1);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
el.addEventListener("wheel", onWheel, { passive: false });
|
|
95
|
-
window.addEventListener("keydown", onKeyDown, { passive: false });
|
|
96
|
-
return () => {
|
|
97
|
-
el.removeEventListener("wheel", onWheel as EventListener);
|
|
98
|
-
window.removeEventListener("keydown", onKeyDown as EventListener);
|
|
99
|
-
};
|
|
100
|
-
}, []);
|
|
101
|
-
|
|
102
|
-
// When source changes, reset auto-fit baseline
|
|
103
|
-
useEffect(() => {
|
|
104
|
-
setNaturalPageSize(null);
|
|
105
|
-
setScale(1);
|
|
106
|
-
}, [url, file]);
|
|
107
|
-
|
|
108
|
-
// Compute base width to fit full page into container, then apply zoom scale.
|
|
109
|
-
// If we don't yet know page's natural size, default to fit-to-width.
|
|
110
|
-
const fitBaseWidth = useMemo(() => {
|
|
111
|
-
if (!naturalPageSize) return containerWidth || 1000;
|
|
112
|
-
const { w, h } = naturalPageSize;
|
|
113
|
-
if (w <= 0 || h <= 0) return containerWidth || 1000;
|
|
114
|
-
// width to fit by height: containerHeight * (w/h)
|
|
115
|
-
const widthByHeight = (containerHeight || 1000) * (w / h);
|
|
116
|
-
// Fit to page means respecting both constraints
|
|
117
|
-
return Math.min(Math.max(1, containerWidth || 1000), Math.max(1, widthByHeight));
|
|
118
|
-
}, [naturalPageSize, containerWidth, containerHeight]);
|
|
119
|
-
|
|
120
|
-
const pageWidth = Math.max(1, Math.round(fitBaseWidth * scale));
|
|
121
|
-
|
|
122
|
-
return (
|
|
123
|
-
<div ref={containerRef} className="pdfViewerContainer">
|
|
124
|
-
<div className="pdfViewerContent">
|
|
125
|
-
<Document
|
|
126
|
-
file={file ?? url}
|
|
127
|
-
onLoadSuccess={({ numPages }) => setNumPages(numPages)}
|
|
128
|
-
onLoadError={(e) => setError((e as Error)?.message ?? "Failed to load PDF")}
|
|
129
|
-
>
|
|
130
|
-
{pages.map((pageNumber) => (
|
|
131
|
-
<Page
|
|
132
|
-
key={pageNumber}
|
|
133
|
-
pageNumber={pageNumber}
|
|
134
|
-
renderTextLayer={false}
|
|
135
|
-
renderAnnotationLayer={false}
|
|
136
|
-
width={pageWidth}
|
|
137
|
-
onLoadSuccess={(p) => {
|
|
138
|
-
// Capture natural page size once for fit-to-page calculation
|
|
139
|
-
try {
|
|
140
|
-
// pdf.js viewport at scale 1
|
|
141
|
-
const vp = p.getViewport({ scale: 1 });
|
|
142
|
-
const w = Number(vp?.width) || Number((p as any).width) || 0;
|
|
143
|
-
const h = Number(vp?.height) || Number((p as any).height) || 0;
|
|
144
|
-
if (w > 0 && h > 0 && !naturalPageSize) {
|
|
145
|
-
setNaturalPageSize({ w, h });
|
|
146
|
-
}
|
|
147
|
-
} catch {
|
|
148
|
-
// Ignore, fallback to fit-to-width when unknown
|
|
149
|
-
}
|
|
150
|
-
}}
|
|
151
|
-
/>
|
|
152
|
-
))}
|
|
153
|
-
</Document>
|
|
154
|
-
</div>
|
|
155
|
-
{error && <div style={{ color: "#b00020", padding: 8 }}>Error loading PDF: {error}</div>}
|
|
156
|
-
</div>
|
|
157
|
-
);
|
|
158
|
-
}
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { Document, Page, pdfjs } from "react-pdf";
|
|
3
|
+
import "./PdfViewer.css";
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* pdf.js parses in a Web Worker that must be loaded from a URL on the page's origin. On the
|
|
7
|
+
* 2026 templates pdfjs-dist comes from the platform bundle through the import map, and the
|
|
8
|
+
* platform serves the worker next to it - so resolve the specifier the way the browser does
|
|
9
|
+
* and step up one level. This makes the viewer platform-only: off the platform (a legacy
|
|
10
|
+
* template bundling this package) the specifier does not resolve, pdf.js logs a warning and
|
|
11
|
+
* parses on the main thread. A `new URL(..., import.meta.url)` fallback would make Vite emit
|
|
12
|
+
* the 1.2 MB worker into every consumer's build output without anything ever serving it.
|
|
13
|
+
*/
|
|
14
|
+
function platformWorkerUrl(): string | undefined {
|
|
15
|
+
try {
|
|
16
|
+
return new URL("../pdf.worker.min.js", import.meta.resolve("pdfjs-dist")).toString();
|
|
17
|
+
} catch {
|
|
18
|
+
console.warn("PdfViewer: pdfjs-dist is not on the page's import map; pdf.js will parse on the main thread.");
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let workerUrl = platformWorkerUrl();
|
|
24
|
+
if (workerUrl) {
|
|
25
|
+
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type PdfViewerProps = { url?: string; file?: string | Blob | ArrayBuffer };
|
|
29
|
+
|
|
30
|
+
export function PdfViewer({ url, file }: PdfViewerProps) {
|
|
31
|
+
let [numPages, setNumPages] = useState(0);
|
|
32
|
+
let [error, setError] = useState<string | null>(null);
|
|
33
|
+
let containerRef = useRef<HTMLDivElement | null>(null);
|
|
34
|
+
let [containerWidth, setContainerWidth] = useState<number>(0);
|
|
35
|
+
let [containerHeight, setContainerHeight] = useState<number>(0);
|
|
36
|
+
let [scale, setScale] = useState<number>(1);
|
|
37
|
+
let [naturalPageSize, setNaturalPageSize] = useState<{
|
|
38
|
+
w: number;
|
|
39
|
+
h: number;
|
|
40
|
+
} | null>(null);
|
|
41
|
+
|
|
42
|
+
const pages = useMemo(() => Array.from({ length: numPages }, (_, i) => i + 1), [numPages]);
|
|
43
|
+
|
|
44
|
+
// Observe container size to support fit-to-page calculations
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
let el = containerRef.current;
|
|
47
|
+
if (!el) return;
|
|
48
|
+
let ro = new ResizeObserver((entries) => {
|
|
49
|
+
let cr = entries[0].contentRect;
|
|
50
|
+
setContainerWidth(Math.max(1, Math.floor(cr.width)));
|
|
51
|
+
setContainerHeight(Math.max(1, Math.floor(cr.height)));
|
|
52
|
+
});
|
|
53
|
+
ro.observe(el);
|
|
54
|
+
setContainerWidth(Math.max(1, Math.floor(el.clientWidth)));
|
|
55
|
+
setContainerHeight(Math.max(1, Math.floor(el.clientHeight)));
|
|
56
|
+
return () => ro.disconnect();
|
|
57
|
+
}, []);
|
|
58
|
+
|
|
59
|
+
// Intercept browser zoom gestures and zoom the PDF instead
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
let el = containerRef.current;
|
|
62
|
+
if (!el) return;
|
|
63
|
+
|
|
64
|
+
const clamp = (v: number, min: number, max: number) => Math.min(max, Math.max(min, v));
|
|
65
|
+
const zoomBy = (delta: number) => {
|
|
66
|
+
// Smooth exponential zoom for trackpads/mice
|
|
67
|
+
const factor = Math.exp(delta * 0.001);
|
|
68
|
+
setScale((s) => clamp(s * factor, 0.25, 5));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const onWheel = (e: WheelEvent) => {
|
|
72
|
+
if (e.ctrlKey) {
|
|
73
|
+
// ctrl+wheel is usually browser zoom; override for PDF zoom
|
|
74
|
+
e.preventDefault();
|
|
75
|
+
zoomBy(-e.deltaY);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
80
|
+
if (!e.ctrlKey) return;
|
|
81
|
+
const key = e.key;
|
|
82
|
+
if (key === "+" || key === "=" || key === "Add") {
|
|
83
|
+
e.preventDefault();
|
|
84
|
+
setScale((s) => clamp(s * 1.1, 0.25, 5));
|
|
85
|
+
} else if (key === "-" || key === "_" || key === "Subtract") {
|
|
86
|
+
e.preventDefault();
|
|
87
|
+
setScale((s) => clamp(s / 1.1, 0.25, 5));
|
|
88
|
+
} else if (key === "0") {
|
|
89
|
+
e.preventDefault();
|
|
90
|
+
setScale(1);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
el.addEventListener("wheel", onWheel, { passive: false });
|
|
95
|
+
window.addEventListener("keydown", onKeyDown, { passive: false });
|
|
96
|
+
return () => {
|
|
97
|
+
el.removeEventListener("wheel", onWheel as EventListener);
|
|
98
|
+
window.removeEventListener("keydown", onKeyDown as EventListener);
|
|
99
|
+
};
|
|
100
|
+
}, []);
|
|
101
|
+
|
|
102
|
+
// When source changes, reset auto-fit baseline
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
setNaturalPageSize(null);
|
|
105
|
+
setScale(1);
|
|
106
|
+
}, [url, file]);
|
|
107
|
+
|
|
108
|
+
// Compute base width to fit full page into container, then apply zoom scale.
|
|
109
|
+
// If we don't yet know page's natural size, default to fit-to-width.
|
|
110
|
+
const fitBaseWidth = useMemo(() => {
|
|
111
|
+
if (!naturalPageSize) return containerWidth || 1000;
|
|
112
|
+
const { w, h } = naturalPageSize;
|
|
113
|
+
if (w <= 0 || h <= 0) return containerWidth || 1000;
|
|
114
|
+
// width to fit by height: containerHeight * (w/h)
|
|
115
|
+
const widthByHeight = (containerHeight || 1000) * (w / h);
|
|
116
|
+
// Fit to page means respecting both constraints
|
|
117
|
+
return Math.min(Math.max(1, containerWidth || 1000), Math.max(1, widthByHeight));
|
|
118
|
+
}, [naturalPageSize, containerWidth, containerHeight]);
|
|
119
|
+
|
|
120
|
+
const pageWidth = Math.max(1, Math.round(fitBaseWidth * scale));
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<div ref={containerRef} className="pdfViewerContainer">
|
|
124
|
+
<div className="pdfViewerContent">
|
|
125
|
+
<Document
|
|
126
|
+
file={file ?? url}
|
|
127
|
+
onLoadSuccess={({ numPages }) => setNumPages(numPages)}
|
|
128
|
+
onLoadError={(e) => setError((e as Error)?.message ?? "Failed to load PDF")}
|
|
129
|
+
>
|
|
130
|
+
{pages.map((pageNumber) => (
|
|
131
|
+
<Page
|
|
132
|
+
key={pageNumber}
|
|
133
|
+
pageNumber={pageNumber}
|
|
134
|
+
renderTextLayer={false}
|
|
135
|
+
renderAnnotationLayer={false}
|
|
136
|
+
width={pageWidth}
|
|
137
|
+
onLoadSuccess={(p) => {
|
|
138
|
+
// Capture natural page size once for fit-to-page calculation
|
|
139
|
+
try {
|
|
140
|
+
// pdf.js viewport at scale 1
|
|
141
|
+
const vp = p.getViewport({ scale: 1 });
|
|
142
|
+
const w = Number(vp?.width) || Number((p as any).width) || 0;
|
|
143
|
+
const h = Number(vp?.height) || Number((p as any).height) || 0;
|
|
144
|
+
if (w > 0 && h > 0 && !naturalPageSize) {
|
|
145
|
+
setNaturalPageSize({ w, h });
|
|
146
|
+
}
|
|
147
|
+
} catch {
|
|
148
|
+
// Ignore, fallback to fit-to-width when unknown
|
|
149
|
+
}
|
|
150
|
+
}}
|
|
151
|
+
/>
|
|
152
|
+
))}
|
|
153
|
+
</Document>
|
|
154
|
+
</div>
|
|
155
|
+
{error && <div style={{ color: "#b00020", padding: 8 }}>Error loading PDF: {error}</div>}
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { StrictMode } from "react";
|
|
3
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { Portal } from "./index";
|
|
6
|
+
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
document.getElementById("dialogs")?.remove();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe("Portal", () => {
|
|
12
|
+
it("renders its children at the end of <body>, outside the component's own tree", () => {
|
|
13
|
+
let { container } = render(
|
|
14
|
+
<Portal>
|
|
15
|
+
<p>Floating</p>
|
|
16
|
+
</Portal>,
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
let content = screen.getByText("Floating");
|
|
20
|
+
expect(container).not.toContainElement(content);
|
|
21
|
+
expect(content.parentElement?.tagName).toBe("DIV");
|
|
22
|
+
expect(content.parentElement?.parentElement).toBe(document.body);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("renders into the element the root selector points at", () => {
|
|
26
|
+
let host = document.createElement("section");
|
|
27
|
+
host.id = "dialogs";
|
|
28
|
+
document.body.append(host);
|
|
29
|
+
|
|
30
|
+
render(
|
|
31
|
+
<Portal root="#dialogs">
|
|
32
|
+
<p>Floating</p>
|
|
33
|
+
</Portal>,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
expect(host).toContainElement(screen.getByText("Floating"));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("falls back to <body> when the root is not on the page", () => {
|
|
40
|
+
render(
|
|
41
|
+
<Portal root="#missing">
|
|
42
|
+
<p>Floating</p>
|
|
43
|
+
</Portal>,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
expect(screen.getByText("Floating").parentElement?.parentElement).toBe(document.body);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("wraps the children in an element of the given type", () => {
|
|
50
|
+
render(
|
|
51
|
+
<Portal type="aside">
|
|
52
|
+
<p>Floating</p>
|
|
53
|
+
</Portal>,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
expect(screen.getByText("Floating").parentElement?.tagName).toBe("ASIDE");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("removes its element when it unmounts", () => {
|
|
60
|
+
let { unmount } = render(
|
|
61
|
+
<Portal type="aside">
|
|
62
|
+
<p>Floating</p>
|
|
63
|
+
</Portal>,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
unmount();
|
|
67
|
+
|
|
68
|
+
expect(screen.queryByText("Floating")).toBeNull();
|
|
69
|
+
expect(document.body.querySelector("aside")).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("moves its children when the root changes", () => {
|
|
73
|
+
let host = document.createElement("section");
|
|
74
|
+
host.id = "dialogs";
|
|
75
|
+
document.body.append(host);
|
|
76
|
+
|
|
77
|
+
let { rerender } = render(
|
|
78
|
+
<Portal>
|
|
79
|
+
<p>Floating</p>
|
|
80
|
+
</Portal>,
|
|
81
|
+
);
|
|
82
|
+
let first = screen.getByText("Floating").parentElement;
|
|
83
|
+
expect(host).not.toContainElement(first);
|
|
84
|
+
|
|
85
|
+
rerender(
|
|
86
|
+
<Portal root="#dialogs">
|
|
87
|
+
<p>Floating</p>
|
|
88
|
+
</Portal>,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(host).toContainElement(screen.getByText("Floating"));
|
|
92
|
+
expect(first).not.toBeInTheDocument();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("shows its children under StrictMode", () => {
|
|
96
|
+
render(
|
|
97
|
+
<StrictMode>
|
|
98
|
+
<Portal>
|
|
99
|
+
<p>Floating</p>
|
|
100
|
+
</Portal>
|
|
101
|
+
</StrictMode>,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
expect(screen.getByText("Floating")).toBeInTheDocument();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { useEffect, useRef, useState } from "react";
|
|
2
2
|
import { createPortal } from "react-dom";
|
|
3
3
|
|
|
4
|
-
function useForceUpdate() {
|
|
5
|
-
const [, setVal] = useState(false);
|
|
6
|
-
|
|
7
|
-
return () => setVal((val) => !val);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
4
|
export type PortalProps = {
|
|
11
5
|
children: React.ReactNode;
|
|
12
6
|
root?: string;
|
|
@@ -14,30 +8,33 @@ export type PortalProps = {
|
|
|
14
8
|
};
|
|
15
9
|
|
|
16
10
|
export function Portal({ children, root, type = "div" }: PortalProps) {
|
|
17
|
-
const forceUpdate = useForceUpdate();
|
|
18
11
|
const mountNode = useRef<HTMLDivElement>(null);
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
const ownerDocument = useRef<Document | null>(null);
|
|
13
|
+
const [portalNode, setPortalNode] = useState<HTMLElement | null>(null);
|
|
14
|
+
|
|
15
|
+
/*
|
|
16
|
+
* The element is created after mount, in the document the placeholder was rendered in, and
|
|
17
|
+
* held in state: a remount (StrictMode) or a new root/type creates a fresh one and renders
|
|
18
|
+
* into it, rather than into the element the previous run removed.
|
|
19
|
+
*/
|
|
22
20
|
useEffect(() => {
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
21
|
+
let doc = mountNode.current?.ownerDocument ?? ownerDocument.current ?? globalThis.document;
|
|
22
|
+
if (!doc) return;
|
|
23
|
+
ownerDocument.current = doc;
|
|
24
|
+
|
|
25
|
+
let parent = (root ? doc.querySelector(root) : null) ?? doc.body;
|
|
26
|
+
let node = doc.createElement(type);
|
|
27
|
+
parent.appendChild(node);
|
|
28
|
+
setPortalNode(node);
|
|
29
|
+
|
|
30
|
+
return () => {
|
|
31
|
+
parent.removeChild(node);
|
|
32
|
+
};
|
|
36
33
|
}, [root, type]);
|
|
37
34
|
|
|
38
|
-
if (!portalNode
|
|
35
|
+
if (!portalNode) {
|
|
39
36
|
return <div ref={mountNode} />;
|
|
40
37
|
}
|
|
41
38
|
|
|
42
|
-
return createPortal(children, portalNode
|
|
39
|
+
return createPortal(children, portalNode);
|
|
43
40
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { userEvent } from "vitest/browser";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { Sidebar } from "./index";
|
|
6
|
+
|
|
7
|
+
describe("Sidebar in a real browser", () => {
|
|
8
|
+
it("slides in from the edge and out again, dropping its content when it has left", async () => {
|
|
9
|
+
let { rerender } = render(
|
|
10
|
+
<Sidebar>
|
|
11
|
+
<h2>Customer</h2>
|
|
12
|
+
</Sidebar>,
|
|
13
|
+
);
|
|
14
|
+
let panel = document.querySelector<HTMLElement>(".ObSidebar-root")!;
|
|
15
|
+
expect(panel.getBoundingClientRect().right).toBeLessThanOrEqual(0);
|
|
16
|
+
|
|
17
|
+
rerender(
|
|
18
|
+
<Sidebar open>
|
|
19
|
+
<h2>Customer</h2>
|
|
20
|
+
</Sidebar>,
|
|
21
|
+
);
|
|
22
|
+
await expect.poll(() => panel.getBoundingClientRect().left).toBe(0);
|
|
23
|
+
|
|
24
|
+
rerender(
|
|
25
|
+
<Sidebar open={false}>
|
|
26
|
+
<h2>Customer</h2>
|
|
27
|
+
</Sidebar>,
|
|
28
|
+
);
|
|
29
|
+
expect(screen.getByRole("heading", { name: "Customer" })).toBeInTheDocument();
|
|
30
|
+
await expect.poll(() => screen.queryByRole("heading", { name: "Customer" })).toBeNull();
|
|
31
|
+
expect(panel.getBoundingClientRect().right).toBeLessThanOrEqual(0);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("slides in from the right with pullRight", async () => {
|
|
35
|
+
render(
|
|
36
|
+
<Sidebar open pullRight>
|
|
37
|
+
<h2>Customer</h2>
|
|
38
|
+
</Sidebar>,
|
|
39
|
+
);
|
|
40
|
+
let panel = document.querySelector<HTMLElement>(".ObSidebar-root")!;
|
|
41
|
+
|
|
42
|
+
await expect.poll(() => Math.round(panel.getBoundingClientRect().right)).toBe(window.innerWidth);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("closes when the user clicks beside it", async () => {
|
|
46
|
+
let onClose = vi.fn();
|
|
47
|
+
render(
|
|
48
|
+
<Sidebar open onClose={onClose} style={{ width: 200 }}>
|
|
49
|
+
<h2>Customer</h2>
|
|
50
|
+
</Sidebar>,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
await userEvent.click(document.body, { position: { x: window.innerWidth - 20, y: 20 } });
|
|
54
|
+
|
|
55
|
+
expect(onClose).toHaveBeenCalledOnce();
|
|
56
|
+
});
|
|
57
|
+
});
|