@olenbetong/synergi-react 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/es/components/Checkbox/index.js +1 -9
  2. package/es/components/CodeEditor/CodeEditor.css +6 -6
  3. package/es/components/FilePond.js +3 -12
  4. package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
  5. package/es/components/ImageEditor/useImageHistory.js +13 -1
  6. package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +1 -1
  7. package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
  8. package/es/components/Portal/index.js +20 -20
  9. package/es/components/SplitContainer/index.js +3 -1
  10. package/es/components/Toolbar/Toolbar.css +137 -137
  11. package/es/components/Toolbar/index.js +6 -1
  12. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
  13. package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
  14. package/es/elements-ds/DescriptionInput.d.ts +16 -0
  15. package/es/elements-ds/DescriptionInput.js +41 -0
  16. package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
  17. package/es/elements-ds/DuplicateElementDamage.js +57 -0
  18. package/es/elements-ds/ElementLookup.d.ts +23 -0
  19. package/es/elements-ds/ElementLookup.js +35 -0
  20. package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
  21. package/es/elements-ds/ElementLookupDialog.js +84 -0
  22. package/es/elements-ds/FormFields.d.ts +43 -0
  23. package/es/elements-ds/FormFields.js +67 -0
  24. package/es/elements-ds/HIDScanner.d.ts +7 -0
  25. package/es/elements-ds/HIDScanner.js +36 -0
  26. package/es/elements-ds/ManualForm.d.ts +7 -0
  27. package/es/elements-ds/ManualForm.js +70 -0
  28. package/es/elements-ds/ProjectSelect.d.ts +28 -0
  29. package/es/elements-ds/ProjectSelect.js +45 -0
  30. package/es/elements-ds/elements-ds.css +157 -0
  31. package/es/elements-ds/index.d.ts +12 -0
  32. package/es/elements-ds/index.js +14 -0
  33. package/es/useTranslation/index.js +5 -3
  34. package/package.json +15 -3
  35. package/src/chrome/AppShell/AppShell.test.tsx +48 -0
  36. package/src/chrome/AppShell/index.tsx +29 -29
  37. package/src/chrome/openReport.test.ts +50 -0
  38. package/src/chrome/openReport.ts +34 -34
  39. package/src/colorScheme.test.ts +188 -0
  40. package/src/colorScheme.ts +125 -125
  41. package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
  42. package/src/components/Checkbox/index.tsx +1 -10
  43. package/src/components/CodeEditor/CodeEditor.css +6 -6
  44. package/src/components/CodeEditor/CodeEditor.tsx +173 -173
  45. package/src/components/CodeEditor/index.ts +1 -1
  46. package/src/components/ColorCard/ColorCard.test.tsx +34 -0
  47. package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
  48. package/src/components/DateNavigator/index.tsx +186 -186
  49. package/src/components/FilePond.browser.test.tsx +389 -0
  50. package/src/components/FilePond.tsx +303 -312
  51. package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
  52. package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
  53. package/src/components/ImageEditor/ImageEditor.tsx +348 -348
  54. package/src/components/ImageEditor/index.ts +9 -9
  55. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  56. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  57. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
  58. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
  59. package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
  60. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  61. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
  62. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
  63. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  64. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  65. package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
  66. package/src/components/ImageEditor/useActiveTool.ts +21 -21
  67. package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
  68. package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
  69. package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
  70. package/src/components/ImageEditor/useImageHistory.ts +206 -197
  71. package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
  72. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  73. package/src/components/LinkedCardList/index.scss +95 -95
  74. package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
  75. package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +1 -0
  76. package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
  77. package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
  78. package/src/components/PageBanner/index.tsx +80 -80
  79. package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
  80. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  81. package/src/components/Portal/Portal.test.tsx +106 -0
  82. package/src/components/Portal/index.tsx +22 -25
  83. package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
  84. package/src/components/Sidebar/Sidebar.test.tsx +123 -0
  85. package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
  86. package/src/components/SplitContainer/index.tsx +335 -333
  87. package/src/components/Toolbar/Toolbar.css +137 -137
  88. package/src/components/Toolbar/Toolbar.test.tsx +216 -0
  89. package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
  90. package/src/components/Toolbar/index.tsx +270 -265
  91. package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
  92. package/src/designsystemet-web-types.d.ts +24 -24
  93. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  94. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  95. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  96. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  97. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  98. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  99. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  100. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  101. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  102. package/src/elements/ElementLookup.tsx +123 -123
  103. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
  104. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
  105. package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
  106. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  107. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
  108. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
  109. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  110. package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
  111. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  112. package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
  113. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  114. package/src/elements/ProjectSelect.tsx +130 -130
  115. package/src/elements/index.ts +15 -15
  116. package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
  117. package/src/elements-ds/DescriptionInput.tsx +114 -0
  118. package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
  119. package/src/elements-ds/ElementLookup.tsx +114 -0
  120. package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
  121. package/src/elements-ds/ElementLookupDialog.tsx +196 -0
  122. package/src/elements-ds/FormFields.tsx +176 -0
  123. package/src/elements-ds/HIDScanner.tsx +53 -0
  124. package/src/elements-ds/ManualForm.tsx +129 -0
  125. package/src/elements-ds/ProjectSelect.tsx +120 -0
  126. package/src/elements-ds/elements-ds.css +157 -0
  127. package/src/elements-ds/index.ts +16 -0
  128. package/src/filter/index.ts +34 -34
  129. package/src/i18n.test.ts +31 -0
  130. package/src/i18n.ts +13 -13
  131. package/src/mui/MuiThemeProvider.tsx +135 -135
  132. package/src/test/fakeCamera.ts +98 -0
  133. package/src/test/fakeDataApi.ts +90 -0
  134. package/src/test/setup.browser.ts +21 -0
  135. package/src/test/setup.ts +23 -23
  136. package/src/useTranslation/index.ts +5 -3
  137. package/src/useTranslation/useTranslation.test.ts +128 -0
  138. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
  139. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
  140. package/es/test/setup.d.ts +0 -6
  141. package/es/test/setup.js +0 -6
@@ -1,405 +1,405 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
2
-
3
- import type { CropCanvasHandle } from "../../CropCanvas.js";
4
- import type { EditAction } from "../../types.js";
5
- import { fullImageQuad, type Point, type Quad, useEdgeDetection } from "../../useEdgeDetection.js";
6
- import { loadOpenCV, useOpenCV } from "../../useOpenCV.js";
7
-
8
- export interface ApplyCropResult {
9
- blob: Blob;
10
- actions: EditAction[];
11
- }
12
-
13
- /**
14
- * `corners` — drag the four corner/edge handles of the current selection.
15
- * `rect` — drag out a brand new axis-aligned rectangle.
16
- */
17
- export type CropMode = "corners" | "rect";
18
-
19
- /** How a stored crop action must be replayed. Axis-aligned rects need no OpenCV. */
20
- type CropGeometry = "perspective" | "rect";
21
-
22
- interface RectDraft {
23
- startX: number;
24
- startY: number;
25
- currentX: number;
26
- currentY: number;
27
- }
28
-
29
- export interface CropToolState {
30
- activeCorners: Quad;
31
- mode: CropMode;
32
- /** False while the user is in rect mode and has not drawn a rectangle yet. */
33
- hasSelection: boolean;
34
- scale: number;
35
- displaySize: { width: number; height: number };
36
- canvasRef: React.RefObject<CropCanvasHandle | null>;
37
- cvReady: boolean;
38
- cvError: Error | null;
39
- cropping: boolean;
40
- error: string | null;
41
- handleCanvasReady: (canvas: HTMLCanvasElement, w: number, h: number) => void;
42
- updateCorner: (index: number, screenX: number, screenY: number) => void;
43
- /** Enters rect mode, temporarily clearing (and remembering) the current selection. */
44
- startRectMode: () => void;
45
- /** Leaves rect mode without drawing, restoring the selection that was active before. */
46
- cancelRectMode: () => void;
47
- startRectDraw: (screenX: number, screenY: number) => void;
48
- updateRectDraw: (screenX: number, screenY: number) => void;
49
- finishRectDraw: () => void;
50
- applyCrop: () => Promise<ApplyCropResult | null>;
51
- }
52
-
53
- /** Relative (0–1) tolerance for treating a quad as axis-aligned. */
54
- const RECT_TOLERANCE = 0.001;
55
- /** Minimum drag distance in CSS pixels before a drawn rectangle is accepted. */
56
- const MIN_RECT_DRAG = 4;
57
-
58
- /**
59
- * Manages all state and logic for the crop tool.
60
- * OpenCV is loaded lazily for corner suggestion and perspective warping, but is optional:
61
- * when it fails to load the tool stays fully usable, only the suggested border is missing.
62
- */
63
- export function useCropTool(imageUrl: string, enabled: boolean): CropToolState {
64
- let { cv, ready: cvReady, error: cvError } = useOpenCV(enabled);
65
- let [corners, setCorners] = useState<Quad | null>(null);
66
- let [canvasPixelSize, setCanvasPixelSize] = useState({ width: 1, height: 1 });
67
- let [displaySize, setDisplaySize] = useState({ width: 0, height: 0 });
68
- let [canvasEl, setCanvasEl] = useState<HTMLCanvasElement | null>(null);
69
- let [cropping, setCropping] = useState(false);
70
- let [error, setError] = useState<string | null>(null);
71
- let [mode, setMode] = useState<CropMode>("corners");
72
- let [rectDraft, setRectDraft] = useState<RectDraft | null>(null);
73
- let canvasRef = useRef<CropCanvasHandle>(null);
74
- let prevSuggested = useRef<Quad | null>(null);
75
- // Selection stashed when entering rect mode, restored if the user backs out.
76
- let stashedCorners = useRef<Quad | null>(null);
77
- // Mirrors rectDraft so pointer handlers never read a stale render closure.
78
- let rectDraftRef = useRef<RectDraft | null>(null);
79
- // Set once the user has touched the selection, so a late suggestion can't overwrite it.
80
- let userAdjusted = useRef(false);
81
-
82
- let { corners: suggestedCorners } = useEdgeDetection(cvReady ? cv : null, canvasEl, cvReady && enabled);
83
-
84
- useEffect(() => {
85
- if (suggestedCorners && suggestedCorners !== prevSuggested.current) {
86
- prevSuggested.current = suggestedCorners;
87
- if (!userAdjusted.current) setCorners(suggestedCorners);
88
- }
89
- }, [suggestedCorners]);
90
-
91
- // Reset when the source image changes (e.g. after a previous crop or rotate)
92
- useEffect(() => {
93
- prevSuggested.current = null;
94
- stashedCorners.current = null;
95
- rectDraftRef.current = null;
96
- userAdjusted.current = false;
97
- setCorners(null);
98
- setCanvasEl(null);
99
- setRectDraft(null);
100
- setMode("corners");
101
- }, [imageUrl]);
102
-
103
- let draftQuad = rectDraft ? draftToQuad(rectDraft) : null;
104
- let hasSelection = mode === "corners" || draftQuad !== null;
105
- let activeCorners: Quad = draftQuad ?? corners ?? fullImageQuad(canvasPixelSize.width, canvasPixelSize.height);
106
- let scale = displaySize.width > 0 ? displaySize.width / canvasPixelSize.width : 1;
107
-
108
- let handleCanvasReady = useCallback((canvas: HTMLCanvasElement, w: number, h: number) => {
109
- // Detect on a bare copy: the displayed canvas carries the selection border, which
110
- // starts out along the image edges and would be found as the document outline.
111
- setCanvasEl(canvasRef.current?.getCleanCanvas() ?? null);
112
- setCanvasPixelSize({ width: w, height: h });
113
- setDisplaySize({ width: canvas.clientWidth, height: canvas.clientHeight });
114
- }, []);
115
-
116
- /** Converts CSS-pixel overlay coords to canvas-pixel coords, clamped to the image. */
117
- function toCanvasPoint(screenX: number, screenY: number): Point {
118
- return {
119
- x: Math.max(0, Math.min(canvasPixelSize.width, screenX / scale)),
120
- y: Math.max(0, Math.min(canvasPixelSize.height, screenY / scale)),
121
- };
122
- }
123
-
124
- function updateCorner(index: number, screenX: number, screenY: number) {
125
- if (mode !== "corners") return;
126
- let point = toCanvasPoint(screenX, screenY);
127
- userAdjusted.current = true;
128
- setCorners((prev) => {
129
- let next = [...(prev ?? activeCorners)] as Quad;
130
- next[index] = point;
131
- return next;
132
- });
133
- }
134
-
135
- function startRectMode() {
136
- if (mode === "rect") return;
137
- stashedCorners.current = corners;
138
- rectDraftRef.current = null;
139
- userAdjusted.current = true;
140
- setRectDraft(null);
141
- setCorners(null);
142
- setMode("rect");
143
- }
144
-
145
- function cancelRectMode() {
146
- if (mode !== "rect") return;
147
- // Fall back to the suggested border when rect mode was entered before any selection existed.
148
- setCorners(stashedCorners.current ?? prevSuggested.current);
149
- stashedCorners.current = null;
150
- rectDraftRef.current = null;
151
- setRectDraft(null);
152
- setMode("corners");
153
- }
154
-
155
- function startRectDraw(screenX: number, screenY: number) {
156
- if (mode !== "rect") return;
157
- let { x, y } = toCanvasPoint(screenX, screenY);
158
- rectDraftRef.current = { startX: x, startY: y, currentX: x, currentY: y };
159
- setRectDraft(rectDraftRef.current);
160
- }
161
-
162
- function updateRectDraw(screenX: number, screenY: number) {
163
- if (!rectDraftRef.current) return;
164
- let { x, y } = toCanvasPoint(screenX, screenY);
165
- rectDraftRef.current = { ...rectDraftRef.current, currentX: x, currentY: y };
166
- setRectDraft(rectDraftRef.current);
167
- }
168
-
169
- function finishRectDraw() {
170
- let draft = rectDraftRef.current;
171
- rectDraftRef.current = null;
172
- setRectDraft(null);
173
- if (!draft) return;
174
-
175
- let width = Math.abs(draft.currentX - draft.startX) * scale;
176
- let height = Math.abs(draft.currentY - draft.startY) * scale;
177
- // Ignore taps and stray micro-drags — stay in rect mode so the user can try again.
178
- if (width < MIN_RECT_DRAG || height < MIN_RECT_DRAG) return;
179
-
180
- // The new rectangle replaces the stashed selection, so there is nothing left to restore.
181
- stashedCorners.current = null;
182
- setCorners(draftToQuad(draft));
183
- setMode("corners");
184
- }
185
-
186
- async function applyCrop(): Promise<ApplyCropResult | null> {
187
- // Nothing to apply until the user has drawn their rectangle.
188
- if (mode === "rect") return null;
189
- setCropping(true);
190
- setError(null);
191
- try {
192
- // Store corners as relative (0–1) coords so replay is resolution-independent.
193
- // The display canvas may be downscaled (MAX_DISPLAY_WIDTH), so we must map
194
- // corners back to relative coords and then re-apply at full resolution.
195
- let relCorners = activeCorners.map((c) => ({
196
- x: c.x / canvasPixelSize.width,
197
- y: c.y / canvasPixelSize.height,
198
- })) as Quad;
199
-
200
- // Warp the original image at its native resolution to avoid quality loss.
201
- let { blob, corners: usedCorners, geometry } = await cropAtFullResolution(cv, imageUrl, relCorners);
202
- let actions: EditAction[] = [{ type: "crop", params: { corners: usedCorners, mode: geometry } }];
203
- return { blob, actions };
204
- } catch (err) {
205
- setError(err instanceof Error ? err.message : "Crop failed");
206
- return null;
207
- } finally {
208
- setCropping(false);
209
- }
210
- }
211
-
212
- return {
213
- activeCorners,
214
- mode,
215
- hasSelection,
216
- scale,
217
- displaySize,
218
- canvasRef,
219
- cvReady,
220
- cvError,
221
- cropping,
222
- error,
223
- handleCanvasReady,
224
- updateCorner,
225
- startRectMode,
226
- cancelRectMode,
227
- startRectDraw,
228
- updateRectDraw,
229
- finishRectDraw,
230
- applyCrop,
231
- };
232
- }
233
-
234
- /** Normalises a drag into a TL, TR, BR, BL quad. */
235
- function draftToQuad(draft: RectDraft): Quad {
236
- let x1 = Math.min(draft.startX, draft.currentX);
237
- let x2 = Math.max(draft.startX, draft.currentX);
238
- let y1 = Math.min(draft.startY, draft.currentY);
239
- let y2 = Math.max(draft.startY, draft.currentY);
240
- return [
241
- { x: x1, y: y1 },
242
- { x: x2, y: y1 },
243
- { x: x2, y: y2 },
244
- { x: x1, y: y2 },
245
- ];
246
- }
247
-
248
- /** True when the quad's edges are horizontal/vertical, so a plain canvas crop is exact. */
249
- function isAxisAlignedRect([tl, tr, br, bl]: Quad): boolean {
250
- return (
251
- Math.abs(tl.y - tr.y) <= RECT_TOLERANCE &&
252
- Math.abs(bl.y - br.y) <= RECT_TOLERANCE &&
253
- Math.abs(tl.x - bl.x) <= RECT_TOLERANCE &&
254
- Math.abs(tr.x - br.x) <= RECT_TOLERANCE
255
- );
256
- }
257
-
258
- /** Axis-aligned bounding box of a quad, as a quad. */
259
- function boundingQuad(corners: Quad): Quad {
260
- let xs = corners.map((c) => c.x);
261
- let ys = corners.map((c) => c.y);
262
- let x1 = Math.min(...xs);
263
- let x2 = Math.max(...xs);
264
- let y1 = Math.min(...ys);
265
- let y2 = Math.max(...ys);
266
- return [
267
- { x: x1, y: y1 },
268
- { x: x2, y: y1 },
269
- { x: x2, y: y2 },
270
- { x: x1, y: y2 },
271
- ];
272
- }
273
-
274
- /**
275
- * Ensures OpenCV is loaded, reporting success as a boolean instead of throwing.
276
- *
277
- * Deliberately does not *return* the module: Emscripten's module object is thenable
278
- * (it has its own `.then`), so returning it from an async function makes the promise
279
- * machinery adopt it and loop forever, freezing the tab. Callers read `window.cv`.
280
- */
281
- async function ensureOpenCV(): Promise<boolean> {
282
- try {
283
- await loadOpenCV();
284
- return typeof window.cv?.imread === "function";
285
- } catch {
286
- return false;
287
- }
288
- }
289
-
290
- interface CropOutput {
291
- blob: Blob;
292
- /** The corners actually used — a bounding box when a perspective crop had to be downgraded. */
293
- corners: Quad;
294
- geometry: CropGeometry;
295
- }
296
-
297
- /**
298
- * Crops at native resolution, picking the cheapest technique that is exact:
299
- * axis-aligned rectangles use plain canvas drawing, other quads need an OpenCV warp.
300
- * When OpenCV cannot be loaded, a perspective crop degrades to its bounding box.
301
- */
302
- // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js types
303
- async function cropAtFullResolution(cv: any | null, imageUrl: string, relCorners: Quad): Promise<CropOutput> {
304
- if (isAxisAlignedRect(relCorners)) {
305
- return { blob: await rectCropAtFullResolution(imageUrl, relCorners), corners: relCorners, geometry: "rect" };
306
- }
307
-
308
- let cvInstance = cv ?? ((await ensureOpenCV()) ? window.cv : null);
309
- if (!cvInstance) {
310
- console.warn(
311
- "[ImageEditor] OpenCV unavailable — cropping to the selection's bounding box without perspective correction.",
312
- );
313
- let bounds = boundingQuad(relCorners);
314
- return { blob: await rectCropAtFullResolution(imageUrl, bounds), corners: bounds, geometry: "rect" };
315
- }
316
-
317
- return {
318
- blob: await cropImageAtFullResolution(cvInstance, imageUrl, relCorners),
319
- corners: relCorners,
320
- geometry: "perspective",
321
- };
322
- }
323
-
324
- /** Crops an axis-aligned rectangle with the 2D canvas API — no OpenCV required. */
325
- async function rectCropAtFullResolution(imageUrl: string, relCorners: Quad): Promise<Blob> {
326
- let img = await loadImage(imageUrl);
327
- let [tl, , br] = boundingQuad(relCorners);
328
- let x = Math.round(tl.x * img.naturalWidth);
329
- let y = Math.round(tl.y * img.naturalHeight);
330
- let width = Math.max(1, Math.round((br.x - tl.x) * img.naturalWidth));
331
- let height = Math.max(1, Math.round((br.y - tl.y) * img.naturalHeight));
332
-
333
- let canvas = document.createElement("canvas");
334
- canvas.width = width;
335
- canvas.height = height;
336
- canvas.getContext("2d")!.drawImage(img, x, y, width, height, 0, 0, width, height);
337
-
338
- return canvasToPngBlob(canvas);
339
- }
340
-
341
- function loadImage(url: string): Promise<HTMLImageElement> {
342
- return new Promise((resolve, reject) => {
343
- let img = new Image();
344
- img.onload = () => resolve(img);
345
- img.onerror = () => reject(new Error("Failed to load image for crop"));
346
- img.src = url;
347
- });
348
- }
349
-
350
- function canvasToPngBlob(canvas: HTMLCanvasElement): Promise<Blob> {
351
- return new Promise<Blob>((resolve, reject) => {
352
- // PNG is lossless — critical for receipts and screenshots where JPEG
353
- // compression would make text unreadable.
354
- canvas.toBlob((b) => (b ? resolve(b) : reject(new Error("toBlob failed"))), "image/png");
355
- });
356
- }
357
-
358
- /** Loads the image at native resolution, maps relative corners, and warps. */
359
- // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js types
360
- async function cropImageAtFullResolution(cv: any, imageUrl: string, relCorners: Quad): Promise<Blob> {
361
- let img = await loadImage(imageUrl);
362
- let canvas = document.createElement("canvas");
363
- canvas.width = img.naturalWidth;
364
- canvas.height = img.naturalHeight;
365
- canvas.getContext("2d")!.drawImage(img, 0, 0);
366
- let absCorners = relCorners.map((c) => ({
367
- x: c.x * img.naturalWidth,
368
- y: c.y * img.naturalHeight,
369
- })) as Quad;
370
- return perspectiveCrop(cv, canvas, absCorners);
371
- }
372
-
373
- /** Performs a perspective warp using OpenCV.js. */
374
- // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js types
375
- function perspectiveCrop(cv: any, srcCanvas: HTMLCanvasElement, corners: Quad): Promise<Blob> {
376
- let [tl, tr, br, bl] = corners;
377
- let outW = Math.round(Math.max(Math.hypot(tr.x - tl.x, tr.y - tl.y), Math.hypot(br.x - bl.x, br.y - bl.y)));
378
- let outH = Math.round(Math.max(Math.hypot(bl.x - tl.x, bl.y - tl.y), Math.hypot(br.x - tr.x, br.y - tr.y)));
379
-
380
- let src = cv.imread(srcCanvas);
381
- let dst = new cv.Mat();
382
- let srcPts = cv.matFromArray(4, 1, cv.CV_32FC2, [tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y]);
383
- let dstPts = cv.matFromArray(4, 1, cv.CV_32FC2, [0, 0, outW, 0, outW, outH, 0, outH]);
384
- let M = cv.getPerspectiveTransform(srcPts, dstPts);
385
- cv.warpPerspective(src, dst, M, new cv.Size(outW, outH));
386
-
387
- let outCanvas = document.createElement("canvas");
388
- cv.imshow(outCanvas, dst);
389
- for (let m of [src, dst, srcPts, dstPts, M]) m.delete();
390
-
391
- return canvasToPngBlob(outCanvas);
392
- }
393
-
394
- /**
395
- * Standalone replay handler for the "crop" action type.
396
- * Corners are stored as relative (0–1) values so they work at any resolution.
397
- * Rectangular crops replay without OpenCV; older actions without a stored mode
398
- * are treated as perspective crops.
399
- */
400
- export async function replayCrop(imageUrl: string, params: Record<string, unknown>): Promise<Blob> {
401
- let relCorners = params.corners as Quad;
402
- if (params.mode === "rect") return rectCropAtFullResolution(imageUrl, relCorners);
403
- let { blob } = await cropAtFullResolution(null, imageUrl, relCorners);
404
- return blob;
405
- }
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+
3
+ import type { CropCanvasHandle } from "../../CropCanvas.js";
4
+ import type { EditAction } from "../../types.js";
5
+ import { fullImageQuad, type Point, type Quad, useEdgeDetection } from "../../useEdgeDetection.js";
6
+ import { loadOpenCV, useOpenCV } from "../../useOpenCV.js";
7
+
8
+ export interface ApplyCropResult {
9
+ blob: Blob;
10
+ actions: EditAction[];
11
+ }
12
+
13
+ /**
14
+ * `corners` — drag the four corner/edge handles of the current selection.
15
+ * `rect` — drag out a brand new axis-aligned rectangle.
16
+ */
17
+ export type CropMode = "corners" | "rect";
18
+
19
+ /** How a stored crop action must be replayed. Axis-aligned rects need no OpenCV. */
20
+ type CropGeometry = "perspective" | "rect";
21
+
22
+ interface RectDraft {
23
+ startX: number;
24
+ startY: number;
25
+ currentX: number;
26
+ currentY: number;
27
+ }
28
+
29
+ export interface CropToolState {
30
+ activeCorners: Quad;
31
+ mode: CropMode;
32
+ /** False while the user is in rect mode and has not drawn a rectangle yet. */
33
+ hasSelection: boolean;
34
+ scale: number;
35
+ displaySize: { width: number; height: number };
36
+ canvasRef: React.RefObject<CropCanvasHandle | null>;
37
+ cvReady: boolean;
38
+ cvError: Error | null;
39
+ cropping: boolean;
40
+ error: string | null;
41
+ handleCanvasReady: (canvas: HTMLCanvasElement, w: number, h: number) => void;
42
+ updateCorner: (index: number, screenX: number, screenY: number) => void;
43
+ /** Enters rect mode, temporarily clearing (and remembering) the current selection. */
44
+ startRectMode: () => void;
45
+ /** Leaves rect mode without drawing, restoring the selection that was active before. */
46
+ cancelRectMode: () => void;
47
+ startRectDraw: (screenX: number, screenY: number) => void;
48
+ updateRectDraw: (screenX: number, screenY: number) => void;
49
+ finishRectDraw: () => void;
50
+ applyCrop: () => Promise<ApplyCropResult | null>;
51
+ }
52
+
53
+ /** Relative (0–1) tolerance for treating a quad as axis-aligned. */
54
+ const RECT_TOLERANCE = 0.001;
55
+ /** Minimum drag distance in CSS pixels before a drawn rectangle is accepted. */
56
+ const MIN_RECT_DRAG = 4;
57
+
58
+ /**
59
+ * Manages all state and logic for the crop tool.
60
+ * OpenCV is loaded lazily for corner suggestion and perspective warping, but is optional:
61
+ * when it fails to load the tool stays fully usable, only the suggested border is missing.
62
+ */
63
+ export function useCropTool(imageUrl: string, enabled: boolean): CropToolState {
64
+ let { cv, ready: cvReady, error: cvError } = useOpenCV(enabled);
65
+ let [corners, setCorners] = useState<Quad | null>(null);
66
+ let [canvasPixelSize, setCanvasPixelSize] = useState({ width: 1, height: 1 });
67
+ let [displaySize, setDisplaySize] = useState({ width: 0, height: 0 });
68
+ let [canvasEl, setCanvasEl] = useState<HTMLCanvasElement | null>(null);
69
+ let [cropping, setCropping] = useState(false);
70
+ let [error, setError] = useState<string | null>(null);
71
+ let [mode, setMode] = useState<CropMode>("corners");
72
+ let [rectDraft, setRectDraft] = useState<RectDraft | null>(null);
73
+ let canvasRef = useRef<CropCanvasHandle>(null);
74
+ let prevSuggested = useRef<Quad | null>(null);
75
+ // Selection stashed when entering rect mode, restored if the user backs out.
76
+ let stashedCorners = useRef<Quad | null>(null);
77
+ // Mirrors rectDraft so pointer handlers never read a stale render closure.
78
+ let rectDraftRef = useRef<RectDraft | null>(null);
79
+ // Set once the user has touched the selection, so a late suggestion can't overwrite it.
80
+ let userAdjusted = useRef(false);
81
+
82
+ let { corners: suggestedCorners } = useEdgeDetection(cvReady ? cv : null, canvasEl, cvReady && enabled);
83
+
84
+ useEffect(() => {
85
+ if (suggestedCorners && suggestedCorners !== prevSuggested.current) {
86
+ prevSuggested.current = suggestedCorners;
87
+ if (!userAdjusted.current) setCorners(suggestedCorners);
88
+ }
89
+ }, [suggestedCorners]);
90
+
91
+ // Reset when the source image changes (e.g. after a previous crop or rotate)
92
+ useEffect(() => {
93
+ prevSuggested.current = null;
94
+ stashedCorners.current = null;
95
+ rectDraftRef.current = null;
96
+ userAdjusted.current = false;
97
+ setCorners(null);
98
+ setCanvasEl(null);
99
+ setRectDraft(null);
100
+ setMode("corners");
101
+ }, [imageUrl]);
102
+
103
+ let draftQuad = rectDraft ? draftToQuad(rectDraft) : null;
104
+ let hasSelection = mode === "corners" || draftQuad !== null;
105
+ let activeCorners: Quad = draftQuad ?? corners ?? fullImageQuad(canvasPixelSize.width, canvasPixelSize.height);
106
+ let scale = displaySize.width > 0 ? displaySize.width / canvasPixelSize.width : 1;
107
+
108
+ let handleCanvasReady = useCallback((canvas: HTMLCanvasElement, w: number, h: number) => {
109
+ // Detect on a bare copy: the displayed canvas carries the selection border, which
110
+ // starts out along the image edges and would be found as the document outline.
111
+ setCanvasEl(canvasRef.current?.getCleanCanvas() ?? null);
112
+ setCanvasPixelSize({ width: w, height: h });
113
+ setDisplaySize({ width: canvas.clientWidth, height: canvas.clientHeight });
114
+ }, []);
115
+
116
+ /** Converts CSS-pixel overlay coords to canvas-pixel coords, clamped to the image. */
117
+ function toCanvasPoint(screenX: number, screenY: number): Point {
118
+ return {
119
+ x: Math.max(0, Math.min(canvasPixelSize.width, screenX / scale)),
120
+ y: Math.max(0, Math.min(canvasPixelSize.height, screenY / scale)),
121
+ };
122
+ }
123
+
124
+ function updateCorner(index: number, screenX: number, screenY: number) {
125
+ if (mode !== "corners") return;
126
+ let point = toCanvasPoint(screenX, screenY);
127
+ userAdjusted.current = true;
128
+ setCorners((prev) => {
129
+ let next = [...(prev ?? activeCorners)] as Quad;
130
+ next[index] = point;
131
+ return next;
132
+ });
133
+ }
134
+
135
+ function startRectMode() {
136
+ if (mode === "rect") return;
137
+ stashedCorners.current = corners;
138
+ rectDraftRef.current = null;
139
+ userAdjusted.current = true;
140
+ setRectDraft(null);
141
+ setCorners(null);
142
+ setMode("rect");
143
+ }
144
+
145
+ function cancelRectMode() {
146
+ if (mode !== "rect") return;
147
+ // Fall back to the suggested border when rect mode was entered before any selection existed.
148
+ setCorners(stashedCorners.current ?? prevSuggested.current);
149
+ stashedCorners.current = null;
150
+ rectDraftRef.current = null;
151
+ setRectDraft(null);
152
+ setMode("corners");
153
+ }
154
+
155
+ function startRectDraw(screenX: number, screenY: number) {
156
+ if (mode !== "rect") return;
157
+ let { x, y } = toCanvasPoint(screenX, screenY);
158
+ rectDraftRef.current = { startX: x, startY: y, currentX: x, currentY: y };
159
+ setRectDraft(rectDraftRef.current);
160
+ }
161
+
162
+ function updateRectDraw(screenX: number, screenY: number) {
163
+ if (!rectDraftRef.current) return;
164
+ let { x, y } = toCanvasPoint(screenX, screenY);
165
+ rectDraftRef.current = { ...rectDraftRef.current, currentX: x, currentY: y };
166
+ setRectDraft(rectDraftRef.current);
167
+ }
168
+
169
+ function finishRectDraw() {
170
+ let draft = rectDraftRef.current;
171
+ rectDraftRef.current = null;
172
+ setRectDraft(null);
173
+ if (!draft) return;
174
+
175
+ let width = Math.abs(draft.currentX - draft.startX) * scale;
176
+ let height = Math.abs(draft.currentY - draft.startY) * scale;
177
+ // Ignore taps and stray micro-drags — stay in rect mode so the user can try again.
178
+ if (width < MIN_RECT_DRAG || height < MIN_RECT_DRAG) return;
179
+
180
+ // The new rectangle replaces the stashed selection, so there is nothing left to restore.
181
+ stashedCorners.current = null;
182
+ setCorners(draftToQuad(draft));
183
+ setMode("corners");
184
+ }
185
+
186
+ async function applyCrop(): Promise<ApplyCropResult | null> {
187
+ // Nothing to apply until the user has drawn their rectangle.
188
+ if (mode === "rect") return null;
189
+ setCropping(true);
190
+ setError(null);
191
+ try {
192
+ // Store corners as relative (0–1) coords so replay is resolution-independent.
193
+ // The display canvas may be downscaled (MAX_DISPLAY_WIDTH), so we must map
194
+ // corners back to relative coords and then re-apply at full resolution.
195
+ let relCorners = activeCorners.map((c) => ({
196
+ x: c.x / canvasPixelSize.width,
197
+ y: c.y / canvasPixelSize.height,
198
+ })) as Quad;
199
+
200
+ // Warp the original image at its native resolution to avoid quality loss.
201
+ let { blob, corners: usedCorners, geometry } = await cropAtFullResolution(cv, imageUrl, relCorners);
202
+ let actions: EditAction[] = [{ type: "crop", params: { corners: usedCorners, mode: geometry } }];
203
+ return { blob, actions };
204
+ } catch (err) {
205
+ setError(err instanceof Error ? err.message : "Crop failed");
206
+ return null;
207
+ } finally {
208
+ setCropping(false);
209
+ }
210
+ }
211
+
212
+ return {
213
+ activeCorners,
214
+ mode,
215
+ hasSelection,
216
+ scale,
217
+ displaySize,
218
+ canvasRef,
219
+ cvReady,
220
+ cvError,
221
+ cropping,
222
+ error,
223
+ handleCanvasReady,
224
+ updateCorner,
225
+ startRectMode,
226
+ cancelRectMode,
227
+ startRectDraw,
228
+ updateRectDraw,
229
+ finishRectDraw,
230
+ applyCrop,
231
+ };
232
+ }
233
+
234
+ /** Normalises a drag into a TL, TR, BR, BL quad. */
235
+ function draftToQuad(draft: RectDraft): Quad {
236
+ let x1 = Math.min(draft.startX, draft.currentX);
237
+ let x2 = Math.max(draft.startX, draft.currentX);
238
+ let y1 = Math.min(draft.startY, draft.currentY);
239
+ let y2 = Math.max(draft.startY, draft.currentY);
240
+ return [
241
+ { x: x1, y: y1 },
242
+ { x: x2, y: y1 },
243
+ { x: x2, y: y2 },
244
+ { x: x1, y: y2 },
245
+ ];
246
+ }
247
+
248
+ /** True when the quad's edges are horizontal/vertical, so a plain canvas crop is exact. */
249
+ function isAxisAlignedRect([tl, tr, br, bl]: Quad): boolean {
250
+ return (
251
+ Math.abs(tl.y - tr.y) <= RECT_TOLERANCE &&
252
+ Math.abs(bl.y - br.y) <= RECT_TOLERANCE &&
253
+ Math.abs(tl.x - bl.x) <= RECT_TOLERANCE &&
254
+ Math.abs(tr.x - br.x) <= RECT_TOLERANCE
255
+ );
256
+ }
257
+
258
+ /** Axis-aligned bounding box of a quad, as a quad. */
259
+ function boundingQuad(corners: Quad): Quad {
260
+ let xs = corners.map((c) => c.x);
261
+ let ys = corners.map((c) => c.y);
262
+ let x1 = Math.min(...xs);
263
+ let x2 = Math.max(...xs);
264
+ let y1 = Math.min(...ys);
265
+ let y2 = Math.max(...ys);
266
+ return [
267
+ { x: x1, y: y1 },
268
+ { x: x2, y: y1 },
269
+ { x: x2, y: y2 },
270
+ { x: x1, y: y2 },
271
+ ];
272
+ }
273
+
274
+ /**
275
+ * Ensures OpenCV is loaded, reporting success as a boolean instead of throwing.
276
+ *
277
+ * Deliberately does not *return* the module: Emscripten's module object is thenable
278
+ * (it has its own `.then`), so returning it from an async function makes the promise
279
+ * machinery adopt it and loop forever, freezing the tab. Callers read `window.cv`.
280
+ */
281
+ async function ensureOpenCV(): Promise<boolean> {
282
+ try {
283
+ await loadOpenCV();
284
+ return typeof window.cv?.imread === "function";
285
+ } catch {
286
+ return false;
287
+ }
288
+ }
289
+
290
+ interface CropOutput {
291
+ blob: Blob;
292
+ /** The corners actually used — a bounding box when a perspective crop had to be downgraded. */
293
+ corners: Quad;
294
+ geometry: CropGeometry;
295
+ }
296
+
297
+ /**
298
+ * Crops at native resolution, picking the cheapest technique that is exact:
299
+ * axis-aligned rectangles use plain canvas drawing, other quads need an OpenCV warp.
300
+ * When OpenCV cannot be loaded, a perspective crop degrades to its bounding box.
301
+ */
302
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js types
303
+ async function cropAtFullResolution(cv: any | null, imageUrl: string, relCorners: Quad): Promise<CropOutput> {
304
+ if (isAxisAlignedRect(relCorners)) {
305
+ return { blob: await rectCropAtFullResolution(imageUrl, relCorners), corners: relCorners, geometry: "rect" };
306
+ }
307
+
308
+ let cvInstance = cv ?? ((await ensureOpenCV()) ? window.cv : null);
309
+ if (!cvInstance) {
310
+ console.warn(
311
+ "[ImageEditor] OpenCV unavailable — cropping to the selection's bounding box without perspective correction.",
312
+ );
313
+ let bounds = boundingQuad(relCorners);
314
+ return { blob: await rectCropAtFullResolution(imageUrl, bounds), corners: bounds, geometry: "rect" };
315
+ }
316
+
317
+ return {
318
+ blob: await cropImageAtFullResolution(cvInstance, imageUrl, relCorners),
319
+ corners: relCorners,
320
+ geometry: "perspective",
321
+ };
322
+ }
323
+
324
+ /** Crops an axis-aligned rectangle with the 2D canvas API — no OpenCV required. */
325
+ async function rectCropAtFullResolution(imageUrl: string, relCorners: Quad): Promise<Blob> {
326
+ let img = await loadImage(imageUrl);
327
+ let [tl, , br] = boundingQuad(relCorners);
328
+ let x = Math.round(tl.x * img.naturalWidth);
329
+ let y = Math.round(tl.y * img.naturalHeight);
330
+ let width = Math.max(1, Math.round((br.x - tl.x) * img.naturalWidth));
331
+ let height = Math.max(1, Math.round((br.y - tl.y) * img.naturalHeight));
332
+
333
+ let canvas = document.createElement("canvas");
334
+ canvas.width = width;
335
+ canvas.height = height;
336
+ canvas.getContext("2d")!.drawImage(img, x, y, width, height, 0, 0, width, height);
337
+
338
+ return canvasToPngBlob(canvas);
339
+ }
340
+
341
+ function loadImage(url: string): Promise<HTMLImageElement> {
342
+ return new Promise((resolve, reject) => {
343
+ let img = new Image();
344
+ img.onload = () => resolve(img);
345
+ img.onerror = () => reject(new Error("Failed to load image for crop"));
346
+ img.src = url;
347
+ });
348
+ }
349
+
350
+ function canvasToPngBlob(canvas: HTMLCanvasElement): Promise<Blob> {
351
+ return new Promise<Blob>((resolve, reject) => {
352
+ // PNG is lossless — critical for receipts and screenshots where JPEG
353
+ // compression would make text unreadable.
354
+ canvas.toBlob((b) => (b ? resolve(b) : reject(new Error("toBlob failed"))), "image/png");
355
+ });
356
+ }
357
+
358
+ /** Loads the image at native resolution, maps relative corners, and warps. */
359
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js types
360
+ async function cropImageAtFullResolution(cv: any, imageUrl: string, relCorners: Quad): Promise<Blob> {
361
+ let img = await loadImage(imageUrl);
362
+ let canvas = document.createElement("canvas");
363
+ canvas.width = img.naturalWidth;
364
+ canvas.height = img.naturalHeight;
365
+ canvas.getContext("2d")!.drawImage(img, 0, 0);
366
+ let absCorners = relCorners.map((c) => ({
367
+ x: c.x * img.naturalWidth,
368
+ y: c.y * img.naturalHeight,
369
+ })) as Quad;
370
+ return perspectiveCrop(cv, canvas, absCorners);
371
+ }
372
+
373
+ /** Performs a perspective warp using OpenCV.js. */
374
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js types
375
+ function perspectiveCrop(cv: any, srcCanvas: HTMLCanvasElement, corners: Quad): Promise<Blob> {
376
+ let [tl, tr, br, bl] = corners;
377
+ let outW = Math.round(Math.max(Math.hypot(tr.x - tl.x, tr.y - tl.y), Math.hypot(br.x - bl.x, br.y - bl.y)));
378
+ let outH = Math.round(Math.max(Math.hypot(bl.x - tl.x, bl.y - tl.y), Math.hypot(br.x - tr.x, br.y - tr.y)));
379
+
380
+ let src = cv.imread(srcCanvas);
381
+ let dst = new cv.Mat();
382
+ let srcPts = cv.matFromArray(4, 1, cv.CV_32FC2, [tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y]);
383
+ let dstPts = cv.matFromArray(4, 1, cv.CV_32FC2, [0, 0, outW, 0, outW, outH, 0, outH]);
384
+ let M = cv.getPerspectiveTransform(srcPts, dstPts);
385
+ cv.warpPerspective(src, dst, M, new cv.Size(outW, outH));
386
+
387
+ let outCanvas = document.createElement("canvas");
388
+ cv.imshow(outCanvas, dst);
389
+ for (let m of [src, dst, srcPts, dstPts, M]) m.delete();
390
+
391
+ return canvasToPngBlob(outCanvas);
392
+ }
393
+
394
+ /**
395
+ * Standalone replay handler for the "crop" action type.
396
+ * Corners are stored as relative (0–1) values so they work at any resolution.
397
+ * Rectangular crops replay without OpenCV; older actions without a stored mode
398
+ * are treated as perspective crops.
399
+ */
400
+ export async function replayCrop(imageUrl: string, params: Record<string, unknown>): Promise<Blob> {
401
+ let relCorners = params.corners as Quad;
402
+ if (params.mode === "rect") return rectCropAtFullResolution(imageUrl, relCorners);
403
+ let { blob } = await cropAtFullResolution(null, imageUrl, relCorners);
404
+ return blob;
405
+ }