@olenbetong/synergi-react 3.2.2 → 4.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 (157) hide show
  1. package/es/chrome/AppShell/index.d.ts +10 -2
  2. package/es/chrome/AppShell/index.js +13 -97
  3. package/es/chrome/openReport.d.ts +22 -0
  4. package/es/chrome/openReport.js +21 -0
  5. package/es/colorScheme.d.ts +4 -4
  6. package/es/components/DateNavigator/index.js +1 -1
  7. package/es/components/FilePond.js +1 -1
  8. package/es/components/ImageEditor/ImageEditor.js +1 -1
  9. package/es/components/ImageEditor/plugins/cropPlugin/CropOverlay.js +1 -1
  10. package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
  11. package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
  12. package/es/components/ImageEditor/plugins/zoomPlugin/ZoomControls.js +1 -1
  13. package/es/components/MergeProgressDialog/index.js +1 -1
  14. package/es/components/Toolbar/index.d.ts +20 -11
  15. package/es/components/Toolbar/index.js +47 -6
  16. package/es/elements/ElementDamage/DamageFormButtons.js +1 -1
  17. package/es/elements/ElementDamage/DescriptionInput.js +1 -1
  18. package/es/elements/ElementDamage/DuplicateDamage/DesktopView.js +1 -1
  19. package/es/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.js +1 -1
  20. package/es/elements/ElementDamage/DuplicateDamage/PhoneView.js +1 -1
  21. package/es/elements/ElementDamage/ElementNumberTextField.js +1 -1
  22. package/es/elements/ElementDamage/RegisterFix.js +1 -1
  23. package/es/elements/ElementDamage/SubmitButtons.js +1 -1
  24. package/es/elements/ElementDamage/TransportElementSelect.js +1 -1
  25. package/es/elements/ElementLookup.d.ts +7 -2
  26. package/es/elements/ElementLookup.js +5 -7
  27. package/es/elements/ElementLookupDialog/BarcodeScanner/decoder.d.ts +32 -0
  28. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.d.ts +28 -0
  29. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.js +81 -0
  30. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.d.ts +26 -0
  31. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.js +70 -0
  32. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.d.ts +20 -0
  33. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +69 -0
  34. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +103 -0
  35. package/es/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.d.ts +6 -0
  36. package/es/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.js +19 -0
  37. package/es/elements/ElementLookupDialog/BarcodeScanner/index.d.ts +14 -7
  38. package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +11 -7
  39. package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.d.ts +14 -12
  40. package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.js +223 -117
  41. package/es/elements/ElementLookupDialog/HIDScanner/index.js +1 -1
  42. package/es/elements/ElementLookupDialog/Manual/ManualForm.js +1 -1
  43. package/es/elements/ElementLookupDialog/index.d.ts +6 -0
  44. package/es/elements/ElementLookupDialog/index.js +3 -3
  45. package/es/elements/ElementLookupDialog/utils/Giai.js +1 -1
  46. package/es/elements/ProjectSelect.js +1 -1
  47. package/es/i18n.d.ts +6 -0
  48. package/es/i18n.js +10 -0
  49. package/es/index.d.ts +3 -4
  50. package/es/index.js +5 -4
  51. package/package.json +9 -8
  52. package/src/chrome/AppShell/index.tsx +13 -134
  53. package/src/chrome/openReport.ts +34 -0
  54. package/src/colorScheme.ts +4 -4
  55. package/src/components/DateNavigator/index.tsx +1 -1
  56. package/src/components/FilePond.tsx +1 -1
  57. package/src/components/ImageEditor/ImageEditor.tsx +1 -1
  58. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +1 -1
  59. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +1 -1
  60. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +1 -1
  61. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +1 -1
  62. package/src/components/MergeProgressDialog/index.tsx +1 -1
  63. package/src/components/Toolbar/index.tsx +65 -14
  64. package/src/elements/ElementDamage/DamageFormButtons.tsx +1 -1
  65. package/src/elements/ElementDamage/DescriptionInput.tsx +1 -1
  66. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +1 -1
  67. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +1 -1
  68. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +1 -1
  69. package/src/elements/ElementDamage/ElementNumberTextField.tsx +1 -1
  70. package/src/elements/ElementDamage/RegisterFix.tsx +1 -1
  71. package/src/elements/ElementDamage/SubmitButtons.tsx +1 -1
  72. package/src/elements/ElementDamage/TransportElementSelect.tsx +1 -1
  73. package/src/elements/ElementLookup.tsx +13 -6
  74. package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -0
  75. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -0
  76. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -0
  77. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -0
  78. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -0
  79. package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -0
  80. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +30 -15
  81. package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +247 -137
  82. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +1 -1
  83. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +1 -1
  84. package/src/elements/ElementLookupDialog/index.tsx +13 -5
  85. package/src/elements/ElementLookupDialog/utils/Giai.ts +1 -1
  86. package/src/elements/ProjectSelect.tsx +1 -1
  87. package/src/i18n.ts +13 -0
  88. package/src/index.ts +6 -37
  89. package/es/chrome/AppMenu/AppMenu.css +0 -648
  90. package/es/chrome/AppMenu/AppMenu.d.ts +0 -56
  91. package/es/chrome/AppMenu/AppMenu.js +0 -145
  92. package/es/chrome/AppMenu/AppMenuColorScheme.d.ts +0 -2
  93. package/es/chrome/AppMenu/AppMenuColorScheme.js +0 -18
  94. package/es/chrome/AppMenu/AppMenuFavoriteButton.d.ts +0 -18
  95. package/es/chrome/AppMenu/AppMenuFavoriteButton.js +0 -11
  96. package/es/chrome/AppMenu/AppMenuHeader.d.ts +0 -24
  97. package/es/chrome/AppMenu/AppMenuHeader.js +0 -18
  98. package/es/chrome/AppMenu/AppMenuList.d.ts +0 -18
  99. package/es/chrome/AppMenu/AppMenuList.js +0 -39
  100. package/es/chrome/AppMenu/AppMenuRail.d.ts +0 -26
  101. package/es/chrome/AppMenu/AppMenuRail.js +0 -30
  102. package/es/chrome/AppMenu/AppMenuToggle.d.ts +0 -15
  103. package/es/chrome/AppMenu/AppMenuToggle.js +0 -14
  104. package/es/chrome/AppMenu/AppMenuUserMenu.d.ts +0 -32
  105. package/es/chrome/AppMenu/AppMenuUserMenu.js +0 -110
  106. package/es/chrome/AppMenu/developerCategories.d.ts +0 -24
  107. package/es/chrome/AppMenu/developerCategories.js +0 -116
  108. package/es/chrome/AppMenu/icons.d.ts +0 -15
  109. package/es/chrome/AppMenu/icons.js +0 -13
  110. package/es/chrome/AppMenu/index.d.ts +0 -12
  111. package/es/chrome/AppMenu/index.js +0 -10
  112. package/es/chrome/AppMenu/menuEntries.d.ts +0 -51
  113. package/es/chrome/AppMenu/menuEntries.js +0 -93
  114. package/es/chrome/AppMenu/types.d.ts +0 -43
  115. package/es/chrome/AppMenu/useFavorites.d.ts +0 -13
  116. package/es/chrome/AppMenu/useFavorites.js +0 -28
  117. package/es/chrome/AppMenu/useRovingTabIndex.d.ts +0 -28
  118. package/es/chrome/AppMenu/useRovingTabIndex.js +0 -114
  119. package/es/chrome/AppMenu/useServiceWorkerUpdate.d.ts +0 -16
  120. package/es/chrome/AppMenu/useServiceWorkerUpdate.js +0 -62
  121. package/es/report/ReportLauncher.css +0 -147
  122. package/es/report/ReportLauncher.d.ts +0 -55
  123. package/es/report/ReportLauncher.js +0 -142
  124. package/es/report/ReportLauncher.test.js +0 -231
  125. package/es/report/ReportParameters.css +0 -29
  126. package/es/report/ReportParameters.d.ts +0 -46
  127. package/es/report/ReportParameters.js +0 -65
  128. package/es/report/ReportParameters.test.d.ts +0 -1
  129. package/es/report/ReportParameters.test.js +0 -84
  130. package/es/report/index.d.ts +0 -2
  131. package/es/report/index.js +0 -2
  132. package/src/chrome/AppMenu/AppMenu.css +0 -648
  133. package/src/chrome/AppMenu/AppMenu.tsx +0 -285
  134. package/src/chrome/AppMenu/AppMenuColorScheme.tsx +0 -41
  135. package/src/chrome/AppMenu/AppMenuFavoriteButton.tsx +0 -47
  136. package/src/chrome/AppMenu/AppMenuHeader.tsx +0 -79
  137. package/src/chrome/AppMenu/AppMenuList.tsx +0 -89
  138. package/src/chrome/AppMenu/AppMenuRail.tsx +0 -117
  139. package/src/chrome/AppMenu/AppMenuToggle.tsx +0 -51
  140. package/src/chrome/AppMenu/AppMenuUserMenu.tsx +0 -190
  141. package/src/chrome/AppMenu/developerCategories.ts +0 -142
  142. package/src/chrome/AppMenu/icons.tsx +0 -76
  143. package/src/chrome/AppMenu/index.ts +0 -17
  144. package/src/chrome/AppMenu/menuEntries.ts +0 -128
  145. package/src/chrome/AppMenu/types.ts +0 -45
  146. package/src/chrome/AppMenu/useFavorites.ts +0 -40
  147. package/src/chrome/AppMenu/useRovingTabIndex.ts +0 -130
  148. package/src/chrome/AppMenu/useServiceWorkerUpdate.ts +0 -68
  149. package/src/report/ReportLauncher.css +0 -147
  150. package/src/report/ReportLauncher.test.tsx +0 -281
  151. package/src/report/ReportLauncher.tsx +0 -420
  152. package/src/report/ReportParameters.css +0 -29
  153. package/src/report/ReportParameters.test.tsx +0 -133
  154. package/src/report/ReportParameters.tsx +0 -198
  155. package/src/report/index.ts +0 -2
  156. /package/es/{chrome/AppMenu/types.js → elements/ElementLookupDialog/BarcodeScanner/decoder.js} +0 -0
  157. /package/es/{report/ReportLauncher.test.d.ts → elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts} +0 -0
@@ -1,170 +1,280 @@
1
- import { getLocalizedString } from "@olenbetong/appframe-core";
2
-
3
- import type BarcodeReaderType from "dynamsoft-javascript-barcode";
4
- // import BarcodeReader from "dynamsoft-javascript-barcode";
5
1
  import { type RefObject, useEffect, useRef, useState } from "react";
2
+ import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "./decoder";
3
+ import { createZxingDecoder } from "./decoders/zxing";
4
+ import { createDuplicateFilter } from "./duplicateFilter";
5
+ import { getLocalizedString } from "../../../i18n.js";
6
6
 
7
- declare global {
8
- interface Window {
9
- BarcodeReader: typeof BarcodeReaderType;
10
- }
7
+ export type ScannerError = { message: string; icon: "unsupported" | "camera" | "generic" };
8
+
9
+ /** Longest side of the frame handed to the decoder. Larger frames cost decode time without helping DataMatrix reads. */
10
+ const MAX_FRAME_SIDE = 720;
11
+ /** Minimum time between two decode attempts. */
12
+ const DECODE_INTERVAL = 100;
13
+ /** A code that stays in view is reported again only after it has been gone this long. */
14
+ const DUPLICATE_FORGET_TIME = 1000;
15
+ /** How long the highlight of a detected code stays on the overlay. */
16
+ const HIGHLIGHT_TIME = 300;
17
+
18
+ const CAMERA_ERROR_NAMES = [
19
+ "NotAllowedError",
20
+ "NotFoundError",
21
+ "NotReadableError",
22
+ "OverconstrainedError",
23
+ "SecurityError",
24
+ "AbortError",
25
+ "TypeError",
26
+ ];
27
+
28
+ const defaultDecoder = createZxingDecoder();
29
+
30
+ function videoConstraints(deviceId: string | null): MediaStreamConstraints {
31
+ let iPhone = /iPhone|iPad/.test(navigator.userAgent);
32
+ let size = iPhone ? 1280 : 720;
33
+
34
+ return {
35
+ audio: false,
36
+ video: {
37
+ width: { ideal: size },
38
+ height: { ideal: size },
39
+ ...(deviceId ? { deviceId: { exact: deviceId } } : { facingMode: { ideal: "environment" } }),
40
+ },
41
+ };
11
42
  }
12
43
 
13
- // (window as any).BarcodeReader = BarcodeReader;
14
- const { BarcodeReader } = window;
44
+ function scheduleFrame(video: HTMLVideoElement, callback: (now: number) => void) {
45
+ if ("requestVideoFrameCallback" in video) {
46
+ video.requestVideoFrameCallback((now) => callback(now));
47
+ } else {
48
+ requestAnimationFrame(callback);
49
+ }
50
+ }
15
51
 
52
+ /**
53
+ * Drives the camera and the decode loop behind `BarcodeScanner`. The decoder is created from the
54
+ * factory and replaced whenever a different factory is passed, so callers must pass a stable
55
+ * reference (a module-level constant or a memoised value) and change it only to switch engine.
56
+ */
16
57
  export function useScanner(
17
- rootRef: RefObject<HTMLElement | null>,
58
+ videoRef: RefObject<HTMLVideoElement | null>,
18
59
  canvasRef: RefObject<HTMLCanvasElement | null>,
19
60
  onRead: (barcode: string) => void,
61
+ decoderFactory: DecoderFactory = defaultDecoder,
20
62
  ) {
21
- const [isLoading, setIsLoading] = useState(true);
22
- const [error, setError] = useState<{ message: string; icon: string } | null>(null);
23
- const [rects, setRects] = useState<Array<BarcodeReaderType.LocalizationResult>>([]);
24
- const [devices, setDevices] = useState<BarcodeReaderType.ScannerOpenCallbackInfo>({
25
- all: [],
26
- current: { deviceId: "", label: "" },
27
- width: 0,
28
- height: 0,
29
- });
30
- const currentDevice = useRef<number>(null);
31
- const onReadRef = useRef(onRead);
32
- const scanner = useRef<BarcodeReaderType.Scanner>(null);
63
+ let [isLoading, setIsLoading] = useState(true);
64
+ let [error, setError] = useState<ScannerError | null>(null);
65
+ let [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
66
+ let [deviceId, setDeviceId] = useState<string | null>(null);
67
+ let [engine, setEngine] = useState<string | null>(null);
68
+ let [highlights, setHighlights] = useState<Array<DecodedBarcode["corners"]>>([]);
69
+
70
+ let onReadRef = useRef(onRead);
71
+ let decoderRef = useRef<BarcodeDecoder | null>(null);
72
+ let currentDeviceId = useRef<string | null>(null);
73
+ let highlightTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
33
74
 
34
75
  useEffect(() => {
35
76
  onReadRef.current = onRead;
36
77
  }, [onRead]);
37
78
 
79
+ // Create the decoder; replaced when a different factory is passed.
38
80
  useEffect(() => {
39
- if (!rootRef.current) return;
40
-
41
- let cancel = false;
42
- const iPhone = /iPhone|iPad/.test(navigator.userAgent);
43
- scanner.current = new BarcodeReader.Scanner({
44
- duplicateForgetTime: 1000,
45
- htmlElement: rootRef.current,
46
- videoSettings: {
47
- video: {
48
- width: { ideal: iPhone ? 1280 : 720 },
49
- height: { ideal: iPhone ? 1280 : 720 },
50
- facingMode: { ideal: "environment" },
51
- },
52
- },
53
- runtimeSettings: {
54
- mBarcodeFormatIds: BarcodeReader.EnumBarcodeFormat.DATAMATRIX,
55
- mAntiDamageLevel: 3,
56
- mDeblurLevel: 0,
57
- },
58
- onFrameRead: (results) => {
59
- const newRects: Array<BarcodeReaderType.LocalizationResult> = [];
60
-
61
- for (let result of results) {
62
- newRects.push(result.LocalizationResult);
63
- }
64
-
65
- if (newRects.length > 0) {
66
- setRects((rects) => [...rects, ...newRects]);
81
+ let cancelled = false;
82
+ let created: BarcodeDecoder | null = null;
83
+ setEngine(null);
84
+ setError(null);
67
85
 
68
- setTimeout(() => {
69
- if (!cancel) {
70
- setRects((rects) => rects.slice(newRects.length));
71
- }
72
- }, 300);
73
- }
74
- },
75
-
76
- onNewCodeRead: (txt) => {
77
- if (txt) {
78
- onReadRef.current(txt);
79
- }
80
- },
81
- });
82
-
83
- scanner.current
84
- .open()
85
- .then((videoDeviceInfo) => {
86
- if (cancel === false) {
87
- const { all, current } = videoDeviceInfo;
88
- currentDevice.current = 0;
89
-
90
- if (current?.deviceId) {
91
- for (let i = 0; i < all.length; i++) {
92
- if (all[i].deviceId === current.deviceId) {
93
- currentDevice.current = i;
94
- }
95
- }
96
- }
97
-
98
- setDevices(videoDeviceInfo);
99
- setIsLoading(false);
86
+ decoderFactory()
87
+ .then((decoder) => {
88
+ if (cancelled) {
89
+ decoder.dispose?.();
90
+ return;
100
91
  }
92
+ created = decoder;
93
+ decoderRef.current = decoder;
94
+ setEngine(decoder.name);
101
95
  })
102
- .catch((ex) => {
103
- if (cancel === false) {
104
- let error = ex.message || ex;
105
- setIsLoading(false);
106
-
107
- if (/Webassembly/.test(error)) {
108
- setError({
109
- icon: "unsupported",
110
- message: getLocalizedString(
111
- "Your browser is not supported. A browser that supports WebAssembly is required.",
112
- ),
113
- });
114
- } else if (
115
- /Permission/i.test(error) ||
116
- /video/i.test(error) ||
117
- /device/i.test(error) ||
118
- /Media/i.test(error) ||
119
- /agent/.test(error) ||
120
- /found/.test(error)
121
- )
122
- setError({
123
- icon: "camera",
124
- message: getLocalizedString("No camera available"),
125
- });
126
- else {
127
- setError({ icon: "generic", message: error });
128
- }
96
+ .catch((ex: unknown) => {
97
+ if (cancelled) return;
98
+ let message = ex instanceof Error ? ex.message : String(ex);
99
+ setIsLoading(false);
100
+
101
+ if (/WebAssembly/i.test(message)) {
102
+ setError({
103
+ icon: "unsupported",
104
+ message: getLocalizedString(
105
+ "Your browser is not supported. A browser that supports WebAssembly is required.",
106
+ ),
107
+ });
108
+ } else {
109
+ setError({ icon: "generic", message });
129
110
  }
130
111
  });
131
112
 
132
113
  return () => {
133
- cancel = true;
114
+ cancelled = true;
115
+ decoderRef.current = null;
116
+ created?.dispose?.();
117
+ };
118
+ }, [decoderFactory]);
119
+
120
+ // Start the camera and the decode loop; restarted when the user switches camera.
121
+ useEffect(() => {
122
+ let video = videoRef.current;
123
+ if (!video) return;
124
+
125
+ let cancelled = false;
126
+ let stream: MediaStream | null = null;
127
+ let frame = document.createElement("canvas");
128
+ let frameContext = frame.getContext("2d", { willReadFrequently: true });
129
+ let isNew = createDuplicateFilter(DUPLICATE_FORGET_TIME);
130
+ let busy = false;
131
+ let lastDecode = 0;
134
132
 
135
- if (scanner.current && typeof scanner.current.close === "function") {
136
- scanner.current.close();
133
+ function showHighlights(corners: Array<DecodedBarcode["corners"]>) {
134
+ setHighlights(corners);
135
+
136
+ if (highlightTimeout.current) clearTimeout(highlightTimeout.current);
137
+ highlightTimeout.current = setTimeout(() => {
138
+ if (!cancelled) setHighlights([]);
139
+ }, HIGHLIGHT_TIME);
140
+ }
141
+
142
+ async function decodeCurrentFrame(now: number) {
143
+ let decoder = decoderRef.current;
144
+ if (!decoder || !frameContext || !video || video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) return;
145
+
146
+ let scale = Math.min(1, MAX_FRAME_SIDE / Math.max(video.videoWidth, video.videoHeight));
147
+ let width = Math.round(video.videoWidth * scale);
148
+ let height = Math.round(video.videoHeight * scale);
149
+ if (width === 0 || height === 0) return;
150
+
151
+ if (frame.width !== width || frame.height !== height) {
152
+ frame.width = width;
153
+ frame.height = height;
137
154
  }
155
+ frameContext.drawImage(video, 0, 0, width, height);
156
+
157
+ let results = await decoder.decodeFrame(frame);
158
+ if (cancelled || results.length === 0) return;
159
+
160
+ // Highlights are drawn on an overlay sized like the video, so scale them back up.
161
+ let inverse = 1 / scale;
162
+ showHighlights(
163
+ results.map(
164
+ (result) =>
165
+ result.corners.map((point) => ({
166
+ x: point.x * inverse,
167
+ y: point.y * inverse,
168
+ })) as DecodedBarcode["corners"],
169
+ ),
170
+ );
171
+
172
+ for (let result of results) {
173
+ if (result.text && isNew(result.text, now)) {
174
+ onReadRef.current(result.text);
175
+ }
176
+ }
177
+ }
178
+
179
+ function tick(now: number) {
180
+ if (cancelled || !video) return;
181
+
182
+ if (!busy && now - lastDecode >= DECODE_INTERVAL) {
183
+ busy = true;
184
+ lastDecode = now;
185
+ decodeCurrentFrame(now)
186
+ .catch((ex: unknown) => {
187
+ console.warn("Barcode decode failed", ex);
188
+ })
189
+ .finally(() => {
190
+ busy = false;
191
+ });
192
+ }
193
+
194
+ scheduleFrame(video, tick);
195
+ }
196
+
197
+ async function start() {
198
+ if (!navigator.mediaDevices?.getUserMedia) {
199
+ throw new DOMException("Camera access is not available", "NotFoundError");
200
+ }
201
+
202
+ stream = await navigator.mediaDevices.getUserMedia(videoConstraints(deviceId));
203
+ if (cancelled || !video) return;
204
+
205
+ video.srcObject = stream;
206
+ await video.play();
207
+ if (cancelled) return;
208
+
209
+ let overlay = canvasRef.current;
210
+ if (overlay) {
211
+ overlay.width = video.videoWidth;
212
+ overlay.height = video.videoHeight;
213
+ }
214
+
215
+ currentDeviceId.current = stream.getVideoTracks()[0]?.getSettings().deviceId ?? deviceId;
216
+
217
+ // Labels and the full device list are only available once permission has been granted.
218
+ let all = await navigator.mediaDevices.enumerateDevices();
219
+ if (cancelled) return;
220
+
221
+ setDevices(all.filter((device) => device.kind === "videoinput"));
222
+ setIsLoading(false);
223
+ scheduleFrame(video, tick);
224
+ }
225
+
226
+ start().catch((ex: unknown) => {
227
+ if (cancelled) return;
228
+ setIsLoading(false);
229
+
230
+ let name = ex instanceof DOMException || ex instanceof Error ? ex.name : "";
231
+ let message = ex instanceof Error ? ex.message : String(ex);
232
+
233
+ if (CAMERA_ERROR_NAMES.includes(name) || /camera|video|device|media/i.test(message)) {
234
+ setError({ icon: "camera", message: getLocalizedString("No camera available") });
235
+ } else {
236
+ setError({ icon: "generic", message });
237
+ }
238
+ });
239
+
240
+ return () => {
241
+ cancelled = true;
242
+ if (highlightTimeout.current) clearTimeout(highlightTimeout.current);
243
+ stream?.getTracks().forEach((track) => track.stop());
244
+ if (video) video.srcObject = null;
138
245
  };
139
- }, [rootRef]);
246
+ }, [videoRef, canvasRef, deviceId]);
140
247
 
248
+ // Draw the highlights of detected codes on the overlay canvas.
141
249
  useEffect(() => {
142
- if (canvasRef.current) {
143
- const ctx = canvasRef.current.getContext("2d") as CanvasRenderingContext2D;
144
- ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);
145
- ctx.fillStyle = "rgba(254,180,32,0.3)";
146
-
147
- const { width, height } = canvasRef.current;
148
- const xRatio = width / devices.width;
149
- const yRatio = height / devices.height;
150
-
151
- for (let rect of rects) {
152
- const { X1, X2, X3, X4, Y1, Y2, Y3, Y4 } = rect;
153
-
154
- ctx.beginPath();
155
- ctx.moveTo(X1 * xRatio, Y1 * yRatio);
156
- ctx.lineTo(X2 * xRatio, Y2 * yRatio);
157
- ctx.lineTo(X3 * xRatio, Y3 * yRatio);
158
- ctx.lineTo(X4 * xRatio, Y4 * yRatio);
159
- ctx.fill();
160
- }
250
+ let overlay = canvasRef.current;
251
+ if (!overlay) return;
252
+
253
+ let context = overlay.getContext("2d");
254
+ if (!context) return;
255
+
256
+ context.clearRect(0, 0, overlay.width, overlay.height);
257
+ context.fillStyle = "rgba(254,180,32,0.3)";
258
+
259
+ for (let corners of highlights) {
260
+ context.beginPath();
261
+ context.moveTo(corners[0].x, corners[0].y);
262
+ context.lineTo(corners[1].x, corners[1].y);
263
+ context.lineTo(corners[2].x, corners[2].y);
264
+ context.lineTo(corners[3].x, corners[3].y);
265
+ context.closePath();
266
+ context.fill();
161
267
  }
162
- }, [devices, canvasRef, rects]);
268
+ }, [canvasRef, highlights]);
163
269
 
164
270
  function setNextDevice() {
165
- currentDevice.current = ((currentDevice.current ?? 0) + 1) % devices.all.length;
166
- scanner.current?.play(devices.all[currentDevice.current].deviceId);
271
+ if (devices.length <= 1) return;
272
+
273
+ let index = devices.findIndex((device) => device.deviceId === currentDeviceId.current);
274
+ let next = devices[(index + 1) % devices.length];
275
+
276
+ if (next) setDeviceId(next.deviceId);
167
277
  }
168
278
 
169
- return { devices: devices.all, error, isLoading, setNextDevice };
279
+ return { devices, engine, error, isLoading, setNextDevice };
170
280
  }
@@ -1,11 +1,11 @@
1
1
  import "./index.css";
2
2
 
3
3
  import { Box, Button } from "@mui/material";
4
- import { getLocalizedString } from "@olenbetong/appframe-core";
5
4
 
6
5
  import { useEffect, useRef, useState } from "react";
7
6
 
8
7
  import { useHIDScanner } from "./useHIDScanner";
8
+ import { getLocalizedString } from "../../../i18n.js";
9
9
 
10
10
  export function AddElementHIDScanner({ onScan }: { onScan: (id: string) => void }) {
11
11
  let [enabled, setEnabled] = useState(false);
@@ -1,10 +1,10 @@
1
1
  import Add from "@mui/icons-material/Add";
2
2
  import { Alert, Button, TextField } from "@mui/material";
3
- import { getLocalizedString } from "@olenbetong/appframe-core";
4
3
  import { useId, useRef, useState } from "react";
5
4
  import type { ProjectsRecord } from "../../data/dsProjects";
6
5
  import { ProjectSelect } from "../../ProjectSelect";
7
6
  import { type ElementDetails, getElement } from "../getElement";
7
+ import { getLocalizedString } from "../../../i18n.js";
8
8
 
9
9
  const isElementsOnly = /^([a-z]{2,4}[\s]*[0-9]{4,8}[\s,]*)+$/i;
10
10
  const matchElements = /([a-z]{2,4}[\s]*[0-9]{4,8})/gi;
@@ -4,16 +4,16 @@ import Edit from "@mui/icons-material/Edit";
4
4
  import QrCodeScanner from "@mui/icons-material/QrCodeScanner";
5
5
  import TapAndPlay from "@mui/icons-material/TapAndPlay";
6
6
  import { Alert, Box, Button, DialogActions, List, ListItem, ListItemText, Tab, Tabs } from "@mui/material";
7
- import { getLocalizedString, localize } from "@olenbetong/appframe-core";
8
7
  import { ResponsiveDialog } from "@olenbetong/appframe-mui";
9
- import { useEffect, useState } from "react";
8
+ import { type ReactNode, useEffect, useState } from "react";
10
9
  import { flushSync } from "react-dom";
11
- import { BarcodeScanner } from "./BarcodeScanner";
10
+ import { BarcodeScanner, type DecoderFactory } from "./BarcodeScanner";
12
11
  import { type AddElementSource, type ElementDetails, getElement } from "./getElement";
13
12
  import { AddElementHIDScanner } from "./HIDScanner";
14
13
  import { AddElementManualForm } from "./Manual/ManualForm";
15
14
  import { useBeeper } from "./utils/beep";
16
15
  import Giai from "./utils/Giai";
16
+ import { getLocalizedString, localize } from "../../i18n.js";
17
17
 
18
18
  export type AddElement = {
19
19
  element: ElementDetails;
@@ -29,6 +29,10 @@ export type ElementLookupDialogSharedProps = {
29
29
  initialTab?: InitialTabProps | null;
30
30
  useSearch?: boolean;
31
31
  allowEmptyElement?: boolean;
32
+ /** Engine for the camera tab. Defaults to zxing-wasm. */
33
+ decoder?: DecoderFactory;
34
+ /** Extra controls in the camera tab footer, e.g. an engine picker. */
35
+ scannerActions?: ReactNode;
32
36
  };
33
37
 
34
38
  export type ElementLookupDialogSingleModeProps = ElementLookupDialogSharedProps & {
@@ -44,7 +48,7 @@ export type ElementLookupDialogMultiModeProps = ElementLookupDialogSharedProps &
44
48
  export type ElementLookupDialogProps = ElementLookupDialogSingleModeProps | ElementLookupDialogMultiModeProps;
45
49
 
46
50
  export function ElementLookupDialog(props: ElementLookupDialogProps) {
47
- let { open, onClose, initialTab } = props;
51
+ let { open, onClose, initialTab, decoder, scannerActions } = props;
48
52
  let [tab, setTab] = useState<"camera" | "scanner" | "manual">("camera");
49
53
  let [elements, setElements] = useState<Array<AddElement>>([]);
50
54
  let [adding, setAdding] = useState(false);
@@ -124,7 +128,11 @@ export function ElementLookupDialog(props: ElementLookupDialogProps) {
124
128
  </Tabs>
125
129
  {tab === "camera" && (
126
130
  <Box sx={{ flexBasis: "12rem", flexGrow: 1, flexShrink: 2 }}>
127
- <BarcodeScanner onRead={(element) => handleElementAdd(element, "camera-barcode")} />
131
+ <BarcodeScanner
132
+ decoder={decoder}
133
+ actions={scannerActions}
134
+ onRead={(element) => handleElementAdd(element, "camera-barcode")}
135
+ />
128
136
  </Box>
129
137
  )}
130
138
  {tab === "manual" && (
@@ -1,4 +1,4 @@
1
- import { getLocalizedString } from "@olenbetong/appframe-core";
1
+ import { getLocalizedString } from "../../../i18n.js";
2
2
 
3
3
  export type GiaiProps = {
4
4
  assetReference: string | number | bigint;
@@ -1,9 +1,9 @@
1
1
  import { Autocomplete, ListItem, ListItemText, type SxProps, TextField, type Theme } from "@mui/material";
2
- import { getLocalizedString } from "@olenbetong/appframe-core";
3
2
  import { useDataWithFilter, useDebounce } from "@olenbetong/appframe-react";
4
3
  import { useMemo, useRef, useState } from "react";
5
4
  import { dsProjects, type ProjectsRecord } from "./data/dsProjects";
6
5
  import { dsReportProjects } from "./data/dsReportProjects";
6
+ import { getLocalizedString } from "../i18n.js";
7
7
 
8
8
  type ProjectSelectProps = {
9
9
  className?: string;
package/src/i18n.ts ADDED
@@ -0,0 +1,13 @@
1
+ /*
2
+ * The package's strings live in their own globalization project, `synergi-react`, so one
3
+ * translation covers every app and the package can word a term its own way. The templates
4
+ * render the dictionary as af.i18n["synergi-react"]; on a page that does not (a legacy
5
+ * template) the localizer falls back to the article's strings, exactly as before. See
6
+ * `createLocalizer` in @olenbetong/appframe-core.
7
+ */
8
+
9
+ import { createLocalizer } from "@olenbetong/appframe-core";
10
+
11
+ export const PROJECT = "synergi-react";
12
+
13
+ export const { getLocalizedString, localize } = createLocalizer(PROJECT);
package/src/index.ts CHANGED
@@ -1,31 +1,6 @@
1
- export {
2
- AppMenu,
3
- type AppMenuArticle,
4
- type AppMenuCategory,
5
- AppMenuColorScheme,
6
- AppMenuFavoriteButton,
7
- type AppMenuFavoriteButtonProps,
8
- AppMenuHeader,
9
- type AppMenuHeaderProps,
10
- type AppMenuLink,
11
- type AppMenuLinkGroup,
12
- AppMenuList,
13
- type AppMenuListProps,
14
- type AppMenuProps,
15
- AppMenuRail,
16
- type AppMenuRailProps,
17
- type AppMenuSelection,
18
- AppMenuToggle,
19
- type AppMenuToggleProps,
20
- type AppMenuUser,
21
- AppMenuUserMenu,
22
- type AppMenuUserMenuProps,
23
- createDeveloperCategories,
24
- defaultUserMenuItems,
25
- type DeveloperCategoryOptions,
26
- useServiceWorkerUpdate,
27
- } from "./chrome/AppMenu/index.js";
28
- export { AppShell, type AppShellProps } from "./chrome/AppShell/index.js";
1
+ // The app menu and the rest of the site chrome live in @olenbetong/synergi-chrome,
2
+ // loaded once per page by the site template rather than bundled into every app.
3
+ export { AppShell, type AppShellProps, hasSiteChrome } from "./chrome/AppShell/index.js";
29
4
  export { Checkbox, type CheckboxProps } from "./components/Checkbox/index.js";
30
5
  export {
31
6
  COLOR_SCHEME_CHANGE_EVENT,
@@ -67,14 +42,7 @@ export {
67
42
  } from "./filter/index.js";
68
43
  export { Card, LinkedList, LinkedListItem } from "./components/LinkedCardList/index.js";
69
44
  export { MergeProgressDialog, type MergeProgressDialogProps } from "./components/MergeProgressDialog/index.js";
70
- export {
71
- ReportLauncher,
72
- type ReportLauncherProps,
73
- type ReportLauncherTab,
74
- ReportParameterField,
75
- ReportParameters,
76
- type ReportParametersProps,
77
- } from "./report/index.js";
45
+ export { type OpenReportOptions, openReport } from "./chrome/openReport.js";
78
46
  export {
79
47
  BannerImage as PageBannerImage,
80
48
  type BannerImageProps,
@@ -94,11 +62,12 @@ export {
94
62
  export { Portal } from "./components/Portal/index.js";
95
63
  export {
96
64
  ToolbarBreadcrumbs,
65
+ ToolbarDrawerToggle,
97
66
  ToolbarPortal,
98
67
  type BreadcrumbItem,
99
68
  type ToolbarBreadcrumbsProps,
69
+ type ToolbarDrawerToggleProps,
100
70
  type ToolbarPortalProps,
101
- type ToolbarPortalSlot,
102
71
  } from "./components/Toolbar/index.js";
103
72
  export { ProgressBar } from "./components/ProgressBar/index.js";
104
73
  export { Sidebar, type SidebarProps } from "./components/Sidebar/index.js";