@olenbetong/synergi-react 5.0.0 → 5.2.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.css +1 -1
- package/es/components/Checkbox/index.js +1 -9
- package/es/components/CodeEditor/CodeEditor.css +6 -6
- package/es/components/ColorCard/index.css +17 -28
- package/es/components/DateNavigator/index.css +5 -7
- package/es/components/FilePond.js +3 -12
- package/es/components/ImageEditor/ImageEditor.css +10 -10
- package/es/components/ImageEditor/ImageEditor.js +4 -4
- package/es/components/ImageEditor/ResponsiveButton.js +2 -2
- package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
- package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
- package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
- package/es/components/ImageEditor/useImageHistory.js +13 -1
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +3 -3
- package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
- package/es/components/PageBanner/index.css +1 -1
- package/es/components/PdfViewer/PdfViewer.js +1 -1
- package/es/components/Portal/index.js +20 -20
- package/es/components/Spinner/index.css +1 -1
- package/es/components/SplitContainer/index.css +1 -1
- 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/components/ValueToggleGroup/index.css +2 -2
- package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
- 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/index.css +1 -1
- package/es/useTranslation/index.js +5 -3
- package/package.json +16 -4
- 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.scss +2 -2
- 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/ColorCard/index.css +17 -28
- package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
- package/src/components/DateNavigator/index.css +5 -7
- 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.css +10 -10
- package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
- package/src/components/ImageEditor/ImageEditor.tsx +348 -348
- package/src/components/ImageEditor/ResponsiveButton.tsx +2 -2
- 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 +42 -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 +4 -3
- package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
- package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
- package/src/components/PageBanner/index.scss +10 -10
- 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/Spinner/index.css +1 -1
- package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
- package/src/components/SplitContainer/index.scss +5 -1
- 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/components/ValueToggleGroup/index.css +2 -2
- 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/BarcodeScanner.css +7 -7
- 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 +194 -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,154 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { MAX_ZOOM, MIN_ZOOM, useZoomPan } from "./useZoomPan";
|
|
5
|
+
|
|
6
|
+
function setup() {
|
|
7
|
+
return renderHook(() => useZoomPan()).result;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Where a point of the image (in unzoomed px from the container centre) lands on screen:
|
|
12
|
+
* the inner element is translated by pan, then scaled about the centre.
|
|
13
|
+
*/
|
|
14
|
+
function screenPos(state: { zoom: number; panX: number; panY: number }, x: number, y: number) {
|
|
15
|
+
return { x: state.panX + x * state.zoom, y: state.panY + y * state.zoom };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("useZoomPan", () => {
|
|
19
|
+
it("starts unzoomed and centred", () => {
|
|
20
|
+
let result = setup();
|
|
21
|
+
|
|
22
|
+
expect(result.current).toMatchObject({ zoom: 1, panX: 0, panY: 0, isZoomed: false });
|
|
23
|
+
expect(result.current.canZoomIn).toBe(true);
|
|
24
|
+
expect(result.current.canZoomOut).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("zooms in and out in 25 % steps", () => {
|
|
28
|
+
let result = setup();
|
|
29
|
+
|
|
30
|
+
act(() => result.current.zoomIn());
|
|
31
|
+
expect(result.current.zoom).toBe(1.25);
|
|
32
|
+
expect(result.current.isZoomed).toBe(true);
|
|
33
|
+
|
|
34
|
+
act(() => result.current.zoomOut());
|
|
35
|
+
act(() => result.current.zoomOut());
|
|
36
|
+
expect(result.current.zoom).toBe(0.75);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("stops at the zoom limits and reports it so the buttons disable", () => {
|
|
40
|
+
let result = setup();
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < 30; i++) act(() => result.current.zoomIn());
|
|
43
|
+
expect(result.current.zoom).toBe(MAX_ZOOM);
|
|
44
|
+
expect(result.current.canZoomIn).toBe(false);
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < 30; i++) act(() => result.current.zoomOut());
|
|
47
|
+
expect(result.current.zoom).toBe(MIN_ZOOM);
|
|
48
|
+
expect(result.current.canZoomOut).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("does not drift from repeated steps (no 1.2500000001 zoom levels)", () => {
|
|
52
|
+
let result = setup();
|
|
53
|
+
|
|
54
|
+
for (let i = 0; i < 7; i++) act(() => result.current.zoomIn());
|
|
55
|
+
for (let i = 0; i < 7; i++) act(() => result.current.zoomOut());
|
|
56
|
+
|
|
57
|
+
expect(result.current.zoom).toBe(1);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("button zoom keeps the point at the centre of the view in place", () => {
|
|
61
|
+
let result = setup();
|
|
62
|
+
act(() => result.current.pan(40, -20));
|
|
63
|
+
|
|
64
|
+
act(() => result.current.zoomIn());
|
|
65
|
+
|
|
66
|
+
// The pan scales with the zoom, so the image point under the centre stays there.
|
|
67
|
+
expect(result.current.panX).toBeCloseTo(50);
|
|
68
|
+
expect(result.current.panY).toBeCloseTo(-25);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("pans by screen pixels", () => {
|
|
72
|
+
let result = setup();
|
|
73
|
+
|
|
74
|
+
act(() => result.current.pan(10, 5));
|
|
75
|
+
act(() => result.current.pan(-3, 2));
|
|
76
|
+
|
|
77
|
+
expect(result.current).toMatchObject({ panX: 7, panY: 7, zoom: 1, isZoomed: true });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("reset returns to the unzoomed, centred view", () => {
|
|
81
|
+
let result = setup();
|
|
82
|
+
act(() => result.current.zoomIn());
|
|
83
|
+
act(() => result.current.pan(30, 30));
|
|
84
|
+
|
|
85
|
+
act(() => result.current.resetZoom());
|
|
86
|
+
|
|
87
|
+
expect(result.current).toMatchObject({ zoom: 1, panX: 0, panY: 0, isZoomed: false });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("ctrl+scroll zoom keeps the point under the cursor still", () => {
|
|
91
|
+
let result = setup();
|
|
92
|
+
act(() => result.current.pan(15, -10));
|
|
93
|
+
let focal = { x: 120, y: -80 };
|
|
94
|
+
// The image point currently under the cursor, in unzoomed coordinates.
|
|
95
|
+
let before = result.current;
|
|
96
|
+
let imgX = (focal.x - before.panX) / before.zoom;
|
|
97
|
+
let imgY = (focal.y - before.panY) / before.zoom;
|
|
98
|
+
|
|
99
|
+
act(() => result.current.zoomByDeltaAtPoint(0.25, focal.x, focal.y));
|
|
100
|
+
|
|
101
|
+
let after = screenPos(result.current, imgX, imgY);
|
|
102
|
+
expect(result.current.zoom).toBe(1.25);
|
|
103
|
+
expect(after.x).toBeCloseTo(focal.x);
|
|
104
|
+
expect(after.y).toBeCloseTo(focal.y);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("pinch zoom keeps the point between the fingers still", () => {
|
|
108
|
+
let result = setup();
|
|
109
|
+
let focal = { x: -60, y: 45 };
|
|
110
|
+
|
|
111
|
+
act(() => result.current.zoomByScaleAtPoint(2, focal.x, focal.y));
|
|
112
|
+
|
|
113
|
+
let after = screenPos(result.current, focal.x, focal.y);
|
|
114
|
+
expect(result.current.zoom).toBe(2);
|
|
115
|
+
expect(after.x).toBeCloseTo(focal.x);
|
|
116
|
+
expect(after.y).toBeCloseTo(focal.y);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("clamps pinch zoom and anchors on the clamped zoom, not the requested one", () => {
|
|
120
|
+
let result = setup();
|
|
121
|
+
let focal = { x: 100, y: 100 };
|
|
122
|
+
|
|
123
|
+
act(() => result.current.zoomByScaleAtPoint(50, focal.x, focal.y));
|
|
124
|
+
|
|
125
|
+
let after = screenPos(result.current, focal.x, focal.y);
|
|
126
|
+
expect(result.current.zoom).toBe(MAX_ZOOM);
|
|
127
|
+
expect(after.x).toBeCloseTo(focal.x);
|
|
128
|
+
expect(after.y).toBeCloseTo(focal.y);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("a two-finger drag moves and zooms in one update", () => {
|
|
132
|
+
let result = setup();
|
|
133
|
+
let focal = { x: 20, y: 30 };
|
|
134
|
+
|
|
135
|
+
act(() => result.current.panAndZoomAtPoint(10, -5, 1.5, focal.x, focal.y));
|
|
136
|
+
|
|
137
|
+
// Equivalent to panning first and then pinching around the (new) midpoint.
|
|
138
|
+
let reference = setup();
|
|
139
|
+
act(() => reference.current.pan(10, -5));
|
|
140
|
+
act(() => reference.current.zoomByScaleAtPoint(1.5, focal.x, focal.y));
|
|
141
|
+
|
|
142
|
+
expect(result.current.zoom).toBe(1.5);
|
|
143
|
+
expect(result.current.panX).toBeCloseTo(reference.current.panX);
|
|
144
|
+
expect(result.current.panY).toBeCloseTo(reference.current.panY);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("a pure two-finger drag (no pinch) only pans", () => {
|
|
148
|
+
let result = setup();
|
|
149
|
+
|
|
150
|
+
act(() => result.current.panAndZoomAtPoint(12, 8, 1, 50, 50));
|
|
151
|
+
|
|
152
|
+
expect(result.current).toMatchObject({ zoom: 1, panX: 12, panY: 8 });
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
2
|
-
|
|
3
|
-
import type { Plugin, ToolPlugin } from "./types.js";
|
|
4
|
-
|
|
5
|
-
interface UseActiveToolResult {
|
|
6
|
-
activePlugin: ToolPlugin | null;
|
|
7
|
-
activateTool: (id: string) => void;
|
|
8
|
-
deactivateTool: () => void;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/** Manages which tool plugin is currently active. `initialTool` names the tool to start in. */
|
|
12
|
-
export function useActiveTool(plugins: Plugin[], initialTool: string | null = null): UseActiveToolResult {
|
|
13
|
-
let [activeId, setActiveId] = useState<string | null>(initialTool);
|
|
14
|
-
let activePlugin = (plugins.find((p) => p.type === "tool" && p.id === activeId) ?? null) as ToolPlugin | null;
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
activePlugin,
|
|
18
|
-
activateTool: setActiveId,
|
|
19
|
-
deactivateTool: () => setActiveId(null),
|
|
20
|
-
};
|
|
21
|
-
}
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
|
|
3
|
+
import type { Plugin, ToolPlugin } from "./types.js";
|
|
4
|
+
|
|
5
|
+
interface UseActiveToolResult {
|
|
6
|
+
activePlugin: ToolPlugin | null;
|
|
7
|
+
activateTool: (id: string) => void;
|
|
8
|
+
deactivateTool: () => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Manages which tool plugin is currently active. `initialTool` names the tool to start in. */
|
|
12
|
+
export function useActiveTool(plugins: Plugin[], initialTool: string | null = null): UseActiveToolResult {
|
|
13
|
+
let [activeId, setActiveId] = useState<string | null>(initialTool);
|
|
14
|
+
let activePlugin = (plugins.find((p) => p.type === "tool" && p.id === activeId) ?? null) as ToolPlugin | null;
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
activePlugin,
|
|
18
|
+
activateTool: setActiveId,
|
|
19
|
+
deactivateTool: () => setActiveId(null),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { fullImageQuad, useEdgeDetection } from "./useEdgeDetection";
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* The detection itself needs the real OpenCV.js (a multi-megabyte download), so only the
|
|
8
|
+
* hook's contract around it is tested here: when it runs and how it falls back.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function canvas(width: number, height: number) {
|
|
12
|
+
return { width, height } as HTMLCanvasElement;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A stand-in for OpenCV whose matrices record whether they were freed. */
|
|
16
|
+
function fakeCv(options: { imread?: () => unknown }) {
|
|
17
|
+
let mats: { deleted: boolean }[] = [];
|
|
18
|
+
class Mat {
|
|
19
|
+
deleted = false;
|
|
20
|
+
constructor() {
|
|
21
|
+
mats.push(this);
|
|
22
|
+
}
|
|
23
|
+
delete() {
|
|
24
|
+
this.deleted = true;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
let cv = {
|
|
28
|
+
Mat,
|
|
29
|
+
Size: class {},
|
|
30
|
+
imread: options.imread ?? (() => new Mat()),
|
|
31
|
+
// The first step of detection; failing here exercises the clean-up path.
|
|
32
|
+
resize: () => {
|
|
33
|
+
throw new Error("not implemented");
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
return { cv, mats };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
vi.restoreAllMocks();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("fullImageQuad", () => {
|
|
48
|
+
it("lists the image corners clockwise from top-left", () => {
|
|
49
|
+
expect(fullImageQuad(80, 40)).toEqual([
|
|
50
|
+
{ x: 0, y: 0 },
|
|
51
|
+
{ x: 80, y: 0 },
|
|
52
|
+
{ x: 80, y: 40 },
|
|
53
|
+
{ x: 0, y: 40 },
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("useEdgeDetection", () => {
|
|
59
|
+
it("suggests nothing until OpenCV and the image are both there", () => {
|
|
60
|
+
let { cv } = fakeCv({});
|
|
61
|
+
let image = canvas(10, 10);
|
|
62
|
+
|
|
63
|
+
expect(renderHook(() => useEdgeDetection(null, image, true)).result.current.corners).toBeNull();
|
|
64
|
+
expect(renderHook(() => useEdgeDetection(cv, null, true)).result.current.corners).toBeNull();
|
|
65
|
+
expect(renderHook(() => useEdgeDetection(cv, image, false)).result.current.corners).toBeNull();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("falls back to the whole image when the image cannot be read", () => {
|
|
69
|
+
let { cv } = fakeCv({
|
|
70
|
+
imread: () => {
|
|
71
|
+
throw new Error("bad canvas");
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Created once: the hook re-runs detection whenever it is handed a new canvas.
|
|
76
|
+
let image = canvas(300, 200);
|
|
77
|
+
|
|
78
|
+
let { result } = renderHook(() => useEdgeDetection(cv, image, true));
|
|
79
|
+
|
|
80
|
+
expect(result.current.corners).toEqual(fullImageQuad(300, 200));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("falls back to the whole image when detection fails, freeing every matrix it made", () => {
|
|
84
|
+
let { cv, mats } = fakeCv({});
|
|
85
|
+
|
|
86
|
+
// Created once: the hook re-runs detection whenever it is handed a new canvas.
|
|
87
|
+
let image = canvas(300, 200);
|
|
88
|
+
|
|
89
|
+
let { result } = renderHook(() => useEdgeDetection(cv, image, true));
|
|
90
|
+
|
|
91
|
+
expect(result.current.corners).toEqual(fullImageQuad(300, 200));
|
|
92
|
+
// The source image plus the detector's working matrices; WASM memory leaks otherwise.
|
|
93
|
+
expect(mats.length).toBeGreaterThan(1);
|
|
94
|
+
expect(mats.every((m) => m.deleted)).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
});
|