@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,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
|
+
});
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import type { ActionRegistry, EditAction } from "./types";
|
|
5
|
+
import { useImageHistory } from "./useImageHistory";
|
|
6
|
+
|
|
7
|
+
const SOURCE = "blob:source";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Object URLs are faked as `blob:<n>` so the tests can follow which image is current and
|
|
11
|
+
* which URLs were released. Each fake blob's text is the chain of edits that produced it.
|
|
12
|
+
*/
|
|
13
|
+
let created: string[] = [];
|
|
14
|
+
let revoked: string[] = [];
|
|
15
|
+
let blobsByUrl = new Map<string, string>();
|
|
16
|
+
let blobText = new WeakMap<Blob, string>();
|
|
17
|
+
|
|
18
|
+
function blob(text: string) {
|
|
19
|
+
let b = new Blob([text], { type: "text/plain" });
|
|
20
|
+
blobText.set(b, text);
|
|
21
|
+
return b;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
created = [];
|
|
26
|
+
revoked = [];
|
|
27
|
+
blobsByUrl = new Map([[SOURCE, "source"]]);
|
|
28
|
+
let counter = 0;
|
|
29
|
+
vi.spyOn(URL, "createObjectURL").mockImplementation((b) => {
|
|
30
|
+
let url = `blob:${++counter}`;
|
|
31
|
+
created.push(url);
|
|
32
|
+
blobsByUrl.set(url, blobText.get(b as Blob) ?? "?");
|
|
33
|
+
return url;
|
|
34
|
+
});
|
|
35
|
+
vi.spyOn(URL, "revokeObjectURL").mockImplementation((url) => {
|
|
36
|
+
revoked.push(url);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
vi.restoreAllMocks();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** A replay handler that appends its action type to the text of the image it is given. */
|
|
45
|
+
function appendingRegistry(...types: string[]) {
|
|
46
|
+
let calls: { url: string; type: string; params: Record<string, unknown> }[] = [];
|
|
47
|
+
let registry: ActionRegistry = new Map();
|
|
48
|
+
for (let type of types) {
|
|
49
|
+
registry.set(type, async (url, params) => {
|
|
50
|
+
calls.push({ url, type, params });
|
|
51
|
+
return blob(`${blobsByUrl.get(url) ?? url}+${type}`);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return { registry, calls };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function action(type: string, params: Record<string, unknown> = {}): EditAction[] {
|
|
58
|
+
return [{ type, params }];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe("useImageHistory", () => {
|
|
62
|
+
it("starts on the source image with nothing to undo or redo", () => {
|
|
63
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
64
|
+
|
|
65
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
66
|
+
expect(result.current.currentBlob).toBeNull();
|
|
67
|
+
expect(result.current.canUndo).toBe(false);
|
|
68
|
+
expect(result.current.canRedo).toBe(false);
|
|
69
|
+
expect(result.current.replaying).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("makes an applied image current at once, before React re-renders", () => {
|
|
73
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
74
|
+
let rotated = blob("rotated");
|
|
75
|
+
|
|
76
|
+
let url = "";
|
|
77
|
+
act(() => {
|
|
78
|
+
url = result.current.applyActions(rotated, action("rotate-cw"));
|
|
79
|
+
// The imperative getters are what ImageEditorRef.getCurrentBlob() reads.
|
|
80
|
+
expect(result.current.getCurrentImageUrl()).toBe(url);
|
|
81
|
+
expect(result.current.getCurrentBlob()).toBe(rotated);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(result.current.currentImageUrl).toBe(url);
|
|
85
|
+
expect(result.current.currentBlob).toBe(rotated);
|
|
86
|
+
expect(result.current.canUndo).toBe(true);
|
|
87
|
+
expect(result.current.canRedo).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("releases the previous edited image when a new one is applied, but never the source", () => {
|
|
91
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
92
|
+
|
|
93
|
+
let first = "";
|
|
94
|
+
act(() => {
|
|
95
|
+
first = result.current.applyActions(blob("a"), action("a"));
|
|
96
|
+
});
|
|
97
|
+
expect(revoked).toEqual([]);
|
|
98
|
+
|
|
99
|
+
act(() => {
|
|
100
|
+
result.current.applyActions(blob("b"), action("b"));
|
|
101
|
+
});
|
|
102
|
+
expect(revoked).toEqual([first]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("undoes by replaying the remaining edits from the source image", async () => {
|
|
106
|
+
let { registry, calls } = appendingRegistry("a", "b");
|
|
107
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
108
|
+
|
|
109
|
+
act(() => {
|
|
110
|
+
result.current.applyActions(blob("source+a"), action("a", { n: 1 }));
|
|
111
|
+
});
|
|
112
|
+
act(() => {
|
|
113
|
+
result.current.applyActions(blob("source+a+b"), action("b"));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
act(() => result.current.undo());
|
|
117
|
+
expect(result.current.replaying).toBe(true);
|
|
118
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
119
|
+
|
|
120
|
+
// Only "a" is replayed, from the original source and with its recorded params.
|
|
121
|
+
expect(calls).toEqual([{ url: SOURCE, type: "a", params: { n: 1 } }]);
|
|
122
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a");
|
|
123
|
+
expect(result.current.canUndo).toBe(true);
|
|
124
|
+
expect(result.current.canRedo).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("goes back to the untouched source when the last edit is undone", async () => {
|
|
128
|
+
let { registry, calls } = appendingRegistry("a");
|
|
129
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
130
|
+
|
|
131
|
+
act(() => {
|
|
132
|
+
result.current.applyActions(blob("source+a"), action("a"));
|
|
133
|
+
});
|
|
134
|
+
act(() => result.current.undo());
|
|
135
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
136
|
+
|
|
137
|
+
expect(calls).toEqual([]);
|
|
138
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
139
|
+
expect(result.current.currentBlob).toBeNull();
|
|
140
|
+
expect(result.current.canUndo).toBe(false);
|
|
141
|
+
expect(result.current.canRedo).toBe(true);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("redoes an undone edit by replaying it on top of the others", async () => {
|
|
145
|
+
let { registry, calls } = appendingRegistry("a", "b");
|
|
146
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
147
|
+
|
|
148
|
+
act(() => {
|
|
149
|
+
result.current.applyActions(blob("source+a"), action("a"));
|
|
150
|
+
});
|
|
151
|
+
act(() => {
|
|
152
|
+
result.current.applyActions(blob("source+a+b"), action("b"));
|
|
153
|
+
});
|
|
154
|
+
act(() => result.current.undo());
|
|
155
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
156
|
+
calls.length = 0;
|
|
157
|
+
|
|
158
|
+
act(() => result.current.redo());
|
|
159
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
160
|
+
|
|
161
|
+
expect(calls.map((c) => c.type)).toEqual(["a", "b"]);
|
|
162
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a+b");
|
|
163
|
+
expect(result.current.canRedo).toBe(false);
|
|
164
|
+
expect(result.current.canUndo).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("replays every action of a multi-action edit (several redactions in one apply)", async () => {
|
|
168
|
+
let { registry, calls } = appendingRegistry("a", "r");
|
|
169
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
170
|
+
|
|
171
|
+
act(() => {
|
|
172
|
+
result.current.applyActions(blob("x"), [
|
|
173
|
+
{ type: "r", params: { x: 1 } },
|
|
174
|
+
{ type: "r", params: { x: 2 } },
|
|
175
|
+
]);
|
|
176
|
+
});
|
|
177
|
+
act(() => {
|
|
178
|
+
result.current.applyActions(blob("y"), action("a"));
|
|
179
|
+
});
|
|
180
|
+
act(() => result.current.undo());
|
|
181
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
182
|
+
|
|
183
|
+
expect(calls.map((c) => c.params)).toEqual([{ x: 1 }, { x: 2 }]);
|
|
184
|
+
expect(await result.current.currentBlob?.text()).toBe("source+r+r");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("releases the intermediate images it creates while replaying", async () => {
|
|
188
|
+
let { registry } = appendingRegistry("a", "b", "c");
|
|
189
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
190
|
+
|
|
191
|
+
for (let type of ["a", "b", "c"]) {
|
|
192
|
+
act(() => {
|
|
193
|
+
result.current.applyActions(blob(type), action(type));
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
revoked = [];
|
|
197
|
+
let before = created.length;
|
|
198
|
+
|
|
199
|
+
act(() => result.current.undo());
|
|
200
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
201
|
+
|
|
202
|
+
// Replaying a then b creates two URLs; the one for "a" is an intermediate.
|
|
203
|
+
let replayUrls = created.slice(before);
|
|
204
|
+
expect(replayUrls).toHaveLength(2);
|
|
205
|
+
expect(revoked).toContain(replayUrls[0]);
|
|
206
|
+
expect(revoked).not.toContain(replayUrls[1]);
|
|
207
|
+
expect(result.current.currentImageUrl).toBe(replayUrls[1]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("drops the redo stack when a new edit is applied", async () => {
|
|
211
|
+
let { registry } = appendingRegistry("a");
|
|
212
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
213
|
+
|
|
214
|
+
act(() => {
|
|
215
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
216
|
+
});
|
|
217
|
+
act(() => result.current.undo());
|
|
218
|
+
await waitFor(() => expect(result.current.canRedo).toBe(true));
|
|
219
|
+
|
|
220
|
+
act(() => {
|
|
221
|
+
result.current.applyActions(blob("b"), action("b"));
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
expect(result.current.canRedo).toBe(false);
|
|
225
|
+
expect(result.current.canUndo).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("ignores undo and redo when there is nothing to undo or redo", () => {
|
|
229
|
+
let { registry, calls } = appendingRegistry("a");
|
|
230
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
231
|
+
|
|
232
|
+
act(() => result.current.undo());
|
|
233
|
+
act(() => result.current.redo());
|
|
234
|
+
|
|
235
|
+
expect(result.current.replaying).toBe(false);
|
|
236
|
+
expect(calls).toEqual([]);
|
|
237
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("ignores a second undo while the first is still replaying", async () => {
|
|
241
|
+
let release: () => void = () => {};
|
|
242
|
+
let registry: ActionRegistry = new Map([
|
|
243
|
+
[
|
|
244
|
+
"a",
|
|
245
|
+
() =>
|
|
246
|
+
new Promise<Blob>((resolve) => {
|
|
247
|
+
release = () => resolve(blob("source+a"));
|
|
248
|
+
}),
|
|
249
|
+
],
|
|
250
|
+
]);
|
|
251
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
252
|
+
|
|
253
|
+
act(() => {
|
|
254
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
255
|
+
});
|
|
256
|
+
act(() => {
|
|
257
|
+
result.current.applyActions(blob("ab"), action("a"));
|
|
258
|
+
});
|
|
259
|
+
act(() => result.current.undo());
|
|
260
|
+
act(() => result.current.undo());
|
|
261
|
+
|
|
262
|
+
await act(async () => release());
|
|
263
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
264
|
+
|
|
265
|
+
// One undo: one edit left to undo, one to redo.
|
|
266
|
+
expect(result.current.canUndo).toBe(true);
|
|
267
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a");
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("lets a new edit win over an undo that is still replaying", async () => {
|
|
271
|
+
let release: () => void = () => {};
|
|
272
|
+
let registry: ActionRegistry = new Map([
|
|
273
|
+
[
|
|
274
|
+
"a",
|
|
275
|
+
() =>
|
|
276
|
+
new Promise<Blob>((resolve) => {
|
|
277
|
+
release = () => resolve(blob("stale"));
|
|
278
|
+
}),
|
|
279
|
+
],
|
|
280
|
+
]);
|
|
281
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
282
|
+
|
|
283
|
+
act(() => {
|
|
284
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
285
|
+
});
|
|
286
|
+
act(() => {
|
|
287
|
+
result.current.applyActions(blob("aa"), action("a"));
|
|
288
|
+
});
|
|
289
|
+
act(() => result.current.undo());
|
|
290
|
+
|
|
291
|
+
let fresh = blob("fresh");
|
|
292
|
+
act(() => {
|
|
293
|
+
result.current.applyActions(fresh, action("b"));
|
|
294
|
+
});
|
|
295
|
+
expect(result.current.replaying).toBe(false);
|
|
296
|
+
|
|
297
|
+
await act(async () => release());
|
|
298
|
+
|
|
299
|
+
expect(result.current.currentBlob).toBe(fresh);
|
|
300
|
+
expect(result.current.canRedo).toBe(false);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("skips actions it has no replay handler for", async () => {
|
|
304
|
+
let warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
305
|
+
let { registry, calls } = appendingRegistry("a");
|
|
306
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
307
|
+
|
|
308
|
+
act(() => {
|
|
309
|
+
result.current.applyActions(blob("x"), action("unknown"));
|
|
310
|
+
});
|
|
311
|
+
act(() => {
|
|
312
|
+
result.current.applyActions(blob("y"), action("a"));
|
|
313
|
+
});
|
|
314
|
+
act(() => {
|
|
315
|
+
result.current.applyActions(blob("z"), action("a"));
|
|
316
|
+
});
|
|
317
|
+
act(() => result.current.undo());
|
|
318
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
319
|
+
|
|
320
|
+
expect(calls.map((c) => c.type)).toEqual(["a"]);
|
|
321
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a");
|
|
322
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('"unknown"'));
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("stays on the current image when an edit cannot be replayed", async () => {
|
|
326
|
+
let warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
327
|
+
let registry: ActionRegistry = new Map([["a", () => Promise.reject(new Error("Failed to load image"))]]);
|
|
328
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
329
|
+
|
|
330
|
+
act(() => {
|
|
331
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
332
|
+
});
|
|
333
|
+
let current = "";
|
|
334
|
+
act(() => {
|
|
335
|
+
current = result.current.applyActions(blob("b"), action("b"));
|
|
336
|
+
});
|
|
337
|
+
act(() => result.current.undo());
|
|
338
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
339
|
+
|
|
340
|
+
// The undo did not happen, so the user can try again; the failure is logged, not thrown.
|
|
341
|
+
expect(result.current.currentImageUrl).toBe(current);
|
|
342
|
+
expect(result.current.canUndo).toBe(true);
|
|
343
|
+
expect(result.current.canRedo).toBe(false);
|
|
344
|
+
expect(warn).toHaveBeenCalled();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("releases the images of a replay that failed half-way", async () => {
|
|
348
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
349
|
+
let { registry } = appendingRegistry("a");
|
|
350
|
+
registry.set("b", () => Promise.reject(new Error("Failed to load image")));
|
|
351
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
352
|
+
|
|
353
|
+
for (let type of ["a", "b", "c"]) {
|
|
354
|
+
act(() => {
|
|
355
|
+
result.current.applyActions(blob(type), action(type));
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
let before = created.length;
|
|
359
|
+
act(() => result.current.undo());
|
|
360
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
361
|
+
|
|
362
|
+
let replayUrls = created.slice(before);
|
|
363
|
+
expect(replayUrls).toHaveLength(1);
|
|
364
|
+
expect(revoked).toContain(replayUrls[0]);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("reset returns to the source and clears the history", () => {
|
|
368
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
369
|
+
|
|
370
|
+
let url = "";
|
|
371
|
+
act(() => {
|
|
372
|
+
url = result.current.applyActions(blob("a"), action("a"));
|
|
373
|
+
});
|
|
374
|
+
act(() => result.current.reset());
|
|
375
|
+
|
|
376
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
377
|
+
expect(result.current.getCurrentBlob()).toBeNull();
|
|
378
|
+
expect(result.current.canUndo).toBe(false);
|
|
379
|
+
expect(result.current.canRedo).toBe(false);
|
|
380
|
+
expect(revoked).toContain(url);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("starts over when the app passes a new source image", () => {
|
|
384
|
+
let { result, rerender } = renderHook(({ src }) => useImageHistory(src, new Map()), {
|
|
385
|
+
initialProps: { src: SOURCE },
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
act(() => {
|
|
389
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
390
|
+
});
|
|
391
|
+
rerender({ src: "blob:saved" });
|
|
392
|
+
|
|
393
|
+
expect(result.current.currentImageUrl).toBe("blob:saved");
|
|
394
|
+
expect(result.current.currentBlob).toBeNull();
|
|
395
|
+
expect(result.current.canUndo).toBe(false);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("releases its edited image on unmount", () => {
|
|
399
|
+
let { result, unmount } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
400
|
+
|
|
401
|
+
let url = "";
|
|
402
|
+
act(() => {
|
|
403
|
+
url = result.current.applyActions(blob("a"), action("a"));
|
|
404
|
+
});
|
|
405
|
+
unmount();
|
|
406
|
+
|
|
407
|
+
expect(revoked).toContain(url);
|
|
408
|
+
});
|
|
409
|
+
});
|
|
@@ -105,6 +105,9 @@ export function useImageHistory(sourceUrl: string, registry: ActionRegistry): Us
|
|
|
105
105
|
replaceCurrent({ url: sourceUrl, blob: null, generated: false });
|
|
106
106
|
}
|
|
107
107
|
setHistory({ past: newPast, future: newFuture });
|
|
108
|
+
} catch (err) {
|
|
109
|
+
// Leave image and history as they were, so the user can simply try again.
|
|
110
|
+
console.warn("[ImageHistory] Could not replay the edit history:", err);
|
|
108
111
|
} finally {
|
|
109
112
|
if (!abort.signal.aborted) {
|
|
110
113
|
isReplaying.current = false;
|
|
@@ -178,7 +181,13 @@ async function replayAll(
|
|
|
178
181
|
continue;
|
|
179
182
|
}
|
|
180
183
|
|
|
181
|
-
let blob
|
|
184
|
+
let blob: Blob;
|
|
185
|
+
try {
|
|
186
|
+
blob = await apply(prevUrl, action.params);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
if (prevGenerated) URL.revokeObjectURL(prevUrl);
|
|
189
|
+
throw err;
|
|
190
|
+
}
|
|
182
191
|
|
|
183
192
|
if (signal.aborted) {
|
|
184
193
|
if (prevGenerated) URL.revokeObjectURL(prevUrl);
|