@olenbetong/synergi-react 3.2.2 → 4.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 (112) 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/Toolbar/index.d.ts +20 -11
  7. package/es/components/Toolbar/index.js +47 -6
  8. package/es/elements/ElementLookup.d.ts +7 -2
  9. package/es/elements/ElementLookup.js +4 -6
  10. package/es/elements/ElementLookupDialog/BarcodeScanner/decoder.d.ts +32 -0
  11. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.d.ts +28 -0
  12. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.js +81 -0
  13. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.d.ts +26 -0
  14. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.js +70 -0
  15. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.d.ts +20 -0
  16. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +69 -0
  17. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +103 -0
  18. package/es/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.d.ts +6 -0
  19. package/es/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.js +19 -0
  20. package/es/elements/ElementLookupDialog/BarcodeScanner/index.d.ts +14 -7
  21. package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +10 -6
  22. package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.d.ts +14 -12
  23. package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.js +222 -116
  24. package/es/elements/ElementLookupDialog/index.d.ts +6 -0
  25. package/es/elements/ElementLookupDialog/index.js +2 -2
  26. package/es/index.d.ts +3 -4
  27. package/es/index.js +5 -4
  28. package/package.json +9 -8
  29. package/src/chrome/AppShell/index.tsx +13 -134
  30. package/src/chrome/openReport.ts +34 -0
  31. package/src/colorScheme.ts +4 -4
  32. package/src/components/Toolbar/index.tsx +65 -14
  33. package/src/elements/ElementLookup.tsx +12 -5
  34. package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -0
  35. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -0
  36. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -0
  37. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -0
  38. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -0
  39. package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -0
  40. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +29 -14
  41. package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +246 -136
  42. package/src/elements/ElementLookupDialog/index.tsx +12 -4
  43. package/src/index.ts +6 -37
  44. package/es/chrome/AppMenu/AppMenu.css +0 -648
  45. package/es/chrome/AppMenu/AppMenu.d.ts +0 -56
  46. package/es/chrome/AppMenu/AppMenu.js +0 -145
  47. package/es/chrome/AppMenu/AppMenuColorScheme.d.ts +0 -2
  48. package/es/chrome/AppMenu/AppMenuColorScheme.js +0 -18
  49. package/es/chrome/AppMenu/AppMenuFavoriteButton.d.ts +0 -18
  50. package/es/chrome/AppMenu/AppMenuFavoriteButton.js +0 -11
  51. package/es/chrome/AppMenu/AppMenuHeader.d.ts +0 -24
  52. package/es/chrome/AppMenu/AppMenuHeader.js +0 -18
  53. package/es/chrome/AppMenu/AppMenuList.d.ts +0 -18
  54. package/es/chrome/AppMenu/AppMenuList.js +0 -39
  55. package/es/chrome/AppMenu/AppMenuRail.d.ts +0 -26
  56. package/es/chrome/AppMenu/AppMenuRail.js +0 -30
  57. package/es/chrome/AppMenu/AppMenuToggle.d.ts +0 -15
  58. package/es/chrome/AppMenu/AppMenuToggle.js +0 -14
  59. package/es/chrome/AppMenu/AppMenuUserMenu.d.ts +0 -32
  60. package/es/chrome/AppMenu/AppMenuUserMenu.js +0 -110
  61. package/es/chrome/AppMenu/developerCategories.d.ts +0 -24
  62. package/es/chrome/AppMenu/developerCategories.js +0 -116
  63. package/es/chrome/AppMenu/icons.d.ts +0 -15
  64. package/es/chrome/AppMenu/icons.js +0 -13
  65. package/es/chrome/AppMenu/index.d.ts +0 -12
  66. package/es/chrome/AppMenu/index.js +0 -10
  67. package/es/chrome/AppMenu/menuEntries.d.ts +0 -51
  68. package/es/chrome/AppMenu/menuEntries.js +0 -93
  69. package/es/chrome/AppMenu/types.d.ts +0 -43
  70. package/es/chrome/AppMenu/useFavorites.d.ts +0 -13
  71. package/es/chrome/AppMenu/useFavorites.js +0 -28
  72. package/es/chrome/AppMenu/useRovingTabIndex.d.ts +0 -28
  73. package/es/chrome/AppMenu/useRovingTabIndex.js +0 -114
  74. package/es/chrome/AppMenu/useServiceWorkerUpdate.d.ts +0 -16
  75. package/es/chrome/AppMenu/useServiceWorkerUpdate.js +0 -62
  76. package/es/report/ReportLauncher.css +0 -147
  77. package/es/report/ReportLauncher.d.ts +0 -55
  78. package/es/report/ReportLauncher.js +0 -142
  79. package/es/report/ReportLauncher.test.js +0 -231
  80. package/es/report/ReportParameters.css +0 -29
  81. package/es/report/ReportParameters.d.ts +0 -46
  82. package/es/report/ReportParameters.js +0 -65
  83. package/es/report/ReportParameters.test.d.ts +0 -1
  84. package/es/report/ReportParameters.test.js +0 -84
  85. package/es/report/index.d.ts +0 -2
  86. package/es/report/index.js +0 -2
  87. package/src/chrome/AppMenu/AppMenu.css +0 -648
  88. package/src/chrome/AppMenu/AppMenu.tsx +0 -285
  89. package/src/chrome/AppMenu/AppMenuColorScheme.tsx +0 -41
  90. package/src/chrome/AppMenu/AppMenuFavoriteButton.tsx +0 -47
  91. package/src/chrome/AppMenu/AppMenuHeader.tsx +0 -79
  92. package/src/chrome/AppMenu/AppMenuList.tsx +0 -89
  93. package/src/chrome/AppMenu/AppMenuRail.tsx +0 -117
  94. package/src/chrome/AppMenu/AppMenuToggle.tsx +0 -51
  95. package/src/chrome/AppMenu/AppMenuUserMenu.tsx +0 -190
  96. package/src/chrome/AppMenu/developerCategories.ts +0 -142
  97. package/src/chrome/AppMenu/icons.tsx +0 -76
  98. package/src/chrome/AppMenu/index.ts +0 -17
  99. package/src/chrome/AppMenu/menuEntries.ts +0 -128
  100. package/src/chrome/AppMenu/types.ts +0 -45
  101. package/src/chrome/AppMenu/useFavorites.ts +0 -40
  102. package/src/chrome/AppMenu/useRovingTabIndex.ts +0 -130
  103. package/src/chrome/AppMenu/useServiceWorkerUpdate.ts +0 -68
  104. package/src/report/ReportLauncher.css +0 -147
  105. package/src/report/ReportLauncher.test.tsx +0 -281
  106. package/src/report/ReportLauncher.tsx +0 -420
  107. package/src/report/ReportParameters.css +0 -29
  108. package/src/report/ReportParameters.test.tsx +0 -133
  109. package/src/report/ReportParameters.tsx +0 -198
  110. package/src/report/index.ts +0 -2
  111. /package/es/{chrome/AppMenu/types.js → elements/ElementLookupDialog/BarcodeScanner/decoder.js} +0 -0
  112. /package/es/{report/ReportLauncher.test.d.ts → elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts} +0 -0
@@ -0,0 +1,118 @@
1
+ import type BarcodeReaderType from "dynamsoft-javascript-barcode";
2
+ import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "../decoder";
3
+
4
+ export type DbrDecoderOptions = {
5
+ /** URL of the Dynamsoft Barcode Reader 6.x script, e.g. `/lib/dbr/dbr-6.5.1.min.js`. */
6
+ scriptUrl: string;
7
+ licenseKey: string;
8
+ };
9
+
10
+ type DbrGlobal = typeof BarcodeReaderType;
11
+
12
+ /** The parts of a Dynamsoft `TextResult` the mapper reads, typed locally so the vendor types stay out of the public API. */
13
+ export type DbrTextResult = {
14
+ BarcodeText: string;
15
+ BarcodeFormat: number;
16
+ LocalizationResult: {
17
+ X1: number;
18
+ Y1: number;
19
+ X2: number;
20
+ Y2: number;
21
+ X3: number;
22
+ Y3: number;
23
+ X4: number;
24
+ Y4: number;
25
+ };
26
+ };
27
+
28
+ let scriptLoading: Promise<DbrGlobal> | undefined;
29
+
30
+ function getGlobal(): DbrGlobal | undefined {
31
+ return (window as { BarcodeReader?: DbrGlobal }).BarcodeReader;
32
+ }
33
+
34
+ /** Loads the vendor script once, reusing `window.BarcodeReader` if the page already has it. */
35
+ function loadBarcodeReader(scriptUrl: string): Promise<DbrGlobal> {
36
+ let existing = getGlobal();
37
+ if (existing) return Promise.resolve(existing);
38
+
39
+ if (!scriptLoading) {
40
+ scriptLoading = new Promise<DbrGlobal>((resolve, reject) => {
41
+ let script = document.createElement("script");
42
+ script.src = scriptUrl;
43
+ script.async = true;
44
+ script.onload = () => {
45
+ let loaded = getGlobal();
46
+ if (loaded) {
47
+ resolve(loaded);
48
+ } else {
49
+ scriptLoading = undefined;
50
+ reject(new Error(`${scriptUrl} did not define BarcodeReader`));
51
+ }
52
+ };
53
+ script.onerror = () => {
54
+ scriptLoading = undefined;
55
+ script.remove();
56
+ reject(new Error(`Failed to load ${scriptUrl}`));
57
+ };
58
+ document.head.append(script);
59
+ });
60
+ }
61
+
62
+ return scriptLoading;
63
+ }
64
+
65
+ export function mapDbrResult(result: DbrTextResult): DecodedBarcode {
66
+ let { X1, Y1, X2, Y2, X3, Y3, X4, Y4 } = result.LocalizationResult;
67
+
68
+ return {
69
+ text: result.BarcodeText,
70
+ format: String(result.BarcodeFormat),
71
+ corners: [
72
+ { x: X1, y: Y1 },
73
+ { x: X2, y: Y2 },
74
+ { x: X3, y: Y3 },
75
+ { x: X4, y: Y4 },
76
+ ],
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Decoder backed by the legacy Dynamsoft Barcode Reader JavaScript SDK (6.x). Kept for A/B
82
+ * comparison against the free engines; the script is loaded on demand rather than by the article
83
+ * so users who never scan do not pay for it.
84
+ */
85
+ export function createDbrDecoder({ scriptUrl, licenseKey }: DbrDecoderOptions): DecoderFactory {
86
+ return async () => {
87
+ if (typeof WebAssembly === "undefined") {
88
+ throw new Error("WebAssembly is not supported");
89
+ }
90
+
91
+ let BarcodeReader = await loadBarcodeReader(scriptUrl);
92
+ BarcodeReader.licenseKey = licenseKey;
93
+
94
+ let reader = await BarcodeReader.createInstance();
95
+ let settings = reader.getRuntimeSettings();
96
+ settings.mBarcodeFormatIds = BarcodeReader.EnumBarcodeFormat.DATAMATRIX;
97
+ settings.mAntiDamageLevel = 3;
98
+ settings.mDeblurLevel = 0;
99
+ await reader.updateRuntimeSettings(settings);
100
+
101
+ return {
102
+ name: "dbr",
103
+ async decodeFrame(frame) {
104
+ let results = await reader.decode(frame);
105
+
106
+ return results.map(mapDbrResult);
107
+ },
108
+ async decodeImage(file) {
109
+ let results = await reader.decode(file);
110
+
111
+ return results.map(mapDbrResult);
112
+ },
113
+ dispose() {
114
+ reader.deleteInstance();
115
+ },
116
+ } satisfies BarcodeDecoder;
117
+ };
118
+ }
@@ -0,0 +1,103 @@
1
+ import { getLocalizedString } from "@olenbetong/appframe-core";
2
+ import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "../decoder";
3
+
4
+ /**
5
+ * Minimal typing of the Barcode Detection API. It is not part of the TypeScript DOM lib because
6
+ * only Chromium-based browsers implement it.
7
+ */
8
+ export type NativeDetectedBarcode = {
9
+ rawValue: string;
10
+ format: string;
11
+ cornerPoints: Array<{ x: number; y: number }>;
12
+ boundingBox: DOMRectReadOnly;
13
+ };
14
+
15
+ type NativeBarcodeDetector = {
16
+ detect(source: ImageBitmapSource): Promise<NativeDetectedBarcode[]>;
17
+ };
18
+
19
+ type NativeBarcodeDetectorConstructor = {
20
+ new (options?: { formats?: string[] }): NativeBarcodeDetector;
21
+ getSupportedFormats(): Promise<string[]>;
22
+ };
23
+
24
+ const DATA_MATRIX = "data_matrix";
25
+
26
+ function getNativeDetector(): NativeBarcodeDetectorConstructor | undefined {
27
+ return (globalThis as { BarcodeDetector?: NativeBarcodeDetectorConstructor }).BarcodeDetector;
28
+ }
29
+
30
+ /**
31
+ * Whether the browser can detect DataMatrix codes natively. Chrome on desktop exposes the
32
+ * `BarcodeDetector` class without any formats, so the class alone is not enough.
33
+ */
34
+ export async function isNativeDecoderAvailable(): Promise<boolean> {
35
+ try {
36
+ let detector = getNativeDetector();
37
+ if (!detector) return false;
38
+
39
+ let formats = await detector.getSupportedFormats();
40
+
41
+ return formats.includes(DATA_MATRIX);
42
+ } catch {
43
+ return false;
44
+ }
45
+ }
46
+
47
+ export function mapNativeResult(result: NativeDetectedBarcode): DecodedBarcode {
48
+ let points = result.cornerPoints;
49
+ let { left, top, right, bottom } = result.boundingBox;
50
+ let corners: DecodedBarcode["corners"] =
51
+ points.length >= 4
52
+ ? [
53
+ { x: points[0]!.x, y: points[0]!.y },
54
+ { x: points[1]!.x, y: points[1]!.y },
55
+ { x: points[2]!.x, y: points[2]!.y },
56
+ { x: points[3]!.x, y: points[3]!.y },
57
+ ]
58
+ : [
59
+ { x: left, y: top },
60
+ { x: right, y: top },
61
+ { x: right, y: bottom },
62
+ { x: left, y: bottom },
63
+ ];
64
+
65
+ return { text: result.rawValue, format: result.format, corners };
66
+ }
67
+
68
+ /**
69
+ * Decoder backed by the browser's own Barcode Detection API (ML Kit on Android). Nothing to
70
+ * download, but only available where the browser supports it — check `isNativeDecoderAvailable`
71
+ * first and fall back to another decoder when it returns false.
72
+ */
73
+ export function createNativeDecoder(): DecoderFactory {
74
+ return async () => {
75
+ let Detector = getNativeDetector();
76
+
77
+ if (!Detector || !(await isNativeDecoderAvailable())) {
78
+ throw new Error(getLocalizedString("This browser cannot detect barcodes natively"));
79
+ }
80
+
81
+ let detector = new Detector({ formats: [DATA_MATRIX] });
82
+
83
+ return {
84
+ name: "native",
85
+ async decodeFrame(frame) {
86
+ let results = await detector.detect(frame);
87
+
88
+ return results.map(mapNativeResult);
89
+ },
90
+ async decodeImage(file) {
91
+ let bitmap = await createImageBitmap(file);
92
+
93
+ try {
94
+ let results = await detector.detect(bitmap);
95
+
96
+ return results.map(mapNativeResult);
97
+ } finally {
98
+ bitmap.close();
99
+ }
100
+ },
101
+ } satisfies BarcodeDecoder;
102
+ };
103
+ }
@@ -0,0 +1,95 @@
1
+ import type { ReaderOptions, ReadResult } from "zxing-wasm/reader";
2
+ import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "../decoder";
3
+
4
+ export type ZxingDecoderOptions = {
5
+ /**
6
+ * URL of `zxing_reader.wasm`. When omitted, zxing-wasm fetches the binary matching its own version
7
+ * from the jsDelivr CDN.
8
+ */
9
+ wasmUrl?: string;
10
+ /** Reader options for live camera frames. Merged over the defaults. */
11
+ frameOptions?: ReaderOptions;
12
+ /** Reader options for still images. Merged over the defaults. */
13
+ imageOptions?: ReaderOptions;
14
+ };
15
+
16
+ const defaultFrameOptions: ReaderOptions = {
17
+ formats: ["DataMatrix"],
18
+ tryHarder: false,
19
+ tryRotate: true,
20
+ tryInvert: false,
21
+ tryDownscale: true,
22
+ maxNumberOfSymbols: 4,
23
+ };
24
+
25
+ const defaultImageOptions: ReaderOptions = {
26
+ formats: ["DataMatrix"],
27
+ tryHarder: true,
28
+ tryRotate: true,
29
+ tryInvert: true,
30
+ tryDownscale: true,
31
+ maxNumberOfSymbols: 8,
32
+ };
33
+
34
+ export function mapZxingResult(result: ReadResult): DecodedBarcode {
35
+ let { topLeft, topRight, bottomRight, bottomLeft } = result.position;
36
+
37
+ return {
38
+ text: result.text,
39
+ format: result.format,
40
+ corners: [
41
+ { x: topLeft.x, y: topLeft.y },
42
+ { x: topRight.x, y: topRight.y },
43
+ { x: bottomRight.x, y: bottomRight.y },
44
+ { x: bottomLeft.x, y: bottomLeft.y },
45
+ ],
46
+ };
47
+ }
48
+
49
+ /**
50
+ * Decoder backed by zxing-cpp compiled to WebAssembly (`zxing-wasm`). Free, runs everywhere
51
+ * WebAssembly does, including iOS Safari. The module is imported when the factory runs, so apps
52
+ * that never open the scanner do not download it.
53
+ */
54
+ export function createZxingDecoder(options: ZxingDecoderOptions = {}): DecoderFactory {
55
+ let frameOptions = { ...defaultFrameOptions, ...options.frameOptions };
56
+ let imageOptions = { ...defaultImageOptions, ...options.imageOptions };
57
+
58
+ return async () => {
59
+ if (typeof WebAssembly === "undefined") {
60
+ throw new Error("WebAssembly is not supported");
61
+ }
62
+
63
+ let zxing = await import("zxing-wasm/reader");
64
+ let { wasmUrl } = options;
65
+
66
+ if (wasmUrl) {
67
+ zxing.prepareZXingModule({
68
+ overrides: {
69
+ locateFile: (path: string, prefix: string) => (path.endsWith(".wasm") ? wasmUrl : prefix + path),
70
+ },
71
+ });
72
+ }
73
+
74
+ // Instantiate up front so a broken download surfaces as a scanner error instead of on the first frame.
75
+ await zxing.prepareZXingModule({ fireImmediately: true });
76
+
77
+ return {
78
+ name: "zxing",
79
+ async decodeFrame(frame) {
80
+ let context = frame.getContext("2d", { willReadFrequently: true });
81
+ if (!context) return [];
82
+
83
+ let imageData = context.getImageData(0, 0, frame.width, frame.height);
84
+ let results = await zxing.readBarcodes(imageData, frameOptions);
85
+
86
+ return results.filter((result) => result.isValid).map(mapZxingResult);
87
+ },
88
+ async decodeImage(file) {
89
+ let results = await zxing.readBarcodes(file, imageOptions);
90
+
91
+ return results.filter((result) => result.isValid).map(mapZxingResult);
92
+ },
93
+ } satisfies BarcodeDecoder;
94
+ };
95
+ }
@@ -0,0 +1,118 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { mapDbrResult } from "./decoders/dbr";
3
+ import { mapNativeResult, type NativeDetectedBarcode } from "./decoders/native";
4
+ import { mapZxingResult } from "./decoders/zxing";
5
+ import { createDuplicateFilter } from "./duplicateFilter";
6
+
7
+ const GS = "";
8
+ const GIAI = `8004${"7043210"}${GS}${"12345"}`;
9
+
10
+ describe("result mapping", () => {
11
+ it("maps a zxing result to corners in reading order", () => {
12
+ let mapped = mapZxingResult({
13
+ text: GIAI,
14
+ format: "DataMatrix",
15
+ isValid: true,
16
+ position: {
17
+ topLeft: { x: 10, y: 20 },
18
+ topRight: { x: 110, y: 20 },
19
+ bottomRight: { x: 110, y: 120 },
20
+ bottomLeft: { x: 10, y: 120 },
21
+ },
22
+ } as Parameters<typeof mapZxingResult>[0]);
23
+
24
+ expect(mapped.text).toBe(GIAI);
25
+ expect(mapped.format).toBe("DataMatrix");
26
+ expect(mapped.corners).toEqual([
27
+ { x: 10, y: 20 },
28
+ { x: 110, y: 20 },
29
+ { x: 110, y: 120 },
30
+ { x: 10, y: 120 },
31
+ ]);
32
+ });
33
+
34
+ it("maps a native result from its corner points", () => {
35
+ let result: NativeDetectedBarcode = {
36
+ rawValue: GIAI,
37
+ format: "data_matrix",
38
+ cornerPoints: [
39
+ { x: 1, y: 2 },
40
+ { x: 3, y: 2 },
41
+ { x: 3, y: 4 },
42
+ { x: 1, y: 4 },
43
+ ],
44
+ boundingBox: { left: 0, top: 0, right: 0, bottom: 0 } as DOMRectReadOnly,
45
+ };
46
+
47
+ expect(mapNativeResult(result)).toEqual({
48
+ text: GIAI,
49
+ format: "data_matrix",
50
+ corners: [
51
+ { x: 1, y: 2 },
52
+ { x: 3, y: 2 },
53
+ { x: 3, y: 4 },
54
+ { x: 1, y: 4 },
55
+ ],
56
+ });
57
+ });
58
+
59
+ it("falls back to the bounding box when a native result has no corner points", () => {
60
+ let result: NativeDetectedBarcode = {
61
+ rawValue: GIAI,
62
+ format: "data_matrix",
63
+ cornerPoints: [],
64
+ boundingBox: { left: 5, top: 6, right: 15, bottom: 16 } as DOMRectReadOnly,
65
+ };
66
+
67
+ expect(mapNativeResult(result).corners).toEqual([
68
+ { x: 5, y: 6 },
69
+ { x: 15, y: 6 },
70
+ { x: 15, y: 16 },
71
+ { x: 5, y: 16 },
72
+ ]);
73
+ });
74
+
75
+ it("maps a Dynamsoft result from its localization result", () => {
76
+ let mapped = mapDbrResult({
77
+ BarcodeText: GIAI,
78
+ BarcodeFormat: 134217728,
79
+ LocalizationResult: { X1: 1, Y1: 2, X2: 3, Y2: 2, X3: 3, Y3: 4, X4: 1, Y4: 4 },
80
+ });
81
+
82
+ expect(mapped.text).toBe(GIAI);
83
+ expect(mapped.corners).toEqual([
84
+ { x: 1, y: 2 },
85
+ { x: 3, y: 2 },
86
+ { x: 3, y: 4 },
87
+ { x: 1, y: 4 },
88
+ ]);
89
+ });
90
+ });
91
+
92
+ describe("createDuplicateFilter", () => {
93
+ it("reports a code once while it stays in view", () => {
94
+ let isNew = createDuplicateFilter(1000);
95
+
96
+ expect(isNew("a", 0)).toBe(true);
97
+ expect(isNew("a", 100)).toBe(false);
98
+ expect(isNew("a", 900)).toBe(false);
99
+ // Still in view: every sighting refreshes the timer.
100
+ expect(isNew("a", 1500)).toBe(false);
101
+ });
102
+
103
+ it("reports a code again once it has been out of view for the forget time", () => {
104
+ let isNew = createDuplicateFilter(1000);
105
+
106
+ expect(isNew("a", 0)).toBe(true);
107
+ expect(isNew("a", 1001)).toBe(true);
108
+ });
109
+
110
+ it("tracks codes independently", () => {
111
+ let isNew = createDuplicateFilter(1000);
112
+
113
+ expect(isNew("a", 0)).toBe(true);
114
+ expect(isNew("b", 0)).toBe(true);
115
+ expect(isNew("a", 10)).toBe(false);
116
+ expect(isNew("b", 10)).toBe(false);
117
+ });
118
+ });
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Suppresses repeated reads of a code that stays in view. A value is reported once, and again only
3
+ * after it has been out of view for `forgetTime` milliseconds — every sighting refreshes the timer,
4
+ * matching the `duplicateForgetTime` behaviour of the Dynamsoft scanner the apps were built around.
5
+ */
6
+ export function createDuplicateFilter(forgetTime: number) {
7
+ let lastSeen = new Map<string, number>();
8
+
9
+ return function isNew(text: string, now: number): boolean {
10
+ let previous = lastSeen.get(text);
11
+ lastSeen.set(text, now);
12
+
13
+ if (lastSeen.size > 64) {
14
+ for (let [key, seen] of lastSeen) {
15
+ if (now - seen > forgetTime) lastSeen.delete(key);
16
+ }
17
+ }
18
+
19
+ return previous === undefined || now - previous > forgetTime;
20
+ };
21
+ }
@@ -1,19 +1,25 @@
1
1
  import SwitchCameraOutlined from "@mui/icons-material/SwitchCameraOutlined";
2
- import { Box, IconButton, type SxProps } from "@mui/material";
2
+ import { Box, IconButton, type SxProps, Typography } from "@mui/material";
3
3
  import { getLocalizedString } from "@olenbetong/appframe-core";
4
- import type BarcodeReaderType from "dynamsoft-javascript-barcode";
5
- import { useRef } from "react";
4
+ import { type ReactNode, useRef } from "react";
5
+ import type { DecoderFactory } from "./decoder";
6
6
  import { ErrorIcon } from "./Error";
7
7
  import { useScanner } from "./useScanner";
8
8
 
9
- declare global {
10
- interface Window {
11
- BarcodeReader: typeof BarcodeReaderType;
12
- }
13
- }
9
+ export type { BarcodeDecoder, DecodedBarcode, DecodedPoint, DecoderFactory } from "./decoder";
10
+ export { createDbrDecoder, type DbrDecoderOptions } from "./decoders/dbr";
11
+ export { createNativeDecoder, isNativeDecoderAvailable } from "./decoders/native";
12
+ export { createZxingDecoder, type ZxingDecoderOptions } from "./decoders/zxing";
14
13
 
15
14
  export type BarcodeScannerProps = {
16
15
  onRead: (barcode: string) => void;
16
+ /**
17
+ * Engine used to decode the camera frames. Defaults to zxing-wasm. Pass a stable reference and
18
+ * change it only to switch engine — the decoder is recreated whenever it changes.
19
+ */
20
+ decoder?: DecoderFactory;
21
+ /** Extra controls rendered in the scanner footer, next to the engine name — e.g. an engine picker. */
22
+ actions?: ReactNode;
17
23
  };
18
24
 
19
25
  const frameSx: SxProps = {
@@ -24,12 +30,13 @@ const frameSx: SxProps = {
24
30
  top: 0,
25
31
  height: "100%",
26
32
  width: "100%",
33
+ objectFit: "contain",
27
34
  };
28
35
 
29
- export function BarcodeScanner({ onRead }: BarcodeScannerProps) {
30
- let readerRoot = useRef<HTMLElement | null>(null);
36
+ export function BarcodeScanner({ onRead, decoder, actions }: BarcodeScannerProps) {
37
+ let video = useRef<HTMLVideoElement | null>(null);
31
38
  let canvas = useRef<HTMLCanvasElement | null>(null);
32
- let { devices, error, setNextDevice } = useScanner(readerRoot, canvas, onRead);
39
+ let { devices, engine, error, setNextDevice } = useScanner(video, canvas, onRead, decoder);
33
40
 
34
41
  return (
35
42
  <Box
@@ -39,9 +46,9 @@ export function BarcodeScanner({ onRead }: BarcodeScannerProps) {
39
46
  height: "100%",
40
47
  }}
41
48
  >
42
- <Box sx={{ height: "100%" }} ref={readerRoot}>
43
- <Box component="video" className="dbrScanner-video" playsInline sx={frameSx} />
44
- <Box component="canvas" sx={frameSx} width="1280" height="720" ref={canvas} />
49
+ <Box sx={{ height: "100%" }}>
50
+ <Box component="video" playsInline muted autoPlay sx={frameSx} ref={video} />
51
+ <Box component="canvas" sx={frameSx} ref={canvas} />
45
52
  {error && <ErrorIcon {...error} />}
46
53
  </Box>
47
54
  <Box
@@ -69,6 +76,14 @@ export function BarcodeScanner({ onRead }: BarcodeScannerProps) {
69
76
  <SwitchCameraOutlined />
70
77
  </IconButton>
71
78
  )}
79
+ <Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
80
+ {engine && (
81
+ <Typography variant="caption" sx={{ opacity: 0.7 }} data-testid="barcode-engine">
82
+ {engine}
83
+ </Typography>
84
+ )}
85
+ {actions}
86
+ </Box>
72
87
  </Box>
73
88
  </Box>
74
89
  );