@olenbetong/synergi-react 2.8.0 → 3.0.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 (128) hide show
  1. package/dist/styles/styles.css.map +1 -1
  2. package/es/chrome/AppShell/index.d.ts +8 -0
  3. package/es/chrome/AppShell/index.js +10 -130
  4. package/es/components/ImageEditor/ImageEditor.css +10 -0
  5. package/es/components/ImageEditor/ImageEditor.d.ts +12 -0
  6. package/es/components/ImageEditor/ImageEditor.js +27 -6
  7. package/es/components/ImageEditor/index.d.ts +3 -0
  8. package/es/components/ImageEditor/index.js +3 -0
  9. package/es/components/ImageEditor/plugins/cropPlugin/CropOverlay.js +27 -5
  10. package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +4 -3
  11. package/es/components/ImageEditor/plugins/cropPlugin/useCropTool.d.ts +20 -3
  12. package/es/components/ImageEditor/plugins/cropPlugin/useCropTool.js +216 -33
  13. package/es/components/ImageEditor/plugins/zoomPlugin/ZoomContext.d.ts +5 -0
  14. package/es/components/ImageEditor/plugins/zoomPlugin/ZoomContext.js +7 -0
  15. package/es/components/ImageEditor/plugins/zoomPlugin/ZoomControls.js +1 -1
  16. package/es/components/ImageEditor/useEdgeDetection.d.ts +0 -2
  17. package/es/components/ImageEditor/useEdgeDetection.js +11 -30
  18. package/es/components/ImageEditor/useImageHistory.d.ts +6 -1
  19. package/es/components/ImageEditor/useImageHistory.js +21 -23
  20. package/es/components/ImageEditor/useOpenCV.d.ts +26 -1
  21. package/es/components/ImageEditor/useOpenCV.js +108 -42
  22. package/es/components/SplitContainer/index.js +2 -4
  23. package/es/elements/ElementDamage/DamageFormButtons.d.ts +2 -3
  24. package/es/elements/ElementDamage/DamageFormButtons.js +1 -1
  25. package/es/elements/ElementDamage/TransportElementSelect.d.ts +1 -2
  26. package/es/elements/index.d.ts +5 -0
  27. package/es/elements/index.js +5 -0
  28. package/es/filter/ChipListInput.d.ts +13 -0
  29. package/es/filter/ChipListInput.js +34 -0
  30. package/es/filter/FilterBuilder.css +152 -0
  31. package/es/filter/FilterBuilder.d.ts +115 -0
  32. package/es/filter/FilterBuilder.js +146 -0
  33. package/es/filter/FilterBuilder.test.d.ts +1 -0
  34. package/es/filter/FilterBuilder.test.js +120 -0
  35. package/es/filter/FilterEditor.css +231 -0
  36. package/es/filter/FilterEditor.d.ts +17 -0
  37. package/es/filter/FilterEditor.js +38 -0
  38. package/es/filter/FilterEditor.test.d.ts +1 -0
  39. package/es/filter/FilterEditor.test.js +183 -0
  40. package/es/filter/FilterGroupEditor.d.ts +22 -0
  41. package/es/filter/FilterGroupEditor.js +34 -0
  42. package/es/filter/FilterNameDialog.css +22 -0
  43. package/es/filter/FilterNameDialog.d.ts +18 -0
  44. package/es/filter/FilterNameDialog.js +32 -0
  45. package/es/filter/FilterRow.d.ts +14 -0
  46. package/es/filter/FilterRow.js +32 -0
  47. package/es/filter/FilterShareDialog.css +28 -0
  48. package/es/filter/FilterShareDialog.d.ts +23 -0
  49. package/es/filter/FilterShareDialog.js +63 -0
  50. package/es/filter/FilterStringField.d.ts +15 -0
  51. package/es/filter/FilterStringField.js +37 -0
  52. package/es/filter/FilterValueEditor.d.ts +16 -0
  53. package/es/filter/FilterValueEditor.js +85 -0
  54. package/es/filter/SavedFilterTree.css +107 -0
  55. package/es/filter/SavedFilterTree.d.ts +45 -0
  56. package/es/filter/SavedFilterTree.js +44 -0
  57. package/es/filter/SavedFilterTree.test.d.ts +1 -0
  58. package/es/filter/SavedFilterTree.test.js +75 -0
  59. package/es/filter/index.d.ts +11 -0
  60. package/es/filter/index.js +10 -0
  61. package/es/filter/types.d.ts +34 -0
  62. package/es/filter/types.js +1 -0
  63. package/es/index.d.ts +2 -1
  64. package/es/index.js +6 -1
  65. package/es/mui/MuiThemeProvider.d.ts +24 -0
  66. package/es/mui/MuiThemeProvider.js +152 -0
  67. package/es/mui/index.d.ts +1 -0
  68. package/es/mui/index.js +1 -0
  69. package/es/report/ReportLauncher.css +147 -0
  70. package/es/report/ReportLauncher.d.ts +55 -0
  71. package/es/report/ReportLauncher.js +142 -0
  72. package/es/report/ReportLauncher.test.d.ts +1 -0
  73. package/es/report/ReportLauncher.test.js +231 -0
  74. package/es/report/ReportParameters.css +29 -0
  75. package/es/report/ReportParameters.d.ts +46 -0
  76. package/es/report/ReportParameters.js +65 -0
  77. package/es/report/ReportParameters.test.d.ts +1 -0
  78. package/es/report/ReportParameters.test.js +84 -0
  79. package/es/report/index.d.ts +2 -0
  80. package/es/report/index.js +2 -0
  81. package/es/test/setup.d.ts +1 -0
  82. package/es/test/setup.js +6 -0
  83. package/package.json +22 -7
  84. package/src/chrome/AppShell/index.tsx +34 -171
  85. package/src/components/ImageEditor/ImageEditor.css +10 -0
  86. package/src/components/ImageEditor/ImageEditor.tsx +40 -5
  87. package/src/components/ImageEditor/index.ts +1 -0
  88. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +66 -20
  89. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +19 -3
  90. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +266 -39
  91. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +8 -0
  92. package/src/components/ImageEditor/useEdgeDetection.ts +11 -51
  93. package/src/components/ImageEditor/useImageHistory.ts +27 -19
  94. package/src/components/ImageEditor/useOpenCV.ts +112 -62
  95. package/src/components/LinkedCardList/index.scss +4 -2
  96. package/src/components/ProgressBar/index.scss +4 -2
  97. package/src/components/SplitContainer/index.tsx +3 -4
  98. package/src/filter/ChipListInput.tsx +79 -0
  99. package/src/filter/FilterBuilder.css +152 -0
  100. package/src/filter/FilterBuilder.test.tsx +153 -0
  101. package/src/filter/FilterBuilder.tsx +435 -0
  102. package/src/filter/FilterEditor.css +231 -0
  103. package/src/filter/FilterEditor.test.tsx +259 -0
  104. package/src/filter/FilterEditor.tsx +74 -0
  105. package/src/filter/FilterGroupEditor.tsx +160 -0
  106. package/src/filter/FilterNameDialog.css +22 -0
  107. package/src/filter/FilterNameDialog.tsx +81 -0
  108. package/src/filter/FilterRow.tsx +117 -0
  109. package/src/filter/FilterShareDialog.css +28 -0
  110. package/src/filter/FilterShareDialog.tsx +201 -0
  111. package/src/filter/FilterStringField.tsx +77 -0
  112. package/src/filter/FilterValueEditor.tsx +220 -0
  113. package/src/filter/SavedFilterTree.css +107 -0
  114. package/src/filter/SavedFilterTree.test.tsx +94 -0
  115. package/src/filter/SavedFilterTree.tsx +222 -0
  116. package/src/filter/index.ts +11 -0
  117. package/src/filter/types.ts +38 -0
  118. package/src/index.ts +39 -1
  119. package/src/mui/MuiThemeProvider.tsx +174 -0
  120. package/src/mui/index.ts +1 -0
  121. package/src/report/ReportLauncher.css +147 -0
  122. package/src/report/ReportLauncher.test.tsx +281 -0
  123. package/src/report/ReportLauncher.tsx +421 -0
  124. package/src/report/ReportParameters.css +29 -0
  125. package/src/report/ReportParameters.test.tsx +133 -0
  126. package/src/report/ReportParameters.tsx +198 -0
  127. package/src/report/index.ts +2 -0
  128. package/src/test/setup.ts +8 -0
@@ -17,13 +17,7 @@ export function fullImageQuad(width: number, height: number): Quad {
17
17
  ];
18
18
  }
19
19
 
20
- interface FindQuadResult {
21
- quad: Quad | null;
22
- /** Canvas containing the Canny edge image (with detected quad overlay) for debugging */
23
- debugCanvas: HTMLCanvasElement;
24
- }
25
-
26
- function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): FindQuadResult {
20
+ function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): Quad | null {
27
21
  let gray = new cv.Mat();
28
22
  let blurred = new cv.Mat();
29
23
  let edges = new cv.Mat();
@@ -42,10 +36,6 @@ function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): FindQuadResu
42
36
  cv.dilate(edges, dilated, kernel);
43
37
  kernel.delete();
44
38
 
45
- // Build RGBA debug canvas so we can draw a coloured quad overlay
46
- let debugRgba = new cv.Mat();
47
- cv.cvtColor(dilated, debugRgba, cv.COLOR_GRAY2RGBA);
48
-
49
39
  cv.findContours(dilated, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
50
40
 
51
41
  let best: unknown = null;
@@ -91,25 +81,9 @@ function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): FindQuadResu
91
81
  let tr = remaining[0].x > remaining[1].x ? remaining[0] : remaining[1];
92
82
  let bl = remaining[0].x < remaining[1].x ? remaining[0] : remaining[1];
93
83
  quad = [tl, tr, br, bl];
94
-
95
- // Draw the detected quad on the debug canvas in green
96
- let green = new cv.Scalar(0, 255, 0, 255);
97
- let ordered = [tl, tr, br, bl];
98
- for (let i = 0; i < 4; i++) {
99
- let a = ordered[i];
100
- let b = ordered[(i + 1) % 4];
101
- cv.line(debugRgba, new cv.Point(a.x, a.y), new cv.Point(b.x, b.y), green, 3);
102
- }
103
- for (let pt of ordered) {
104
- cv.circle(debugRgba, new cv.Point(pt.x, pt.y), 8, green, -1);
105
- }
106
84
  }
107
85
 
108
- let debugCanvas = document.createElement("canvas");
109
- cv.imshow(debugCanvas, debugRgba);
110
- debugRgba.delete();
111
-
112
- return { quad, debugCanvas };
86
+ return quad;
113
87
  } finally {
114
88
  gray.delete();
115
89
  blurred.delete();
@@ -122,8 +96,6 @@ function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): FindQuadResu
122
96
 
123
97
  interface UseEdgeDetectionResult {
124
98
  corners: Quad | null;
125
- /** Data URL of the Canny edge image, for debug visualisation */
126
- debugImageUrl: string | null;
127
99
  }
128
100
 
129
101
  /**
@@ -132,36 +104,24 @@ interface UseEdgeDetectionResult {
132
104
  */
133
105
  export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
134
106
  let [corners, setCorners] = useState<Quad | null>(null);
135
- let [debugImageUrl, setDebugImageUrl] = useState<string | null>(null);
136
107
 
137
108
  useEffect(() => {
138
- console.log(
139
- "[useEdgeDetection] effect: enabled=",
140
- enabled,
141
- "cv=",
142
- typeof cv,
143
- "canvas=",
144
- canvas?.width,
145
- "x",
146
- canvas?.height,
147
- );
148
109
  if (!enabled || !cv || !canvas) return;
149
110
 
150
111
  try {
151
- console.log("[useEdgeDetection] running findLargestQuad...");
152
112
  let src = cv.imread(canvas);
153
- let { quad, debugCanvas } = findLargestQuad(cv, src);
154
- src.delete();
155
- console.log("[useEdgeDetection] quad=", quad);
156
- setDebugImageUrl(debugCanvas.toDataURL());
113
+ let quad: Quad | null;
114
+ try {
115
+ quad = findLargestQuad(cv, src);
116
+ } finally {
117
+ src.delete();
118
+ }
157
119
  setCorners(quad ?? fullImageQuad(canvas.width, canvas.height));
158
120
  } catch (err) {
159
- console.error("[useEdgeDetection] error:", err);
160
- if (canvas) {
161
- setCorners(fullImageQuad(canvas.width, canvas.height));
162
- }
121
+ console.warn("[ImageEditor] Edge detection failed, falling back to the full image:", err);
122
+ setCorners(fullImageQuad(canvas.width, canvas.height));
163
123
  }
164
124
  }, [cv, canvas, enabled]);
165
125
 
166
- return { corners, debugImageUrl };
126
+ return { corners };
167
127
  }
@@ -17,11 +17,16 @@ interface CurrentState {
17
17
  export interface UseImageHistoryResult {
18
18
  currentImageUrl: string;
19
19
  currentBlob: Blob | null;
20
+ /** Synchronous read of the current image URL — valid immediately after `applyActions`. */
21
+ getCurrentImageUrl: () => string;
22
+ /** Synchronous read of the current blob — valid immediately after `applyActions`. */
23
+ getCurrentBlob: () => Blob | null;
20
24
  canUndo: boolean;
21
25
  canRedo: boolean;
22
26
  /** True while an async replay (undo/redo) is in progress. */
23
27
  replaying: boolean;
24
- applyActions: (blob: Blob, actions: EditAction[]) => void;
28
+ /** Applies a new blob to the current state and records it in history. Returns the new object URL. */
29
+ applyActions: (blob: Blob, actions: EditAction[]) => string;
25
30
  undo: () => void;
26
31
  redo: () => void;
27
32
  reset: () => void;
@@ -41,20 +46,25 @@ export function useImageHistory(sourceUrl: string, registry: ActionRegistry): Us
41
46
 
42
47
  let historyRef = useRef(history);
43
48
  historyRef.current = history;
49
+ // Written synchronously by `replaceCurrent` so imperative callers can read the current
50
+ // image/blob before React has committed the corresponding render.
44
51
  let currentRef = useRef(current);
45
- currentRef.current = current;
46
52
  let isReplaying = useRef(false);
47
53
  let replayAbort = useRef<AbortController | null>(null);
48
54
 
55
+ function replaceCurrent(next: CurrentState) {
56
+ let prev = currentRef.current;
57
+ if (prev.generated && prev.url !== next.url) URL.revokeObjectURL(prev.url);
58
+ currentRef.current = next;
59
+ setCurrent(next);
60
+ }
61
+
49
62
  // When source changes (e.g. component remount with new image), reset fully.
50
63
  useEffect(() => {
51
64
  replayAbort.current?.abort();
52
65
  isReplaying.current = false;
53
66
  setReplaying(false);
54
- setCurrent((prev) => {
55
- if (prev.generated) URL.revokeObjectURL(prev.url);
56
- return { url: sourceUrl, blob: null, generated: false };
57
- });
67
+ replaceCurrent({ url: sourceUrl, blob: null, generated: false });
58
68
  setHistory({ past: [], future: [] });
59
69
  }, [sourceUrl]);
60
70
 
@@ -72,11 +82,10 @@ export function useImageHistory(sourceUrl: string, registry: ActionRegistry): Us
72
82
  setReplaying(false);
73
83
 
74
84
  let newUrl = URL.createObjectURL(blob);
75
- setCurrent((prev) => {
76
- if (prev.generated) URL.revokeObjectURL(prev.url);
77
- return { url: newUrl, blob, generated: true };
78
- });
85
+ replaceCurrent({ url: newUrl, blob, generated: true });
79
86
  setHistory((prev) => ({ past: [...prev.past, actions], future: [] }));
87
+
88
+ return newUrl;
80
89
  }
81
90
 
82
91
  async function doReplay(newPast: EditAction[][], newFuture: EditAction[][]) {
@@ -90,11 +99,11 @@ export function useImageHistory(sourceUrl: string, registry: ActionRegistry): Us
90
99
  let result = await replayAll(sourceUrl, newPast, registry, abort.signal);
91
100
  if (abort.signal.aborted) return;
92
101
 
93
- setCurrent((prev) => {
94
- if (prev.generated) URL.revokeObjectURL(prev.url);
95
- if (result) return { url: result.url, blob: result.blob, generated: true };
96
- return { url: sourceUrl, blob: null, generated: false };
97
- });
102
+ if (result) {
103
+ replaceCurrent({ url: result.url, blob: result.blob, generated: true });
104
+ } else {
105
+ replaceCurrent({ url: sourceUrl, blob: null, generated: false });
106
+ }
98
107
  setHistory({ past: newPast, future: newFuture });
99
108
  } finally {
100
109
  if (!abort.signal.aborted) {
@@ -122,16 +131,15 @@ export function useImageHistory(sourceUrl: string, registry: ActionRegistry): Us
122
131
  replayAbort.current?.abort();
123
132
  isReplaying.current = false;
124
133
  setReplaying(false);
125
- setCurrent((prev) => {
126
- if (prev.generated) URL.revokeObjectURL(prev.url);
127
- return { url: sourceUrl, blob: null, generated: false };
128
- });
134
+ replaceCurrent({ url: sourceUrl, blob: null, generated: false });
129
135
  setHistory({ past: [], future: [] });
130
136
  }
131
137
 
132
138
  return {
133
139
  currentImageUrl: current.url,
134
140
  currentBlob: current.blob,
141
+ getCurrentImageUrl: () => currentRef.current.url,
142
+ getCurrentBlob: () => currentRef.current.blob,
135
143
  canUndo: history.past.length > 0,
136
144
  canRedo: history.future.length > 0,
137
145
  replaying,
@@ -1,23 +1,53 @@
1
1
  import { useEffect, useState } from "react";
2
2
 
3
- let loadPromise: Promise<unknown> | null = null;
4
-
5
3
  declare global {
6
4
  interface Window {
5
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
7
6
  cv: any;
8
7
  }
9
8
  }
10
9
 
11
- function waitForCvMat(timeoutMs = 30_000): Promise<void> {
10
+ /**
11
+ * OpenCV.js builds to try, in order. Both are single-file builds with the WASM embedded,
12
+ * so no companion `.wasm` request is needed.
13
+ *
14
+ * Note: opencv.org only publishes rolling branch builds — pinned version paths such as
15
+ * `/4.10.0/opencv.js` were removed upstream and now return 404. `5.x` is the current
16
+ * development line and `4.x` the stable line; the 4.x entry is a fallback for the case
17
+ * where the 5.x build is temporarily broken or unavailable.
18
+ */
19
+ export const DEFAULT_OPENCV_URLS = ["https://docs.opencv.org/5.x/opencv.js", "https://docs.opencv.org/4.x/opencv.js"];
20
+
21
+ /** Milliseconds to wait for the WASM runtime to initialise before giving up on a build. */
22
+ const INIT_TIMEOUT_MS = 60_000;
23
+
24
+ let scriptUrls = [...DEFAULT_OPENCV_URLS];
25
+ let loadPromise: Promise<void> | null = null;
26
+
27
+ /**
28
+ * Overrides which OpenCV.js build(s) to load — e.g. to self-host instead of using the CDN.
29
+ * Must be called before anything triggers a load; ignored once loading has started.
30
+ */
31
+ export function configureOpenCV(urls: string | string[]) {
32
+ if (loadPromise) {
33
+ console.warn("[OpenCV] configureOpenCV() ignored — OpenCV is already loading or loaded");
34
+ return;
35
+ }
36
+ scriptUrls = Array.isArray(urls) ? [...urls] : [urls];
37
+ }
38
+
39
+ function isReady() {
40
+ return typeof window.cv?.Mat !== "undefined";
41
+ }
42
+
43
+ /** Polls for cv.Mat, which signals that the WASM runtime has fully initialised. */
44
+ function waitForCvMat(timeoutMs: number): Promise<void> {
12
45
  return new Promise((resolve, reject) => {
13
46
  let elapsed = 0;
14
47
  let interval = setInterval(() => {
15
- console.log("[OpenCV] polling... cv=", typeof window.cv, "cv.Mat=", typeof window.cv?.Mat);
16
- // cv.Mat signals that the WASM runtime has fully initialised
17
- if (window.cv?.Mat) {
48
+ if (isReady()) {
18
49
  clearInterval(interval);
19
- console.log("[OpenCV] cv.Mat found — ready!");
20
- resolve(); // callers read window.cv directly
50
+ resolve();
21
51
  return;
22
52
  }
23
53
  elapsed += 100;
@@ -29,76 +59,94 @@ function waitForCvMat(timeoutMs = 30_000): Promise<void> {
29
59
  });
30
60
  }
31
61
 
32
- export function loadOpenCV(): Promise<unknown> {
33
- if (loadPromise) {
34
- console.log("[OpenCV] loadOpenCV() — reusing existing promise");
35
- return loadPromise;
36
- }
37
-
38
- // Already initialised (e.g. hot-reload)
39
- if (window.cv?.Mat) {
40
- console.log("[OpenCV] already initialised on window.cv");
41
- loadPromise = Promise.resolve(window.cv);
42
- return loadPromise;
43
- }
62
+ /** Loads a single OpenCV.js build, handling both the promise-based and polling Emscripten builds. */
63
+ function loadScript(url: string): Promise<void> {
64
+ return new Promise<void>((resolve, reject) => {
65
+ // A previous failed attempt may have left a half-initialised global behind.
66
+ if (!isReady()) window.cv = undefined;
44
67
 
45
- console.log("[OpenCV] injecting script tag...");
46
- loadPromise = new Promise<unknown>((resolve, reject) => {
47
68
  let script = document.createElement("script");
48
- script.src = "https://docs.opencv.org/4.10.0/opencv.js";
69
+ script.src = url;
49
70
  script.async = true;
50
71
 
72
+ let settle = (err?: Error) => {
73
+ if (err) {
74
+ script.remove();
75
+ reject(err);
76
+ } else {
77
+ resolve();
78
+ }
79
+ };
80
+
51
81
  script.onload = () => {
52
- console.log(
53
- "[OpenCV] script onload fired. typeof window.cv=",
54
- typeof window.cv,
55
- "cv?.then=",
56
- typeof window.cv?.then,
57
- "cv?.Mat=",
58
- typeof window.cv?.Mat,
59
- );
60
82
  if (typeof window.cv?.then === "function") {
61
- console.log("[OpenCV] promise-based build detected, awaiting cv...");
62
83
  // Emscripten promise-based build: cv itself is the promise.
63
- // IMPORTANT: we must NOT pass `instance` into resolve() — if the resolved
64
- // value is itself thenable, Promise will follow it forever and never settle.
65
- // Instead we mutate window.cv and signal readiness with undefined.
84
+ // IMPORTANT: never resolve *with* the instance — if the resolved value is
85
+ // itself thenable, the promise chain follows it forever and never settles.
86
+ // Instead mutate window.cv and signal readiness with undefined.
66
87
  (window.cv as Promise<unknown>).then(
67
88
  (instance) => {
68
- console.log(
69
- "[OpenCV] promise resolved, instance=",
70
- typeof instance,
71
- "hasReadMethod=",
72
- typeof (instance as any)?.imread,
73
- );
74
89
  window.cv = instance;
75
- resolve(undefined); // signal only; callers read window.cv
76
- },
77
- (err) => {
78
- console.error("[OpenCV] promise rejected", err);
79
- reject(err);
90
+ settle();
80
91
  },
92
+ (err: Error) => settle(err instanceof Error ? err : new Error(String(err))),
81
93
  );
94
+ } else if (isReady()) {
95
+ settle();
82
96
  } else {
83
- console.log("[OpenCV] polling-based build, starting waitForCvMat...");
84
- // Polling build: WASM initialises asynchronously after script loads
85
- waitForCvMat().then(() => resolve(undefined), reject);
97
+ // Polling build: the WASM initialises asynchronously after the script loads.
98
+ waitForCvMat(INIT_TIMEOUT_MS).then(() => settle(), settle);
86
99
  }
87
100
  };
88
101
 
89
- script.onerror = (err) => {
90
- console.error("[OpenCV] script load error", err);
91
- loadPromise = null;
92
- reject(new Error("Failed to load OpenCV.js"));
93
- };
102
+ script.onerror = () => settle(new Error(`Failed to load OpenCV.js from ${url}`));
94
103
 
95
104
  document.head.appendChild(script);
96
105
  });
106
+ }
107
+
108
+ /**
109
+ * Lazily loads OpenCV.js, trying each configured build in order.
110
+ * Rejects only when every candidate fails. Callers read `window.cv`.
111
+ *
112
+ * IMPORTANT: this promise must never resolve *with* the OpenCV module. Emscripten's
113
+ * module object has its own `.then` method, so it is thenable — resolving a promise
114
+ * with it makes the promise machinery try to adopt it and call `.then` forever, which
115
+ * wedges the whole tab in an infinite microtask loop. Always resolve with `undefined`
116
+ * and let callers read `window.cv`.
117
+ */
118
+ export function loadOpenCV(): Promise<void> {
119
+ if (loadPromise) return loadPromise;
120
+
121
+ // Already initialised (e.g. hot-reload, or loaded by the host page)
122
+ if (isReady()) {
123
+ loadPromise = Promise.resolve();
124
+ return loadPromise;
125
+ }
126
+
127
+ loadPromise = (async () => {
128
+ let errors: Error[] = [];
129
+ for (let url of scriptUrls) {
130
+ try {
131
+ await loadScript(url);
132
+ if (!isReady()) throw new Error(`OpenCV.js loaded from ${url} but exposes no cv.Mat`);
133
+ return; // never `return window.cv` — see the note above
134
+ } catch (err) {
135
+ let error = err instanceof Error ? err : new Error(String(err));
136
+ errors.push(error);
137
+ console.warn(`[OpenCV] Could not load ${url}:`, error.message);
138
+ }
139
+ }
140
+ // Allow a later retry (e.g. after the network comes back) rather than caching the failure.
141
+ loadPromise = null;
142
+ throw new Error(`Failed to load OpenCV.js from: ${scriptUrls.join(", ")}`, { cause: errors });
143
+ })();
97
144
 
98
145
  return loadPromise;
99
146
  }
100
147
 
101
148
  interface UseOpenCVResult {
149
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
102
150
  cv: any | null;
103
151
  ready: boolean;
104
152
  error: Error | null;
@@ -106,28 +154,30 @@ interface UseOpenCVResult {
106
154
 
107
155
  /** Lazily loads OpenCV.js from CDN. Starts loading only when `enabled` is true. */
108
156
  export function useOpenCV(enabled = true): UseOpenCVResult {
157
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
109
158
  let [cv, setCv] = useState<any | null>(null);
110
159
  let [ready, setReady] = useState(false);
111
160
  let [error, setError] = useState<Error | null>(null);
112
161
 
113
162
  useEffect(() => {
114
163
  if (!enabled) return;
115
- console.log("[useOpenCV] enabled=true, calling loadOpenCV()");
164
+ let cancelled = false;
116
165
  loadOpenCV()
117
166
  .then(() => {
118
- console.log(
119
- "[useOpenCV] loadOpenCV resolved. window.cv=",
120
- typeof window.cv,
121
- "imread=",
122
- typeof window.cv?.imread,
123
- );
167
+ if (cancelled) return;
124
168
  setCv(window.cv);
125
169
  setReady(true);
126
170
  })
127
171
  .catch((err: Error) => {
128
- console.error("[useOpenCV] loadOpenCV rejected:", err);
172
+ if (cancelled) return;
173
+ // OpenCV only powers optional features (border suggestion, perspective warp),
174
+ // so a load failure must never break the tools that depend on it.
175
+ console.warn("[useOpenCV] OpenCV failed to load — continuing without it:", err.message);
129
176
  setError(err);
130
177
  });
178
+ return () => {
179
+ cancelled = true;
180
+ };
131
181
  }, [enabled]);
132
182
 
133
183
  return { cv, ready, error };
@@ -9,7 +9,6 @@ $line-width: 0.2em;
9
9
  }
10
10
 
11
11
  .ObLinkedList-root {
12
-
13
12
  list-style-type: none;
14
13
  margin-left: 0;
15
14
  padding-left: 0;
@@ -53,7 +52,10 @@ $line-width: 0.2em;
53
52
  left: 0;
54
53
  top: 50%;
55
54
 
56
- transform: translate(calc(-1 * (var(--ob-linked-list-bullet-size) / 2) + (var(--ob-linked-list-line-width) / 2)), -50%);
55
+ transform: translate(
56
+ calc(-1 * (var(--ob-linked-list-bullet-size) / 2) + (var(--ob-linked-list-line-width) / 2)),
57
+ -50%
58
+ );
57
59
  }
58
60
 
59
61
  .ObLinkedList-card {
@@ -19,7 +19,8 @@
19
19
  }
20
20
 
21
21
  &::-webkit-progress-value {
22
- background-image: linear-gradient(
22
+ background-image:
23
+ linear-gradient(
23
24
  135deg,
24
25
  transparent,
25
26
  transparent 33%,
@@ -63,7 +64,8 @@
63
64
  }
64
65
 
65
66
  &::-moz-progress-bar {
66
- background-image: linear-gradient(
67
+ background-image:
68
+ linear-gradient(
67
69
  135deg,
68
70
  transparent,
69
71
  transparent 33%,
@@ -1,14 +1,13 @@
1
1
  import "./index.scss";
2
2
 
3
+ import { createSettingsStore } from "@olenbetong/appframe-react";
4
+
3
5
  import clsx from "clsx";
4
- import localforage from "localforage";
5
6
  import type React from "react";
6
7
  import { Children, forwardRef, useEffect, useRef, useState } from "react";
7
8
 
8
9
  const article = globalThis.af?.article?.id ?? globalThis.window?.location.pathname.substring(1).split("/")[0];
9
- const settingsStore = localforage.createInstance({
10
- name: `AfSplitContainers.${globalThis.af?.userSession.login ?? "anonymous"}`,
11
- });
10
+ const settingsStore = createSettingsStore(`AfSplitContainers.${globalThis.af?.userSession?.login ?? "anonymous"}`);
12
11
 
13
12
  type SplitContainerSettings = {
14
13
  layoutVersion: string | number;
@@ -0,0 +1,79 @@
1
+ import { ChipRemovable, Textfield } from "@digdir/designsystemet-react";
2
+ import { getLocalizedString } from "@olenbetong/appframe-core";
3
+ import { useState } from "react";
4
+
5
+ export type ChipListInputProps = {
6
+ value: string[];
7
+ onChange: (value: string[]) => void;
8
+ readOnly?: boolean;
9
+ "aria-label": string;
10
+ "data-size"?: "sm" | "md" | "lg";
11
+ type?: "text" | "number" | "date" | "datetime-local";
12
+ };
13
+
14
+ /**
15
+ * Free text editor for the list operators. Values are added with Enter and
16
+ * rendered as removable chips.
17
+ */
18
+ export function ChipListInput({
19
+ value,
20
+ onChange,
21
+ readOnly,
22
+ type = "text",
23
+ "aria-label": ariaLabel,
24
+ "data-size": size,
25
+ }: ChipListInputProps) {
26
+ let [draft, setDraft] = useState("");
27
+
28
+ function commit() {
29
+ let trimmed = draft.trim();
30
+ if (!trimmed || value.includes(trimmed)) {
31
+ setDraft("");
32
+ return;
33
+ }
34
+
35
+ onChange([...value, trimmed]);
36
+ setDraft("");
37
+ }
38
+
39
+ function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
40
+ if (event.key === "Enter") {
41
+ // The editor is often rendered inside a dialog; committing a value
42
+ // must not submit or close it.
43
+ event.preventDefault();
44
+ commit();
45
+ } else if (event.key === "Backspace" && draft === "" && value.length > 0) {
46
+ onChange(value.slice(0, -1));
47
+ }
48
+ }
49
+
50
+ return (
51
+ <div className="ObFilterEditor-chipList">
52
+ {value.map((item, index) => (
53
+ <ChipRemovable
54
+ // Values are deduplicated on add, so the value is a stable key.
55
+ key={item}
56
+ data-size={size}
57
+ disabled={readOnly}
58
+ aria-label={`${getLocalizedString("Remove")} ${item}`}
59
+ onClick={() => onChange(value.filter((_, i) => i !== index))}
60
+ >
61
+ {item}
62
+ </ChipRemovable>
63
+ ))}
64
+ {!readOnly && (
65
+ <Textfield
66
+ className="ObFilterEditor-chipListInput"
67
+ aria-label={ariaLabel}
68
+ data-size={size}
69
+ type={type}
70
+ value={draft}
71
+ placeholder={getLocalizedString("Add value")}
72
+ onChange={(event) => setDraft(event.target.value)}
73
+ onKeyDown={handleKeyDown}
74
+ onBlur={commit}
75
+ />
76
+ )}
77
+ </div>
78
+ );
79
+ }