@nocobase/flow-engine 2.3.0-alpha.1 → 2.3.0-beta.2

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 (53) hide show
  1. package/lib/acl/Acl.d.ts +2 -1
  2. package/lib/acl/Acl.js +28 -0
  3. package/lib/components/FlowContextSelector.js +7 -1
  4. package/lib/components/MobilePopup.js +14 -3
  5. package/lib/components/subModel/LazyDropdown.js +62 -33
  6. package/lib/flowContext.d.ts +12 -1
  7. package/lib/flowContext.js +47 -6
  8. package/lib/locale/en-US.json +2 -0
  9. package/lib/locale/index.d.ts +4 -0
  10. package/lib/locale/zh-CN.json +2 -0
  11. package/lib/resources/flowResource.js +1 -0
  12. package/lib/utils/associationObjectVariable.d.ts +10 -0
  13. package/lib/utils/associationObjectVariable.js +10 -7
  14. package/lib/utils/dateVariable.d.ts +22 -0
  15. package/lib/utils/dateVariable.js +123 -16
  16. package/lib/utils/dirtyAwareApiClient.d.ts +1 -0
  17. package/lib/utils/dirtyAwareApiClient.js +15 -2
  18. package/lib/utils/index.d.ts +3 -3
  19. package/lib/utils/index.js +8 -0
  20. package/lib/utils/params-resolvers.d.ts +3 -0
  21. package/lib/utils/params-resolvers.js +10 -0
  22. package/lib/utils/variablesParams.js +5 -0
  23. package/lib/views/createViewMeta.d.ts +1 -0
  24. package/lib/views/createViewMeta.js +53 -22
  25. package/package.json +4 -4
  26. package/src/__tests__/createViewMeta.popup.test.ts +84 -1
  27. package/src/__tests__/flowContext.test.ts +8 -0
  28. package/src/__tests__/objectVariable.test.ts +6 -1
  29. package/src/__tests__/runjsFormSubmit.test.ts +138 -0
  30. package/src/acl/Acl.tsx +36 -1
  31. package/src/acl/__tests__/Acl.test.tsx +70 -0
  32. package/src/components/FlowContextSelector.tsx +7 -1
  33. package/src/components/MobilePopup.tsx +16 -4
  34. package/src/components/__tests__/MobilePopup.test.tsx +42 -1
  35. package/src/components/subModel/LazyDropdown.tsx +71 -38
  36. package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +85 -2
  37. package/src/components/subModel/__tests__/LazyDropdown.test.tsx +202 -0
  38. package/src/components/variables/__tests__/FlowContextSelector.test.tsx +35 -0
  39. package/src/flowContext.ts +79 -6
  40. package/src/locale/__tests__/index.test.ts +21 -0
  41. package/src/locale/en-US.json +2 -0
  42. package/src/locale/zh-CN.json +2 -0
  43. package/src/resources/__tests__/flowResource.test.ts +3 -0
  44. package/src/resources/flowResource.ts +1 -0
  45. package/src/utils/__tests__/dateVariable.test.ts +57 -4
  46. package/src/utils/__tests__/variablesParams.test.ts +28 -1
  47. package/src/utils/associationObjectVariable.ts +9 -6
  48. package/src/utils/dateVariable.ts +145 -18
  49. package/src/utils/dirtyAwareApiClient.ts +25 -2
  50. package/src/utils/index.ts +17 -2
  51. package/src/utils/params-resolvers.ts +12 -0
  52. package/src/utils/variablesParams.ts +10 -0
  53. package/src/views/createViewMeta.ts +52 -18
@@ -42,9 +42,19 @@ type ResourceRequestOptions = RequestOptions & {
42
42
  type DirtyResourceAction = {
43
43
  dataSourceKey?: string;
44
44
  resourceName: string;
45
+ resourceOf?: unknown;
45
46
  actionName: string;
46
47
  };
47
48
 
49
+ export const PREPARE_CONTEXT_RESOURCE_ACTION_PARAMS = Symbol('prepareContextResourceActionParams');
50
+
51
+ type ContextResourceActionParamsPreparer = {
52
+ [PREPARE_CONTEXT_RESOURCE_ACTION_PARAMS]?: (
53
+ action: DirtyResourceAction,
54
+ params: ActionParams | undefined,
55
+ ) => ActionParams | undefined;
56
+ };
57
+
48
58
  type ApiUrlProvider = {
49
59
  getApiUrl?: (pathname?: string) => string;
50
60
  };
@@ -328,6 +338,7 @@ function resolveDirtyResourceActionFromResource(
328
338
 
329
339
  return {
330
340
  resourceName: normalizedResourceName,
341
+ resourceOf,
331
342
  actionName: normalizedActionName,
332
343
  };
333
344
  }
@@ -442,6 +453,18 @@ function createDirtyAwareResource(
442
453
  return async (...args: Parameters<ResourceActionFn>) => {
443
454
  const actionOptions = isObjectRecord(args[1]) ? args[1] : undefined;
444
455
  const dirtyResourceAction = resolveDirtyResourceActionFromResource(resourceName, resourceOf, prop, context);
456
+ const prepareParams = (context as ContextResourceActionParamsPreparer)[PREPARE_CONTEXT_RESOURCE_ACTION_PARAMS];
457
+ const actionParams =
458
+ dirtyResourceAction && typeof prepareParams === 'function'
459
+ ? prepareParams.call(
460
+ context,
461
+ {
462
+ ...dirtyResourceAction,
463
+ dataSourceKey: dirtyResourceAction.dataSourceKey || getDataSourceKeyFromHeaders(headers),
464
+ },
465
+ args[0],
466
+ )
467
+ : args[0];
445
468
  const requestKey = getDirtyResourceActionDispatchKey(dirtyResourceAction, headers);
446
469
  const resourceKey = getResourceDispatchKey(resourceName, resourceOf, headers);
447
470
  const inheritedToken =
@@ -459,13 +482,13 @@ function createDirtyAwareResource(
459
482
  const forwardedArgs: Parameters<ResourceActionFn> =
460
483
  actionOptions || args[1] == null
461
484
  ? [
462
- args[0],
485
+ actionParams,
463
486
  {
464
487
  ...actionOptions,
465
488
  [DIRTY_DISPATCH_TOKEN]: token,
466
489
  },
467
490
  ]
468
- : args;
491
+ : [actionParams, args[1]];
469
492
  let actionResult: Promise<unknown>;
470
493
  requestTokenStack.push({ key: requestKey, token });
471
494
  try {
@@ -31,7 +31,12 @@ export { defineAction } from './flow-definitions';
31
31
  export { isInheritedFrom } from './inheritance';
32
32
 
33
33
  // 参数解析器
34
- export { resolveCreateModelOptions, resolveDefaultParams, resolveExpressions } from './params-resolvers';
34
+ export {
35
+ buildFlowModelResolveDescriptor,
36
+ resolveCreateModelOptions,
37
+ resolveDefaultParams,
38
+ resolveExpressions,
39
+ } from './params-resolvers';
35
40
 
36
41
  // Schema 工具
37
42
  export {
@@ -48,7 +53,11 @@ export { setupRuntimeContextSteps } from './setupRuntimeContextSteps';
48
53
 
49
54
  // Record Proxy 工具
50
55
  export { createCollectionContextMeta } from './createCollectionContextMeta';
51
- export { createAssociationAwareObjectMetaFactory, createAssociationSubpathResolver } from './associationObjectVariable';
56
+ export {
57
+ createAssociationAwareObjectMetaFactory,
58
+ createAssociationSubpathResolver,
59
+ getAssociationFilterByTk,
60
+ } from './associationObjectVariable';
52
61
  export {
53
62
  buildRecordMeta,
54
63
  collectContextParamsForTemplate,
@@ -83,8 +92,14 @@ export {
83
92
  isCtxDatePathPrefix,
84
93
  isCtxDateExpression,
85
94
  parseCtxDateExpression,
95
+ parseCtxDateExpressionConfig,
86
96
  resolveCtxDatePath,
97
+ serializeCtxDateExpressionConfig,
87
98
  serializeCtxDateValue,
99
+ type CtxDateExpressionConfig,
100
+ type CtxDatePreset,
101
+ type CtxDateRelativeDirection,
102
+ type CtxDateRelativeUnit,
88
103
  } from './dateVariable';
89
104
 
90
105
  // RunJS value helpers
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  import { getValuesByPath } from '@nocobase/shared';
11
+ import { generateFlowModelRdFromToken } from '@nocobase/utils/client';
11
12
  import _ from 'lodash';
12
13
  import { FlowContext, FlowModelContext, FlowRuntimeContext } from '../flowContext';
13
14
  import type { FlowModel } from '../models';
@@ -78,6 +79,8 @@ export type JSONValue = string | { [key: string]: JSONValue } | JSONValue[];
78
79
  // =========================
79
80
 
80
81
  type BatchPayload = {
82
+ contractRd?: string;
83
+ rd?: string;
81
84
  template: JSONValue;
82
85
  contextParams?: ServerContextParams | undefined;
83
86
  };
@@ -170,6 +173,8 @@ export function enqueueVariablesResolve(ctx: FlowRuntimeContext, payload: BatchP
170
173
  try {
171
174
  const batch = items.map((it) => ({
172
175
  id: it.id,
176
+ contractRd: it.payload.contractRd,
177
+ rd: it.payload.rd,
173
178
  template: it.payload.template,
174
179
  contextParams: it.payload.contextParams || {},
175
180
  }));
@@ -218,6 +223,13 @@ export function enqueueVariablesResolve(ctx: FlowRuntimeContext, payload: BatchP
218
223
  return p;
219
224
  }
220
225
 
226
+ export function buildFlowModelResolveDescriptor(
227
+ ctx: Pick<FlowModelContext, 'api'>,
228
+ flowModelUid?: string | number | null,
229
+ ) {
230
+ return generateFlowModelRdFromToken(flowModelUid, ctx?.api?.auth?.token);
231
+ }
232
+
221
233
  /**
222
234
  * 解析参数中的 {{xxx}} 表达式,自动处理异步属性访问
223
235
  */
@@ -263,5 +263,15 @@ export async function collectContextParamsForTemplate(
263
263
  input[key] = built;
264
264
  }
265
265
  }
266
+
267
+ const viewPaths = usage.view || [];
268
+ if (
269
+ !input.view &&
270
+ viewPaths.some((path) => path === 'record' || path.startsWith('record.') || path.startsWith('record['))
271
+ ) {
272
+ const recordRef = inferViewRecordRef(ctx);
273
+ if (recordRef) input.view = { record: recordRef };
274
+ }
275
+
266
276
  return buildServerContextParams(ctx, input);
267
277
  }
@@ -15,6 +15,13 @@ import type { FlowView } from './FlowView';
15
15
 
16
16
  type PopupModelLike = { getStepParams?: (a: string, b: string) => any } | undefined;
17
17
 
18
+ function buildPopupSourceRecordRef(ref?: Pick<RecordRef, 'associationName' | 'dataSourceKey' | 'sourceId'>) {
19
+ if (ref?.sourceId == null || ref.sourceId === '' || typeof ref.associationName !== 'string') return undefined;
20
+ const collection = ref.associationName.split('.')[0];
21
+ if (!collection) return undefined;
22
+ return { collection, dataSourceKey: ref.dataSourceKey || 'main', filterByTk: ref.sourceId };
23
+ }
24
+
18
25
  function isDefined(value: any) {
19
26
  return value !== undefined && value !== null;
20
27
  }
@@ -327,13 +334,15 @@ export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): Proper
327
334
  const stack = getViewStack(view);
328
335
  const currentIndex = getAnchoredViewStackIndex(view, stack);
329
336
  if (currentIndex >= 2) {
330
- let cur: Record<string, any> = params;
337
+ let cur = params;
331
338
  let level = 1;
332
339
  let parentRef = await getParentRecordRef(level, c);
333
340
  while (parentRef) {
334
- if (!cur.parent) cur.parent = {};
335
- cur.parent.record = parentRef;
336
- cur = cur.parent;
341
+ const parent: PopupVariableParams = { record: parentRef };
342
+ const sourceRecord = buildPopupSourceRecordRef(parentRef);
343
+ if (sourceRecord) parent.sourceRecord = sourceRecord;
344
+ cur.parent = parent;
345
+ cur = parent;
337
346
  level += 1;
338
347
  parentRef = await getParentRecordRef(level, c);
339
348
  }
@@ -343,20 +352,12 @@ export function createPopupMeta(ctx: FlowContext, anchorView?: FlowView): Proper
343
352
  }
344
353
 
345
354
  try {
346
- const srcId = inputArgs?.sourceId;
347
- const assoc: string | undefined = inputArgs?.associationName;
348
- const dsKey: string = inputArgs?.dataSourceKey || 'main';
349
- if (srcId != null && srcId !== '' && assoc && typeof assoc === 'string') {
350
- // associationName 形如 `posts.comments`,父级集合为 `posts`
351
- const parentCollectionName = String(assoc).split('.')[0];
352
- if (parentCollectionName) {
353
- params.sourceRecord = {
354
- collection: parentCollectionName,
355
- dataSourceKey: dsKey,
356
- filterByTk: srcId,
357
- };
358
- }
359
- }
355
+ const sourceRecord = buildPopupSourceRecordRef({
356
+ sourceId: inputArgs?.sourceId,
357
+ associationName: inputArgs?.associationName,
358
+ dataSourceKey: inputArgs?.dataSourceKey,
359
+ });
360
+ if (sourceRecord) params.sourceRecord = sourceRecord;
360
361
  } catch (err) {
361
362
  c.logger?.debug?.({ err }, '[FlowEngine] buildVariablesParams: infer sourceRecord failed');
362
363
  }
@@ -474,12 +475,14 @@ interface PopupNodeResource {
474
475
  interface PopupNode {
475
476
  uid?: string;
476
477
  resource: PopupNodeResource;
478
+ sourceRecord?: unknown;
477
479
  parent?: PopupNode;
478
480
  }
479
481
 
480
482
  export async function buildPopupRuntime(ctx: FlowContext, view: FlowView): Promise<PopupNode | undefined> {
481
483
  const stack = getViewStack(view);
482
484
  const currentIndex = getAnchoredViewStackIndex(view, stack);
485
+ const sourceRecord = view?.inputArgs?.parentItem?.value;
483
486
 
484
487
  const openerUids = view?.inputArgs?.openerUids;
485
488
  const hasOpener = Array.isArray(openerUids) && openerUids.length > 0;
@@ -502,6 +505,7 @@ export async function buildPopupRuntime(ctx: FlowContext, view: FlowView): Promi
502
505
  filterByTk: args.filterByTk,
503
506
  sourceId: args.sourceId,
504
507
  },
508
+ ...(typeof sourceRecord !== 'undefined' ? { sourceRecord } : {}),
505
509
  };
506
510
  }
507
511
 
@@ -530,6 +534,9 @@ export async function buildPopupRuntime(ctx: FlowContext, view: FlowView): Promi
530
534
  return node;
531
535
  };
532
536
  const currentNode = await buildNode(currentIndex);
537
+ if (currentNode && typeof sourceRecord !== 'undefined') {
538
+ currentNode.sourceRecord = sourceRecord;
539
+ }
533
540
  return currentNode;
534
541
  }
535
542
 
@@ -541,6 +548,30 @@ export function registerPopupVariable(ctx: FlowContext, view: FlowView) {
541
548
  // - 任意层级 parent.parent... 下的 record / sourceRecord 及其子字段
542
549
  const POPUP_SERVER_PATH_RE =
543
550
  /^(?:record|sourceRecord)(?:\.|$)|^parent(?:\.parent)*(?:\.(?:record|sourceRecord))(?:\.|$)/;
551
+ const shouldResolveSourceRecordOnServer = (path: string): boolean => {
552
+ if (path !== 'sourceRecord' && !path.startsWith('sourceRecord.')) return false;
553
+
554
+ const parentItem = view?.inputArgs?.parentItem;
555
+ if (typeof parentItem?.value === 'undefined') return true;
556
+
557
+ const sourcePath = path === 'sourceRecord' ? '' : path.slice('sourceRecord.'.length);
558
+ if (!sourcePath) return false;
559
+
560
+ const parentItemResolver = view?.inputArgs?.parentItemResolver;
561
+ if (typeof parentItemResolver === 'function') {
562
+ return parentItemResolver(`value.${sourcePath}`);
563
+ }
564
+
565
+ const segments = sourcePath.split('.').filter(Boolean);
566
+ let current = parentItem.value;
567
+ for (const segment of segments) {
568
+ if (current === null || typeof current !== 'object' || !(segment in current)) {
569
+ return true;
570
+ }
571
+ current = current[segment];
572
+ }
573
+ return false;
574
+ };
544
575
  // 始终注册 popup 变量:
545
576
  // - 若当前视图无可推断记录,仅在元信息中不呈现 record 字段;
546
577
  // - 但仍可依据 navigation 推断并展示上级弹窗信息。
@@ -549,6 +580,9 @@ export function registerPopupVariable(ctx: FlowContext, view: FlowView) {
549
580
  meta: createPopupMeta(ctx, view),
550
581
  resolveOnServer: (p: string) => {
551
582
  try {
583
+ if (p === 'sourceRecord' || p.startsWith('sourceRecord.')) {
584
+ return shouldResolveSourceRecordOnServer(p);
585
+ }
552
586
  return !!p && POPUP_SERVER_PATH_RE.test(p);
553
587
  } catch (_) {
554
588
  return false;