@nocobase/flow-engine 2.1.0-beta.37 → 2.1.0-beta.40

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 (41) hide show
  1. package/lib/components/settings/wrappers/contextual/DefaultSettingsIcon.js +8 -1
  2. package/lib/components/subModel/LazyDropdown.js +200 -16
  3. package/lib/data-source/index.d.ts +9 -0
  4. package/lib/data-source/index.js +12 -0
  5. package/lib/flowContext.js +3 -0
  6. package/lib/flowEngine.js +3 -3
  7. package/lib/models/flowModel.js +3 -3
  8. package/lib/utils/parsePathnameToViewParams.d.ts +5 -1
  9. package/lib/utils/parsePathnameToViewParams.js +28 -4
  10. package/lib/views/ViewNavigation.d.ts +12 -2
  11. package/lib/views/ViewNavigation.js +22 -7
  12. package/lib/views/createViewMeta.js +114 -50
  13. package/lib/views/inheritLayoutContext.d.ts +10 -0
  14. package/lib/views/inheritLayoutContext.js +50 -0
  15. package/lib/views/useDialog.js +2 -0
  16. package/lib/views/useDrawer.js +2 -0
  17. package/lib/views/usePage.js +2 -0
  18. package/package.json +4 -4
  19. package/src/__tests__/createViewMeta.popup.test.ts +115 -1
  20. package/src/__tests__/flowEngine.removeModel.test.ts +47 -3
  21. package/src/components/settings/wrappers/contextual/DefaultSettingsIcon.tsx +11 -1
  22. package/src/components/settings/wrappers/contextual/__tests__/DefaultSettingsIcon.test.tsx +5 -2
  23. package/src/components/subModel/LazyDropdown.tsx +228 -16
  24. package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +203 -1
  25. package/src/data-source/index.ts +18 -0
  26. package/src/executor/__tests__/flowExecutor.test.ts +28 -0
  27. package/src/flowContext.ts +3 -0
  28. package/src/flowEngine.ts +4 -3
  29. package/src/models/__tests__/flowEngine.resolveUse.test.ts +0 -15
  30. package/src/models/__tests__/flowModel.test.ts +33 -34
  31. package/src/models/flowModel.tsx +3 -3
  32. package/src/utils/__tests__/parsePathnameToViewParams.test.ts +21 -0
  33. package/src/utils/parsePathnameToViewParams.ts +45 -5
  34. package/src/views/ViewNavigation.ts +40 -7
  35. package/src/views/__tests__/ViewNavigation.test.ts +52 -0
  36. package/src/views/__tests__/inheritLayoutContext.test.ts +53 -0
  37. package/src/views/createViewMeta.ts +106 -34
  38. package/src/views/inheritLayoutContext.ts +26 -0
  39. package/src/views/useDialog.tsx +2 -0
  40. package/src/views/useDrawer.tsx +2 -0
  41. package/src/views/usePage.tsx +2 -0
@@ -15,6 +15,82 @@ import type { FlowView } from './FlowView';
15
15
 
16
16
  type PopupModelLike = { getStepParams?: (a: string, b: string) => any } | undefined;
17
17
 
18
+ function isDefined(value: any) {
19
+ return value !== undefined && value !== null;
20
+ }
21
+
22
+ function isSameViewParamValue(left: any, right: any) {
23
+ if (left === right) return true;
24
+ if (!isDefined(left) || !isDefined(right)) return false;
25
+
26
+ try {
27
+ return JSON.stringify(left) === JSON.stringify(right);
28
+ } catch (_) {
29
+ return String(left) === String(right);
30
+ }
31
+ }
32
+
33
+ function getViewStack(view?: FlowView): any[] {
34
+ const stack = view?.navigation?.viewStack;
35
+ return Array.isArray(stack) ? stack : [];
36
+ }
37
+
38
+ function getAnchoredViewStackIndex(view?: FlowView, stack = getViewStack(view)): number {
39
+ if (!stack.length) return -1;
40
+
41
+ const args = view?.inputArgs || {};
42
+ const navParams = view?.navigation?.viewParams || {};
43
+ const viewUid = args.viewUid ?? navParams.viewUid;
44
+
45
+ if (!viewUid) {
46
+ return stack.length - 1;
47
+ }
48
+
49
+ const candidates = stack.map((item, index) => ({ item, index })).filter(({ item }) => item?.viewUid === viewUid);
50
+
51
+ if (!candidates.length) {
52
+ return stack.length - 1;
53
+ }
54
+
55
+ const keys = ['filterByTk', 'sourceId', 'tabUid'];
56
+ let bestIndex = candidates[candidates.length - 1].index;
57
+ let bestScore = -1;
58
+
59
+ for (const { item, index } of candidates) {
60
+ let score = 0;
61
+ let matched = true;
62
+
63
+ for (const key of keys) {
64
+ if (!isDefined(args[key])) continue;
65
+ if (!isSameViewParamValue(item?.[key], args[key])) {
66
+ matched = false;
67
+ break;
68
+ }
69
+ score += 1;
70
+ }
71
+
72
+ if (!matched) continue;
73
+ if (score >= bestScore) {
74
+ bestIndex = index;
75
+ bestScore = score;
76
+ }
77
+ }
78
+
79
+ return bestIndex;
80
+ }
81
+
82
+ function getPopupView(ctx: FlowContext, anchorView?: FlowView) {
83
+ return anchorView ?? ctx.view;
84
+ }
85
+
86
+ function isPopupView(view?: FlowView): boolean {
87
+ if (!view) return false;
88
+ const stack = getViewStack(view);
89
+ const openerUids = view?.inputArgs?.openerUids;
90
+ const hasOpener = Array.isArray(openerUids) && openerUids.length > 0;
91
+ return getAnchoredViewStackIndex(view, stack) >= 1 || hasOpener;
92
+ }
93
+
18
94
  // 判断是否为普通对象(Plain Object),避免对类实例/代理等进行深度遍历
19
95
  function isPlainObject(val: any) {
20
96
  if (val === null || typeof val !== 'object') return false;
@@ -79,19 +155,11 @@ function makeMetaFromValue(value: any, title?: string, seen?: WeakSet<any>): any
79
155
  export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): PropertyMetaFactory {
80
156
  const t = (k: string) => ctx.t(k);
81
157
 
82
- const isPopupView = (view?: FlowView): boolean => {
83
- if (!view) return false;
84
- const stack = Array.isArray(view.navigation?.viewStack) ? view.navigation.viewStack : [];
85
- const openerUids = view?.inputArgs?.openerUids;
86
- const hasOpener = Array.isArray(openerUids) && openerUids.length > 0;
87
- return stack.length >= 2 || hasOpener;
88
- };
89
-
90
- const hasPopupNow = (): boolean => isPopupView(anchorView ?? ctx.view);
158
+ const hasPopupNow = (flowCtx: FlowContext = ctx): boolean => isPopupView(getPopupView(flowCtx, anchorView));
91
159
 
92
160
  // 统一解析锚定视图下的 RecordRef,避免在设置弹窗等二级视图中被误导
93
161
  const resolveRecordRef = async (flowCtx: FlowContext): Promise<RecordRef | undefined> => {
94
- const view = anchorView ?? flowCtx.view;
162
+ const view = getPopupView(flowCtx, anchorView);
95
163
  if (!view || !isPopupView(view)) return undefined;
96
164
 
97
165
  const base = await buildPopupRuntime(flowCtx, view);
@@ -119,11 +187,12 @@ export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): Proper
119
187
  const getParentRecordRef = async (level: number, flowCtx?: FlowContext): Promise<RecordRef | undefined> => {
120
188
  try {
121
189
  const useCtx = flowCtx || ctx;
122
- const nav = useCtx.view?.navigation;
123
- const stack = Array.isArray(nav?.viewStack) ? nav.viewStack : [];
124
- if (stack.length < 2 || level < 1) return undefined;
125
- const idx = stack.length - 1 - level;
126
- if (idx < 0) return undefined;
190
+ const view = getPopupView(useCtx, anchorView);
191
+ const stack = getViewStack(view);
192
+ const currentIndex = getAnchoredViewStackIndex(view, stack);
193
+ if (currentIndex < 1 || level < 1) return undefined;
194
+ const idx = currentIndex - level;
195
+ if (idx < 1) return undefined;
127
196
  const parent = stack[idx];
128
197
  if (!parent?.viewUid) return undefined;
129
198
 
@@ -156,9 +225,10 @@ export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): Proper
156
225
 
157
226
  const hasParentNow = (level: number): boolean => {
158
227
  try {
159
- const nav = (anchorView ?? ctx.view)?.navigation;
160
- const stack = Array.isArray(nav?.viewStack) ? nav.viewStack : [];
161
- return stack.length >= level + 1; // level=1 需要至少2层
228
+ const view = getPopupView(ctx, anchorView);
229
+ const stack = getViewStack(view);
230
+ const currentIndex = getAnchoredViewStackIndex(view, stack);
231
+ return currentIndex - level >= 1; // level=1 需要至少一个上级弹窗
162
232
  } catch (_) {
163
233
  return false;
164
234
  }
@@ -231,9 +301,10 @@ export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): Proper
231
301
  disabled: () => !hasPopupNow(),
232
302
  hidden: () => !hasPopupNow(),
233
303
  buildVariablesParams: async (c) => {
234
- if (!hasPopupNow()) return undefined;
304
+ if (!hasPopupNow(c)) return undefined;
235
305
  const ref = await resolveRecordRef(c);
236
- const inputArgs = c.view?.inputArgs;
306
+ const view = getPopupView(c, anchorView);
307
+ const inputArgs = view?.inputArgs;
237
308
  type PopupVariableParams = {
238
309
  record?: RecordRef;
239
310
  sourceRecord?: RecordRef;
@@ -253,9 +324,9 @@ export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): Proper
253
324
 
254
325
  // 构建 parent 链(用于服务端解析 ctx.popup.parent[.parent...].record.*)
255
326
  try {
256
- const nav = c.view?.navigation;
257
- const stack = Array.isArray(nav?.viewStack) ? nav.viewStack : [];
258
- if (stack.length >= 2) {
327
+ const stack = getViewStack(view);
328
+ const currentIndex = getAnchoredViewStackIndex(view, stack);
329
+ if (currentIndex >= 2) {
259
330
  let cur: Record<string, any> = params;
260
331
  let level = 1;
261
332
  let parentRef = await getParentRecordRef(level, c);
@@ -315,20 +386,21 @@ export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): Proper
315
386
  }
316
387
  // 当 view.inputArgs 带有 sourceId + associationName 时,提供“上级记录”变量(基于 sourceId 推断)
317
388
  try {
318
- const inputArgs = ctx.view?.inputArgs;
389
+ const view = getPopupView(ctx, anchorView);
390
+ const inputArgs = view?.inputArgs;
319
391
  const srcId = inputArgs?.sourceId;
320
392
  let assoc: string | undefined = inputArgs?.associationName;
321
393
  let dsKey: string = inputArgs?.dataSourceKey || 'main';
322
394
 
323
395
  // 兜底:若 associationName 缺失或不含“.”,尝试从当前视图模型的 openView 参数推断
324
396
  if (!assoc || typeof assoc !== 'string' || !assoc.includes('.')) {
325
- const nav = ctx.view?.navigation;
326
- const stack = Array.isArray(nav?.viewStack) ? nav.viewStack : [];
327
- const last = stack?.[stack.length - 1];
328
- if (last?.viewUid) {
329
- let model = ctx?.engine?.getModel(last.viewUid, true) as PopupModelLike;
397
+ const stack = getViewStack(view);
398
+ const currentIndex = getAnchoredViewStackIndex(view, stack);
399
+ const current = currentIndex >= 0 ? stack?.[currentIndex] : undefined;
400
+ if (current?.viewUid) {
401
+ let model = ctx?.engine?.getModel(current.viewUid, true) as PopupModelLike;
330
402
  if (!model) {
331
- model = (await ctx.engine.loadModel({ uid: last.viewUid })) as PopupModelLike;
403
+ model = (await ctx.engine.loadModel({ uid: current.viewUid })) as PopupModelLike;
332
404
  }
333
405
  const p = model?.getStepParams?.('popupSettings', 'openView') || {};
334
406
  assoc = p?.associationName || assoc;
@@ -406,12 +478,12 @@ interface PopupNode {
406
478
  }
407
479
 
408
480
  export async function buildPopupRuntime(ctx: FlowContext, view: FlowView): Promise<PopupNode | undefined> {
409
- const nav = view?.navigation;
410
- const stack = Array.isArray(nav?.viewStack) ? nav.viewStack : [];
481
+ const stack = getViewStack(view);
482
+ const currentIndex = getAnchoredViewStackIndex(view, stack);
411
483
 
412
484
  const openerUids = view?.inputArgs?.openerUids;
413
485
  const hasOpener = Array.isArray(openerUids) && openerUids.length > 0;
414
- const hasStackPopup = stack.length >= 2;
486
+ const hasStackPopup = currentIndex >= 1;
415
487
  const isPopup = hasStackPopup || hasOpener;
416
488
  if (!isPopup) return undefined;
417
489
 
@@ -457,7 +529,7 @@ export async function buildPopupRuntime(ctx: FlowContext, view: FlowView): Promi
457
529
  if (parentNode) node.parent = parentNode;
458
530
  return node;
459
531
  };
460
- const currentNode = await buildNode((view?.navigation?.viewStack?.length || 1) - 1);
532
+ const currentNode = await buildNode(currentIndex);
461
533
  return currentNode;
462
534
  }
463
535
 
@@ -0,0 +1,26 @@
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 { FlowContext } from '../flowContext';
11
+
12
+ export function inheritLayoutContextForDetachedView(viewContext: FlowContext, sourceContext: FlowContext) {
13
+ const layoutContext = sourceContext?.layoutContext;
14
+ const engineContext = sourceContext?.engine?.context;
15
+
16
+ if (!(layoutContext instanceof FlowContext)) {
17
+ return;
18
+ }
19
+
20
+ if (layoutContext === sourceContext || layoutContext === engineContext) {
21
+ return;
22
+ }
23
+
24
+ // inheritContext=false 只隔离业务上下文,Layout 运行时上下文仍需要传给弹窗/嵌入视图。
25
+ viewContext.addDelegate(layoutContext);
26
+ }
@@ -20,6 +20,7 @@ import { FlowEngineProvider } from '../provider';
20
20
  import { createViewScopedEngine } from '../ViewScopedFlowEngine';
21
21
  import { createViewRecordResolveOnServer, getViewRecordFromParent } from '../utils/variablesParams';
22
22
  import { runViewBeforeClose } from './runViewBeforeClose';
23
+ import { inheritLayoutContextForDetachedView } from './inheritLayoutContext';
23
24
 
24
25
  let uuid = 0;
25
26
 
@@ -88,6 +89,7 @@ export function useDialog() {
88
89
  ctx.addDelegate(flowContext);
89
90
  } else {
90
91
  ctx.addDelegate(flowContext.engine.context);
92
+ inheritLayoutContextForDetachedView(ctx, flowContext);
91
93
  }
92
94
 
93
95
  // 幂等保护:防止 FlowPage 路由清理时二次调用 destroy
@@ -20,6 +20,7 @@ import { FlowEngineProvider } from '../provider';
20
20
  import { createViewScopedEngine } from '../ViewScopedFlowEngine';
21
21
  import { createViewRecordResolveOnServer, getViewRecordFromParent } from '../utils/variablesParams';
22
22
  import { runViewBeforeClose } from './runViewBeforeClose';
23
+ import { inheritLayoutContextForDetachedView } from './inheritLayoutContext';
23
24
 
24
25
  export function useDrawer() {
25
26
  const holderRef = React.useRef(null);
@@ -117,6 +118,7 @@ export function useDrawer() {
117
118
  ctx.addDelegate(flowContext);
118
119
  } else {
119
120
  ctx.addDelegate(flowContext.engine.context);
121
+ inheritLayoutContextForDetachedView(ctx, flowContext);
120
122
  }
121
123
 
122
124
  // 幂等保护:防止 FlowPage 路由清理时二次调用 destroy
@@ -20,6 +20,7 @@ import { FlowEngineProvider } from '../provider';
20
20
  import { createViewScopedEngine } from '../ViewScopedFlowEngine';
21
21
  import { createViewRecordResolveOnServer, getViewRecordFromParent } from '../utils/variablesParams';
22
22
  import { runViewBeforeClose } from './runViewBeforeClose';
23
+ import { inheritLayoutContextForDetachedView } from './inheritLayoutContext';
23
24
 
24
25
  let uuid = 0;
25
26
 
@@ -223,6 +224,7 @@ export function usePage() {
223
224
  ctx.addDelegate(flowContext);
224
225
  } else {
225
226
  ctx.addDelegate(flowContext.engine.context);
227
+ inheritLayoutContextForDetachedView(ctx, flowContext);
226
228
  }
227
229
 
228
230
  // 构造 currentPage 实例