@olenbetong/synergi-react 4.0.0 → 4.1.1

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 (85) hide show
  1. package/es/components/DateNavigator/index.js +1 -1
  2. package/es/components/FilePond.js +1 -1
  3. package/es/components/ImageEditor/ImageEditor.js +1 -1
  4. package/es/components/ImageEditor/plugins/cropPlugin/CropOverlay.js +1 -1
  5. package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
  6. package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
  7. package/es/components/ImageEditor/plugins/zoomPlugin/ZoomControls.js +1 -1
  8. package/es/components/MergeProgressDialog/index.js +1 -1
  9. package/es/components/PdfViewer/PdfViewer.js +22 -3
  10. package/es/components/Toolbar/index.js +1 -1
  11. package/es/elements/ElementDamage/DamageFormButtons.js +1 -1
  12. package/es/elements/ElementDamage/DescriptionInput.js +1 -1
  13. package/es/elements/ElementDamage/DuplicateDamage/DesktopView.js +1 -1
  14. package/es/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.js +1 -1
  15. package/es/elements/ElementDamage/DuplicateDamage/PhoneView.js +1 -1
  16. package/es/elements/ElementDamage/ElementNumberTextField.js +1 -1
  17. package/es/elements/ElementDamage/RegisterFix.js +1 -1
  18. package/es/elements/ElementDamage/SubmitButtons.js +1 -1
  19. package/es/elements/ElementDamage/TransportElementSelect.js +1 -1
  20. package/es/elements/ElementLookup.js +1 -1
  21. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.js +1 -1
  22. package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +1 -1
  23. package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.js +1 -1
  24. package/es/elements/ElementLookupDialog/HIDScanner/index.js +1 -1
  25. package/es/elements/ElementLookupDialog/Manual/ManualForm.js +1 -1
  26. package/es/elements/ElementLookupDialog/index.js +1 -1
  27. package/es/elements/ElementLookupDialog/utils/Giai.js +1 -1
  28. package/es/elements/ProjectSelect.js +1 -1
  29. package/es/i18n.d.ts +6 -0
  30. package/es/i18n.js +10 -0
  31. package/package.json +7 -7
  32. package/src/chrome/AppShell/index.tsx +29 -29
  33. package/src/chrome/openReport.ts +34 -34
  34. package/src/colorScheme.ts +81 -81
  35. package/src/components/DateNavigator/index.tsx +186 -186
  36. package/src/components/FilePond.tsx +312 -312
  37. package/src/components/ImageEditor/ImageEditor.tsx +343 -343
  38. package/src/components/ImageEditor/index.ts +9 -9
  39. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  40. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  41. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +403 -403
  42. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  43. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  44. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  45. package/src/components/ImageEditor/useEdgeDetection.ts +127 -127
  46. package/src/components/ImageEditor/useImageHistory.ts +197 -197
  47. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  48. package/src/components/LinkedCardList/index.scss +95 -95
  49. package/src/components/MergeProgressDialog/index.tsx +249 -249
  50. package/src/components/PageBanner/index.tsx +80 -80
  51. package/src/components/PdfViewer/PdfViewer.tsx +158 -139
  52. package/src/components/ProgressBar/index.scss +96 -96
  53. package/src/components/SplitContainer/index.tsx +333 -333
  54. package/src/components/Toolbar/index.tsx +212 -212
  55. package/src/designsystemet-web-types.d.ts +24 -24
  56. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  57. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  58. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  59. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  60. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  61. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  62. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  63. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  64. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  65. package/src/elements/ElementLookup.tsx +123 -123
  66. package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
  67. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
  68. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
  69. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
  70. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
  71. package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
  72. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
  73. package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
  74. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  75. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  76. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  77. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  78. package/src/elements/ProjectSelect.tsx +130 -130
  79. package/src/elements/index.ts +15 -15
  80. package/src/filter/index.ts +34 -34
  81. package/src/i18n.ts +13 -0
  82. package/src/index.ts +77 -77
  83. package/src/mui/MuiThemeProvider.tsx +174 -174
  84. package/src/mui/index.ts +1 -1
  85. package/src/test/setup.ts +8 -8
@@ -1,123 +1,123 @@
1
- import SearchIcon from "@mui/icons-material/Search";
2
- import { IconButton, TextField } from "@mui/material";
3
- import { getLocalizedString } from "@olenbetong/appframe-core";
4
- import { type ReactNode, type RefObject, useEffect, useImperativeHandle, useRef, useState } from "react";
5
- import { type AddElement, ElementLookupDialog } from "./ElementLookupDialog";
6
- import type { DecoderFactory } from "./ElementLookupDialog/BarcodeScanner";
7
-
8
- export type Element = {
9
- Domain: string;
10
- PrimKey: string;
11
- ProjectID: string;
12
- ProjectName: string;
13
- ElementID: string | null;
14
- };
15
-
16
- export type ElementLookupHandle = {
17
- openDialog: () => void;
18
- closeDialog: () => void;
19
- };
20
-
21
- export type ElementLookupProps = {
22
- value?: Element | null;
23
- defaultValue?: Element | null;
24
- ref?: RefObject<ElementLookupHandle | null>;
25
- elementIDRequired?: boolean;
26
- openLookupDialogIcon?: boolean;
27
- onChange?: (value: Element | null) => void;
28
- onDialogChange?: (open: boolean) => void;
29
- /** Engine for the camera tab of the lookup dialog. Defaults to zxing-wasm. */
30
- decoder?: DecoderFactory;
31
- /** Extra controls in the camera tab footer, e.g. an engine picker. */
32
- scannerActions?: ReactNode;
33
- };
34
-
35
- export function ElementLookup({
36
- value,
37
- defaultValue,
38
- ref,
39
- elementIDRequired = false,
40
- openLookupDialogIcon,
41
- onChange,
42
- onDialogChange,
43
- decoder,
44
- scannerActions,
45
- }: ElementLookupProps) {
46
- let initializedRef = useRef<boolean>(false);
47
-
48
- let [openElementLookupDialog, setOpenElementLookupDialog] = useState<boolean>(false);
49
- let [element, setElement] = useState<Element | null>(defaultValue ?? null);
50
- let elementValue = value === undefined ? element : value;
51
-
52
- useEffect(() => {
53
- if (initializedRef.current) {
54
- return;
55
- }
56
-
57
- if (value === undefined && defaultValue) {
58
- setElement(defaultValue);
59
- initializedRef.current = true;
60
- }
61
- }, [defaultValue]); // oxlint-disable-line react-hooks/exhaustive-deps (intentionally skipping "value" dependency as this shouldn't run when "value" changes)
62
-
63
- useImperativeHandle(ref, () => ({
64
- openDialog: () => setOpenElementLookupDialog(true),
65
- closeDialog: () => setOpenElementLookupDialog(false),
66
- }));
67
-
68
- async function handleAddElement(addElement: AddElement) {
69
- onChange?.(addElement.element);
70
- setElement(addElement.element);
71
- }
72
-
73
- useEffect(() => {
74
- onDialogChange?.(openElementLookupDialog);
75
- }, [openElementLookupDialog]); // oxlint-disable-line react-hooks/exhaustive-deps (only run when openElementLookupDialog changes)
76
-
77
- return (
78
- <>
79
- <ElementLookupDialog
80
- open={openElementLookupDialog}
81
- mode="single"
82
- onClose={() => setOpenElementLookupDialog(false)}
83
- onAdd={handleAddElement}
84
- allowEmptyElement={!elementIDRequired}
85
- decoder={decoder}
86
- scannerActions={scannerActions}
87
- />
88
-
89
- <input type="hidden" name="ProjectID" value={elementValue?.ProjectID ?? ""} />
90
- <input type="hidden" name="ElementRef" value={elementValue?.PrimKey ?? ""} />
91
-
92
- <TextField
93
- id="NewElementDamge_Project"
94
- value={elementValue ? `${elementValue.ProjectID}: ${elementValue.ProjectName}` : ""}
95
- label={getLocalizedString("Project")}
96
- fullWidth
97
- required
98
- autoComplete="off"
99
- slotProps={{ htmlInput: { readOnly: true } }}
100
- />
101
- <TextField
102
- name="ElementID"
103
- id="NewElementDamage_ElementID"
104
- value={elementValue ? elementValue.ElementID : ""}
105
- label={getLocalizedString("Element ID")}
106
- fullWidth
107
- required={elementIDRequired}
108
- autoComplete="off"
109
- slotProps={{
110
- htmlInput: { readOnly: true },
111
- input: {
112
- endAdornment: openLookupDialogIcon ? (
113
- <IconButton onClick={() => setOpenElementLookupDialog(true)}>
114
- <SearchIcon />
115
- </IconButton>
116
- ) : undefined,
117
- },
118
- inputLabel: { shrink: !!elementValue?.ElementID },
119
- }}
120
- />
121
- </>
122
- );
123
- }
1
+ import SearchIcon from "@mui/icons-material/Search";
2
+ import { IconButton, TextField } from "@mui/material";
3
+ import { type ReactNode, type RefObject, useEffect, useImperativeHandle, useRef, useState } from "react";
4
+ import { type AddElement, ElementLookupDialog } from "./ElementLookupDialog";
5
+ import type { DecoderFactory } from "./ElementLookupDialog/BarcodeScanner";
6
+ import { getLocalizedString } from "../i18n.js";
7
+
8
+ export type Element = {
9
+ Domain: string;
10
+ PrimKey: string;
11
+ ProjectID: string;
12
+ ProjectName: string;
13
+ ElementID: string | null;
14
+ };
15
+
16
+ export type ElementLookupHandle = {
17
+ openDialog: () => void;
18
+ closeDialog: () => void;
19
+ };
20
+
21
+ export type ElementLookupProps = {
22
+ value?: Element | null;
23
+ defaultValue?: Element | null;
24
+ ref?: RefObject<ElementLookupHandle | null>;
25
+ elementIDRequired?: boolean;
26
+ openLookupDialogIcon?: boolean;
27
+ onChange?: (value: Element | null) => void;
28
+ onDialogChange?: (open: boolean) => void;
29
+ /** Engine for the camera tab of the lookup dialog. Defaults to zxing-wasm. */
30
+ decoder?: DecoderFactory;
31
+ /** Extra controls in the camera tab footer, e.g. an engine picker. */
32
+ scannerActions?: ReactNode;
33
+ };
34
+
35
+ export function ElementLookup({
36
+ value,
37
+ defaultValue,
38
+ ref,
39
+ elementIDRequired = false,
40
+ openLookupDialogIcon,
41
+ onChange,
42
+ onDialogChange,
43
+ decoder,
44
+ scannerActions,
45
+ }: ElementLookupProps) {
46
+ let initializedRef = useRef<boolean>(false);
47
+
48
+ let [openElementLookupDialog, setOpenElementLookupDialog] = useState<boolean>(false);
49
+ let [element, setElement] = useState<Element | null>(defaultValue ?? null);
50
+ let elementValue = value === undefined ? element : value;
51
+
52
+ useEffect(() => {
53
+ if (initializedRef.current) {
54
+ return;
55
+ }
56
+
57
+ if (value === undefined && defaultValue) {
58
+ setElement(defaultValue);
59
+ initializedRef.current = true;
60
+ }
61
+ }, [defaultValue]); // oxlint-disable-line react-hooks/exhaustive-deps (intentionally skipping "value" dependency as this shouldn't run when "value" changes)
62
+
63
+ useImperativeHandle(ref, () => ({
64
+ openDialog: () => setOpenElementLookupDialog(true),
65
+ closeDialog: () => setOpenElementLookupDialog(false),
66
+ }));
67
+
68
+ async function handleAddElement(addElement: AddElement) {
69
+ onChange?.(addElement.element);
70
+ setElement(addElement.element);
71
+ }
72
+
73
+ useEffect(() => {
74
+ onDialogChange?.(openElementLookupDialog);
75
+ }, [openElementLookupDialog]); // oxlint-disable-line react-hooks/exhaustive-deps (only run when openElementLookupDialog changes)
76
+
77
+ return (
78
+ <>
79
+ <ElementLookupDialog
80
+ open={openElementLookupDialog}
81
+ mode="single"
82
+ onClose={() => setOpenElementLookupDialog(false)}
83
+ onAdd={handleAddElement}
84
+ allowEmptyElement={!elementIDRequired}
85
+ decoder={decoder}
86
+ scannerActions={scannerActions}
87
+ />
88
+
89
+ <input type="hidden" name="ProjectID" value={elementValue?.ProjectID ?? ""} />
90
+ <input type="hidden" name="ElementRef" value={elementValue?.PrimKey ?? ""} />
91
+
92
+ <TextField
93
+ id="NewElementDamge_Project"
94
+ value={elementValue ? `${elementValue.ProjectID}: ${elementValue.ProjectName}` : ""}
95
+ label={getLocalizedString("Project")}
96
+ fullWidth
97
+ required
98
+ autoComplete="off"
99
+ slotProps={{ htmlInput: { readOnly: true } }}
100
+ />
101
+ <TextField
102
+ name="ElementID"
103
+ id="NewElementDamage_ElementID"
104
+ value={elementValue ? elementValue.ElementID : ""}
105
+ label={getLocalizedString("Element ID")}
106
+ fullWidth
107
+ required={elementIDRequired}
108
+ autoComplete="off"
109
+ slotProps={{
110
+ htmlInput: { readOnly: true },
111
+ input: {
112
+ endAdornment: openLookupDialogIcon ? (
113
+ <IconButton onClick={() => setOpenElementLookupDialog(true)}>
114
+ <SearchIcon />
115
+ </IconButton>
116
+ ) : undefined,
117
+ },
118
+ inputLabel: { shrink: !!elementValue?.ElementID },
119
+ }}
120
+ />
121
+ </>
122
+ );
123
+ }
@@ -1,32 +1,32 @@
1
- export type DecodedPoint = { x: number; y: number };
2
-
3
- export type DecodedBarcode = {
4
- /** Decoded text. GS1 DataMatrix keeps the FNC1 separators as U+001D. */
5
- text: string;
6
- /** Corners of the symbol in frame pixel coordinates: top-left, top-right, bottom-right, bottom-left. */
7
- corners: [DecodedPoint, DecodedPoint, DecodedPoint, DecodedPoint];
8
- /** Engine-specific format name, when reported. */
9
- format?: string;
10
- };
11
-
12
- /**
13
- * A barcode decoding engine. `BarcodeScanner` owns the camera and the frame loop and only hands
14
- * frames to the decoder, so any engine that can read a canvas or an image file can be plugged in.
15
- */
16
- export interface BarcodeDecoder {
17
- /** Short identifier shown in the scanner UI, e.g. "zxing", "native" or "dbr". */
18
- readonly name: string;
19
- /** Decode one camera frame drawn on a canvas. The scanner never has more than one call in flight. */
20
- decodeFrame(frame: HTMLCanvasElement): Promise<DecodedBarcode[]>;
21
- /** Decode a still image, such as a photo upload. May use slower, more thorough settings. */
22
- decodeImage(file: Blob): Promise<DecodedBarcode[]>;
23
- /** Release engine resources. Called when the scanner unmounts. */
24
- dispose?(): void;
25
- }
26
-
27
- /**
28
- * Creates a decoder. The heavy work — downloading a WebAssembly build or a vendor script — happens
29
- * here, not when the factory itself is created, so the factory is cheap to pass around and nothing
30
- * is loaded until the scanner mounts.
31
- */
32
- export type DecoderFactory = () => Promise<BarcodeDecoder>;
1
+ export type DecodedPoint = { x: number; y: number };
2
+
3
+ export type DecodedBarcode = {
4
+ /** Decoded text. GS1 DataMatrix keeps the FNC1 separators as U+001D. */
5
+ text: string;
6
+ /** Corners of the symbol in frame pixel coordinates: top-left, top-right, bottom-right, bottom-left. */
7
+ corners: [DecodedPoint, DecodedPoint, DecodedPoint, DecodedPoint];
8
+ /** Engine-specific format name, when reported. */
9
+ format?: string;
10
+ };
11
+
12
+ /**
13
+ * A barcode decoding engine. `BarcodeScanner` owns the camera and the frame loop and only hands
14
+ * frames to the decoder, so any engine that can read a canvas or an image file can be plugged in.
15
+ */
16
+ export interface BarcodeDecoder {
17
+ /** Short identifier shown in the scanner UI, e.g. "zxing", "native" or "dbr". */
18
+ readonly name: string;
19
+ /** Decode one camera frame drawn on a canvas. The scanner never has more than one call in flight. */
20
+ decodeFrame(frame: HTMLCanvasElement): Promise<DecodedBarcode[]>;
21
+ /** Decode a still image, such as a photo upload. May use slower, more thorough settings. */
22
+ decodeImage(file: Blob): Promise<DecodedBarcode[]>;
23
+ /** Release engine resources. Called when the scanner unmounts. */
24
+ dispose?(): void;
25
+ }
26
+
27
+ /**
28
+ * Creates a decoder. The heavy work — downloading a WebAssembly build or a vendor script — happens
29
+ * here, not when the factory itself is created, so the factory is cheap to pass around and nothing
30
+ * is loaded until the scanner mounts.
31
+ */
32
+ export type DecoderFactory = () => Promise<BarcodeDecoder>;
@@ -1,118 +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
- }
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
+ }