@nocobase/client-v2 2.2.0-beta.9 → 2.3.0-alpha.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.
- package/es/APIClient.d.ts +1 -3
- package/es/BaseApplication.d.ts +1 -0
- package/es/collection-field-interface/CollectionFieldInterface.d.ts +1 -0
- package/es/collection-field-interface/CollectionFieldInterfaceManager.d.ts +1 -0
- 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 -1
- 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 +2 -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/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/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/form/QuickEditFormModel.d.ts +17 -2
- package/es/flow-compat/fieldValidationConstants.d.ts +1 -1
- package/es/flow-compat/routeTypes.d.ts +1 -0
- package/es/index.d.ts +4 -0
- package/es/index.mjs +153 -118
- package/es/utils/getRouteRuntimeVersion.d.ts +23 -0
- package/es/utils/index.d.ts +1 -0
- package/lib/index.js +162 -127
- package/lib/locale/languageCodes.js +2 -1
- package/package.json +8 -7
- package/src/APIClient.ts +1 -10
- package/src/Application.tsx +4 -0
- package/src/BaseApplication.tsx +11 -6
- package/src/__tests__/app.test.tsx +73 -0
- 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 +28 -0
- package/src/collection-field-interface/CollectionFieldInterface.ts +1 -0
- package/src/collection-field-interface/CollectionFieldInterfaceManager.ts +1 -0
- package/src/collection-manager/field-validation.ts +1 -1
- 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 +23 -6
- package/src/components/form/ScanInput/__tests__/useCodeScanner.test.tsx +180 -3
- package/src/components/form/ScanInput/useCodeScanner.ts +170 -5
- 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/FlowPage.tsx +9 -1
- package/src/flow/__tests__/FlowPage.test.tsx +50 -3
- package/src/flow/__tests__/FlowRoute.test.tsx +2 -2
- package/src/flow/actions/__tests__/linkageRules.tab.test.ts +171 -0
- package/src/flow/actions/__tests__/linkageRulesRefresh.test.ts +7 -3
- package/src/flow/actions/index.ts +2 -0
- package/src/flow/actions/linkageRules.tsx +86 -11
- package/src/flow/actions/linkageRulesRefresh.tsx +2 -6
- 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/components/FieldAssignExactDatePicker.tsx +25 -11
- package/src/flow/components/FieldAssignValueInput.tsx +91 -20
- package/src/flow/components/FlowRoute.tsx +7 -3
- package/src/flow/components/RunJSValueEditor.tsx +9 -1
- package/src/flow/components/__tests__/FieldAssignValueInput.context.test.tsx +216 -0
- 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 +1 -0
- 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 +184 -3
- 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 +572 -1
- package/src/flow/models/blocks/filter-form/FilterFormBlockModel.tsx +1 -0
- package/src/flow/models/blocks/filter-form/FilterFormItemModel.tsx +27 -12
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormItemModel.defineChildren.test.ts +36 -0
- package/src/flow/models/blocks/filter-form/__tests__/defaultValues.wiring.test.ts +34 -0
- package/src/flow/models/blocks/form/QuickEditFormModel.tsx +189 -36
- package/src/flow/models/blocks/form/__tests__/QuickEditFormModel.quickEdit.test.ts +350 -2
- package/src/flow/models/blocks/table/TableActionsColumnModel.tsx +2 -1
- package/src/flow/models/blocks/table/TableBlockModel.tsx +1 -1
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/PopupSubTableFieldModel.tsx +2 -1
- package/src/flow/models/fields/VariableFieldFormModel.tsx +3 -3
- package/src/flow/models/fields/__tests__/VariableFieldFormModel.test.tsx +51 -0
- package/src/flow/models/fields/mobile-components/MobileLazySelect.tsx +20 -10
- package/src/flow/models/fields/mobile-components/MobileSelect.tsx +24 -12
- package/src/flow-compat/fieldValidationConstants.ts +1 -1
- package/src/flow-compat/routeTypes.ts +1 -0
- package/src/index.ts +4 -0
- package/src/locale/languageCodes.ts +2 -1
- package/src/nocobase-buildin-plugin/index.tsx +12 -0
- package/src/utils/getRouteRuntimeVersion.ts +185 -0
- package/src/utils/index.tsx +1 -0
|
@@ -0,0 +1,210 @@
|
|
|
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 { MenuOutlined } from '@ant-design/icons';
|
|
11
|
+
import {
|
|
12
|
+
DndContext,
|
|
13
|
+
DragOverlay,
|
|
14
|
+
MouseSensor,
|
|
15
|
+
useDraggable,
|
|
16
|
+
useDroppable,
|
|
17
|
+
useSensor,
|
|
18
|
+
useSensors,
|
|
19
|
+
type DragEndEvent,
|
|
20
|
+
type DragStartEvent,
|
|
21
|
+
} from '@dnd-kit/core';
|
|
22
|
+
import { Badge, Button, Dropdown, Space, Tabs } from 'antd';
|
|
23
|
+
import type { TabsProps } from 'antd';
|
|
24
|
+
import React, { useCallback, useMemo, useState } from 'react';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A category descriptor for {@link SortableCategoryTabs}. The component is
|
|
28
|
+
* i18n-agnostic: `label` is rendered verbatim, so consumers pass an
|
|
29
|
+
* already-compiled/translated node. `color` is an antd preset color name or a
|
|
30
|
+
* hex string; `'default'` / `undefined` renders no badge color.
|
|
31
|
+
*/
|
|
32
|
+
export type SortableCategoryTabItem = {
|
|
33
|
+
id: string | number;
|
|
34
|
+
label: React.ReactNode;
|
|
35
|
+
color?: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type SortableCategoryTabsProps = {
|
|
39
|
+
activeKey: string;
|
|
40
|
+
onChange: (key: string) => void;
|
|
41
|
+
/** Draggable category tabs (the fixed leading tab is configured via `allTab`). */
|
|
42
|
+
categories: SortableCategoryTabItem[];
|
|
43
|
+
/** The fixed, non-draggable leading tab, e.g. "All". */
|
|
44
|
+
allTab: { key: string; label: React.ReactNode };
|
|
45
|
+
/** Show the "+" add button and handle its click. */
|
|
46
|
+
onAdd?: () => void;
|
|
47
|
+
/** Per-category edit menu item handler; menu is hidden when omitted. */
|
|
48
|
+
onEdit?: (id: string | number) => void;
|
|
49
|
+
/** Per-category delete menu item handler; menu is hidden when omitted. */
|
|
50
|
+
onDelete?: (id: string | number) => void;
|
|
51
|
+
/** Reorder handler. Receives the dragged and dropped category ids. */
|
|
52
|
+
onSort?: (sourceId: string | number, targetId: string | number) => void | Promise<void>;
|
|
53
|
+
/** Consumer-translated labels for the per-category dropdown menu. */
|
|
54
|
+
menuLabels?: { edit?: string; delete?: string };
|
|
55
|
+
className?: string;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function DraggableTab(props: { id: string; children: React.ReactNode }) {
|
|
59
|
+
const { attributes, listeners, setNodeRef } = useDraggable({ id: props.id });
|
|
60
|
+
return (
|
|
61
|
+
<div ref={setNodeRef} {...listeners} {...attributes}>
|
|
62
|
+
{props.children}
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function DroppableTab(props: { id: string; children: React.ReactNode }) {
|
|
68
|
+
const { isOver, setNodeRef } = useDroppable({ id: props.id });
|
|
69
|
+
return (
|
|
70
|
+
<div ref={setNodeRef} style={isOver ? { color: 'green' } : undefined}>
|
|
71
|
+
{props.children}
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function CategoryTabContent(props: {
|
|
77
|
+
item: SortableCategoryTabItem;
|
|
78
|
+
onEdit?: (id: string | number) => void;
|
|
79
|
+
onDelete?: (id: string | number) => void;
|
|
80
|
+
menuLabels?: { edit?: string; delete?: string };
|
|
81
|
+
}) {
|
|
82
|
+
const { item, onEdit, onDelete, menuLabels } = props;
|
|
83
|
+
const hasMenu = Boolean(onEdit || onDelete);
|
|
84
|
+
const editLabel = menuLabels?.edit ?? 'Edit';
|
|
85
|
+
const deleteLabel = menuLabels?.delete ?? 'Delete';
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<Space size={6}>
|
|
89
|
+
<Badge color={item.color === 'default' ? undefined : item.color} />
|
|
90
|
+
{item.label}
|
|
91
|
+
{hasMenu ? (
|
|
92
|
+
<Dropdown
|
|
93
|
+
trigger={['click']}
|
|
94
|
+
menu={{
|
|
95
|
+
items: [
|
|
96
|
+
onEdit ? { key: 'edit', label: editLabel } : null,
|
|
97
|
+
onDelete ? { key: 'delete', label: deleteLabel } : null,
|
|
98
|
+
].filter(Boolean) as { key: string; label: string }[],
|
|
99
|
+
onClick({ key, domEvent }) {
|
|
100
|
+
domEvent.stopPropagation();
|
|
101
|
+
if (key === 'edit') {
|
|
102
|
+
onEdit?.(item.id);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
onDelete?.(item.id);
|
|
106
|
+
},
|
|
107
|
+
}}
|
|
108
|
+
>
|
|
109
|
+
<Button
|
|
110
|
+
aria-label={editLabel}
|
|
111
|
+
icon={<MenuOutlined />}
|
|
112
|
+
size="small"
|
|
113
|
+
type="text"
|
|
114
|
+
onClick={(event) => event.stopPropagation()}
|
|
115
|
+
/>
|
|
116
|
+
</Dropdown>
|
|
117
|
+
) : null}
|
|
118
|
+
</Space>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A draggable, editable category tab bar. Renders only the tab bar (no content
|
|
124
|
+
* panes) — the page renders its content below and reacts to `activeKey`.
|
|
125
|
+
*
|
|
126
|
+
* Shared by settings pages that group records under reorderable categories
|
|
127
|
+
* (data-source collections, workflows, …). Drag-to-reorder is powered by
|
|
128
|
+
* `@dnd-kit`; the "+" / per-tab edit-delete affordances are optional.
|
|
129
|
+
*/
|
|
130
|
+
export function SortableCategoryTabs(props: SortableCategoryTabsProps) {
|
|
131
|
+
const { activeKey, onChange, categories, allTab, onAdd, onEdit, onDelete, onSort, menuLabels, className } = props;
|
|
132
|
+
const [activeDragId, setActiveDragId] = useState<string | null>(null);
|
|
133
|
+
const sensors = useSensors(useSensor(MouseSensor, { activationConstraint: { distance: 10 } }));
|
|
134
|
+
|
|
135
|
+
const handleDragStart = useCallback((event: DragStartEvent) => {
|
|
136
|
+
setActiveDragId(String(event.active.id));
|
|
137
|
+
}, []);
|
|
138
|
+
|
|
139
|
+
const handleDragEnd = useCallback(
|
|
140
|
+
async (event: DragEndEvent) => {
|
|
141
|
+
const { active, over } = event;
|
|
142
|
+
setActiveDragId(null);
|
|
143
|
+
if (!over || over.id === active.id) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
await onSort?.(active.id as string | number, over.id as string | number);
|
|
147
|
+
},
|
|
148
|
+
[onSort],
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const items = useMemo<TabsProps['items']>(
|
|
152
|
+
() => [
|
|
153
|
+
{ key: allTab.key, label: allTab.label, closable: false },
|
|
154
|
+
...categories.map((item) => ({
|
|
155
|
+
key: String(item.id),
|
|
156
|
+
closable: false,
|
|
157
|
+
label: onSort ? (
|
|
158
|
+
<DroppableTab id={String(item.id)}>
|
|
159
|
+
<DraggableTab id={String(item.id)}>
|
|
160
|
+
<CategoryTabContent item={item} onEdit={onEdit} onDelete={onDelete} menuLabels={menuLabels} />
|
|
161
|
+
</DraggableTab>
|
|
162
|
+
</DroppableTab>
|
|
163
|
+
) : (
|
|
164
|
+
<CategoryTabContent item={item} onEdit={onEdit} onDelete={onDelete} menuLabels={menuLabels} />
|
|
165
|
+
),
|
|
166
|
+
})),
|
|
167
|
+
],
|
|
168
|
+
[allTab.key, allTab.label, categories, onSort, onEdit, onDelete, menuLabels],
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
const activeDragItem = useMemo(
|
|
172
|
+
() => categories.find((item) => String(item.id) === activeDragId),
|
|
173
|
+
[categories, activeDragId],
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const tabs = (
|
|
177
|
+
<Tabs
|
|
178
|
+
className={className}
|
|
179
|
+
activeKey={activeKey}
|
|
180
|
+
type="editable-card"
|
|
181
|
+
hideAdd={!onAdd}
|
|
182
|
+
items={items}
|
|
183
|
+
onChange={onChange}
|
|
184
|
+
onEdit={(_, action) => {
|
|
185
|
+
if (action === 'add') {
|
|
186
|
+
onAdd?.();
|
|
187
|
+
}
|
|
188
|
+
}}
|
|
189
|
+
/>
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
if (!onSort) {
|
|
193
|
+
return tabs;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<DndContext sensors={sensors} onDragStart={handleDragStart} onDragEnd={handleDragEnd}>
|
|
198
|
+
{tabs}
|
|
199
|
+
<DragOverlay dropAnimation={null}>
|
|
200
|
+
{activeDragItem ? (
|
|
201
|
+
<span style={{ whiteSpace: 'nowrap' }}>
|
|
202
|
+
<CategoryTabContent item={activeDragItem} menuLabels={menuLabels} />
|
|
203
|
+
</span>
|
|
204
|
+
) : null}
|
|
205
|
+
</DragOverlay>
|
|
206
|
+
</DndContext>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export default SortableCategoryTabs;
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
export * from './SortableCategoryTabs';
|
|
@@ -49,6 +49,21 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
|
|
|
49
49
|
message.error(t('Code recognition failed, please scan again'));
|
|
50
50
|
}, [t]);
|
|
51
51
|
|
|
52
|
+
const showCameraStartFailure = useCallback(
|
|
53
|
+
(error: unknown) => {
|
|
54
|
+
const errorName = error instanceof Error ? error.name : '';
|
|
55
|
+
const errorMessage = error instanceof Error ? error.message : '';
|
|
56
|
+
const errorMap: Record<string, string> = {
|
|
57
|
+
NotFoundError: t('No camera device detected'),
|
|
58
|
+
NotAllowedError: t('You have not granted permission to use the camera'),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
message.error(errorMap[errorName] || errorMessage || t('You have not granted permission to use the camera'));
|
|
62
|
+
onClose();
|
|
63
|
+
},
|
|
64
|
+
[onClose, t],
|
|
65
|
+
);
|
|
66
|
+
|
|
52
67
|
const handleScanSuccess = useCallback(
|
|
53
68
|
(text: string) => {
|
|
54
69
|
onScanSuccess(text);
|
|
@@ -64,6 +79,7 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
|
|
|
64
79
|
scanBoxSize,
|
|
65
80
|
onScanSuccess: handleScanSuccess,
|
|
66
81
|
onScanFailure: showScanFailure,
|
|
82
|
+
onCameraStartFailure: showCameraStartFailure,
|
|
67
83
|
});
|
|
68
84
|
|
|
69
85
|
useEffect(() => {
|
|
@@ -129,7 +145,7 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
|
|
|
129
145
|
inset: 0;
|
|
130
146
|
z-index: ${token.zIndexPopupBase + 1000};
|
|
131
147
|
overflow: hidden;
|
|
132
|
-
background:
|
|
148
|
+
background: #000;
|
|
133
149
|
`;
|
|
134
150
|
const scannerWrapperClass = css`
|
|
135
151
|
position: absolute;
|
|
@@ -137,16 +153,17 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
|
|
|
137
153
|
overflow: hidden;
|
|
138
154
|
`;
|
|
139
155
|
const scannerClass = css`
|
|
156
|
+
position: relative;
|
|
140
157
|
width: 100%;
|
|
141
158
|
height: 100%;
|
|
142
|
-
|
|
143
|
-
justify-content: center;
|
|
144
|
-
align-items: center;
|
|
159
|
+
overflow: hidden;
|
|
145
160
|
|
|
146
161
|
video {
|
|
147
|
-
|
|
162
|
+
position: absolute !important;
|
|
163
|
+
inset: 0 !important;
|
|
164
|
+
width: 100% !important;
|
|
148
165
|
height: 100% !important;
|
|
149
|
-
|
|
166
|
+
object-fit: cover !important;
|
|
150
167
|
}
|
|
151
168
|
|
|
152
169
|
#qr-shaded-region {
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { render, waitFor } from '@testing-library/react';
|
|
10
|
+
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
11
11
|
import React, { useCallback } from 'react';
|
|
12
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
12
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
13
13
|
import { DEFAULT_CODE_FORMATS, getCodeScanBoxSize, useCodeScanner } from '../useCodeScanner';
|
|
14
14
|
|
|
15
15
|
type MockScannerInstance = {
|
|
@@ -44,6 +44,12 @@ const mocks = vi.hoisted(() => {
|
|
|
44
44
|
};
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
+
const jsQrMocks = vi.hoisted(() => {
|
|
48
|
+
return {
|
|
49
|
+
default: vi.fn(),
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
|
|
47
53
|
vi.mock('html5-qrcode', () => ({
|
|
48
54
|
Html5Qrcode: mocks.Html5Qrcode,
|
|
49
55
|
Html5QrcodeScannerState: {
|
|
@@ -66,22 +72,99 @@ vi.mock('html5-qrcode', () => ({
|
|
|
66
72
|
},
|
|
67
73
|
}));
|
|
68
74
|
|
|
69
|
-
|
|
75
|
+
vi.mock('jsqr', () => jsQrMocks);
|
|
76
|
+
|
|
77
|
+
function ScannerHost({
|
|
78
|
+
onCameraStartFailure,
|
|
79
|
+
onScanFailure,
|
|
80
|
+
scanBoxSize,
|
|
81
|
+
}: {
|
|
82
|
+
onCameraStartFailure?: (error: unknown) => void;
|
|
83
|
+
onScanFailure?: () => void;
|
|
84
|
+
scanBoxSize?: { width: number; height: number };
|
|
85
|
+
} = {}) {
|
|
70
86
|
const handleScanSuccess = useCallback(() => undefined, []);
|
|
71
87
|
|
|
72
88
|
useCodeScanner({
|
|
73
89
|
elementId: 'scanner',
|
|
74
90
|
enabled: true,
|
|
75
91
|
scanBoxSize,
|
|
92
|
+
onCameraStartFailure,
|
|
93
|
+
onScanFailure,
|
|
76
94
|
onScanSuccess: handleScanSuccess,
|
|
77
95
|
});
|
|
78
96
|
|
|
79
97
|
return <div id="scanner" />;
|
|
80
98
|
}
|
|
81
99
|
|
|
100
|
+
function FileScannerHost({ onScanSuccess }: { onScanSuccess: (text: string) => void }) {
|
|
101
|
+
const { startScanFile } = useCodeScanner({
|
|
102
|
+
elementId: 'scanner',
|
|
103
|
+
enabled: true,
|
|
104
|
+
onScanSuccess,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<>
|
|
109
|
+
<div id="scanner" />
|
|
110
|
+
<button onClick={() => startScanFile(new File(['qr'], 'qr.png', { type: 'image/png' }))}>scan file</button>
|
|
111
|
+
</>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function stubSafariBrowser() {
|
|
116
|
+
vi.spyOn(window.navigator, 'vendor', 'get').mockReturnValue('Apple Computer, Inc.');
|
|
117
|
+
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(
|
|
118
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15',
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function stubImageElement(size: { width: number; height: number } = { width: 600, height: 400 }) {
|
|
123
|
+
vi.stubGlobal(
|
|
124
|
+
'Image',
|
|
125
|
+
class MockImage {
|
|
126
|
+
onabort: ((event: Event) => void) | null = null;
|
|
127
|
+
onerror: ((event: Event) => void) | null = null;
|
|
128
|
+
onload: (() => void) | null = null;
|
|
129
|
+
height = size.height;
|
|
130
|
+
naturalHeight = size.height;
|
|
131
|
+
naturalWidth = size.width;
|
|
132
|
+
width = size.width;
|
|
133
|
+
|
|
134
|
+
set src(_value: string) {
|
|
135
|
+
this.onload?.();
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
);
|
|
139
|
+
vi.stubGlobal('URL', {
|
|
140
|
+
...URL,
|
|
141
|
+
createObjectURL: vi.fn(() => 'blob:qr'),
|
|
142
|
+
revokeObjectURL: vi.fn(),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function stubCanvas() {
|
|
147
|
+
const canvasContext = {
|
|
148
|
+
drawImage: vi.fn(),
|
|
149
|
+
getImageData: vi.fn((_x: number, _y: number, width: number, height: number) => ({
|
|
150
|
+
data: new Uint8ClampedArray(width * height * 4),
|
|
151
|
+
height,
|
|
152
|
+
width,
|
|
153
|
+
})),
|
|
154
|
+
} as unknown as CanvasRenderingContext2D;
|
|
155
|
+
|
|
156
|
+
vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(canvasContext);
|
|
157
|
+
}
|
|
158
|
+
|
|
82
159
|
describe('useCodeScanner', () => {
|
|
83
160
|
beforeEach(() => {
|
|
84
161
|
vi.clearAllMocks();
|
|
162
|
+
jsQrMocks.default.mockReturnValue(undefined);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
afterEach(() => {
|
|
166
|
+
vi.unstubAllGlobals();
|
|
167
|
+
vi.restoreAllMocks();
|
|
85
168
|
});
|
|
86
169
|
|
|
87
170
|
it('starts scanning with QR code and barcode formats by default', async () => {
|
|
@@ -120,4 +203,98 @@ describe('useCodeScanner', () => {
|
|
|
120
203
|
|
|
121
204
|
expect(config?.qrbox?.(1200, 844)).toEqual({ width: 319, height: 240 });
|
|
122
205
|
});
|
|
206
|
+
|
|
207
|
+
it('clamps the scan box to the camera viewfinder size', async () => {
|
|
208
|
+
render(<ScannerHost scanBoxSize={{ width: 319, height: 240 }} />);
|
|
209
|
+
|
|
210
|
+
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
211
|
+
|
|
212
|
+
const config = mocks.start.mock.calls[0]?.[1] as
|
|
213
|
+
| { qrbox?: (width: number, height: number) => { width: number; height: number } }
|
|
214
|
+
| undefined;
|
|
215
|
+
|
|
216
|
+
expect(config?.qrbox?.(200, 160)).toEqual({ width: 200, height: 160 });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('reports camera start failures through the dedicated handler', async () => {
|
|
220
|
+
const cameraStartError = Object.assign(new Error('Camera failed'), { name: 'NotAllowedError' });
|
|
221
|
+
const handleCameraStartFailure = vi.fn();
|
|
222
|
+
const handleScanFailure = vi.fn();
|
|
223
|
+
mocks.start.mockRejectedValueOnce(cameraStartError);
|
|
224
|
+
|
|
225
|
+
render(<ScannerHost onCameraStartFailure={handleCameraStartFailure} onScanFailure={handleScanFailure} />);
|
|
226
|
+
|
|
227
|
+
await waitFor(() => expect(handleCameraStartFailure).toHaveBeenCalledWith(cameraStartError));
|
|
228
|
+
expect(handleScanFailure).not.toHaveBeenCalled();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('uses jsQR first for Safari uploaded QR images', async () => {
|
|
232
|
+
const handleScanSuccess = vi.fn();
|
|
233
|
+
jsQrMocks.default.mockReturnValueOnce({ data: 'JSQR-CODE' });
|
|
234
|
+
stubSafariBrowser();
|
|
235
|
+
stubImageElement();
|
|
236
|
+
stubCanvas();
|
|
237
|
+
|
|
238
|
+
render(<FileScannerHost onScanSuccess={handleScanSuccess} />);
|
|
239
|
+
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
240
|
+
|
|
241
|
+
fireEvent.click(screen.getByText('scan file'));
|
|
242
|
+
|
|
243
|
+
await waitFor(() => expect(handleScanSuccess).toHaveBeenCalledWith('JSQR-CODE'));
|
|
244
|
+
expect(jsQrMocks.default).toHaveBeenCalledWith(expect.any(Uint8ClampedArray), 600, 400, {
|
|
245
|
+
inversionAttempts: 'attemptBoth',
|
|
246
|
+
});
|
|
247
|
+
expect(mocks.scanFileV2).not.toHaveBeenCalled();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('falls back to html5-qrcode for Safari uploaded files when jsQR does not decode a QR code', async () => {
|
|
251
|
+
const handleScanSuccess = vi.fn();
|
|
252
|
+
mocks.scanFileV2.mockResolvedValueOnce({ decodedText: 'BARCODE-CODE' });
|
|
253
|
+
stubSafariBrowser();
|
|
254
|
+
stubImageElement();
|
|
255
|
+
stubCanvas();
|
|
256
|
+
|
|
257
|
+
render(<FileScannerHost onScanSuccess={handleScanSuccess} />);
|
|
258
|
+
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
259
|
+
|
|
260
|
+
fireEvent.click(screen.getByText('scan file'));
|
|
261
|
+
|
|
262
|
+
await waitFor(() => expect(handleScanSuccess).toHaveBeenCalledWith('BARCODE-CODE'));
|
|
263
|
+
expect(jsQrMocks.default).toHaveBeenCalled();
|
|
264
|
+
expect(mocks.scanFileV2).toHaveBeenCalled();
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('tries multiple image scales before falling back from Safari jsQR scanning', async () => {
|
|
268
|
+
const handleScanSuccess = vi.fn();
|
|
269
|
+
Array.from({ length: 8 }).forEach((_, index) => {
|
|
270
|
+
jsQrMocks.default.mockReturnValueOnce(index === 7 ? { data: 'SCALED-CODE' } : undefined);
|
|
271
|
+
});
|
|
272
|
+
stubSafariBrowser();
|
|
273
|
+
stubImageElement({ width: 4000, height: 3000 });
|
|
274
|
+
stubCanvas();
|
|
275
|
+
|
|
276
|
+
render(<FileScannerHost onScanSuccess={handleScanSuccess} />);
|
|
277
|
+
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
278
|
+
|
|
279
|
+
fireEvent.click(screen.getByText('scan file'));
|
|
280
|
+
|
|
281
|
+
await waitFor(() => expect(handleScanSuccess).toHaveBeenCalledWith('SCALED-CODE'));
|
|
282
|
+
expect(jsQrMocks.default).toHaveBeenCalledTimes(8);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('tries enhanced grayscale QR images for blurry Safari uploads', async () => {
|
|
286
|
+
const handleScanSuccess = vi.fn();
|
|
287
|
+
jsQrMocks.default.mockReturnValueOnce(undefined).mockReturnValueOnce({ data: 'ENHANCED-CODE' });
|
|
288
|
+
stubSafariBrowser();
|
|
289
|
+
stubImageElement();
|
|
290
|
+
stubCanvas();
|
|
291
|
+
|
|
292
|
+
render(<FileScannerHost onScanSuccess={handleScanSuccess} />);
|
|
293
|
+
await waitFor(() => expect(mocks.start).toHaveBeenCalled());
|
|
294
|
+
|
|
295
|
+
fireEvent.click(screen.getByText('scan file'));
|
|
296
|
+
|
|
297
|
+
await waitFor(() => expect(handleScanSuccess).toHaveBeenCalledWith('ENHANCED-CODE'));
|
|
298
|
+
expect(jsQrMocks.default).toHaveBeenCalledTimes(2);
|
|
299
|
+
});
|
|
123
300
|
});
|