@nocobase/client-v2 2.3.0-beta.3 → 2.3.0-beta.31

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 (138) hide show
  1. package/es/BaseApplication.d.ts +3 -0
  2. package/es/RouteRepository.d.ts +6 -0
  3. package/es/components/form/ScanInput/useCodeScanner.d.ts +12 -2
  4. package/es/components/form/ScanInput/zxingWasmDecoder.d.ts +9 -0
  5. package/es/components/form/VariableInput.d.ts +1 -0
  6. package/es/flow/FlowModelRepository.d.ts +4 -1
  7. package/es/flow/FlowPage.d.ts +1 -1
  8. package/es/flow/admin-shell/admin-layout/AdminLayoutMenuModels.d.ts +1 -0
  9. package/es/flow/admin-shell/admin-layout/AdminLayoutMenuUtils.d.ts +1 -0
  10. package/es/flow/admin-shell/admin-layout/AdminLayoutModel.d.ts +2 -0
  11. package/es/flow/admin-shell/admin-layout/MobileTopbarActionsContext.d.ts +10 -0
  12. package/es/flow/admin-shell/admin-layout/index.d.ts +1 -0
  13. package/es/flow/admin-shell/admin-layout/observeAdminMenuRoutes.d.ts +26 -0
  14. package/es/flow/components/FieldAssignValueInput.d.ts +2 -0
  15. package/es/flow/components/field-value-variable/FieldValueVariableInput.d.ts +1 -0
  16. package/es/flow/components/filter/VariableFilterItem.d.ts +10 -1
  17. package/es/flow/internal/utils/quickEditDataScope.d.ts +48 -0
  18. package/es/flow/internal/utils/quickEditViewContainer.d.ts +27 -0
  19. package/es/flow/models/actions/LinkActionUtils.d.ts +1 -0
  20. package/es/flow/models/actions/getHasManyDeleteFilterByTk.d.ts +12 -0
  21. package/es/flow/models/base/PageModel/closeGuard.d.ts +8 -1
  22. package/es/flow/models/blocks/assign-form/assignFormValue.d.ts +29 -0
  23. package/es/flow/models/blocks/form/EditFormModel.d.ts +2 -0
  24. package/es/flow/models/blocks/form/QuickEditFormModel.d.ts +1 -0
  25. package/es/flow/models/blocks/table/TableJSFieldItemModel.d.ts +1 -1
  26. package/es/flow/models/blocks/table/sortUtils.d.ts +3 -0
  27. package/es/flow/models/topbar/UserCenterTopbarActionModel.d.ts +2 -0
  28. package/es/flow/useRouteContentRequest.d.ts +20 -0
  29. package/es/index.mjs +143 -106
  30. package/lib/index.js +143 -106
  31. package/package.json +9 -8
  32. package/src/BaseApplication.tsx +27 -0
  33. package/src/RouteRepository.ts +14 -0
  34. package/src/__tests__/RouteRepository.test.ts +51 -0
  35. package/src/__tests__/app.test.tsx +13 -0
  36. package/src/collection-manager/__tests__/field-configure.test.ts +52 -0
  37. package/src/collection-manager/field-configure.ts +2 -2
  38. package/src/components/form/ScanInput/CodeScanner.tsx +3 -1
  39. package/src/components/form/ScanInput/__tests__/CodeScanner.test.tsx +7 -1
  40. package/src/components/form/ScanInput/__tests__/useCodeScanner.test.tsx +182 -8
  41. package/src/components/form/ScanInput/__tests__/zxingWasmDecoder.test.ts +78 -0
  42. package/src/components/form/ScanInput/useCodeScanner.ts +135 -20
  43. package/src/components/form/ScanInput/zxingWasmDecoder.ts +64 -0
  44. package/src/components/form/VariableInput.tsx +1 -0
  45. package/src/flow/FlowModelRepository.ts +47 -1
  46. package/src/flow/FlowPage.tsx +25 -10
  47. package/src/flow/__tests__/FlowModelRepository.test.ts +38 -1
  48. package/src/flow/__tests__/FlowPage.test.tsx +78 -46
  49. package/src/flow/actions/__tests__/validation.test.ts +48 -0
  50. package/src/flow/admin-shell/admin-layout/AdminLayoutComponent.tsx +15 -10
  51. package/src/flow/admin-shell/admin-layout/AdminLayoutEntryGuard.test.tsx +48 -1
  52. package/src/flow/admin-shell/admin-layout/AdminLayoutEntryGuard.tsx +6 -1
  53. package/src/flow/admin-shell/admin-layout/AdminLayoutMenuModels.tsx +17 -21
  54. package/src/flow/admin-shell/admin-layout/AdminLayoutMenuUtils.tsx +1 -0
  55. package/src/flow/admin-shell/admin-layout/AdminLayoutModel.tsx +34 -0
  56. package/src/flow/admin-shell/admin-layout/MobileTopbarActionsContext.ts +13 -0
  57. package/src/flow/admin-shell/admin-layout/TopbarActionsBar.tsx +13 -2
  58. package/src/flow/admin-shell/admin-layout/__tests__/AdminLayoutMenuModels.test.ts +146 -0
  59. package/src/flow/admin-shell/admin-layout/__tests__/TopbarActionsBar.mobile.test.tsx +259 -0
  60. package/src/flow/admin-shell/admin-layout/__tests__/TopbarActionsBar.test.tsx +42 -23
  61. package/src/flow/admin-shell/admin-layout/index.ts +1 -0
  62. package/src/flow/admin-shell/admin-layout/observeAdminMenuRoutes.ts +60 -0
  63. package/src/flow/components/FieldAssignValueInput.tsx +4 -0
  64. package/src/flow/components/field-value-variable/FieldValueVariableInput.tsx +21 -12
  65. package/src/flow/components/field-value-variable/__tests__/FieldValueVariableInput.test.tsx +9 -0
  66. package/src/flow/components/filter/VariableFilterItem.tsx +14 -4
  67. package/src/flow/components/filter/__tests__/VariableFilterItem.rightMetaTree.test.tsx +158 -0
  68. package/src/flow/internal/components/Markdown/DisplayMarkdown.tsx +14 -9
  69. package/src/flow/internal/components/Markdown/__tests__/DisplayMarkdown.test.tsx +46 -0
  70. package/src/flow/internal/utils/__tests__/quickEditDataScope.test.ts +156 -0
  71. package/src/flow/internal/utils/__tests__/quickEditViewContainer.test.ts +52 -0
  72. package/src/flow/internal/utils/quickEditDataScope.tsx +177 -0
  73. package/src/flow/internal/utils/quickEditViewContainer.ts +46 -0
  74. package/src/flow/models/actions/BulkDeleteActionModel.tsx +13 -2
  75. package/src/flow/models/actions/DeleteActionModel.tsx +16 -2
  76. package/src/flow/models/actions/JSItemActionModel.tsx +19 -3
  77. package/src/flow/models/actions/LinkActionModel.tsx +11 -9
  78. package/src/flow/models/actions/LinkActionUtils.ts +4 -2
  79. package/src/flow/models/actions/__tests__/DeleteActionModel.test.ts +256 -0
  80. package/src/flow/models/actions/__tests__/JSItemActionModel.test.tsx +47 -1
  81. package/src/flow/models/actions/__tests__/LinkActionModel.test.ts +14 -0
  82. package/src/flow/models/actions/getHasManyDeleteFilterByTk.ts +40 -0
  83. package/src/flow/models/base/GridModel.tsx +0 -1
  84. package/src/flow/models/base/PageModel/PageTabModel.tsx +27 -12
  85. package/src/flow/models/base/PageModel/closeGuard.ts +9 -8
  86. package/src/flow/models/blocks/assign-form/AssignFormGridModel.tsx +22 -1
  87. package/src/flow/models/blocks/assign-form/AssignFormItemModel.tsx +40 -12
  88. package/src/flow/models/blocks/assign-form/__tests__/assignFieldValuesFlow.editor.test.tsx +140 -0
  89. package/src/flow/models/blocks/assign-form/__tests__/assignFieldValuesFlow.multipleSelect.test.tsx +216 -0
  90. package/src/flow/models/blocks/assign-form/__tests__/assignFormValue.test.ts +41 -0
  91. package/src/flow/models/blocks/assign-form/assignFormValue.ts +64 -0
  92. package/src/flow/models/blocks/details/DetailsBlockModel.tsx +5 -1
  93. package/src/flow/models/blocks/details/DetailsItemModel.tsx +62 -1
  94. package/src/flow/models/blocks/details/__tests__/DetailsItemModel.spacing.test.tsx +77 -0
  95. package/src/flow/models/blocks/filter-form/__tests__/FilterFormGridModel.toggleFormFieldsCollapse.test.ts +29 -0
  96. package/src/flow/models/blocks/filter-form/fields/FieldComponentProps.tsx +1 -1
  97. package/src/flow/models/blocks/filter-form/fields/__tests__/FieldComponentProps.options.test.tsx +61 -0
  98. package/src/flow/models/blocks/form/EditFormModel.tsx +23 -1
  99. package/src/flow/models/blocks/form/FormBlockModel.tsx +26 -5
  100. package/src/flow/models/blocks/form/QuickEditFormModel.tsx +45 -11
  101. package/src/flow/models/blocks/form/__tests__/EditFormModel.pagination.test.tsx +77 -0
  102. package/src/flow/models/blocks/form/__tests__/FormBlockModel.test.tsx +145 -0
  103. package/src/flow/models/blocks/form/__tests__/QuickEditFormModel.quickEdit.test.ts +120 -0
  104. package/src/flow/models/blocks/form/__tests__/popupLinkage.test.tsx +175 -0
  105. package/src/flow/models/blocks/js-block/JSBlock.tsx +5 -1
  106. package/src/flow/models/blocks/js-block/__tests__/JSBlockModel.test.tsx +23 -1
  107. package/src/flow/models/blocks/table/TableBlockModel.tsx +4 -10
  108. package/src/flow/models/blocks/table/TableColumnModel.tsx +31 -0
  109. package/src/flow/models/blocks/table/TableJSFieldItemModel.tsx +11 -3
  110. package/src/flow/models/blocks/table/__tests__/TableColumnModel.test.tsx +90 -0
  111. package/src/flow/models/blocks/table/__tests__/sortUtils.test.ts +11 -1
  112. package/src/flow/models/blocks/table/sortUtils.ts +10 -0
  113. package/src/flow/models/fields/AssociationFieldModel/CascadeSelectFieldModel.tsx +11 -2
  114. package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/PopupSubTableFieldModel.tsx +4 -3
  115. package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/blocks/PopupSubTableFormModel.tsx +1 -3
  116. package/src/flow/models/fields/AssociationFieldModel/RecordPickerFieldModel.tsx +6 -3
  117. package/src/flow/models/fields/AssociationFieldModel/RecordSelectFieldModel.tsx +13 -1
  118. package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableColumnModel.tsx +47 -17
  119. package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableField.tsx +6 -1
  120. package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/__tests__/SubTableColumnModel.largeField.test.tsx +178 -0
  121. package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/__tests__/SubTableField.disabled.test.tsx +90 -0
  122. package/src/flow/models/fields/AssociationFieldModel/__tests__/RecordPickerFieldModel.itemContext.test.ts +26 -2
  123. package/src/flow/models/fields/AssociationFieldModel/__tests__/RecordSelectFieldModel.quickCreatePopup.test.tsx +183 -0
  124. package/src/flow/models/fields/AssociationFieldModel/__tests__/associationSelect.search.test.ts +113 -0
  125. package/src/flow/models/fields/DisplayAssociationField/DisplaySubTableFieldModel.tsx +42 -7
  126. package/src/flow/models/fields/DisplayAssociationField/__tests__/DisplaySubTableFieldModel.test.tsx +351 -0
  127. package/src/flow/models/fields/DisplayHtmlFieldModel.tsx +2 -1
  128. package/src/flow/models/fields/InputFieldModel.tsx +1 -1
  129. package/src/flow/models/fields/RichTextFieldModel/__tests__/RichTextFieldModel.test.tsx +74 -0
  130. package/src/flow/models/fields/RichTextFieldModel/index.tsx +27 -6
  131. package/src/flow/models/fields/SelectFieldModel.tsx +5 -0
  132. package/src/flow/models/fields/__tests__/InputFieldModel.test.tsx +16 -0
  133. package/src/flow/models/fields/__tests__/SelectFieldModel.test.tsx +29 -1
  134. package/src/flow/models/fields/mobile-components/MobileLazySelect.tsx +1 -0
  135. package/src/flow/models/fields/mobile-components/__tests__/MobileSelect.test.tsx +20 -0
  136. package/src/flow/models/topbar/TopbarActionModel.tsx +5 -5
  137. package/src/flow/models/topbar/UserCenterTopbarActionModel.tsx +64 -30
  138. package/src/flow/useRouteContentRequest.tsx +46 -0
@@ -10,7 +10,9 @@
10
10
  import { Html5Qrcode, Html5QrcodeScannerState, Html5QrcodeSupportedFormats } from 'html5-qrcode';
11
11
  import jsQR from 'jsqr';
12
12
  import { useCallback, useEffect, useRef, useState } from 'react';
13
+ import type { RefObject } from 'react';
13
14
  import type { CodeFormatsToSupport } from './types';
15
+ import { decodeQrCodeWithZxingWasm } from './zxingWasmDecoder';
14
16
 
15
17
  type ScannerSize = {
16
18
  width: number;
@@ -22,6 +24,7 @@ type UseCodeScannerOptions = {
22
24
  elementId: string;
23
25
  formatsToSupport?: CodeFormatsToSupport;
24
26
  onScannerSizeChanged?: (size: ScannerSize) => void;
27
+ scanViewportRef?: RefObject<HTMLElement | null>;
25
28
  onScanSuccess: (text: string) => void;
26
29
  onScanFailure?: () => void;
27
30
  onCameraStartFailure?: (error: unknown) => void;
@@ -37,6 +40,12 @@ type JsQRImageTransform = {
37
40
  threshold?: number;
38
41
  };
39
42
 
43
+ type QrFrameCaptureLimits = {
44
+ maxHeight: number;
45
+ maxPixels: number;
46
+ maxWidth: number;
47
+ };
48
+
40
49
  type FocusMediaTrackCapabilities = MediaTrackCapabilities & {
41
50
  focusMode?: string[];
42
51
  };
@@ -47,8 +56,15 @@ type FocusMediaTrackConstraintSet = MediaTrackConstraintSet & {
47
56
 
48
57
  const QR_SCAN_IMAGE_SIZES = [3200, 2400, 1600, 1000];
49
58
  const LIVE_QR_SCAN_INTERVAL = 120;
59
+ const IOS_ZXING_SCAN_INTERVAL = 200;
50
60
  const LIVE_QR_SCAN_MAX_WIDTH = 960;
51
61
  const LIVE_QR_SCAN_MAX_HEIGHT = 540;
62
+ const LIVE_QR_SCAN_MAX_PIXELS = LIVE_QR_SCAN_MAX_WIDTH * 420;
63
+ const IOS_ZXING_SCAN_LIMITS: QrFrameCaptureLimits = {
64
+ maxHeight: 720,
65
+ maxPixels: 720 * 960,
66
+ maxWidth: 1280,
67
+ };
52
68
  const QR_SCAN_IMAGE_TRANSFORMS: JsQRImageTransform[] = [
53
69
  {},
54
70
  { contrast: 3, threshold: 105 },
@@ -81,7 +97,49 @@ export function getCodeScanBoxSize(width: number, height: number) {
81
97
  };
82
98
  }
83
99
 
84
- export function scanQrVideoFrame(video: HTMLVideoElement, canvas: HTMLCanvasElement) {
100
+ export function isIOSBrowser() {
101
+ return (
102
+ /iPad|iPhone|iPod/i.test(navigator.userAgent) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
103
+ );
104
+ }
105
+
106
+ function getVisibleVideoFrameRegion(video: HTMLVideoElement, scanViewport: Element) {
107
+ const rect = video.getBoundingClientRect();
108
+ const viewportRect = scanViewport.getBoundingClientRect();
109
+ const visibleLeft = Math.max(viewportRect.left, rect.left);
110
+ const visibleTop = Math.max(viewportRect.top, rect.top);
111
+ const visibleRight = Math.min(viewportRect.right, rect.right);
112
+ const visibleBottom = Math.min(viewportRect.bottom, rect.bottom);
113
+ if (!rect.width || !rect.height || visibleRight <= visibleLeft || visibleBottom <= visibleTop) {
114
+ return;
115
+ }
116
+
117
+ const sourceX = Math.max(0, Math.floor(((visibleLeft - rect.left) / rect.width) * video.videoWidth));
118
+ const sourceY = Math.max(0, Math.floor(((visibleTop - rect.top) / rect.height) * video.videoHeight));
119
+ return {
120
+ x: sourceX,
121
+ y: sourceY,
122
+ width: Math.min(
123
+ video.videoWidth - sourceX,
124
+ Math.ceil(((visibleRight - visibleLeft) / rect.width) * video.videoWidth),
125
+ ),
126
+ height: Math.min(
127
+ video.videoHeight - sourceY,
128
+ Math.ceil(((visibleBottom - visibleTop) / rect.height) * video.videoHeight),
129
+ ),
130
+ };
131
+ }
132
+
133
+ export function getQrVideoFrameImageData(
134
+ video: HTMLVideoElement,
135
+ canvas: HTMLCanvasElement,
136
+ scanViewport?: Element,
137
+ limits: QrFrameCaptureLimits = {
138
+ maxHeight: LIVE_QR_SCAN_MAX_HEIGHT,
139
+ maxPixels: LIVE_QR_SCAN_MAX_PIXELS,
140
+ maxWidth: LIVE_QR_SCAN_MAX_WIDTH,
141
+ },
142
+ ) {
85
143
  if (!video.videoWidth || !video.videoHeight || video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
86
144
  return;
87
145
  }
@@ -89,16 +147,27 @@ export function scanQrVideoFrame(video: HTMLVideoElement, canvas: HTMLCanvasElem
89
147
  const viewfinderWidth = video.clientWidth || video.videoWidth;
90
148
  const viewfinderHeight = video.clientHeight || video.videoHeight;
91
149
  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));
150
+ const scanBoxRegion = {
151
+ width: Math.min(video.videoWidth, Math.floor(scanBoxSize.width * (video.videoWidth / viewfinderWidth))),
152
+ height: Math.min(video.videoHeight, Math.floor(scanBoxSize.height * (video.videoHeight / viewfinderHeight))),
153
+ };
154
+ const sourceRegion = scanViewport
155
+ ? getVisibleVideoFrameRegion(video, scanViewport)
156
+ : {
157
+ x: Math.floor((video.videoWidth - scanBoxRegion.width) / 2),
158
+ y: Math.floor((video.videoHeight - scanBoxRegion.height) / 2),
159
+ ...scanBoxRegion,
160
+ };
161
+ if (!sourceRegion) {
162
+ return;
163
+ }
164
+
165
+ const maxWidth = scanViewport && sourceRegion.height > sourceRegion.width ? limits.maxHeight : limits.maxWidth;
166
+ const maxHeight = scanViewport && sourceRegion.height > sourceRegion.width ? limits.maxWidth : limits.maxHeight;
167
+ const pixelScale = scanViewport ? Math.sqrt(limits.maxPixels / (sourceRegion.width * sourceRegion.height)) : 1;
168
+ const targetScale = Math.min(1, maxWidth / sourceRegion.width, maxHeight / sourceRegion.height, pixelScale);
169
+ const targetWidth = Math.max(1, Math.floor(sourceRegion.width * targetScale));
170
+ const targetHeight = Math.max(1, Math.floor(sourceRegion.height * targetScale));
102
171
  if (canvas.width !== targetWidth) {
103
172
  canvas.width = targetWidth;
104
173
  }
@@ -110,30 +179,75 @@ export function scanQrVideoFrame(video: HTMLVideoElement, canvas: HTMLCanvasElem
110
179
  return;
111
180
  }
112
181
 
113
- context.drawImage(video, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0, targetWidth, targetHeight);
114
- const imageData = context.getImageData(0, 0, targetWidth, targetHeight);
182
+ context.drawImage(
183
+ video,
184
+ sourceRegion.x,
185
+ sourceRegion.y,
186
+ sourceRegion.width,
187
+ sourceRegion.height,
188
+ 0,
189
+ 0,
190
+ targetWidth,
191
+ targetHeight,
192
+ );
193
+ return context.getImageData(0, 0, targetWidth, targetHeight);
194
+ }
195
+
196
+ export function scanQrVideoFrame(video: HTMLVideoElement, canvas: HTMLCanvasElement, scanViewport?: Element) {
197
+ const imageData = getQrVideoFrameImageData(video, canvas, scanViewport);
198
+ if (!imageData) {
199
+ return;
200
+ }
115
201
  return jsQR(imageData.data, imageData.width, imageData.height, { inversionAttempts: 'dontInvert' })?.data;
116
202
  }
117
203
 
118
- function startLiveQrScan(elementId: string, onScanSuccess: (text: string) => void) {
204
+ export async function scanQrVideoFrameWithZxingWasm(
205
+ video: HTMLVideoElement,
206
+ canvas: HTMLCanvasElement,
207
+ scanViewport: Element,
208
+ ) {
209
+ const imageData = getQrVideoFrameImageData(video, canvas, scanViewport, IOS_ZXING_SCAN_LIMITS);
210
+ if (!imageData) {
211
+ return;
212
+ }
213
+ return decodeQrCodeWithZxingWasm(imageData);
214
+ }
215
+
216
+ function startLiveQrScan(
217
+ elementId: string,
218
+ onScanSuccess: (text: string) => void,
219
+ scanViewportRef?: RefObject<HTMLElement | null>,
220
+ ) {
119
221
  const canvas = document.createElement('canvas');
222
+ const useVisiblePreview = isIOSBrowser();
120
223
  let timer: number | undefined;
121
224
  let stopped = false;
122
225
 
123
- const scan = () => {
226
+ const scan = async () => {
124
227
  if (stopped) {
125
228
  return;
126
229
  }
127
230
  const video = document.getElementById(elementId)?.querySelector('video');
231
+ const scanViewport = useVisiblePreview ? scanViewportRef?.current : undefined;
128
232
  if (video) {
129
- const decodedText = scanQrVideoFrame(video, canvas);
130
- if (decodedText) {
233
+ let decodedText: string | undefined;
234
+ try {
235
+ decodedText =
236
+ useVisiblePreview && scanViewport
237
+ ? await scanQrVideoFrameWithZxingWasm(video, canvas, scanViewport)
238
+ : scanQrVideoFrame(video, canvas);
239
+ } catch {
240
+ // Keep the existing scanners active when the optional WASM decoder cannot load or decode a frame.
241
+ }
242
+ if (decodedText && !stopped) {
131
243
  stopped = true;
132
244
  onScanSuccess(decodedText);
133
245
  return;
134
246
  }
135
247
  }
136
- timer = window.setTimeout(scan, LIVE_QR_SCAN_INTERVAL);
248
+ if (!stopped) {
249
+ timer = window.setTimeout(scan, useVisiblePreview ? IOS_ZXING_SCAN_INTERVAL : LIVE_QR_SCAN_INTERVAL);
250
+ }
137
251
  };
138
252
 
139
253
  timer = window.setTimeout(scan, 0);
@@ -304,6 +418,7 @@ export function useCodeScanner({
304
418
  elementId,
305
419
  formatsToSupport,
306
420
  onScannerSizeChanged,
421
+ scanViewportRef,
307
422
  onScanSuccess,
308
423
  onScanFailure,
309
424
  onCameraStartFailure,
@@ -377,11 +492,11 @@ export function useCodeScanner({
377
492
  return;
378
493
  }
379
494
  if (shouldScanQrWithJsQR(formatsToSupport)) {
380
- liveQrScanStopRef.current = startLiveQrScan(elementId, reportScanSuccess);
495
+ liveQrScanStopRef.current = startLiveQrScan(elementId, reportScanSuccess, scanViewportRef);
381
496
  }
382
497
  await enableContinuousFocus(scannerInstance);
383
498
  },
384
- [cancelActiveScan, elementId, formatsToSupport, onScannerSizeChanged, reportScanSuccess],
499
+ [cancelActiveScan, elementId, formatsToSupport, onScannerSizeChanged, reportScanSuccess, scanViewportRef],
385
500
  );
386
501
 
387
502
  const startScanFile = useCallback(
@@ -0,0 +1,64 @@
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
+ let decoderPromise: Promise<typeof import('zxing-wasm/reader')> | undefined;
11
+
12
+ async function loadDecoder() {
13
+ if (!decoderPromise) {
14
+ decoderPromise = Promise.all([import('zxing-wasm/reader'), import('zxing-wasm/reader/zxing_reader.wasm')])
15
+ .then(async ([decoder, { default: wasmUrl }]) => {
16
+ try {
17
+ await decoder.prepareZXingModule({
18
+ fireImmediately: true,
19
+ overrides: { locateFile: () => wasmUrl },
20
+ });
21
+ } catch (error) {
22
+ decoder.purgeZXingModule();
23
+ throw error;
24
+ }
25
+ return decoder;
26
+ })
27
+ .catch((error: unknown) => {
28
+ decoderPromise = undefined;
29
+ throw error;
30
+ });
31
+ }
32
+ return decoderPromise;
33
+ }
34
+
35
+ export async function decodeQrCodeWithZxingWasm(imageData: ImageData) {
36
+ const decoder = await loadDecoder();
37
+ const results = await decoder.readBarcodes(imageData, {
38
+ binarizer: 'GlobalHistogram',
39
+ downscaleThreshold: 300,
40
+ formats: ['QRCode'],
41
+ maxNumberOfSymbols: 1,
42
+ tryDenoise: true,
43
+ tryDownscale: true,
44
+ tryHarder: true,
45
+ tryInvert: true,
46
+ tryRotate: true,
47
+ });
48
+ if (results[0]?.text) {
49
+ return results[0].text;
50
+ }
51
+
52
+ const fallbackResults = await decoder.readBarcodes(imageData, {
53
+ binarizer: 'LocalAverage',
54
+ downscaleThreshold: 300,
55
+ formats: ['QRCode'],
56
+ maxNumberOfSymbols: 1,
57
+ tryDenoise: true,
58
+ tryDownscale: true,
59
+ tryHarder: true,
60
+ tryInvert: true,
61
+ tryRotate: true,
62
+ });
63
+ return fallbackResults[0]?.text;
64
+ }
@@ -150,6 +150,7 @@ export interface VariableInputProps {
150
150
  value?: string;
151
151
  onChange?: (value: string) => void;
152
152
  disabled?: boolean;
153
+ readOnly?: boolean;
153
154
  placeholder?: string;
154
155
  addonBefore?: React.ReactNode;
155
156
  /**
@@ -7,7 +7,7 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
- import { FlowModel, IFlowModelRepository } from '@nocobase/flow-engine';
10
+ import { type CreateModelOptions, type FlowEngine, FlowModel, IFlowModelRepository } from '@nocobase/flow-engine';
11
11
  import type { BaseApplication } from '../BaseApplication';
12
12
  import _ from 'lodash';
13
13
 
@@ -180,6 +180,21 @@ export class FlowModelRepository implements IFlowModelRepository<FlowModel> {
180
180
  return data ? _.cloneDeep(data) : data;
181
181
  }
182
182
 
183
+ async ensureRouteContent(options: CreateModelOptions) {
184
+ const existing = await this.findOne({ parentId: options.parentId, subKey: options.subKey });
185
+ if (existing) return existing;
186
+
187
+ const response = await this.app.apiClient.request({
188
+ method: 'POST',
189
+ url: 'flowModels:save',
190
+ params: { ensureRouteContent: true },
191
+ data: options,
192
+ });
193
+ const persisted = await this.findOne({ uid: response.data.data });
194
+ if (!persisted) throw new Error('Could not load the initialized route content');
195
+ return persisted;
196
+ }
197
+
183
198
  async save(model: FlowModel, options?: { onlyStepParams?: boolean }) {
184
199
  const data = model.serialize();
185
200
  if (options?.onlyStepParams) {
@@ -220,3 +235,34 @@ export class FlowModelRepository implements IFlowModelRepository<FlowModel> {
220
235
  return response.data?.data;
221
236
  }
222
237
  }
238
+
239
+ // A tab renderer can be mounted again when the UI Editor adds or removes its controls.
240
+ const confirmedRouteModels = new WeakSet<FlowModel>();
241
+
242
+ export function isRouteContentConfirmed(model: FlowModel) {
243
+ return confirmedRouteModels.has(model);
244
+ }
245
+
246
+ export async function loadRouteContentForEditing(engine: FlowEngine, options: CreateModelOptions) {
247
+ const repository = engine.modelRepository;
248
+ if (!(repository instanceof FlowModelRepository)) {
249
+ return engine.loadOrCreateModel(options);
250
+ }
251
+
252
+ // A route model can live in the outer engine while its page lives in this view's engine.
253
+ let cached = engine.findModelByParentId(options.parentId, options.subKey);
254
+ if (!cached) {
255
+ engine.forEachModel((model) => {
256
+ if (model.parentId === options.parentId && model.subKey === options.subKey) cached = model;
257
+ });
258
+ }
259
+ if (cached && isRouteContentConfirmed(cached)) return cached;
260
+ const data = await repository.ensureRouteContent({ ...options, uid: cached?.uid });
261
+ if (cached && cached.uid !== data.uid) engine.removeModelWithSubModels(cached.uid);
262
+ const model = await engine.createModelAsync(data);
263
+ if (model.parent && model.parent.subModels[options.subKey] !== model) {
264
+ model.parent.setSubModel(options.subKey, model);
265
+ }
266
+ confirmedRouteModels.add(model);
267
+ return model;
268
+ }
@@ -15,6 +15,10 @@ import {
15
15
  useFlowViewContext,
16
16
  } from '@nocobase/flow-engine';
17
17
  import type { FlowEngineContext, FlowModel, FlowModelRendererProps, ModelConstructor } from '@nocobase/flow-engine';
18
+ import { observer } from '@formily/reactive-react';
19
+ import { loadRouteContentForEditing } from './FlowModelRepository';
20
+ import { RouteContent, useRouteContentRequest } from './useRouteContentRequest';
21
+ import { RouteModel } from './models/base/RouteModel';
18
22
  import { useRequest } from 'ahooks';
19
23
  import React from 'react';
20
24
  import FlowRoute from './components/FlowRoute';
@@ -85,12 +89,13 @@ const bindViewLayoutState = (model: FlowModel, ctx?: FlowPageViewContext | null)
85
89
  });
86
90
  };
87
91
 
88
- export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknown>) => {
92
+ export const FlowPage = observer((props: FlowPageProps & Record<string, unknown>) => {
89
93
  const { pageModelClass = 'ChildPageModel', parentId, onModelLoaded, defaultTabTitle, ...rest } = props;
90
94
  const flowEngine = useFlowEngine();
95
+ const isRoute = !!parentId && flowEngine.getModel(parentId, true) instanceof RouteModel;
91
96
  const ctx = useFlowViewContext<FlowPageViewContext>();
92
- const { loading, data, error } = useRequest(
93
- async () => {
97
+ const { model, loading, error, needsConfirmation } = useRouteContentRequest(
98
+ async (forEditing) => {
94
99
  const ModelClass = await flowEngine.getModelClassAsync(pageModelClass);
95
100
  if (!ModelClass) {
96
101
  throw new Error(`[NocoBase] Page model class '${pageModelClass}' is not registered.`);
@@ -101,7 +106,7 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
101
106
  async: true,
102
107
  parentId,
103
108
  subKey: 'page',
104
- subType: 'object',
109
+ subType: 'object' as const,
105
110
  use: pageModelClass,
106
111
  };
107
112
  const isRuntimeRecordScopedPage =
@@ -136,7 +141,11 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
136
141
  }
137
142
  const data =
138
143
  (isRuntimeRecordScopedPage && (await flowEngine.loadModel({ ...options, refresh: true }))) ||
139
- (await flowEngine.loadOrCreateModel(options, { skipSave: !flowEngine.context.flowSettingsEnabled }));
144
+ (forEditing
145
+ ? await loadRouteContentForEditing(flowEngine, options)
146
+ : await flowEngine.loadOrCreateModel(options, {
147
+ skipSave: isRoute || !flowEngine.context.flowSettingsEnabled,
148
+ }));
140
149
  if (data?.uid && onModelLoaded) {
141
150
  data.context.addDelegate(ctx);
142
151
  bindViewLayoutState(data, ctx);
@@ -145,17 +154,23 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
145
154
  }
146
155
  return data;
147
156
  },
148
- {
149
- refreshDeps: [parentId, pageModelClass, defaultTabTitle],
150
- },
157
+ isRoute && flowEngine.context.flowSettingsEnabled,
158
+ [parentId, pageModelClass, defaultTabTitle],
151
159
  );
152
160
  if (error) {
153
161
  throw error;
154
162
  }
155
- if (loading || !data?.uid) {
163
+ if (!model?.uid || (loading && !isRoute)) {
156
164
  return <SkeletonFallback style={{ margin: ctx?.isMobileLayout ? 8 : ctx?.themeToken.marginBlock }} />;
157
165
  }
158
- return <InternalFlowPage uid={data.uid} {...rest} />;
166
+ const page = <InternalFlowPage uid={model.uid} {...rest} />;
167
+ return isRoute ? (
168
+ <RouteContent className="nb-flow-route-page" pending={needsConfirmation}>
169
+ {page}
170
+ </RouteContent>
171
+ ) : (
172
+ page
173
+ );
159
174
  });
160
175
 
161
176
  FlowPage.displayName = 'FlowPage';
@@ -8,9 +8,46 @@
8
8
  */
9
9
 
10
10
  import { describe, it, expect, vi } from 'vitest';
11
- import { FlowModelRepository } from '../FlowModelRepository';
11
+ import { FlowEngine, FlowModel } from '@nocobase/flow-engine';
12
+ import type { BaseApplication } from '../../BaseApplication';
13
+ import { FlowModelRepository, loadRouteContentForEditing } from '../FlowModelRepository';
12
14
 
13
15
  describe('FlowModelRepository', () => {
16
+ it('keeps the browse UID when the route parent is outside the current engine', async () => {
17
+ const engine = new FlowEngine();
18
+ engine.registerModels({ FlowModel });
19
+ const request = vi
20
+ .fn()
21
+ .mockResolvedValueOnce({ data: {} })
22
+ .mockResolvedValueOnce({ data: { data: 'browse-uid' } })
23
+ .mockResolvedValueOnce({
24
+ data: { data: { uid: 'browse-uid', use: 'FlowModel', parentId: 'route', subKey: 'page' } },
25
+ });
26
+ engine.setModelRepository(new FlowModelRepository({ apiClient: { request } } as unknown as BaseApplication));
27
+ const options = { use: 'FlowModel', parentId: 'route', subKey: 'page', subType: 'object' as const };
28
+ const browsing = engine.createModel({ ...options, uid: 'browse-uid' });
29
+ expect(await loadRouteContentForEditing(engine, options)).toBe(browsing);
30
+ expect(await loadRouteContentForEditing(engine, options)).toBe(browsing);
31
+ expect(request).toHaveBeenCalledTimes(3);
32
+ expect(request.mock.calls[1][0].data).toMatchObject({ ...options, uid: browsing.uid });
33
+ });
34
+
35
+ it('retries the first edit when the initialization request fails', async () => {
36
+ const engine = new FlowEngine();
37
+ engine.registerModels({ FlowModel });
38
+ const options = { use: 'FlowModel', parentId: 'route', subKey: 'page', subType: 'object' as const };
39
+ const model = engine.createModel({ ...options, uid: 'browse-uid' });
40
+ const request = vi
41
+ .fn()
42
+ .mockResolvedValueOnce({ data: {} })
43
+ .mockRejectedValueOnce(new Error('Save failed'))
44
+ .mockResolvedValueOnce({ data: { data: { ...options, uid: model.uid } } });
45
+ engine.setModelRepository(new FlowModelRepository({ apiClient: { request } } as unknown as BaseApplication));
46
+ await expect(loadRouteContentForEditing(engine, options)).rejects.toThrow('Save failed');
47
+ expect(await loadRouteContentForEditing(engine, options)).toBe(model);
48
+ expect(request).toHaveBeenCalledTimes(3);
49
+ });
50
+
14
51
  it('dedupes concurrent findOne requests with same key', async () => {
15
52
  const request = vi.fn(async () => {
16
53
  await new Promise((resolve) => setTimeout(resolve, 10));