@olenbetong/synergi-react 4.1.1 → 4.1.2

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 (59) hide show
  1. package/es/components/ImageEditor/plugins/cropPlugin/useCropTool.js +3 -1
  2. package/es/components/ImageEditor/useEdgeDetection.d.ts +17 -2
  3. package/es/components/ImageEditor/useEdgeDetection.js +238 -61
  4. package/es/components/Toolbar/index.js +20 -1
  5. package/package.json +3 -3
  6. package/src/chrome/AppShell/index.tsx +29 -29
  7. package/src/chrome/openReport.ts +34 -34
  8. package/src/colorScheme.ts +81 -81
  9. package/src/components/DateNavigator/index.tsx +186 -186
  10. package/src/components/FilePond.tsx +312 -312
  11. package/src/components/ImageEditor/ImageEditor.tsx +343 -343
  12. package/src/components/ImageEditor/index.ts +9 -9
  13. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  14. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  15. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -403
  16. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  17. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  18. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  19. package/src/components/ImageEditor/useEdgeDetection.ts +316 -127
  20. package/src/components/ImageEditor/useImageHistory.ts +197 -197
  21. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  22. package/src/components/LinkedCardList/index.scss +95 -95
  23. package/src/components/MergeProgressDialog/index.tsx +249 -249
  24. package/src/components/PageBanner/index.tsx +80 -80
  25. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  26. package/src/components/ProgressBar/index.scss +96 -96
  27. package/src/components/SplitContainer/index.tsx +333 -333
  28. package/src/components/Toolbar/index.tsx +232 -212
  29. package/src/designsystemet-web-types.d.ts +24 -24
  30. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  31. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  32. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  33. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  34. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  35. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  36. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  37. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  38. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  39. package/src/elements/ElementLookup.tsx +123 -123
  40. package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
  41. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
  42. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
  43. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
  44. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
  45. package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
  46. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
  47. package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
  48. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  49. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  50. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  51. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  52. package/src/elements/ProjectSelect.tsx +130 -130
  53. package/src/elements/index.ts +15 -15
  54. package/src/filter/index.ts +34 -34
  55. package/src/i18n.ts +13 -13
  56. package/src/index.ts +77 -77
  57. package/src/mui/MuiThemeProvider.tsx +174 -174
  58. package/src/mui/index.ts +1 -1
  59. package/src/test/setup.ts +8 -8
@@ -1,127 +1,316 @@
1
- import { useEffect, useState } from "react";
2
-
3
- export interface Point {
4
- x: number;
5
- y: number;
6
- }
7
-
8
- /** TL, TR, BR, BL */
9
- export type Quad = [Point, Point, Point, Point];
10
-
11
- export function fullImageQuad(width: number, height: number): Quad {
12
- return [
13
- { x: 0, y: 0 },
14
- { x: width, y: 0 },
15
- { x: width, y: height },
16
- { x: 0, y: height },
17
- ];
18
- }
19
-
20
- function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): Quad | null {
21
- let gray = new cv.Mat();
22
- let blurred = new cv.Mat();
23
- let edges = new cv.Mat();
24
- let dilated = new cv.Mat();
25
- let contours = new cv.MatVector();
26
- let hierarchy = new cv.Mat();
27
-
28
- try {
29
- cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY);
30
- cv.GaussianBlur(gray, blurred, new cv.Size(5, 5), 0);
31
- // Lower thresholds catch document-background boundaries even in low-contrast images
32
- cv.Canny(blurred, edges, 40, 120);
33
-
34
- // Dilate to close small gaps along document edges
35
- let kernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(3, 3));
36
- cv.dilate(edges, dilated, kernel);
37
- kernel.delete();
38
-
39
- cv.findContours(dilated, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
40
-
41
- let best: unknown = null;
42
- let bestArea = 0;
43
- let minArea = src.cols * src.rows * minAreaFraction;
44
-
45
- for (let i = 0; i < contours.size(); i++) {
46
- let contour = contours.get(i);
47
- let peri = cv.arcLength(contour, true);
48
- let approx = new cv.Mat();
49
- // Larger epsilon is more aggressive — helps when the outline isn't perfectly straight
50
- cv.approxPolyDP(contour, approx, 0.04 * peri, true);
51
-
52
- if (approx.rows === 4) {
53
- let area = cv.contourArea(approx);
54
- if (area > bestArea && area > minArea) {
55
- bestArea = area;
56
- (best as any)?.delete();
57
- best = approx;
58
- } else {
59
- approx.delete();
60
- }
61
- } else {
62
- approx.delete();
63
- }
64
- contour.delete();
65
- }
66
-
67
- let quad: Quad | null = null;
68
- if (best) {
69
- let mat = best as any;
70
- let pts: Point[] = [];
71
- for (let i = 0; i < 4; i++) {
72
- pts.push({ x: mat.data32S[i * 2], y: mat.data32S[i * 2 + 1] });
73
- }
74
- mat.delete();
75
-
76
- // Sort into TL, TR, BR, BL
77
- pts.sort((a, b) => a.x + a.y - (b.x + b.y));
78
- let tl = pts[0];
79
- let br = pts[3];
80
- let remaining = [pts[1], pts[2]];
81
- let tr = remaining[0].x > remaining[1].x ? remaining[0] : remaining[1];
82
- let bl = remaining[0].x < remaining[1].x ? remaining[0] : remaining[1];
83
- quad = [tl, tr, br, bl];
84
- }
85
-
86
- return quad;
87
- } finally {
88
- gray.delete();
89
- blurred.delete();
90
- edges.delete();
91
- dilated.delete();
92
- contours.delete();
93
- hierarchy.delete();
94
- }
95
- }
96
-
97
- interface UseEdgeDetectionResult {
98
- corners: Quad | null;
99
- }
100
-
101
- /**
102
- * Runs Canny edge detection on the image to suggest document corners.
103
- * Falls back to full-image corners if no quadrilateral is found.
104
- */
105
- export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
106
- let [corners, setCorners] = useState<Quad | null>(null);
107
-
108
- useEffect(() => {
109
- if (!enabled || !cv || !canvas) return;
110
-
111
- try {
112
- let src = cv.imread(canvas);
113
- let quad: Quad | null;
114
- try {
115
- quad = findLargestQuad(cv, src);
116
- } finally {
117
- src.delete();
118
- }
119
- setCorners(quad ?? fullImageQuad(canvas.width, canvas.height));
120
- } catch (err) {
121
- console.warn("[ImageEditor] Edge detection failed, falling back to the full image:", err);
122
- setCorners(fullImageQuad(canvas.width, canvas.height));
123
- }
124
- }, [cv, canvas, enabled]);
125
-
126
- return { corners };
127
- }
1
+ import { useEffect, useState } from "react";
2
+
3
+ export interface Point {
4
+ x: number;
5
+ y: number;
6
+ }
7
+
8
+ /** TL, TR, BR, BL */
9
+ export type Quad = [Point, Point, Point, Point];
10
+
11
+ export function fullImageQuad(width: number, height: number): Quad {
12
+ return [
13
+ { x: 0, y: 0 },
14
+ { x: width, y: 0 },
15
+ { x: width, y: height },
16
+ { x: 0, y: height },
17
+ ];
18
+ }
19
+
20
+ /** Longest side of the downscaled working image. Detection is scale-dependent, so it is fixed. */
21
+ const WORK_SIZE = 512;
22
+ /** Closing kernel that erases dark print (text, thin rules) so only surface boundaries remain. */
23
+ const CLOSE_KERNEL = 5;
24
+ /** Candidate lines kept per orientation family. The search is O(n⁴), so keep this small. */
25
+ const MAX_LINES_PER_FAMILY = 14;
26
+ /** Distances (working pixels) sampled on each side of a candidate edge. */
27
+ const SAMPLE_OFFSET_MIN = 3;
28
+ const SAMPLE_OFFSET_MAX = 9;
29
+ /** Minimum Lab colour difference between the two sides for an edge sample to count. */
30
+ const MIN_CONTRAST = 8;
31
+ /** Cost of an unsupported stretch of side, relative to the gain of a supported one. */
32
+ const UNSUPPORTED_COST = 0.5;
33
+ /** Cost per pixel of using the image border as a side (document running off the frame). */
34
+ const BORDER_COST = 0.1;
35
+ /** Minimum normalised score for a suggestion; below it the full image is used. */
36
+ const MIN_SCORE = 0.3;
37
+ const MIN_AREA_FRACTION = 0.1;
38
+
39
+ /** A line in normal form: x·cosθ + y·sinθ = ρ. */
40
+ interface Line {
41
+ theta: number;
42
+ rho: number;
43
+ weight: number;
44
+ border?: boolean;
45
+ }
46
+
47
+ /**
48
+ * Finds the most likely document outline in an RGBA image.
49
+ *
50
+ * Print is first removed with a morphological close, so text and table rules do not
51
+ * compete with the paper's outline. Straight segments from a Hough transform are merged
52
+ * into candidate lines, and every combination of two near-vertical and two near-horizontal
53
+ * lines (the image borders included, for receipts that run off the photo) is scored by how
54
+ * much of each side both lies on an edge and separates two differently coloured surfaces.
55
+ * Scoring by supported length, rather than by the share of each side, lets a long receipt
56
+ * whose edges bend at a fold beat a small, perfectly straight section of it.
57
+ */
58
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
59
+ export function findDocumentQuad(cv: any, src: any): Quad | null {
60
+ let scale = Math.min(1, WORK_SIZE / Math.max(src.cols, src.rows));
61
+ let small = new cv.Mat();
62
+ let rgb = new cv.Mat();
63
+ let colour = new cv.Mat();
64
+ let lab = new cv.Mat();
65
+ let gray = new cv.Mat();
66
+ let edges = new cv.Mat();
67
+ let support = new cv.Mat();
68
+ let segments = new cv.Mat();
69
+
70
+ try {
71
+ cv.resize(src, small, new cv.Size(Math.round(src.cols * scale), Math.round(src.rows * scale)), 0, 0, cv.INTER_AREA);
72
+ let w = small.cols;
73
+ let h = small.rows;
74
+ let minDim = Math.min(w, h);
75
+
76
+ cv.cvtColor(small, rgb, cv.COLOR_RGBA2RGB);
77
+ let closeKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(CLOSE_KERNEL, CLOSE_KERNEL));
78
+ cv.morphologyEx(rgb, rgb, cv.MORPH_CLOSE, closeKernel);
79
+ closeKernel.delete();
80
+ cv.GaussianBlur(rgb, colour, new cv.Size(5, 5), 0);
81
+ cv.cvtColor(colour, lab, cv.COLOR_RGB2Lab);
82
+ // Blurred after the grey conversion, not taken from `colour`: the rounding differs,
83
+ // and the weaker receipt edges sit close enough to the Hough threshold to notice.
84
+ cv.cvtColor(rgb, gray, cv.COLOR_RGB2GRAY);
85
+ cv.GaussianBlur(gray, gray, new cv.Size(5, 5), 0);
86
+
87
+ // Low thresholds: a receipt on a white table has very little contrast.
88
+ cv.Canny(gray, edges, 20, 60);
89
+ // A tolerant copy for scoring, so a side a pixel or two off its edge still counts.
90
+ let supportKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(5, 5));
91
+ cv.dilate(edges, support, supportKernel);
92
+ supportKernel.delete();
93
+ cv.HoughLinesP(edges, segments, 1, Math.PI / 180, 30, minDim * 0.12, 8);
94
+
95
+ let { vertical, horizontal } = groupLines(segments, minDim);
96
+ vertical.push({ theta: 0, rho: 0, weight: 0, border: true }, { theta: 0, rho: w - 1, weight: 0, border: true });
97
+ horizontal.push(
98
+ { theta: Math.PI / 2, rho: 0, weight: 0, border: true },
99
+ { theta: Math.PI / 2, rho: h - 1, weight: 0, border: true },
100
+ );
101
+
102
+ let labData: Uint8Array = lab.data;
103
+ let supportData: Uint8Array = support.data;
104
+ let labAt = (x: number, y: number, channel: number) => {
105
+ let xi = Math.min(w - 1, Math.max(0, Math.round(x)));
106
+ let yi = Math.min(h - 1, Math.max(0, Math.round(y)));
107
+ return labData[(yi * w + xi) * 3 + channel];
108
+ };
109
+
110
+ /** Share of a side that lies on an edge with different surfaces on either side of it. */
111
+ let coverage = (a: Point, b: Point) => {
112
+ let len = distance(a, b);
113
+ let n = Math.max(8, Math.round(len / 3));
114
+ let nx = -(b.y - a.y) / len;
115
+ let ny = (b.x - a.x) / len;
116
+ let hits = 0;
117
+ for (let i = 1; i < n; i++) {
118
+ let px = a.x + ((b.x - a.x) * i) / n;
119
+ let py = a.y + ((b.y - a.y) * i) / n;
120
+ let x = Math.round(px);
121
+ let y = Math.round(py);
122
+ if (x < 0 || y < 0 || x >= w || y >= h || !supportData[y * w + x]) continue;
123
+
124
+ let diff = [0, 0, 0];
125
+ let count = 0;
126
+ for (let d = SAMPLE_OFFSET_MIN; d <= SAMPLE_OFFSET_MAX; d += 2) {
127
+ for (let c = 0; c < 3; c++) {
128
+ diff[c] += labAt(px + nx * d, py + ny * d, c) - labAt(px - nx * d, py - ny * d, c);
129
+ }
130
+ count++;
131
+ }
132
+ // Chroma weighs double: a cream receipt on a grey-white table differs mostly in tint.
133
+ if (Math.hypot(diff[0] / count, (2 * diff[1]) / count, (2 * diff[2]) / count) > MIN_CONTRAST) hits++;
134
+ }
135
+ return hits / (n - 1);
136
+ };
137
+
138
+ let margin = minDim * 0.02;
139
+ let inBounds = (p: Point | null): p is Point =>
140
+ p !== null && p.x >= -margin && p.y >= -margin && p.x <= w + margin && p.y <= h + margin;
141
+ let minGap = minDim * 0.1;
142
+
143
+ let best: Quad | null = null;
144
+ let bestScore = -Infinity;
145
+ for (let left of vertical) {
146
+ for (let right of vertical) {
147
+ if (xAt(left, h / 2) >= xAt(right, h / 2) - minGap) continue;
148
+ for (let top of horizontal) {
149
+ for (let bottom of horizontal) {
150
+ if (yAt(top, w / 2) >= yAt(bottom, w / 2) - minGap) continue;
151
+ let sides = [top, right, bottom, left];
152
+ if (sides.filter((l) => l.border).length > 2) continue;
153
+
154
+ let corners = [
155
+ intersect(left, top),
156
+ intersect(right, top),
157
+ intersect(right, bottom),
158
+ intersect(left, bottom),
159
+ ];
160
+ if (!corners.every(inBounds)) continue;
161
+ let quad = corners as Quad;
162
+ if (polygonArea(quad) < w * h * MIN_AREA_FRACTION || !isConvex(quad)) continue;
163
+
164
+ let score = 0;
165
+ for (let s = 0; s < 4; s++) {
166
+ let a = quad[s];
167
+ let b = quad[(s + 1) % 4];
168
+ let len = distance(a, b);
169
+ if (sides[s].border) {
170
+ score -= BORDER_COST * len;
171
+ } else {
172
+ let cov = coverage(a, b);
173
+ score += len * (cov - UNSUPPORTED_COST * (1 - cov));
174
+ }
175
+ }
176
+ score /= w + h;
177
+ if (score > bestScore) {
178
+ bestScore = score;
179
+ best = quad;
180
+ }
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ if (!best || bestScore < MIN_SCORE) return null;
187
+ return best.map((p) => ({
188
+ x: Math.min(src.cols, Math.max(0, p.x / scale)),
189
+ y: Math.min(src.rows, Math.max(0, p.y / scale)),
190
+ })) as Quad;
191
+ } finally {
192
+ for (let mat of [small, rgb, colour, lab, gray, edges, support, segments]) mat.delete();
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Merges Hough segments that lie on the same line and splits the result into near-vertical
198
+ * and near-horizontal families, each ordered by total segment length.
199
+ */
200
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
201
+ function groupLines(segments: any, minDim: number): { vertical: Line[]; horizontal: Line[] } {
202
+ let lines: Line[] = [];
203
+ let maxAngle = (3 * Math.PI) / 180;
204
+ let maxOffset = minDim * 0.015;
205
+
206
+ for (let i = 0; i < segments.rows; i++) {
207
+ let [x1, y1, x2, y2] = segments.data32S.slice(i * 4, i * 4 + 4);
208
+ let len = Math.hypot(x2 - x1, y2 - y1);
209
+ // Normal of the segment, folded into [0, π)
210
+ let theta = Math.atan2(x1 - x2, y2 - y1);
211
+ if (theta < 0) theta += Math.PI;
212
+ if (theta >= Math.PI) theta -= Math.PI;
213
+ let rho = x1 * Math.cos(theta) + y1 * Math.sin(theta);
214
+
215
+ let match = lines.find((l) => Math.abs(l.theta - theta) < maxAngle && Math.abs(l.rho - rho) < maxOffset);
216
+ if (match) {
217
+ let total = match.weight + len;
218
+ match.theta = (match.theta * match.weight + theta * len) / total;
219
+ match.rho = (match.rho * match.weight + rho * len) / total;
220
+ match.weight = total;
221
+ } else {
222
+ lines.push({ theta, rho, weight: len });
223
+ }
224
+ }
225
+
226
+ lines.sort((a, b) => b.weight - a.weight);
227
+ let isVertical = (l: Line) => l.theta < Math.PI / 4 || l.theta > (3 * Math.PI) / 4;
228
+ return {
229
+ vertical: lines.filter(isVertical).slice(0, MAX_LINES_PER_FAMILY),
230
+ horizontal: lines.filter((l) => !isVertical(l)).slice(0, MAX_LINES_PER_FAMILY),
231
+ };
232
+ }
233
+
234
+ /** x where a near-vertical line crosses the given y. */
235
+ function xAt(line: Line, y: number): number {
236
+ return (line.rho - y * Math.sin(line.theta)) / Math.cos(line.theta);
237
+ }
238
+
239
+ /** y where a near-horizontal line crosses the given x. */
240
+ function yAt(line: Line, x: number): number {
241
+ return (line.rho - x * Math.cos(line.theta)) / Math.sin(line.theta);
242
+ }
243
+
244
+ function intersect(a: Line, b: Line): Point | null {
245
+ let ca = Math.cos(a.theta);
246
+ let sa = Math.sin(a.theta);
247
+ let cb = Math.cos(b.theta);
248
+ let sb = Math.sin(b.theta);
249
+ let det = ca * sb - sa * cb;
250
+ if (Math.abs(det) < 1e-6) return null;
251
+ return { x: (a.rho * sb - b.rho * sa) / det, y: (ca * b.rho - cb * a.rho) / det };
252
+ }
253
+
254
+ function distance(a: Point, b: Point): number {
255
+ return Math.hypot(b.x - a.x, b.y - a.y);
256
+ }
257
+
258
+ function polygonArea(points: Point[]): number {
259
+ let area = 0;
260
+ for (let i = 0; i < points.length; i++) {
261
+ let p = points[i];
262
+ let q = points[(i + 1) % points.length];
263
+ area += p.x * q.y - q.x * p.y;
264
+ }
265
+ return Math.abs(area) / 2;
266
+ }
267
+
268
+ function isConvex(points: Point[]): boolean {
269
+ let sign = 0;
270
+ for (let i = 0; i < points.length; i++) {
271
+ let a = points[i];
272
+ let b = points[(i + 1) % points.length];
273
+ let c = points[(i + 2) % points.length];
274
+ let s = Math.sign((b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x));
275
+ if (s === 0) continue;
276
+ if (sign && s !== sign) return false;
277
+ sign = s;
278
+ }
279
+ return true;
280
+ }
281
+
282
+ interface UseEdgeDetectionResult {
283
+ corners: Quad | null;
284
+ }
285
+
286
+ /**
287
+ * Suggests document corners for the image on `canvas`.
288
+ * Falls back to full-image corners if no document outline is found.
289
+ *
290
+ * `canvas` must hold the bare image: anything drawn over it, such as the crop overlay's
291
+ * selection border, is detected as an edge like any other.
292
+ */
293
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
294
+ export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
295
+ let [corners, setCorners] = useState<Quad | null>(null);
296
+
297
+ useEffect(() => {
298
+ if (!enabled || !cv || !canvas) return;
299
+
300
+ try {
301
+ let src = cv.imread(canvas);
302
+ let quad: Quad | null;
303
+ try {
304
+ quad = findDocumentQuad(cv, src);
305
+ } finally {
306
+ src.delete();
307
+ }
308
+ setCorners(quad ?? fullImageQuad(canvas.width, canvas.height));
309
+ } catch (err) {
310
+ console.warn("[ImageEditor] Edge detection failed, falling back to the full image:", err);
311
+ setCorners(fullImageQuad(canvas.width, canvas.height));
312
+ }
313
+ }, [cv, canvas, enabled]);
314
+
315
+ return { corners };
316
+ }