@nocobase/client-v2 2.2.0-beta.14 → 2.2.0-beta.16
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/es/components/category-tabs/SortableCategoryTabs.d.ts +55 -0
- package/es/components/category-tabs/index.d.ts +9 -0
- package/es/components/form/ScanInput/useCodeScanner.d.ts +2 -2
- package/es/components/form/TypedVariableInput.d.ts +31 -5
- package/es/components/form/filter/CollectionFilter.d.ts +2 -0
- package/es/components/form/filter/CollectionFilterItem.d.ts +11 -1
- package/es/components/form/filter/CollectionFilterPanel.d.ts +2 -0
- package/es/components/form/filter/index.d.ts +2 -0
- package/es/components/form/filter/useFilterActionProps.d.ts +2 -0
- package/es/components/index.d.ts +1 -0
- package/es/flow/actions/index.d.ts +1 -1
- package/es/flow/actions/linkageRules.d.ts +3 -0
- package/es/flow/admin-shell/admin-layout/AdminLayoutMenuModels.d.ts +1 -0
- package/es/flow/components/FieldAssignExactDatePicker.d.ts +1 -0
- package/es/flow/components/FieldAssignValueInput.d.ts +9 -0
- package/es/flow/components/FlowRoute.d.ts +2 -2
- package/es/flow/components/RunJSValueEditor.d.ts +1 -0
- package/es/flow/components/filter/FilterGroup.d.ts +1 -0
- package/es/flow/components/filter/VariableFilterItem.d.ts +6 -1
- package/es/flow/index.d.ts +1 -0
- package/es/flow/internal/registerDeviceTypeContext.d.ts +10 -0
- package/es/flow/models/base/ActionModelCore.d.ts +4 -2
- package/es/flow/models/base/BlockGridModel.d.ts +3 -0
- package/es/flow/models/base/PageModel/PageModel.d.ts +12 -0
- package/es/flow/models/base/PageModel/PageModelTabBar.d.ts +22 -0
- package/es/flow/models/base/PageModel/PageTabModel.d.ts +12 -0
- package/es/flow/models/blocks/table/TableColumnModel.d.ts +1 -0
- package/es/flow/models/fields/DateTimeFieldModel/dateLimit.d.ts +15 -7
- package/es/flow-compat/routeTypes.d.ts +1 -0
- package/es/index.d.ts +4 -0
- package/es/index.mjs +143 -106
- package/es/utils/getRouteRuntimeVersion.d.ts +23 -0
- package/es/utils/index.d.ts +1 -0
- package/lib/index.js +154 -117
- package/lib/locale/languageCodes.js +3 -1
- package/package.json +8 -7
- package/src/__tests__/browserChecker.test.ts +61 -0
- package/src/__tests__/getRouteRuntimeVersion.test.ts +68 -0
- package/src/__tests__/nocobase-buildin-plugin-auth.test.tsx +7 -2
- package/src/components/README.md +7 -1
- package/src/components/README.zh-CN.md +6 -1
- package/src/components/category-tabs/SortableCategoryTabs.tsx +210 -0
- package/src/components/category-tabs/index.ts +10 -0
- package/src/components/form/JsonTextArea.tsx +4 -0
- package/src/components/form/ScanInput/CodeScanner.tsx +61 -34
- package/src/components/form/ScanInput/__tests__/CodeScanner.test.tsx +101 -0
- package/src/components/form/ScanInput/__tests__/useCodeScanner.test.tsx +114 -18
- package/src/components/form/ScanInput/useCodeScanner.ts +166 -23
- package/src/components/form/TypedVariableInput.tsx +452 -101
- package/src/components/form/__tests__/TypedVariableInput.test.tsx +364 -12
- package/src/components/form/filter/CollectionFilter.tsx +4 -0
- package/src/components/form/filter/CollectionFilterItem.tsx +32 -7
- package/src/components/form/filter/CollectionFilterPanel.tsx +4 -0
- package/src/components/form/filter/__tests__/CollectionFilterItem.test.tsx +38 -0
- package/src/components/form/filter/__tests__/useFilterActionProps.test.tsx +95 -0
- package/src/components/form/filter/index.ts +2 -0
- package/src/components/form/filter/useFilterActionProps.ts +37 -6
- package/src/components/index.ts +1 -0
- package/src/flow/__tests__/FlowRoute.test.tsx +126 -8
- package/src/flow/__tests__/PluginFlowEngine.test.ts +58 -0
- package/src/flow/actions/__tests__/actionLinkageRules.forkProps.test.ts +314 -0
- package/src/flow/actions/__tests__/dataScopeFormValueClear.test.ts +102 -0
- package/src/flow/actions/__tests__/linkageRules.tab.test.ts +171 -0
- package/src/flow/actions/index.ts +2 -0
- package/src/flow/actions/linkageRules.tsx +111 -16
- package/src/flow/admin-shell/admin-layout/AdminLayoutComponent.tsx +0 -1
- package/src/flow/admin-shell/admin-layout/__tests__/AdminLayoutComponent.test.tsx +253 -6
- package/src/flow/common/Markdown/style.ts +4 -0
- package/src/flow/components/BlockItemCard.tsx +2 -2
- package/src/flow/components/FieldAssignExactDatePicker.tsx +25 -11
- package/src/flow/components/FieldAssignValueInput.tsx +76 -9
- package/src/flow/components/FlowRoute.tsx +71 -29
- package/src/flow/components/RunJSValueEditor.tsx +9 -1
- package/src/flow/components/__tests__/FieldAssignValueInput.context.test.tsx +84 -2
- package/src/flow/components/filter/FilterGroup.tsx +33 -5
- package/src/flow/components/filter/VariableFilterItem.tsx +158 -10
- package/src/flow/components/filter/__tests__/FilterGroup.test.tsx +45 -0
- package/src/flow/components/filter/__tests__/VariableFilterItem.leftMetaTree.test.tsx +9 -0
- package/src/flow/components/filter/__tests__/VariableFilterItem.test.tsx +70 -1
- package/src/flow/index.ts +3 -0
- package/src/flow/internal/registerDeviceTypeContext.ts +39 -0
- package/src/flow/models/base/ActionModelCore.tsx +11 -2
- package/src/flow/models/base/BlockGridModel.tsx +26 -0
- package/src/flow/models/base/CollectionBlockModel.tsx +6 -2
- package/src/flow/models/base/PageModel/PageModel.tsx +387 -53
- package/src/flow/models/base/PageModel/PageModelTabBar.tsx +114 -0
- package/src/flow/models/base/PageModel/PageTabModel.tsx +249 -18
- package/src/flow/models/base/PageModel/__tests__/PageModel.test.ts +790 -10
- package/src/flow/models/base/PageModel/__tests__/PageModelTabBar.module-isolation.test.tsx +130 -0
- package/src/flow/models/base/PageModel/__tests__/PageModelTabBar.test.tsx +118 -0
- package/src/flow/models/base/PageModel/__tests__/PageTabModel.test.ts +1075 -7
- package/src/flow/models/base/__tests__/ActionModelCore.render.test.tsx +49 -0
- package/src/flow/models/base/__tests__/BlockGridModel.selectSceneActivation.test.ts +124 -0
- package/src/flow/models/base/__tests__/CollectionBlockModel.addAppends.test.ts +41 -0
- package/src/flow/models/blocks/filter-form/FilterFormBlockModel.tsx +27 -3
- package/src/flow/models/blocks/filter-form/__tests__/defaultValues.wiring.test.ts +32 -4
- package/src/flow/models/blocks/js-block/JSBlock.tsx +1 -1
- package/src/flow/models/blocks/table/TableBlockModel.tsx +3 -1
- package/src/flow/models/blocks/table/TableColumnModel.tsx +30 -1
- package/src/flow/models/blocks/table/__tests__/TableBlockModel.quickEditRefresh.test.ts +12 -0
- package/src/flow/models/blocks/table/__tests__/TableColumnModel.test.tsx +33 -0
- package/src/flow/models/fields/DateTimeFieldModel/__tests__/DateTimeNoTzFieldModel.dateLimit.test.tsx +149 -0
- package/src/flow/models/fields/DateTimeFieldModel/dateLimit.ts +149 -113
- package/src/flow/models/fields/JsonFieldModel.tsx +31 -8
- package/src/flow/models/fields/VariableFieldFormModel.tsx +3 -3
- package/src/flow/models/fields/__tests__/JsonFieldModel.test.ts +82 -0
- package/src/flow/models/fields/__tests__/VariableFieldFormModel.test.tsx +51 -0
- package/src/flow/utils/dataScopeFormValueClear.ts +4 -3
- package/src/flow-compat/routeTypes.ts +1 -0
- package/src/index.ts +4 -0
- package/src/locale/languageCodes.ts +3 -1
- package/src/nocobase-buildin-plugin/index.tsx +2 -0
- package/src/utils/getRouteRuntimeVersion.ts +185 -0
- package/src/utils/index.tsx +1 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { act, render, waitFor } from '@testing-library/react';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
13
|
+
import { CodeScanner } from '../CodeScanner';
|
|
14
|
+
|
|
15
|
+
const mocks = vi.hoisted(() => ({
|
|
16
|
+
getCameras: vi.fn().mockResolvedValue([{ id: 'rear-camera' }]),
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
vi.mock('html5-qrcode', () => ({
|
|
20
|
+
Html5Qrcode: {
|
|
21
|
+
getCameras: mocks.getCameras,
|
|
22
|
+
},
|
|
23
|
+
Html5QrcodeScannerState: {
|
|
24
|
+
PAUSED: 3,
|
|
25
|
+
SCANNING: 2,
|
|
26
|
+
},
|
|
27
|
+
Html5QrcodeSupportedFormats: {
|
|
28
|
+
CODABAR: 2,
|
|
29
|
+
CODE_128: 5,
|
|
30
|
+
CODE_39: 3,
|
|
31
|
+
CODE_93: 4,
|
|
32
|
+
DATA_MATRIX: 6,
|
|
33
|
+
EAN_13: 9,
|
|
34
|
+
EAN_8: 10,
|
|
35
|
+
ITF: 8,
|
|
36
|
+
PDF_417: 11,
|
|
37
|
+
QR_CODE: 0,
|
|
38
|
+
UPC_A: 14,
|
|
39
|
+
UPC_E: 15,
|
|
40
|
+
},
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
vi.mock('react-i18next', () => ({
|
|
44
|
+
useTranslation: () => ({ t: (key: string) => key }),
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
vi.mock('../useCodeScanner', async (importOriginal) => {
|
|
48
|
+
const actual = await importOriginal<typeof import('../useCodeScanner')>();
|
|
49
|
+
const ReactModule = await import('react');
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
...actual,
|
|
53
|
+
useCodeScanner: ({
|
|
54
|
+
enabled,
|
|
55
|
+
onScannerSizeChanged,
|
|
56
|
+
}: {
|
|
57
|
+
enabled: boolean;
|
|
58
|
+
onScannerSizeChanged?: (size: { width: number; height: number }) => void;
|
|
59
|
+
}) => {
|
|
60
|
+
ReactModule.useEffect(() => {
|
|
61
|
+
if (enabled) {
|
|
62
|
+
onScannerSizeChanged?.({ width: 1280, height: 720 });
|
|
63
|
+
}
|
|
64
|
+
}, [enabled, onScannerSizeChanged]);
|
|
65
|
+
|
|
66
|
+
return { startScanFile: vi.fn() };
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('CodeScanner', () => {
|
|
72
|
+
afterEach(() => {
|
|
73
|
+
vi.restoreAllMocks();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('keeps the camera preview covering portrait and landscape viewports', async () => {
|
|
77
|
+
let viewportWidth = 390;
|
|
78
|
+
let viewportHeight = 844;
|
|
79
|
+
vi.spyOn(window, 'innerWidth', 'get').mockImplementation(() => viewportWidth);
|
|
80
|
+
vi.spyOn(window, 'innerHeight', 'get').mockImplementation(() => viewportHeight);
|
|
81
|
+
|
|
82
|
+
render(<CodeScanner visible onClose={() => undefined} onScanSuccess={() => undefined} />);
|
|
83
|
+
|
|
84
|
+
await waitFor(() => expect(document.getElementById('code-scan-box')).toBeInTheDocument());
|
|
85
|
+
|
|
86
|
+
const scannerElement = document.querySelector<HTMLElement>('[id^="code-scanner-"]');
|
|
87
|
+
const scannerSurface = scannerElement?.parentElement;
|
|
88
|
+
const scanBox = document.getElementById('code-scan-box');
|
|
89
|
+
expect(scannerSurface?.style.transform).toBe(`translate(-50%, -50%) scale(${844 / 720})`);
|
|
90
|
+
expect(scanBox).toHaveStyle({ width: '351px', height: '540px' });
|
|
91
|
+
|
|
92
|
+
viewportWidth = 844;
|
|
93
|
+
viewportHeight = 390;
|
|
94
|
+
act(() => window.dispatchEvent(new Event('resize')));
|
|
95
|
+
|
|
96
|
+
await waitFor(() => {
|
|
97
|
+
expect(scannerSurface?.style.transform).toBe(`translate(-50%, -50%) scale(${844 / 1280})`);
|
|
98
|
+
expect(scanBox).toHaveStyle({ width: '759px', height: '273px' });
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -7,13 +7,15 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
10
|
+
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
11
11
|
import React, { useCallback } from 'react';
|
|
12
12
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
13
|
-
import { DEFAULT_CODE_FORMATS, getCodeScanBoxSize, useCodeScanner } from '../useCodeScanner';
|
|
13
|
+
import { DEFAULT_CODE_FORMATS, getCodeScanBoxSize, scanQrVideoFrame, useCodeScanner } from '../useCodeScanner';
|
|
14
14
|
|
|
15
15
|
type MockScannerInstance = {
|
|
16
|
+
applyVideoConstraints: ReturnType<typeof vi.fn>;
|
|
16
17
|
clear: ReturnType<typeof vi.fn>;
|
|
18
|
+
getRunningTrackCapabilities: ReturnType<typeof vi.fn>;
|
|
17
19
|
getState: ReturnType<typeof vi.fn>;
|
|
18
20
|
scanFileV2: ReturnType<typeof vi.fn>;
|
|
19
21
|
start: ReturnType<typeof vi.fn>;
|
|
@@ -24,18 +26,24 @@ const mocks = vi.hoisted(() => {
|
|
|
24
26
|
const start = vi.fn().mockResolvedValue(null);
|
|
25
27
|
const stop = vi.fn().mockResolvedValue(null);
|
|
26
28
|
const clear = vi.fn();
|
|
29
|
+
const applyVideoConstraints = vi.fn().mockResolvedValue(undefined);
|
|
30
|
+
const getRunningTrackCapabilities = vi.fn(() => ({}));
|
|
27
31
|
const getState = vi.fn(() => 1);
|
|
28
32
|
const scanFileV2 = vi.fn();
|
|
29
33
|
const Html5Qrcode = vi.fn(function MockHtml5Qrcode(this: MockScannerInstance) {
|
|
30
34
|
this.start = start;
|
|
31
35
|
this.stop = stop;
|
|
32
36
|
this.clear = clear;
|
|
37
|
+
this.applyVideoConstraints = applyVideoConstraints;
|
|
38
|
+
this.getRunningTrackCapabilities = getRunningTrackCapabilities;
|
|
33
39
|
this.getState = getState;
|
|
34
40
|
this.scanFileV2 = scanFileV2;
|
|
35
41
|
});
|
|
36
42
|
|
|
37
43
|
return {
|
|
44
|
+
applyVideoConstraints,
|
|
38
45
|
clear,
|
|
46
|
+
getRunningTrackCapabilities,
|
|
39
47
|
getState,
|
|
40
48
|
Html5Qrcode,
|
|
41
49
|
scanFileV2,
|
|
@@ -77,18 +85,15 @@ vi.mock('jsqr', () => jsQrMocks);
|
|
|
77
85
|
function ScannerHost({
|
|
78
86
|
onCameraStartFailure,
|
|
79
87
|
onScanFailure,
|
|
80
|
-
scanBoxSize,
|
|
81
88
|
}: {
|
|
82
89
|
onCameraStartFailure?: (error: unknown) => void;
|
|
83
90
|
onScanFailure?: () => void;
|
|
84
|
-
scanBoxSize?: { width: number; height: number };
|
|
85
91
|
} = {}) {
|
|
86
92
|
const handleScanSuccess = useCallback(() => undefined, []);
|
|
87
93
|
|
|
88
94
|
useCodeScanner({
|
|
89
95
|
elementId: 'scanner',
|
|
90
96
|
enabled: true,
|
|
91
|
-
scanBoxSize,
|
|
92
97
|
onCameraStartFailure,
|
|
93
98
|
onScanFailure,
|
|
94
99
|
onScanSuccess: handleScanSuccess,
|
|
@@ -159,6 +164,11 @@ function stubCanvas() {
|
|
|
159
164
|
describe('useCodeScanner', () => {
|
|
160
165
|
beforeEach(() => {
|
|
161
166
|
vi.clearAllMocks();
|
|
167
|
+
mocks.applyVideoConstraints.mockResolvedValue(undefined);
|
|
168
|
+
mocks.getRunningTrackCapabilities.mockReturnValue({});
|
|
169
|
+
mocks.start.mockResolvedValue(null);
|
|
170
|
+
mocks.stop.mockResolvedValue(null);
|
|
171
|
+
mocks.getState.mockReturnValue(1);
|
|
162
172
|
jsQrMocks.default.mockReturnValue(undefined);
|
|
163
173
|
});
|
|
164
174
|
|
|
@@ -188,32 +198,73 @@ describe('useCodeScanner', () => {
|
|
|
188
198
|
| { qrbox?: (width: number, height: number) => { width: number; height: number } }
|
|
189
199
|
| undefined;
|
|
190
200
|
|
|
191
|
-
expect(config?.qrbox?.(
|
|
192
|
-
expect(getCodeScanBoxSize(
|
|
201
|
+
expect(config?.qrbox?.(1280, 720)).toEqual({ width: 1152, height: 504 });
|
|
202
|
+
expect(getCodeScanBoxSize(1280, 720)).toEqual({ width: 1152, height: 504 });
|
|
193
203
|
});
|
|
194
204
|
|
|
195
|
-
it('
|
|
196
|
-
render(<ScannerHost
|
|
205
|
+
it('uses optimized camera constraints without requiring a user-selected scan mode', async () => {
|
|
206
|
+
render(<ScannerHost />);
|
|
197
207
|
|
|
198
208
|
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
199
209
|
|
|
200
210
|
const config = mocks.start.mock.calls[0]?.[1] as
|
|
201
|
-
| {
|
|
211
|
+
| {
|
|
212
|
+
disableFlip?: boolean;
|
|
213
|
+
fps?: number;
|
|
214
|
+
qrbox?: (width: number, height: number) => { width: number; height: number };
|
|
215
|
+
videoConstraints?: MediaTrackConstraints;
|
|
216
|
+
}
|
|
202
217
|
| undefined;
|
|
203
218
|
|
|
204
|
-
expect(config?.qrbox?.(
|
|
219
|
+
expect(config?.qrbox?.(1280, 720)).toEqual({ width: 1152, height: 504 });
|
|
220
|
+
expect(config).toMatchObject({
|
|
221
|
+
disableFlip: false,
|
|
222
|
+
fps: 8,
|
|
223
|
+
videoConstraints: {
|
|
224
|
+
facingMode: { ideal: 'environment' },
|
|
225
|
+
width: { ideal: 1920 },
|
|
226
|
+
height: { ideal: 1080 },
|
|
227
|
+
frameRate: { ideal: 30 },
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
expect(mocks.Html5Qrcode).toHaveBeenCalledWith('scanner', {
|
|
231
|
+
formatsToSupport: DEFAULT_CODE_FORMATS,
|
|
232
|
+
verbose: false,
|
|
233
|
+
});
|
|
205
234
|
});
|
|
206
235
|
|
|
207
|
-
it('
|
|
208
|
-
|
|
236
|
+
it('decodes a centered QR code from the raw camera frame fast path', () => {
|
|
237
|
+
jsQrMocks.default.mockReturnValueOnce({ data: 'FAST-QR' });
|
|
238
|
+
stubCanvas();
|
|
239
|
+
const video = document.createElement('video');
|
|
240
|
+
Object.defineProperties(video, {
|
|
241
|
+
clientHeight: { value: 720 },
|
|
242
|
+
clientWidth: { value: 1280 },
|
|
243
|
+
readyState: { value: HTMLMediaElement.HAVE_CURRENT_DATA },
|
|
244
|
+
videoHeight: { value: 1080 },
|
|
245
|
+
videoWidth: { value: 1920 },
|
|
246
|
+
});
|
|
247
|
+
const canvas = document.createElement('canvas');
|
|
248
|
+
|
|
249
|
+
expect(scanQrVideoFrame(video, canvas)).toBe('FAST-QR');
|
|
209
250
|
|
|
210
|
-
|
|
251
|
+
const context = canvas.getContext('2d');
|
|
252
|
+
expect(context?.drawImage).toHaveBeenCalledWith(video, 96, 162, 1728, 756, 0, 0, 960, 420);
|
|
253
|
+
expect(jsQrMocks.default).toHaveBeenCalledWith(expect.any(Uint8ClampedArray), 960, 420, {
|
|
254
|
+
inversionAttempts: 'dontInvert',
|
|
255
|
+
});
|
|
256
|
+
});
|
|
211
257
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
258
|
+
it('enables continuous camera focus when the device supports it', async () => {
|
|
259
|
+
mocks.getRunningTrackCapabilities.mockReturnValue({ focusMode: ['manual', 'continuous'] });
|
|
260
|
+
|
|
261
|
+
render(<ScannerHost />);
|
|
215
262
|
|
|
216
|
-
|
|
263
|
+
await waitFor(() =>
|
|
264
|
+
expect(mocks.applyVideoConstraints).toHaveBeenCalledWith({
|
|
265
|
+
advanced: [{ focusMode: 'continuous' }],
|
|
266
|
+
}),
|
|
267
|
+
);
|
|
217
268
|
});
|
|
218
269
|
|
|
219
270
|
it('reports camera start failures through the dedicated handler', async () => {
|
|
@@ -228,6 +279,51 @@ describe('useCodeScanner', () => {
|
|
|
228
279
|
expect(handleScanFailure).not.toHaveBeenCalled();
|
|
229
280
|
});
|
|
230
281
|
|
|
282
|
+
it('stops a camera that finishes starting after the scanner unmounts', async () => {
|
|
283
|
+
let resolveStart: ((value: null) => void) | undefined;
|
|
284
|
+
mocks.start.mockReturnValueOnce(
|
|
285
|
+
new Promise<null>((resolve) => {
|
|
286
|
+
resolveStart = resolve;
|
|
287
|
+
}),
|
|
288
|
+
);
|
|
289
|
+
mocks.getState.mockReturnValueOnce(1).mockReturnValue(2);
|
|
290
|
+
|
|
291
|
+
const { unmount } = render(<ScannerHost />);
|
|
292
|
+
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
293
|
+
|
|
294
|
+
unmount();
|
|
295
|
+
await act(async () => {
|
|
296
|
+
resolveStart?.(null);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
await waitFor(() => expect(mocks.stop).toHaveBeenCalledTimes(1));
|
|
300
|
+
expect(mocks.clear).toHaveBeenCalled();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('ignores a camera start failure after the scanner unmounts', async () => {
|
|
304
|
+
let rejectStart: ((error: Error) => void) | undefined;
|
|
305
|
+
mocks.start.mockReturnValueOnce(
|
|
306
|
+
new Promise<null>((_resolve, reject) => {
|
|
307
|
+
rejectStart = reject;
|
|
308
|
+
}),
|
|
309
|
+
);
|
|
310
|
+
const handleCameraStartFailure = vi.fn();
|
|
311
|
+
const handleScanFailure = vi.fn();
|
|
312
|
+
|
|
313
|
+
const { unmount } = render(
|
|
314
|
+
<ScannerHost onCameraStartFailure={handleCameraStartFailure} onScanFailure={handleScanFailure} />,
|
|
315
|
+
);
|
|
316
|
+
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
317
|
+
|
|
318
|
+
unmount();
|
|
319
|
+
await act(async () => {
|
|
320
|
+
rejectStart?.(new Error('Camera start canceled'));
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
expect(handleCameraStartFailure).not.toHaveBeenCalled();
|
|
324
|
+
expect(handleScanFailure).not.toHaveBeenCalled();
|
|
325
|
+
});
|
|
326
|
+
|
|
231
327
|
it('uses jsQR first for Safari uploaded QR images', async () => {
|
|
232
328
|
const handleScanSuccess = vi.fn();
|
|
233
329
|
jsQrMocks.default.mockReturnValueOnce({ data: 'JSQR-CODE' });
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { Html5Qrcode, Html5QrcodeScannerState, Html5QrcodeSupportedFormats } from 'html5-qrcode';
|
|
11
11
|
import jsQR from 'jsqr';
|
|
12
|
-
import { useCallback, useEffect, useState } from 'react';
|
|
12
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
13
13
|
import type { CodeFormatsToSupport } from './types';
|
|
14
14
|
|
|
15
15
|
type ScannerSize = {
|
|
@@ -21,7 +21,6 @@ type UseCodeScannerOptions = {
|
|
|
21
21
|
enabled: boolean;
|
|
22
22
|
elementId: string;
|
|
23
23
|
formatsToSupport?: CodeFormatsToSupport;
|
|
24
|
-
scanBoxSize?: ScannerSize;
|
|
25
24
|
onScannerSizeChanged?: (size: ScannerSize) => void;
|
|
26
25
|
onScanSuccess: (text: string) => void;
|
|
27
26
|
onScanFailure?: () => void;
|
|
@@ -38,7 +37,18 @@ type JsQRImageTransform = {
|
|
|
38
37
|
threshold?: number;
|
|
39
38
|
};
|
|
40
39
|
|
|
40
|
+
type FocusMediaTrackCapabilities = MediaTrackCapabilities & {
|
|
41
|
+
focusMode?: string[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type FocusMediaTrackConstraintSet = MediaTrackConstraintSet & {
|
|
45
|
+
focusMode?: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
41
48
|
const QR_SCAN_IMAGE_SIZES = [3200, 2400, 1600, 1000];
|
|
49
|
+
const LIVE_QR_SCAN_INTERVAL = 120;
|
|
50
|
+
const LIVE_QR_SCAN_MAX_WIDTH = 960;
|
|
51
|
+
const LIVE_QR_SCAN_MAX_HEIGHT = 540;
|
|
42
52
|
const QR_SCAN_IMAGE_TRANSFORMS: JsQRImageTransform[] = [
|
|
43
53
|
{},
|
|
44
54
|
{ contrast: 3, threshold: 105 },
|
|
@@ -66,8 +76,72 @@ export const DEFAULT_CODE_FORMATS: CodeFormatsToSupport = [
|
|
|
66
76
|
|
|
67
77
|
export function getCodeScanBoxSize(width: number, height: number) {
|
|
68
78
|
return {
|
|
69
|
-
width: Math.floor(Math.min(width *
|
|
70
|
-
height: Math.floor(Math.min(height *
|
|
79
|
+
width: Math.floor(Math.min((width * 90) / 100, 1152)),
|
|
80
|
+
height: Math.floor(Math.min((height * 70) / 100, 540)),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function scanQrVideoFrame(video: HTMLVideoElement, canvas: HTMLCanvasElement) {
|
|
85
|
+
if (!video.videoWidth || !video.videoHeight || video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const viewfinderWidth = video.clientWidth || video.videoWidth;
|
|
90
|
+
const viewfinderHeight = video.clientHeight || video.videoHeight;
|
|
91
|
+
const scanBoxSize = getCodeScanBoxSize(viewfinderWidth, viewfinderHeight);
|
|
92
|
+
const sourceWidth = Math.min(video.videoWidth, Math.floor(scanBoxSize.width * (video.videoWidth / viewfinderWidth)));
|
|
93
|
+
const sourceHeight = Math.min(
|
|
94
|
+
video.videoHeight,
|
|
95
|
+
Math.floor(scanBoxSize.height * (video.videoHeight / viewfinderHeight)),
|
|
96
|
+
);
|
|
97
|
+
const sourceX = Math.floor((video.videoWidth - sourceWidth) / 2);
|
|
98
|
+
const sourceY = Math.floor((video.videoHeight - sourceHeight) / 2);
|
|
99
|
+
const targetScale = Math.min(1, LIVE_QR_SCAN_MAX_WIDTH / sourceWidth, LIVE_QR_SCAN_MAX_HEIGHT / sourceHeight);
|
|
100
|
+
const targetWidth = Math.max(1, Math.floor(sourceWidth * targetScale));
|
|
101
|
+
const targetHeight = Math.max(1, Math.floor(sourceHeight * targetScale));
|
|
102
|
+
if (canvas.width !== targetWidth) {
|
|
103
|
+
canvas.width = targetWidth;
|
|
104
|
+
}
|
|
105
|
+
if (canvas.height !== targetHeight) {
|
|
106
|
+
canvas.height = targetHeight;
|
|
107
|
+
}
|
|
108
|
+
const context = canvas.getContext('2d', { willReadFrequently: true });
|
|
109
|
+
if (!context) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
context.drawImage(video, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0, targetWidth, targetHeight);
|
|
114
|
+
const imageData = context.getImageData(0, 0, targetWidth, targetHeight);
|
|
115
|
+
return jsQR(imageData.data, imageData.width, imageData.height, { inversionAttempts: 'dontInvert' })?.data;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function startLiveQrScan(elementId: string, onScanSuccess: (text: string) => void) {
|
|
119
|
+
const canvas = document.createElement('canvas');
|
|
120
|
+
let timer: number | undefined;
|
|
121
|
+
let stopped = false;
|
|
122
|
+
|
|
123
|
+
const scan = () => {
|
|
124
|
+
if (stopped) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const video = document.getElementById(elementId)?.querySelector('video');
|
|
128
|
+
if (video) {
|
|
129
|
+
const decodedText = scanQrVideoFrame(video, canvas);
|
|
130
|
+
if (decodedText) {
|
|
131
|
+
stopped = true;
|
|
132
|
+
onScanSuccess(decodedText);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
timer = window.setTimeout(scan, LIVE_QR_SCAN_INTERVAL);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
timer = window.setTimeout(scan, 0);
|
|
140
|
+
return () => {
|
|
141
|
+
stopped = true;
|
|
142
|
+
if (timer !== undefined) {
|
|
143
|
+
window.clearTimeout(timer);
|
|
144
|
+
}
|
|
71
145
|
};
|
|
72
146
|
}
|
|
73
147
|
|
|
@@ -92,6 +166,19 @@ async function stopScanner(scanner?: Html5Qrcode, options: { clear?: boolean } =
|
|
|
92
166
|
}
|
|
93
167
|
}
|
|
94
168
|
|
|
169
|
+
async function enableContinuousFocus(scanner: Html5Qrcode) {
|
|
170
|
+
try {
|
|
171
|
+
const capabilities = scanner.getRunningTrackCapabilities() as FocusMediaTrackCapabilities;
|
|
172
|
+
if (!capabilities.focusMode?.includes('continuous')) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const focusConstraints: FocusMediaTrackConstraintSet = { focusMode: 'continuous' };
|
|
176
|
+
await scanner.applyVideoConstraints({ advanced: [focusConstraints] });
|
|
177
|
+
} catch {
|
|
178
|
+
// Some browsers expose incomplete camera capability APIs. Scanning should continue without explicit focus control.
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
95
182
|
function isSafariBrowser() {
|
|
96
183
|
const { userAgent, vendor } = navigator;
|
|
97
184
|
return /Apple/i.test(vendor) && /Safari/i.test(userAgent) && !/CriOS|FxiOS|EdgiOS|Chrome/i.test(userAgent);
|
|
@@ -216,40 +303,95 @@ export function useCodeScanner({
|
|
|
216
303
|
enabled,
|
|
217
304
|
elementId,
|
|
218
305
|
formatsToSupport,
|
|
219
|
-
scanBoxSize,
|
|
220
306
|
onScannerSizeChanged,
|
|
221
307
|
onScanSuccess,
|
|
222
308
|
onScanFailure,
|
|
223
309
|
onCameraStartFailure,
|
|
224
310
|
}: UseCodeScannerOptions) {
|
|
225
311
|
const [scanner, setScanner] = useState<Html5Qrcode>();
|
|
312
|
+
const liveQrScanStopRef = useRef<() => void>();
|
|
313
|
+
const scanSucceededRef = useRef(false);
|
|
314
|
+
const scanSessionRef = useRef(0);
|
|
315
|
+
|
|
316
|
+
const stopLiveQrScan = useCallback(() => {
|
|
317
|
+
liveQrScanStopRef.current?.();
|
|
318
|
+
liveQrScanStopRef.current = undefined;
|
|
319
|
+
}, []);
|
|
320
|
+
|
|
321
|
+
const cancelActiveScan = useCallback(() => {
|
|
322
|
+
scanSessionRef.current += 1;
|
|
323
|
+
stopLiveQrScan();
|
|
324
|
+
}, [stopLiveQrScan]);
|
|
325
|
+
|
|
326
|
+
const reportScanSuccess = useCallback(
|
|
327
|
+
(text: string) => {
|
|
328
|
+
if (scanSucceededRef.current) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
scanSucceededRef.current = true;
|
|
332
|
+
stopLiveQrScan();
|
|
333
|
+
onScanSuccess(text);
|
|
334
|
+
},
|
|
335
|
+
[onScanSuccess, stopLiveQrScan],
|
|
336
|
+
);
|
|
226
337
|
|
|
227
338
|
const startScanCamera = useCallback(
|
|
228
339
|
async (scannerInstance: Html5Qrcode) => {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
340
|
+
cancelActiveScan();
|
|
341
|
+
const scanSession = scanSessionRef.current;
|
|
342
|
+
scanSucceededRef.current = false;
|
|
343
|
+
try {
|
|
344
|
+
await scannerInstance.start(
|
|
345
|
+
{ facingMode: 'environment' },
|
|
346
|
+
{
|
|
347
|
+
fps: 8,
|
|
348
|
+
disableFlip: false,
|
|
349
|
+
videoConstraints: {
|
|
350
|
+
facingMode: { ideal: 'environment' },
|
|
351
|
+
width: { ideal: 1920 },
|
|
352
|
+
height: { ideal: 1080 },
|
|
353
|
+
frameRate: { ideal: 30 },
|
|
354
|
+
},
|
|
355
|
+
qrbox(width, height) {
|
|
356
|
+
onScannerSizeChanged?.({ width, height });
|
|
357
|
+
return clampCodeScanBoxSize(getCodeScanBoxSize(width, height), width, height);
|
|
358
|
+
},
|
|
359
|
+
},
|
|
360
|
+
(decodedText) => {
|
|
361
|
+
reportScanSuccess(decodedText);
|
|
236
362
|
},
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
363
|
+
undefined,
|
|
364
|
+
);
|
|
365
|
+
} catch (error) {
|
|
366
|
+
if (scanSession !== scanSessionRef.current) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
throw error;
|
|
370
|
+
}
|
|
371
|
+
if (scanSession !== scanSessionRef.current) {
|
|
372
|
+
try {
|
|
373
|
+
await stopScanner(scannerInstance, { clear: true });
|
|
374
|
+
} catch {
|
|
375
|
+
// The scanner may already have been cleared by the canceled session cleanup.
|
|
376
|
+
}
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (shouldScanQrWithJsQR(formatsToSupport)) {
|
|
380
|
+
liveQrScanStopRef.current = startLiveQrScan(elementId, reportScanSuccess);
|
|
381
|
+
}
|
|
382
|
+
await enableContinuousFocus(scannerInstance);
|
|
243
383
|
},
|
|
244
|
-
[
|
|
384
|
+
[cancelActiveScan, elementId, formatsToSupport, onScannerSizeChanged, reportScanSuccess],
|
|
245
385
|
);
|
|
246
386
|
|
|
247
387
|
const startScanFile = useCallback(
|
|
248
388
|
async (file: File) => {
|
|
389
|
+
cancelActiveScan();
|
|
390
|
+
scanSucceededRef.current = false;
|
|
249
391
|
if (isSafariBrowser() && shouldScanQrWithJsQR(formatsToSupport)) {
|
|
250
392
|
try {
|
|
251
393
|
const decodedText = await scanFileWithJsQR(file, formatsToSupport);
|
|
252
|
-
|
|
394
|
+
reportScanSuccess(decodedText);
|
|
253
395
|
return;
|
|
254
396
|
} catch {
|
|
255
397
|
// Fall through to html5-qrcode so barcode uploads still work in Safari.
|
|
@@ -263,13 +405,13 @@ export function useCodeScanner({
|
|
|
263
405
|
await stopScanner(scanner);
|
|
264
406
|
try {
|
|
265
407
|
const result = await scanner.scanFileV2(file, false);
|
|
266
|
-
|
|
408
|
+
reportScanSuccess(result.decodedText);
|
|
267
409
|
} catch {
|
|
268
410
|
onScanFailure?.();
|
|
269
411
|
await startScanCamera(scanner);
|
|
270
412
|
}
|
|
271
413
|
},
|
|
272
|
-
[formatsToSupport, onScanFailure,
|
|
414
|
+
[cancelActiveScan, formatsToSupport, onScanFailure, reportScanSuccess, scanner, startScanCamera],
|
|
273
415
|
);
|
|
274
416
|
|
|
275
417
|
useEffect(() => {
|
|
@@ -291,9 +433,10 @@ export function useCodeScanner({
|
|
|
291
433
|
});
|
|
292
434
|
|
|
293
435
|
return () => {
|
|
436
|
+
cancelActiveScan();
|
|
294
437
|
stopScanner(scannerInstance, { clear: true }).catch(() => undefined);
|
|
295
438
|
};
|
|
296
|
-
}, [elementId, enabled, formatsToSupport, onCameraStartFailure, onScanFailure, startScanCamera]);
|
|
439
|
+
}, [cancelActiveScan, elementId, enabled, formatsToSupport, onCameraStartFailure, onScanFailure, startScanCamera]);
|
|
297
440
|
|
|
298
441
|
return {
|
|
299
442
|
startScanFile,
|