@olenbetong/synergi-react 4.3.1 → 5.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.
- package/dist/styles/styles.css +1 -1
- package/dist/styles/styles.css.map +1 -1
- package/es/components/Checkbox/index.js +1 -9
- package/es/components/FilePond.js +3 -12
- package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
- package/es/components/ImageEditor/useImageHistory.js +13 -1
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.d.ts +7 -0
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +20 -0
- package/es/components/MergeProgressDialog/index.d.ts +3 -8
- package/es/components/MergeProgressDialog/index.js +4 -133
- package/es/components/MergeProgressDialog/useMergeJob.d.ts +36 -0
- package/es/components/MergeProgressDialog/useMergeJob.js +142 -0
- package/es/components/Portal/index.js +20 -20
- package/es/components/ProgressBar/index.css +1 -1
- package/es/components/SplitContainer/index.js +3 -1
- package/es/components/Toolbar/index.js +6 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +81 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.d.ts +2 -2
- package/es/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.js +1 -2
- package/es/elements/ElementLookupDialog/BarcodeScanner/Error.js +3 -29
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/index.d.ts +1 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +3 -28
- package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
- package/es/elements-ds/DescriptionInput.d.ts +16 -0
- package/es/elements-ds/DescriptionInput.js +41 -0
- package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
- package/es/elements-ds/DuplicateElementDamage.js +57 -0
- package/es/elements-ds/ElementLookup.d.ts +23 -0
- package/es/elements-ds/ElementLookup.js +35 -0
- package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
- package/es/elements-ds/ElementLookupDialog.js +84 -0
- package/es/elements-ds/FormFields.d.ts +43 -0
- package/es/elements-ds/FormFields.js +67 -0
- package/es/elements-ds/HIDScanner.d.ts +7 -0
- package/es/elements-ds/HIDScanner.js +36 -0
- package/es/elements-ds/ManualForm.d.ts +7 -0
- package/es/elements-ds/ManualForm.js +70 -0
- package/es/elements-ds/ProjectSelect.d.ts +28 -0
- package/es/elements-ds/ProjectSelect.js +45 -0
- package/es/elements-ds/elements-ds.css +157 -0
- package/es/elements-ds/index.d.ts +12 -0
- package/es/elements-ds/index.js +14 -0
- package/es/index.css +1 -1
- package/es/index.d.ts +2 -1
- package/es/index.js +2 -1
- package/es/mui/index.d.ts +1 -0
- package/es/mui/index.js +1 -0
- package/es/useTranslation/index.js +5 -3
- package/package.json +34 -15
- package/src/chrome/AppShell/AppShell.test.tsx +48 -0
- package/src/chrome/openReport.test.ts +50 -0
- package/src/colorScheme.test.ts +188 -0
- package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
- package/src/components/Checkbox/index.tsx +1 -10
- package/src/components/ColorCard/ColorCard.test.tsx +34 -0
- package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
- package/src/components/FilePond.browser.test.tsx +389 -0
- package/src/components/FilePond.tsx +3 -12
- package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
- package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
- package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
- package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
- package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
- package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
- package/src/components/ImageEditor/useImageHistory.ts +10 -1
- package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +92 -0
- package/src/components/MergeProgressDialog/index.tsx +5 -158
- package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
- package/src/components/MergeProgressDialog/useMergeJob.ts +170 -0
- package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
- package/src/components/Portal/Portal.test.tsx +106 -0
- package/src/components/Portal/index.tsx +22 -25
- package/src/components/ProgressBar/index.scss +23 -54
- package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
- package/src/components/Sidebar/Sidebar.test.tsx +123 -0
- package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
- package/src/components/SplitContainer/index.tsx +3 -1
- package/src/components/Toolbar/Toolbar.test.tsx +216 -0
- package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
- package/src/components/Toolbar/index.tsx +6 -1
- package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +81 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.tsx +4 -4
- package/src/elements/ElementLookupDialog/BarcodeScanner/Error.tsx +7 -36
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +20 -49
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
- package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
- package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
- package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
- package/src/elements-ds/DescriptionInput.tsx +114 -0
- package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
- package/src/elements-ds/ElementLookup.tsx +114 -0
- package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
- package/src/elements-ds/ElementLookupDialog.tsx +196 -0
- package/src/elements-ds/FormFields.tsx +176 -0
- package/src/elements-ds/HIDScanner.tsx +53 -0
- package/src/elements-ds/ManualForm.tsx +129 -0
- package/src/elements-ds/ProjectSelect.tsx +120 -0
- package/src/elements-ds/elements-ds.css +157 -0
- package/src/elements-ds/index.ts +16 -0
- package/src/i18n.test.ts +31 -0
- package/src/index.ts +5 -1
- package/src/mui/index.ts +1 -0
- package/src/test/fakeCamera.ts +98 -0
- package/src/test/fakeDataApi.ts +90 -0
- package/src/test/setup.browser.ts +21 -0
- package/src/test/setup.ts +15 -0
- package/src/useTranslation/index.ts +5 -3
- package/src/useTranslation/useTranslation.test.ts +128 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
- package/es/test/setup.d.ts +0 -1
- package/es/test/setup.js +0 -6
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
.ob-barcode-scanner {
|
|
2
|
+
position: relative;
|
|
3
|
+
height: 100%;
|
|
4
|
+
background-color: black;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.ob-barcode-scanner__frames {
|
|
8
|
+
height: 100%;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.ob-barcode-scanner__frame {
|
|
12
|
+
position: absolute;
|
|
13
|
+
inset: 0;
|
|
14
|
+
height: 100%;
|
|
15
|
+
width: 100%;
|
|
16
|
+
object-fit: contain;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.ob-barcode-scanner__footer {
|
|
20
|
+
position: absolute;
|
|
21
|
+
right: 0;
|
|
22
|
+
bottom: 0;
|
|
23
|
+
left: 0;
|
|
24
|
+
display: flex;
|
|
25
|
+
align-items: center;
|
|
26
|
+
justify-content: space-between;
|
|
27
|
+
padding: 0.5rem 1.5rem;
|
|
28
|
+
color: white;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.ob-barcode-scanner__actions {
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
gap: 0.5rem;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.ob-barcode-scanner__engine {
|
|
38
|
+
font-size: 0.75rem;
|
|
39
|
+
opacity: 0.7;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.ob-barcode-scanner__icon-button {
|
|
43
|
+
display: inline-flex;
|
|
44
|
+
align-items: center;
|
|
45
|
+
justify-content: center;
|
|
46
|
+
padding: 0.75rem;
|
|
47
|
+
border: 0;
|
|
48
|
+
border-radius: 50%;
|
|
49
|
+
background: transparent;
|
|
50
|
+
color: inherit;
|
|
51
|
+
font-size: 1.75rem;
|
|
52
|
+
cursor: pointer;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.ob-barcode-scanner__icon-button:hover {
|
|
56
|
+
background-color: rgb(255 255 255 / 0.1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.ob-barcode-scanner__error {
|
|
60
|
+
position: absolute;
|
|
61
|
+
inset: 0;
|
|
62
|
+
display: flex;
|
|
63
|
+
align-items: center;
|
|
64
|
+
justify-content: center;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.ob-barcode-scanner__error-box {
|
|
68
|
+
width: 335px;
|
|
69
|
+
max-width: 100%;
|
|
70
|
+
border: 1px solid #3e3e3e;
|
|
71
|
+
border-radius: 3px;
|
|
72
|
+
background: #111111;
|
|
73
|
+
color: white;
|
|
74
|
+
text-align: center;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.ob-barcode-scanner__error-icon {
|
|
78
|
+
margin: 1rem 0;
|
|
79
|
+
color: #eee;
|
|
80
|
+
font-size: 3.5rem;
|
|
81
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { SVGProps } from "react";
|
|
2
2
|
|
|
3
|
-
export function Dead(props:
|
|
3
|
+
export function Dead(props: SVGProps<SVGSVGElement>) {
|
|
4
4
|
return (
|
|
5
|
-
<
|
|
5
|
+
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="currentColor" aria-hidden focusable="false" {...props}>
|
|
6
6
|
<path
|
|
7
7
|
fill="currentColor"
|
|
8
8
|
d="M12,2C6.47,2 2,6.47 2,12C2,17.53 6.47,22 12,22A10,10 0 0,0 22,12C22,6.47 17.5,2 12,2M6.76,8.82L7.82,7.76L8.88,8.82L9.94,7.76L11,8.82L9.94,9.88L11,10.94L9.94,12L8.88,10.94L7.82,12L6.76,10.94L7.82,9.88L6.76,8.82M6.89,17.5C7.69,15.46 9.67,14 12,14C14.33,14 16.31,15.46 17.11,17.5H6.89M17.24,10.94L16.18,12L15.12,10.94L14.06,12L13,10.94L14.06,9.88L13,8.82L14.06,7.76L15.12,8.82L16.18,7.76L17.24,8.82L16.18,9.88L17.24,10.94Z"
|
|
9
9
|
/>
|
|
10
|
-
</
|
|
10
|
+
</svg>
|
|
11
11
|
);
|
|
12
12
|
}
|
|
@@ -1,46 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Camera from "@mui/icons-material/Camera";
|
|
1
|
+
import { CameraIcon } from "@navikt/aksel-icons";
|
|
3
2
|
|
|
4
3
|
import { Dead as DeadIcon } from "./DeadIcon";
|
|
5
4
|
|
|
6
|
-
const BarcodeError = styled.div`
|
|
7
|
-
position: absolute;
|
|
8
|
-
bottom: 0;
|
|
9
|
-
left: 0;
|
|
10
|
-
right: 0;
|
|
11
|
-
top: 0;
|
|
12
|
-
|
|
13
|
-
display: flex;
|
|
14
|
-
align-items: center;
|
|
15
|
-
justify-content: center;
|
|
16
|
-
`;
|
|
17
|
-
|
|
18
|
-
const ErrorBox = styled.div`
|
|
19
|
-
background: #111111;
|
|
20
|
-
border: 1px solid #3e3e3e;
|
|
21
|
-
border-radius: 3px;
|
|
22
|
-
|
|
23
|
-
max-width: 100%;
|
|
24
|
-
width: 335px;
|
|
25
|
-
|
|
26
|
-
color: white;
|
|
27
|
-
text-align: center;
|
|
28
|
-
`;
|
|
29
|
-
|
|
30
5
|
export function ErrorIcon({ icon, message }: { icon?: string; message: string }) {
|
|
31
|
-
let Icon
|
|
32
|
-
|
|
33
|
-
if (icon === "camera") {
|
|
34
|
-
Icon = Camera;
|
|
35
|
-
}
|
|
6
|
+
let Icon = icon === "camera" ? CameraIcon : DeadIcon;
|
|
36
7
|
|
|
37
8
|
return (
|
|
38
|
-
<
|
|
39
|
-
<
|
|
40
|
-
<Icon
|
|
9
|
+
<div className="ob-barcode-scanner__error">
|
|
10
|
+
<div className="ob-barcode-scanner__error-box">
|
|
11
|
+
<Icon aria-hidden className="ob-barcode-scanner__error-icon" />
|
|
41
12
|
<br />
|
|
42
13
|
<p>{message}</p>
|
|
43
|
-
</
|
|
44
|
-
</
|
|
14
|
+
</div>
|
|
15
|
+
</div>
|
|
45
16
|
);
|
|
46
17
|
}
|
|
@@ -13,8 +13,15 @@ export type ZxingDecoderOptions = {
|
|
|
13
13
|
imageOptions?: ReaderOptions;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
/*
|
|
17
|
+
* `textMode: "Plain"` returns the symbol's content as encoded, with GS1 separators as U+001D -
|
|
18
|
+
* what `Giai.fromString` parses and the other engines return. zxing's own default ("HRI") gives
|
|
19
|
+
* the human-readable form instead: `(8004)7043210...` for a GS1 label and `<GS>` for a separator,
|
|
20
|
+
* neither of which resolves to an element.
|
|
21
|
+
*/
|
|
16
22
|
const defaultFrameOptions: ReaderOptions = {
|
|
17
23
|
formats: ["DataMatrix"],
|
|
24
|
+
textMode: "Plain",
|
|
18
25
|
tryHarder: false,
|
|
19
26
|
tryRotate: true,
|
|
20
27
|
tryInvert: false,
|
|
@@ -24,6 +31,7 @@ const defaultFrameOptions: ReaderOptions = {
|
|
|
24
31
|
|
|
25
32
|
const defaultImageOptions: ReaderOptions = {
|
|
26
33
|
formats: ["DataMatrix"],
|
|
34
|
+
textMode: "Plain",
|
|
27
35
|
tryHarder: true,
|
|
28
36
|
tryRotate: true,
|
|
29
37
|
tryInvert: true,
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { createDbrDecoder } from "./decoders/dbr";
|
|
4
|
+
import { createNativeDecoder, isNativeDecoderAvailable } from "./decoders/native";
|
|
5
|
+
|
|
6
|
+
const GS1_LABEL = "8004704321012345";
|
|
7
|
+
|
|
8
|
+
let globals = window as unknown as Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* A stand-in for the Dynamsoft Barcode Reader 6 SDK, served as a real script so the loader's
|
|
12
|
+
* script-tag path runs. It records what the decoder configures and decodes one GS1 label.
|
|
13
|
+
*/
|
|
14
|
+
function fakeDbrScript() {
|
|
15
|
+
let source = `
|
|
16
|
+
window.__dbr = { created: 0, deleted: 0, settings: null };
|
|
17
|
+
window.BarcodeReader = {
|
|
18
|
+
licenseKey: null,
|
|
19
|
+
EnumBarcodeFormat: { DATAMATRIX: 134217728, ALL: -1 },
|
|
20
|
+
async createInstance() {
|
|
21
|
+
window.__dbr.created++;
|
|
22
|
+
let settings = { mBarcodeFormatIds: -1, mAntiDamageLevel: 0, mDeblurLevel: 9 };
|
|
23
|
+
return {
|
|
24
|
+
getRuntimeSettings: () => ({ ...settings }),
|
|
25
|
+
async updateRuntimeSettings(next) { window.__dbr.settings = next; },
|
|
26
|
+
async decode() {
|
|
27
|
+
return [{
|
|
28
|
+
BarcodeText: "${GS1_LABEL}",
|
|
29
|
+
BarcodeFormat: 134217728,
|
|
30
|
+
LocalizationResult: { X1: 1, Y1: 2, X2: 3, Y2: 4, X3: 5, Y3: 6, X4: 7, Y4: 8 },
|
|
31
|
+
}];
|
|
32
|
+
},
|
|
33
|
+
deleteInstance() { window.__dbr.deleted++; },
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
`;
|
|
38
|
+
return URL.createObjectURL(new Blob([source], { type: "text/javascript" }));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function scriptsFor(url: string) {
|
|
42
|
+
return document.head.querySelectorAll(`script[src="${url}"]`).length;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
delete globals.BarcodeReader;
|
|
47
|
+
delete globals.__dbr;
|
|
48
|
+
delete globals.BarcodeDetector;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* The DBR loader remembers a successful load for the page's lifetime, so the failure cases run
|
|
53
|
+
* before the ones that load the SDK.
|
|
54
|
+
*/
|
|
55
|
+
describe("Dynamsoft engine (dbr)", () => {
|
|
56
|
+
it("reports a script that cannot be loaded, and tries again next time", async () => {
|
|
57
|
+
let missing = `${location.origin}/no-such-dbr-${Date.now()}.js`;
|
|
58
|
+
|
|
59
|
+
await expect(createDbrDecoder({ scriptUrl: missing, licenseKey: "key" })()).rejects.toThrow(
|
|
60
|
+
`Failed to load ${missing}`,
|
|
61
|
+
);
|
|
62
|
+
expect(scriptsFor(missing)).toBe(0);
|
|
63
|
+
|
|
64
|
+
let url = fakeDbrScript();
|
|
65
|
+
let decoder = await createDbrDecoder({ scriptUrl: url, licenseKey: "key" })();
|
|
66
|
+
expect(decoder.name).toBe("dbr");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("uses an SDK the page has already loaded instead of loading another", async () => {
|
|
70
|
+
let url = fakeDbrScript();
|
|
71
|
+
// Load it the way an article used to: a script tag in the page.
|
|
72
|
+
await new Promise<void>((resolve) => {
|
|
73
|
+
let script = document.createElement("script");
|
|
74
|
+
script.src = url;
|
|
75
|
+
script.onload = () => resolve();
|
|
76
|
+
document.head.append(script);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
await createDbrDecoder({ scriptUrl: "/lib/dbr/should-not-load.js", licenseKey: "key" })();
|
|
80
|
+
|
|
81
|
+
expect(scriptsFor("/lib/dbr/should-not-load.js")).toBe(0);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("sets the licence, reads DataMatrix only and maps what it finds", async () => {
|
|
85
|
+
let url = fakeDbrScript();
|
|
86
|
+
await new Promise<void>((resolve) => {
|
|
87
|
+
let script = document.createElement("script");
|
|
88
|
+
script.src = url;
|
|
89
|
+
script.onload = () => resolve();
|
|
90
|
+
document.head.append(script);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
let decoder = await createDbrDecoder({ scriptUrl: url, licenseKey: "license-123" })();
|
|
94
|
+
let results = await decoder.decodeFrame(document.createElement("canvas"));
|
|
95
|
+
|
|
96
|
+
let sdk = globals.BarcodeReader as { licenseKey: string };
|
|
97
|
+
let recorded = globals.__dbr as { settings: Record<string, number>; deleted: number };
|
|
98
|
+
expect(sdk.licenseKey).toBe("license-123");
|
|
99
|
+
expect(recorded.settings).toMatchObject({ mBarcodeFormatIds: 134217728, mAntiDamageLevel: 3, mDeblurLevel: 0 });
|
|
100
|
+
expect(results).toEqual([
|
|
101
|
+
{
|
|
102
|
+
text: GS1_LABEL,
|
|
103
|
+
format: "134217728",
|
|
104
|
+
corners: [
|
|
105
|
+
{ x: 1, y: 2 },
|
|
106
|
+
{ x: 3, y: 4 },
|
|
107
|
+
{ x: 5, y: 6 },
|
|
108
|
+
{ x: 7, y: 8 },
|
|
109
|
+
],
|
|
110
|
+
},
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
decoder.dispose?.();
|
|
114
|
+
expect(recorded.deleted).toBe(1);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/** A stand-in for the Barcode Detection API as Chrome on Android exposes it. */
|
|
119
|
+
function installBarcodeDetector(formats: string[], detected: unknown[] = []) {
|
|
120
|
+
let constructed: unknown[] = [];
|
|
121
|
+
class FakeBarcodeDetector {
|
|
122
|
+
static getSupportedFormats = vi.fn(async () => formats);
|
|
123
|
+
constructor(options: unknown) {
|
|
124
|
+
constructed.push(options);
|
|
125
|
+
}
|
|
126
|
+
detect = vi.fn(async () => detected);
|
|
127
|
+
}
|
|
128
|
+
globals.BarcodeDetector = FakeBarcodeDetector;
|
|
129
|
+
return { constructed };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
describe("native engine (Barcode Detection API)", () => {
|
|
133
|
+
it("is unavailable where the browser has no BarcodeDetector", async () => {
|
|
134
|
+
expect(await isNativeDecoderAvailable()).toBe(false);
|
|
135
|
+
await expect(createNativeDecoder()()).rejects.toThrow("This browser cannot detect barcodes natively");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("is unavailable where BarcodeDetector cannot read DataMatrix, as on desktop Chrome", async () => {
|
|
139
|
+
installBarcodeDetector(["qr_code"]);
|
|
140
|
+
|
|
141
|
+
expect(await isNativeDecoderAvailable()).toBe(false);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("is unavailable when asking for the formats fails", async () => {
|
|
145
|
+
installBarcodeDetector([]);
|
|
146
|
+
(globals.BarcodeDetector as { getSupportedFormats: () => Promise<string[]> }).getSupportedFormats = async () => {
|
|
147
|
+
throw new Error("not allowed");
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
expect(await isNativeDecoderAvailable()).toBe(false);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("reads DataMatrix only, and maps the corners it reports", async () => {
|
|
154
|
+
let { constructed } = installBarcodeDetector(
|
|
155
|
+
["qr_code", "data_matrix"],
|
|
156
|
+
[
|
|
157
|
+
{
|
|
158
|
+
rawValue: GS1_LABEL,
|
|
159
|
+
format: "data_matrix",
|
|
160
|
+
boundingBox: { left: 0, top: 0, right: 10, bottom: 10 },
|
|
161
|
+
cornerPoints: [
|
|
162
|
+
{ x: 1, y: 2 },
|
|
163
|
+
{ x: 3, y: 4 },
|
|
164
|
+
{ x: 5, y: 6 },
|
|
165
|
+
{ x: 7, y: 8 },
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
let decoder = await createNativeDecoder()();
|
|
172
|
+
let results = await decoder.decodeFrame(document.createElement("canvas"));
|
|
173
|
+
|
|
174
|
+
expect(decoder.name).toBe("native");
|
|
175
|
+
expect(constructed).toEqual([{ formats: ["data_matrix"] }]);
|
|
176
|
+
expect(results[0]).toMatchObject({ text: GS1_LABEL, format: "data_matrix" });
|
|
177
|
+
expect(results[0]!.corners[2]).toEqual({ x: 5, y: 6 });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("falls back to the bounding box when the corners are missing", async () => {
|
|
181
|
+
installBarcodeDetector(
|
|
182
|
+
["data_matrix"],
|
|
183
|
+
[
|
|
184
|
+
{
|
|
185
|
+
rawValue: GS1_LABEL,
|
|
186
|
+
format: "data_matrix",
|
|
187
|
+
boundingBox: { left: 10, top: 20, right: 110, bottom: 120 },
|
|
188
|
+
cornerPoints: [],
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
let decoder = await createNativeDecoder()();
|
|
194
|
+
let [result] = await decoder.decodeFrame(document.createElement("canvas"));
|
|
195
|
+
|
|
196
|
+
expect(result!.corners).toEqual([
|
|
197
|
+
{ x: 10, y: 20 },
|
|
198
|
+
{ x: 110, y: 20 },
|
|
199
|
+
{ x: 110, y: 120 },
|
|
200
|
+
{ x: 10, y: 120 },
|
|
201
|
+
]);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("decodes a photo as well as camera frames", async () => {
|
|
205
|
+
installBarcodeDetector(
|
|
206
|
+
["data_matrix"],
|
|
207
|
+
[
|
|
208
|
+
{
|
|
209
|
+
rawValue: GS1_LABEL,
|
|
210
|
+
format: "data_matrix",
|
|
211
|
+
boundingBox: { left: 0, top: 0, right: 1, bottom: 1 },
|
|
212
|
+
cornerPoints: [],
|
|
213
|
+
},
|
|
214
|
+
],
|
|
215
|
+
);
|
|
216
|
+
let canvas = new OffscreenCanvas(8, 8);
|
|
217
|
+
canvas.getContext("2d")!.fillRect(0, 0, 8, 8);
|
|
218
|
+
let photo = await canvas.convertToBlob({ type: "image/png" });
|
|
219
|
+
|
|
220
|
+
let decoder = await createNativeDecoder()();
|
|
221
|
+
let results = await decoder.decodeImage(photo);
|
|
222
|
+
|
|
223
|
+
expect(results.map((r) => r.text)).toEqual([GS1_LABEL]);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import "./BarcodeScanner.css";
|
|
2
|
+
|
|
3
|
+
import { ArrowsCirclepathIcon } from "@navikt/aksel-icons";
|
|
3
4
|
import { type ReactNode, useRef } from "react";
|
|
4
5
|
import type { DecoderFactory } from "./decoder";
|
|
5
6
|
import { ErrorIcon } from "./Error";
|
|
@@ -22,69 +23,39 @@ export type BarcodeScannerProps = {
|
|
|
22
23
|
actions?: ReactNode;
|
|
23
24
|
};
|
|
24
25
|
|
|
25
|
-
const frameSx: SxProps = {
|
|
26
|
-
position: "absolute",
|
|
27
|
-
bottom: 0,
|
|
28
|
-
left: 0,
|
|
29
|
-
right: 0,
|
|
30
|
-
top: 0,
|
|
31
|
-
height: "100%",
|
|
32
|
-
width: "100%",
|
|
33
|
-
objectFit: "contain",
|
|
34
|
-
};
|
|
35
|
-
|
|
36
26
|
export function BarcodeScanner({ onRead, decoder, actions }: BarcodeScannerProps) {
|
|
37
27
|
let video = useRef<HTMLVideoElement | null>(null);
|
|
38
28
|
let canvas = useRef<HTMLCanvasElement | null>(null);
|
|
39
29
|
let { devices, engine, error, setNextDevice } = useScanner(video, canvas, onRead, decoder);
|
|
40
30
|
|
|
41
31
|
return (
|
|
42
|
-
<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
height: "100%",
|
|
47
|
-
}}
|
|
48
|
-
>
|
|
49
|
-
<Box sx={{ height: "100%" }}>
|
|
50
|
-
<Box component="video" playsInline muted autoPlay sx={frameSx} ref={video} />
|
|
51
|
-
<Box component="canvas" sx={frameSx} ref={canvas} />
|
|
32
|
+
<div className="ob-barcode-scanner">
|
|
33
|
+
<div className="ob-barcode-scanner__frames">
|
|
34
|
+
<video playsInline muted autoPlay className="ob-barcode-scanner__frame" ref={video} />
|
|
35
|
+
<canvas className="ob-barcode-scanner__frame" ref={canvas} />
|
|
52
36
|
{error && <ErrorIcon {...error} />}
|
|
53
|
-
</
|
|
54
|
-
<
|
|
55
|
-
sx={{
|
|
56
|
-
display: "flex",
|
|
57
|
-
alignItems: "center",
|
|
58
|
-
justifyContent: "space-between",
|
|
59
|
-
position: "absolute",
|
|
60
|
-
bottom: 0,
|
|
61
|
-
left: 0,
|
|
62
|
-
right: 0,
|
|
63
|
-
py: 1,
|
|
64
|
-
px: 3,
|
|
65
|
-
color: "white",
|
|
66
|
-
}}
|
|
67
|
-
>
|
|
37
|
+
</div>
|
|
38
|
+
<div className="ob-barcode-scanner__footer">
|
|
68
39
|
{devices.length <= 1 && <span />}
|
|
69
40
|
{devices.length > 1 && (
|
|
70
|
-
<
|
|
71
|
-
|
|
41
|
+
<button
|
|
42
|
+
type="button"
|
|
43
|
+
className="ob-barcode-scanner__icon-button"
|
|
72
44
|
aria-label={getLocalizedString("Switch camera")}
|
|
73
45
|
onClick={() => setNextDevice()}
|
|
74
|
-
size="large"
|
|
75
46
|
>
|
|
76
|
-
<
|
|
77
|
-
</
|
|
47
|
+
<ArrowsCirclepathIcon aria-hidden />
|
|
48
|
+
</button>
|
|
78
49
|
)}
|
|
79
|
-
<
|
|
50
|
+
<div className="ob-barcode-scanner__actions">
|
|
80
51
|
{engine && (
|
|
81
|
-
<
|
|
52
|
+
<span className="ob-barcode-scanner__engine" data-testid="barcode-engine">
|
|
82
53
|
{engine}
|
|
83
|
-
</
|
|
54
|
+
</span>
|
|
84
55
|
)}
|
|
85
56
|
{actions}
|
|
86
|
-
</
|
|
87
|
-
</
|
|
88
|
-
</
|
|
57
|
+
</div>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
89
60
|
);
|
|
90
61
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { useHIDScanner } from "./useHIDScanner";
|
|
5
|
+
|
|
6
|
+
/** A GIAI-96 tag as an RFID reader in keyboard (HID) mode types it: the EPC in hex, then Enter. */
|
|
7
|
+
const EPC = "3415ADE22800000000003039";
|
|
8
|
+
|
|
9
|
+
function type(text: string) {
|
|
10
|
+
for (let key of text) {
|
|
11
|
+
document.body.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function pressEnter() {
|
|
16
|
+
document.body.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
vi.useFakeTimers();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
vi.useRealTimers();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("useHIDScanner", () => {
|
|
28
|
+
it("reads what the scanner types up to Enter", () => {
|
|
29
|
+
let onRead = vi.fn();
|
|
30
|
+
renderHook(() => useHIDScanner(onRead, true));
|
|
31
|
+
|
|
32
|
+
type(EPC);
|
|
33
|
+
pressEnter();
|
|
34
|
+
|
|
35
|
+
expect(onRead).toHaveBeenCalledExactlyOnceWith(EPC);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("reads scans one after another", () => {
|
|
39
|
+
let onRead = vi.fn();
|
|
40
|
+
renderHook(() => useHIDScanner(onRead, true));
|
|
41
|
+
|
|
42
|
+
type(EPC);
|
|
43
|
+
pressEnter();
|
|
44
|
+
type("8004704321012345");
|
|
45
|
+
pressEnter();
|
|
46
|
+
|
|
47
|
+
expect(onRead.mock.calls).toEqual([[EPC], ["8004704321012345"]]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("keeps the 0x prefix some readers send", () => {
|
|
51
|
+
let onRead = vi.fn();
|
|
52
|
+
renderHook(() => useHIDScanner(onRead, true));
|
|
53
|
+
|
|
54
|
+
type(`0x${EPC}`);
|
|
55
|
+
pressEnter();
|
|
56
|
+
|
|
57
|
+
expect(onRead).toHaveBeenCalledWith(`0x${EPC}`);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("drops keys that cannot be part of a tag", () => {
|
|
61
|
+
let onRead = vi.fn();
|
|
62
|
+
renderHook(() => useHIDScanner(onRead, true));
|
|
63
|
+
|
|
64
|
+
type("34-15 ADE2");
|
|
65
|
+
pressEnter();
|
|
66
|
+
|
|
67
|
+
expect(onRead).toHaveBeenCalledWith("3415ADE2");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("forgets a partial read after half a second without keys", () => {
|
|
71
|
+
let onRead = vi.fn();
|
|
72
|
+
renderHook(() => useHIDScanner(onRead, true));
|
|
73
|
+
|
|
74
|
+
type("3415");
|
|
75
|
+
vi.advanceTimersByTime(600);
|
|
76
|
+
type(EPC);
|
|
77
|
+
pressEnter();
|
|
78
|
+
|
|
79
|
+
expect(onRead).toHaveBeenCalledExactlyOnceWith(EPC);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("keeps reading while the keys come quickly", () => {
|
|
83
|
+
let onRead = vi.fn();
|
|
84
|
+
renderHook(() => useHIDScanner(onRead, true));
|
|
85
|
+
|
|
86
|
+
for (let key of EPC) {
|
|
87
|
+
type(key);
|
|
88
|
+
vi.advanceTimersByTime(400);
|
|
89
|
+
}
|
|
90
|
+
pressEnter();
|
|
91
|
+
|
|
92
|
+
expect(onRead).toHaveBeenCalledWith(EPC);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("ignores Enter with nothing scanned", () => {
|
|
96
|
+
let onRead = vi.fn();
|
|
97
|
+
renderHook(() => useHIDScanner(onRead, true));
|
|
98
|
+
|
|
99
|
+
pressEnter();
|
|
100
|
+
|
|
101
|
+
expect(onRead).not.toHaveBeenCalled();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("listens only while enabled", () => {
|
|
105
|
+
let onRead = vi.fn();
|
|
106
|
+
let { rerender, unmount } = renderHook(({ enabled }) => useHIDScanner(onRead, enabled), {
|
|
107
|
+
initialProps: { enabled: false },
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
type(EPC);
|
|
111
|
+
pressEnter();
|
|
112
|
+
expect(onRead).not.toHaveBeenCalled();
|
|
113
|
+
|
|
114
|
+
rerender({ enabled: true });
|
|
115
|
+
type(EPC);
|
|
116
|
+
pressEnter();
|
|
117
|
+
expect(onRead).toHaveBeenCalledOnce();
|
|
118
|
+
|
|
119
|
+
unmount();
|
|
120
|
+
type(EPC);
|
|
121
|
+
pressEnter();
|
|
122
|
+
expect(onRead).toHaveBeenCalledOnce();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -16,7 +16,8 @@ export function useHIDScanner(onRead: (value: string) => void, enabled = false)
|
|
|
16
16
|
buffer = "";
|
|
17
17
|
}, 500);
|
|
18
18
|
} else if (event.key === "Enter") {
|
|
19
|
-
|
|
19
|
+
// An Enter with nothing scanned (a stray key press) is not a read.
|
|
20
|
+
if (buffer) onRead(buffer);
|
|
20
21
|
buffer = "";
|
|
21
22
|
}
|
|
22
23
|
}
|