@olenbetong/synergi-react 4.3.1 → 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/dist/styles/styles.css +1 -1
- package/dist/styles/styles.css.map +1 -1
- package/es/components/Checkbox/index.js +1 -9
- 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.d.ts +7 -0
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +20 -0
- package/es/components/MergeProgressDialog/index.d.ts +3 -8
- package/es/components/MergeProgressDialog/index.js +4 -133
- package/es/components/MergeProgressDialog/useMergeJob.d.ts +36 -0
- package/es/components/MergeProgressDialog/useMergeJob.js +142 -0
- package/es/components/Portal/index.js +20 -20
- package/es/components/ProgressBar/index.css +1 -1
- package/es/components/SplitContainer/index.js +3 -1
- package/es/components/Toolbar/index.js +6 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +81 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.d.ts +2 -2
- package/es/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.js +1 -2
- package/es/elements/ElementLookupDialog/BarcodeScanner/Error.js +3 -29
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/index.d.ts +1 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +3 -28
- 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/index.css +1 -1
- package/es/index.d.ts +2 -1
- package/es/index.js +2 -1
- package/es/mui/index.d.ts +1 -0
- package/es/mui/index.js +1 -0
- package/es/useTranslation/index.js +5 -3
- package/package.json +34 -15
- package/src/chrome/AppShell/AppShell.test.tsx +48 -0
- package/src/chrome/openReport.test.ts +50 -0
- package/src/colorScheme.test.ts +188 -0
- package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
- package/src/components/Checkbox/index.tsx +1 -10
- package/src/components/ColorCard/ColorCard.test.tsx +34 -0
- package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
- package/src/components/FilePond.browser.test.tsx +389 -0
- package/src/components/FilePond.tsx +3 -12
- package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
- package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
- package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
- 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/useZoomPan.test.ts +154 -0
- package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
- package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
- package/src/components/ImageEditor/useImageHistory.ts +10 -1
- package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +92 -0
- package/src/components/MergeProgressDialog/index.tsx +5 -158
- package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
- package/src/components/MergeProgressDialog/useMergeJob.ts +170 -0
- package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
- package/src/components/Portal/Portal.test.tsx +106 -0
- package/src/components/Portal/index.tsx +22 -25
- package/src/components/ProgressBar/index.scss +23 -54
- 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 +3 -1
- 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 +6 -1
- package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +81 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.tsx +4 -4
- package/src/elements/ElementLookupDialog/BarcodeScanner/Error.tsx +7 -36
- 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/BarcodeScanner/index.tsx +20 -49
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
- package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
- package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
- 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/i18n.test.ts +31 -0
- package/src/index.ts +5 -1
- package/src/mui/index.ts +1 -0
- 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 +15 -0
- 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 -1
- package/es/test/setup.js +0 -6
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import type { Quad } from "../../useEdgeDetection";
|
|
5
|
+
import { useCropTool } from "./useCropTool";
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* OpenCV is never downloaded here. The tool's own geometry is tested against a stubbed
|
|
9
|
+
* loader and a border suggestion the test controls.
|
|
10
|
+
*/
|
|
11
|
+
let openCV: { cv: unknown; ready: boolean; error: Error | null } = { cv: null, ready: false, error: null };
|
|
12
|
+
let suggestion: Quad | null = null;
|
|
13
|
+
|
|
14
|
+
vi.mock("../../useOpenCV.js", () => ({
|
|
15
|
+
useOpenCV: () => openCV,
|
|
16
|
+
loadOpenCV: () => Promise.reject(new Error("offline")),
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
vi.mock("../../useEdgeDetection.js", async (importOriginal) => ({
|
|
20
|
+
...(await importOriginal<typeof import("../../useEdgeDetection")>()),
|
|
21
|
+
useEdgeDetection: () => ({ corners: suggestion }),
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
openCV = { cv: null, ready: false, error: null };
|
|
26
|
+
suggestion = null;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** An 800×400 image shown at half size (400×200 CSS px), as the crop canvas reports it. */
|
|
30
|
+
function setup(imageUrl = "blob:image") {
|
|
31
|
+
let hook = renderHook(({ url }) => useCropTool(url, true), { initialProps: { url: imageUrl } });
|
|
32
|
+
act(() => {
|
|
33
|
+
hook.result.current.handleCanvasReady({ clientWidth: 400, clientHeight: 200 } as HTMLCanvasElement, 800, 400);
|
|
34
|
+
});
|
|
35
|
+
return hook;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const FULL: Quad = [
|
|
39
|
+
{ x: 0, y: 0 },
|
|
40
|
+
{ x: 800, y: 0 },
|
|
41
|
+
{ x: 800, y: 400 },
|
|
42
|
+
{ x: 0, y: 400 },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const SUGGESTED: Quad = [
|
|
46
|
+
{ x: 100, y: 50 },
|
|
47
|
+
{ x: 700, y: 60 },
|
|
48
|
+
{ x: 690, y: 350 },
|
|
49
|
+
{ x: 110, y: 340 },
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
describe("useCropTool", () => {
|
|
53
|
+
it("selects the whole image until a border is suggested", () => {
|
|
54
|
+
let { result } = setup();
|
|
55
|
+
|
|
56
|
+
expect(result.current.activeCorners).toEqual(FULL);
|
|
57
|
+
expect(result.current.scale).toBe(0.5);
|
|
58
|
+
expect(result.current.displaySize).toEqual({ width: 400, height: 200 });
|
|
59
|
+
expect(result.current.mode).toBe("corners");
|
|
60
|
+
expect(result.current.hasSelection).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("uses the suggested document border when it arrives", () => {
|
|
64
|
+
let { result, rerender } = setup();
|
|
65
|
+
|
|
66
|
+
suggestion = SUGGESTED;
|
|
67
|
+
rerender({ url: "blob:image" });
|
|
68
|
+
|
|
69
|
+
expect(result.current.activeCorners).toEqual(SUGGESTED);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("moves a corner to where it is dragged, converted from screen to image pixels", () => {
|
|
73
|
+
let { result } = setup();
|
|
74
|
+
|
|
75
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
76
|
+
|
|
77
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 100, y: 50 });
|
|
78
|
+
expect(result.current.activeCorners.slice(1)).toEqual(FULL.slice(1));
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("keeps a dragged corner inside the image", () => {
|
|
82
|
+
let { result } = setup();
|
|
83
|
+
|
|
84
|
+
act(() => result.current.updateCorner(2, 1000, -30));
|
|
85
|
+
|
|
86
|
+
expect(result.current.activeCorners[2]).toEqual({ x: 800, y: 0 });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("moves both corners of an edge dragged in one event", () => {
|
|
90
|
+
let { result } = setup();
|
|
91
|
+
|
|
92
|
+
// What the top edge handle does: shift TL and TR by the same delta.
|
|
93
|
+
act(() => {
|
|
94
|
+
result.current.updateCorner(0, 0, 20);
|
|
95
|
+
result.current.updateCorner(1, 400, 20);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
expect(result.current.activeCorners.slice(0, 2)).toEqual([
|
|
99
|
+
{ x: 0, y: 40 },
|
|
100
|
+
{ x: 800, y: 40 },
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("does not let a late suggestion overwrite corners the user has moved", () => {
|
|
105
|
+
let { result, rerender } = setup();
|
|
106
|
+
|
|
107
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
108
|
+
suggestion = SUGGESTED;
|
|
109
|
+
rerender({ url: "blob:image" });
|
|
110
|
+
|
|
111
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 100, y: 50 });
|
|
112
|
+
expect(result.current.activeCorners[1]).toEqual(FULL[1]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("drawing a rectangle", () => {
|
|
116
|
+
it("clears the selection until a rectangle is drawn", () => {
|
|
117
|
+
let { result } = setup();
|
|
118
|
+
|
|
119
|
+
act(() => result.current.startRectMode());
|
|
120
|
+
|
|
121
|
+
expect(result.current.mode).toBe("rect");
|
|
122
|
+
expect(result.current.hasSelection).toBe(false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("shows the rectangle while dragging and selects it on release", () => {
|
|
126
|
+
let { result } = setup();
|
|
127
|
+
act(() => result.current.startRectMode());
|
|
128
|
+
|
|
129
|
+
// Dragged from bottom-right to top-left, in CSS px.
|
|
130
|
+
act(() => result.current.startRectDraw(110, 60));
|
|
131
|
+
act(() => result.current.updateRectDraw(10, 10));
|
|
132
|
+
let expected: Quad = [
|
|
133
|
+
{ x: 20, y: 20 },
|
|
134
|
+
{ x: 220, y: 20 },
|
|
135
|
+
{ x: 220, y: 120 },
|
|
136
|
+
{ x: 20, y: 120 },
|
|
137
|
+
];
|
|
138
|
+
expect(result.current.hasSelection).toBe(true);
|
|
139
|
+
expect(result.current.activeCorners).toEqual(expected);
|
|
140
|
+
|
|
141
|
+
act(() => result.current.finishRectDraw());
|
|
142
|
+
expect(result.current.mode).toBe("corners");
|
|
143
|
+
expect(result.current.activeCorners).toEqual(expected);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("ignores a tap and stays in rectangle mode", () => {
|
|
147
|
+
let { result } = setup();
|
|
148
|
+
act(() => result.current.startRectMode());
|
|
149
|
+
|
|
150
|
+
act(() => result.current.startRectDraw(50, 50));
|
|
151
|
+
act(() => result.current.updateRectDraw(52, 90));
|
|
152
|
+
act(() => result.current.finishRectDraw());
|
|
153
|
+
|
|
154
|
+
expect(result.current.mode).toBe("rect");
|
|
155
|
+
expect(result.current.hasSelection).toBe(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("does not move corners while in rectangle mode", () => {
|
|
159
|
+
let { result } = setup();
|
|
160
|
+
act(() => result.current.startRectMode());
|
|
161
|
+
|
|
162
|
+
act(() => result.current.updateCorner(0, 50, 50));
|
|
163
|
+
act(() => result.current.cancelRectMode());
|
|
164
|
+
|
|
165
|
+
expect(result.current.activeCorners).toEqual(FULL);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("restores the previous selection when the user backs out", () => {
|
|
169
|
+
let { result } = setup();
|
|
170
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
171
|
+
act(() => result.current.startRectMode());
|
|
172
|
+
|
|
173
|
+
act(() => result.current.cancelRectMode());
|
|
174
|
+
|
|
175
|
+
expect(result.current.mode).toBe("corners");
|
|
176
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 100, y: 50 });
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("falls back to the suggested border when backing out before any selection was made", () => {
|
|
180
|
+
let { result, rerender } = setup();
|
|
181
|
+
suggestion = SUGGESTED;
|
|
182
|
+
rerender({ url: "blob:image" });
|
|
183
|
+
act(() => result.current.startRectMode());
|
|
184
|
+
|
|
185
|
+
act(() => result.current.cancelRectMode());
|
|
186
|
+
|
|
187
|
+
expect(result.current.activeCorners).toEqual(SUGGESTED);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("cannot apply until the rectangle is drawn", async () => {
|
|
191
|
+
let { result } = setup();
|
|
192
|
+
act(() => result.current.startRectMode());
|
|
193
|
+
|
|
194
|
+
let applied: unknown;
|
|
195
|
+
await act(async () => {
|
|
196
|
+
applied = await result.current.applyCrop();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
expect(applied).toBeNull();
|
|
200
|
+
expect(result.current.cropping).toBe(false);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("starts over on a new image, e.g. after a rotate", () => {
|
|
205
|
+
let { result, rerender } = setup();
|
|
206
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
207
|
+
act(() => result.current.startRectMode());
|
|
208
|
+
|
|
209
|
+
rerender({ url: "blob:rotated" });
|
|
210
|
+
|
|
211
|
+
expect(result.current.mode).toBe("corners");
|
|
212
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 0, y: 0 });
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("passes on an OpenCV load failure so the overlay can explain it", () => {
|
|
216
|
+
let failure = new Error("Failed to load OpenCV.js");
|
|
217
|
+
openCV = { cv: null, ready: false, error: failure };
|
|
218
|
+
|
|
219
|
+
let { result } = setup();
|
|
220
|
+
|
|
221
|
+
expect(result.current.cvReady).toBe(false);
|
|
222
|
+
expect(result.current.cvError).toBe(failure);
|
|
223
|
+
expect(result.current.activeCorners).toEqual(FULL);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { configureOpenCV } from "../useOpenCV";
|
|
4
|
+
import { replayCrop } from "./cropPlugin/useCropTool";
|
|
5
|
+
import { replayRedactBlur } from "./redactPlugin/useRedactTool";
|
|
6
|
+
import { rotateCcwPlugin, rotateCwPlugin } from "./rotatePlugin";
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* The images the plugins produce, checked pixel by pixel on small generated pictures.
|
|
10
|
+
* OpenCV is pointed at a port nothing listens on, so a perspective crop takes the
|
|
11
|
+
* no-OpenCV path a user gets when the CDN is unreachable, and nothing is downloaded.
|
|
12
|
+
*/
|
|
13
|
+
configureOpenCV("http://127.0.0.1:1/opencv.js");
|
|
14
|
+
|
|
15
|
+
type Rgb = [number, number, number];
|
|
16
|
+
const RED: Rgb = [230, 30, 30];
|
|
17
|
+
const GREEN: Rgb = [30, 170, 60];
|
|
18
|
+
const BLUE: Rgb = [30, 60, 230];
|
|
19
|
+
const YELLOW: Rgb = [240, 220, 40];
|
|
20
|
+
|
|
21
|
+
let urls: string[] = [];
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
for (let url of urls) URL.revokeObjectURL(url);
|
|
25
|
+
urls = [];
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function toBlob(canvas: HTMLCanvasElement) {
|
|
29
|
+
return new Promise<Blob>((resolve) => canvas.toBlob((b) => resolve(b!), "image/png"));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A PNG with a different colour in each quadrant: red, green / blue, yellow. */
|
|
33
|
+
async function quadrantImage(width = 80, height = 40) {
|
|
34
|
+
let canvas = document.createElement("canvas");
|
|
35
|
+
canvas.width = width;
|
|
36
|
+
canvas.height = height;
|
|
37
|
+
let ctx = canvas.getContext("2d")!;
|
|
38
|
+
let fill = ([r, g, b]: Rgb, x: number, y: number) => {
|
|
39
|
+
ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
|
|
40
|
+
ctx.fillRect(x, y, width / 2, height / 2);
|
|
41
|
+
};
|
|
42
|
+
fill(RED, 0, 0);
|
|
43
|
+
fill(GREEN, width / 2, 0);
|
|
44
|
+
fill(BLUE, 0, height / 2);
|
|
45
|
+
fill(YELLOW, width / 2, height / 2);
|
|
46
|
+
return objectUrl(await toBlob(canvas));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A white PNG with fine black stripes, standing in for printed text. */
|
|
50
|
+
async function stripedImage(width = 80, height = 40) {
|
|
51
|
+
let canvas = document.createElement("canvas");
|
|
52
|
+
canvas.width = width;
|
|
53
|
+
canvas.height = height;
|
|
54
|
+
let ctx = canvas.getContext("2d")!;
|
|
55
|
+
ctx.fillStyle = "white";
|
|
56
|
+
ctx.fillRect(0, 0, width, height);
|
|
57
|
+
ctx.fillStyle = "black";
|
|
58
|
+
for (let x = 0; x < width; x += 2) ctx.fillRect(x, 0, 1, height);
|
|
59
|
+
return objectUrl(await toBlob(canvas));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function objectUrl(blob: Blob) {
|
|
63
|
+
let url = URL.createObjectURL(blob);
|
|
64
|
+
urls.push(url);
|
|
65
|
+
return url;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function read(blob: Blob) {
|
|
69
|
+
let bitmap = await createImageBitmap(blob);
|
|
70
|
+
let canvas = document.createElement("canvas");
|
|
71
|
+
canvas.width = bitmap.width;
|
|
72
|
+
canvas.height = bitmap.height;
|
|
73
|
+
let ctx = canvas.getContext("2d")!;
|
|
74
|
+
ctx.drawImage(bitmap, 0, 0);
|
|
75
|
+
let data = ctx.getImageData(0, 0, bitmap.width, bitmap.height).data;
|
|
76
|
+
return {
|
|
77
|
+
width: bitmap.width,
|
|
78
|
+
height: bitmap.height,
|
|
79
|
+
pixel(x: number, y: number): Rgb {
|
|
80
|
+
let i = (y * bitmap.width + x) * 4;
|
|
81
|
+
return [data[i], data[i + 1], data[i + 2]];
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Colour match with room for JPEG compression. */
|
|
87
|
+
function expectColour(actual: Rgb, expected: Rgb, tolerance = 30) {
|
|
88
|
+
let off = Math.max(...actual.map((v, i) => Math.abs(v - expected[i])));
|
|
89
|
+
expect(off, `rgb(${actual.join(", ")}) should be close to rgb(${expected.join(", ")})`).toBeLessThanOrEqual(
|
|
90
|
+
tolerance,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type Image = Awaited<ReturnType<typeof read>>;
|
|
95
|
+
|
|
96
|
+
/** Samples the centre of each quadrant: top-left, top-right, bottom-right, bottom-left. */
|
|
97
|
+
function quadrants(image: Image): Rgb[] {
|
|
98
|
+
let { width: w, height: h } = image;
|
|
99
|
+
return [
|
|
100
|
+
image.pixel(Math.floor(w / 4), Math.floor(h / 4)),
|
|
101
|
+
image.pixel(Math.floor((3 * w) / 4), Math.floor(h / 4)),
|
|
102
|
+
image.pixel(Math.floor((3 * w) / 4), Math.floor((3 * h) / 4)),
|
|
103
|
+
image.pixel(Math.floor(w / 4), Math.floor((3 * h) / 4)),
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
describe("rotate", () => {
|
|
108
|
+
it("turns the image a quarter clockwise", async () => {
|
|
109
|
+
let image = await read(await rotateCwPlugin.execute(await quadrantImage(80, 40)));
|
|
110
|
+
|
|
111
|
+
expect([image.width, image.height]).toEqual([40, 80]);
|
|
112
|
+
let [tl, tr, br, bl] = quadrants(image);
|
|
113
|
+
expectColour(tl, BLUE);
|
|
114
|
+
expectColour(tr, RED);
|
|
115
|
+
expectColour(br, GREEN);
|
|
116
|
+
expectColour(bl, YELLOW);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("turns the image a quarter anticlockwise", async () => {
|
|
120
|
+
let image = await read(await rotateCcwPlugin.execute(await quadrantImage(80, 40)));
|
|
121
|
+
|
|
122
|
+
expect([image.width, image.height]).toEqual([40, 80]);
|
|
123
|
+
let [tl, tr, br, bl] = quadrants(image);
|
|
124
|
+
expectColour(tl, GREEN);
|
|
125
|
+
expectColour(tr, YELLOW);
|
|
126
|
+
expectColour(br, BLUE);
|
|
127
|
+
expectColour(bl, RED);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("saves the result as JPEG", async () => {
|
|
131
|
+
let blob = await rotateCwPlugin.execute(await quadrantImage());
|
|
132
|
+
|
|
133
|
+
expect(blob.type).toBe("image/jpeg");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("rejects when the image cannot be loaded", async () => {
|
|
137
|
+
await expect(rotateCwPlugin.execute("blob:http://localhost/missing")).rejects.toThrow("Failed to load image");
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("crop", () => {
|
|
142
|
+
it("cuts out a rectangle at full resolution, losslessly", async () => {
|
|
143
|
+
let source = await quadrantImage(80, 40);
|
|
144
|
+
let corners = [
|
|
145
|
+
{ x: 0.5, y: 0 },
|
|
146
|
+
{ x: 1, y: 0 },
|
|
147
|
+
{ x: 1, y: 0.5 },
|
|
148
|
+
{ x: 0.5, y: 0.5 },
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
let blob = await replayCrop(source, { corners, mode: "rect" });
|
|
152
|
+
let image = await read(blob);
|
|
153
|
+
|
|
154
|
+
expect(blob.type).toBe("image/png");
|
|
155
|
+
expect([image.width, image.height]).toEqual([40, 20]);
|
|
156
|
+
// PNG: every pixel is exactly the green quadrant, edges included.
|
|
157
|
+
for (let [x, y] of [
|
|
158
|
+
[0, 0],
|
|
159
|
+
[39, 0],
|
|
160
|
+
[39, 19],
|
|
161
|
+
[0, 19],
|
|
162
|
+
[20, 10],
|
|
163
|
+
]) {
|
|
164
|
+
expectColour(image.pixel(x, y), GREEN, 0);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("crops across quadrants at the exact pixel boundary", async () => {
|
|
169
|
+
let source = await quadrantImage(80, 40);
|
|
170
|
+
let corners = [
|
|
171
|
+
{ x: 0.25, y: 0.25 },
|
|
172
|
+
{ x: 0.75, y: 0.25 },
|
|
173
|
+
{ x: 0.75, y: 0.75 },
|
|
174
|
+
{ x: 0.25, y: 0.75 },
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
let image = await read(await replayCrop(source, { corners, mode: "rect" }));
|
|
178
|
+
|
|
179
|
+
expect([image.width, image.height]).toEqual([40, 20]);
|
|
180
|
+
expectColour(image.pixel(19, 9), RED, 0);
|
|
181
|
+
expectColour(image.pixel(20, 9), GREEN, 0);
|
|
182
|
+
expectColour(image.pixel(19, 10), BLUE, 0);
|
|
183
|
+
expectColour(image.pixel(20, 10), YELLOW, 0);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("crops a rectangle saved before crops recorded their mode, without OpenCV", async () => {
|
|
187
|
+
let source = await quadrantImage(80, 40);
|
|
188
|
+
let corners = [
|
|
189
|
+
{ x: 0, y: 0.5 },
|
|
190
|
+
{ x: 0.5, y: 0.5 },
|
|
191
|
+
{ x: 0.5, y: 1 },
|
|
192
|
+
{ x: 0, y: 1 },
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
let image = await read(await replayCrop(source, { corners }));
|
|
196
|
+
|
|
197
|
+
expect([image.width, image.height]).toEqual([40, 20]);
|
|
198
|
+
expectColour(image.pixel(20, 10), BLUE, 0);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("falls back to the selection's bounding box when OpenCV is unavailable for a skewed selection", async () => {
|
|
202
|
+
let source = await quadrantImage(80, 40);
|
|
203
|
+
// A slightly skewed quad around the green quadrant.
|
|
204
|
+
let corners = [
|
|
205
|
+
{ x: 0.55, y: 0 },
|
|
206
|
+
{ x: 1, y: 0.05 },
|
|
207
|
+
{ x: 0.95, y: 0.5 },
|
|
208
|
+
{ x: 0.5, y: 0.45 },
|
|
209
|
+
];
|
|
210
|
+
|
|
211
|
+
let image = await read(await replayCrop(source, { corners, mode: "perspective" }));
|
|
212
|
+
|
|
213
|
+
expect([image.width, image.height]).toEqual([40, 20]);
|
|
214
|
+
expectColour(image.pixel(20, 10), GREEN, 0);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe("redact", () => {
|
|
219
|
+
it("covers the region and leaves the rest of the image alone", async () => {
|
|
220
|
+
let source = await quadrantImage(80, 40);
|
|
221
|
+
|
|
222
|
+
let image = await read(await replayRedactBlur(source, { x: 0, y: 0, w: 40, h: 20 }));
|
|
223
|
+
|
|
224
|
+
expect([image.width, image.height]).toEqual([80, 40]);
|
|
225
|
+
let [tl, tr, br, bl] = quadrants(image);
|
|
226
|
+
// 85 % grey over the pixelated red: nowhere near the original colour.
|
|
227
|
+
expectColour(tl, [146, 113, 113]);
|
|
228
|
+
expect(tl[0] - tl[1]).toBeLessThan(60);
|
|
229
|
+
expectColour(tr, GREEN);
|
|
230
|
+
expectColour(br, YELLOW);
|
|
231
|
+
expectColour(bl, BLUE);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("makes fine detail such as text unreadable", async () => {
|
|
235
|
+
let source = await stripedImage(80, 40);
|
|
236
|
+
|
|
237
|
+
let image = await read(await replayRedactBlur(source, { x: 0, y: 0, w: 40, h: 40 }));
|
|
238
|
+
|
|
239
|
+
// Inside the region the 1px stripes are gone: the luminance is nearly flat.
|
|
240
|
+
let inside: number[] = [];
|
|
241
|
+
for (let y = 4; y < 36; y++) for (let x = 4; x < 36; x++) inside.push(image.pixel(x, y)[0]);
|
|
242
|
+
expect(Math.max(...inside) - Math.min(...inside)).toBeLessThan(25);
|
|
243
|
+
|
|
244
|
+
// Outside it they are still there.
|
|
245
|
+
let outside = [image.pixel(60, 20)[0], image.pixel(61, 20)[0]];
|
|
246
|
+
expect(Math.abs(outside[0] - outside[1])).toBeGreaterThan(100);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("rejects when the image cannot be loaded", async () => {
|
|
250
|
+
await expect(replayRedactBlur("blob:http://localhost/missing", { x: 0, y: 0, w: 1, h: 1 })).rejects.toThrow();
|
|
251
|
+
});
|
|
252
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { StrictMode } from "react";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { normRect, useRedactTool } from "./useRedactTool";
|
|
6
|
+
|
|
7
|
+
function setup(options?: { strict?: boolean }) {
|
|
8
|
+
return renderHook(() => useRedactTool("blob:image"), { wrapper: options?.strict ? StrictMode : undefined }).result;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type Tool = ReturnType<typeof setup>;
|
|
12
|
+
|
|
13
|
+
function draw(result: Tool, from: [number, number], to: [number, number]) {
|
|
14
|
+
act(() => result.current.startDrawing(...from));
|
|
15
|
+
act(() => result.current.updateDrawing(...to));
|
|
16
|
+
act(() => result.current.finishDrawing());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("normRect", () => {
|
|
20
|
+
it("turns a drag in any direction into a rect with positive size", () => {
|
|
21
|
+
expect(normRect({ startX: 50, startY: 40, currentX: 10, currentY: 60 })).toEqual({ x: 10, y: 40, w: 40, h: 20 });
|
|
22
|
+
expect(normRect({ startX: 10, startY: 60, currentX: 50, currentY: 40 })).toEqual({ x: 10, y: 40, w: 40, h: 20 });
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("useRedactTool", () => {
|
|
27
|
+
it("shows the rectangle being drawn and keeps it when the pointer is released", () => {
|
|
28
|
+
let result = setup();
|
|
29
|
+
|
|
30
|
+
act(() => result.current.startDrawing(20, 30));
|
|
31
|
+
act(() => result.current.updateDrawing(80, 10));
|
|
32
|
+
expect(result.current.drawing).toEqual({ startX: 20, startY: 30, currentX: 80, currentY: 10 });
|
|
33
|
+
|
|
34
|
+
act(() => result.current.finishDrawing());
|
|
35
|
+
expect(result.current.drawing).toBeNull();
|
|
36
|
+
expect(result.current.rects).toEqual([{ x: 20, y: 10, w: 60, h: 20 }]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("ignores taps and tiny drags", () => {
|
|
40
|
+
let result = setup();
|
|
41
|
+
|
|
42
|
+
draw(result, [10, 10], [10, 10]);
|
|
43
|
+
draw(result, [10, 10], [14, 40]);
|
|
44
|
+
draw(result, [10, 10], [40, 14]);
|
|
45
|
+
|
|
46
|
+
expect(result.current.rects).toEqual([]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("collects several rectangles, and can delete one or clear them all", () => {
|
|
50
|
+
let result = setup();
|
|
51
|
+
|
|
52
|
+
draw(result, [0, 0], [10, 10]);
|
|
53
|
+
draw(result, [20, 20], [40, 40]);
|
|
54
|
+
draw(result, [50, 50], [70, 70]);
|
|
55
|
+
act(() => result.current.deleteRect(1));
|
|
56
|
+
expect(result.current.rects).toEqual([
|
|
57
|
+
{ x: 0, y: 0, w: 10, h: 10 },
|
|
58
|
+
{ x: 50, y: 50, w: 20, h: 20 },
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
act(() => result.current.clearAll());
|
|
62
|
+
expect(result.current.rects).toEqual([]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("does not move a rectangle when the pointer moves without a drag in progress", () => {
|
|
66
|
+
let result = setup();
|
|
67
|
+
|
|
68
|
+
act(() => result.current.updateDrawing(30, 30));
|
|
69
|
+
act(() => result.current.finishDrawing());
|
|
70
|
+
|
|
71
|
+
expect(result.current.drawing).toBeNull();
|
|
72
|
+
expect(result.current.rects).toEqual([]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("adds one rectangle per drag in React strict mode", () => {
|
|
76
|
+
// Apps render in <StrictMode> during development, which calls state updaters twice.
|
|
77
|
+
let result = setup({ strict: true });
|
|
78
|
+
|
|
79
|
+
draw(result, [0, 0], [30, 30]);
|
|
80
|
+
|
|
81
|
+
expect(result.current.rects).toEqual([{ x: 0, y: 0, w: 30, h: 30 }]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("has nothing to apply before a rectangle is drawn", async () => {
|
|
85
|
+
let result = setup();
|
|
86
|
+
|
|
87
|
+
await expect(result.current.applyRedact()).resolves.toBeNull();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -41,6 +41,9 @@ export function useRedactTool(imageUrl: string): RedactToolState {
|
|
|
41
41
|
let [displaySize, setDisplaySize] = useState({ width: 0, height: 0 });
|
|
42
42
|
let imageUrlRef = useRef(imageUrl);
|
|
43
43
|
imageUrlRef.current = imageUrl;
|
|
44
|
+
// Mirrors `drawing` so finishing a drag never reads a stale render closure, and so the
|
|
45
|
+
// new rect is added outside a state updater (React may call updaters more than once).
|
|
46
|
+
let drawingRef = useRef<Drawing | null>(null);
|
|
44
47
|
|
|
45
48
|
let handleImageLoad = useCallback((e: React.SyntheticEvent<HTMLImageElement>) => {
|
|
46
49
|
let img = e.currentTarget;
|
|
@@ -48,25 +51,25 @@ export function useRedactTool(imageUrl: string): RedactToolState {
|
|
|
48
51
|
}, []);
|
|
49
52
|
|
|
50
53
|
function startDrawing(x: number, y: number) {
|
|
51
|
-
|
|
54
|
+
drawingRef.current = { startX: x, startY: y, currentX: x, currentY: y };
|
|
55
|
+
setDrawing(drawingRef.current);
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
function updateDrawing(x: number, y: number) {
|
|
55
|
-
|
|
59
|
+
if (!drawingRef.current) return;
|
|
60
|
+
drawingRef.current = { ...drawingRef.current, currentX: x, currentY: y };
|
|
61
|
+
setDrawing(drawingRef.current);
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
function finishDrawing() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
return null;
|
|
69
|
-
});
|
|
65
|
+
let d = drawingRef.current;
|
|
66
|
+
drawingRef.current = null;
|
|
67
|
+
setDrawing(null);
|
|
68
|
+
if (!d) return;
|
|
69
|
+
let rect = normRect(d);
|
|
70
|
+
if (rect.w > 4 && rect.h > 4) {
|
|
71
|
+
setRects((prev) => [...prev, rect]);
|
|
72
|
+
}
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
function deleteRect(index: number) {
|