@nocobase/flow-engine 2.1.0-beta.1 → 2.1.0-beta.11

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 (63) hide show
  1. package/LICENSE +201 -661
  2. package/README.md +79 -10
  3. package/lib/JSRunner.d.ts +10 -1
  4. package/lib/JSRunner.js +50 -5
  5. package/lib/ViewScopedFlowEngine.js +5 -1
  6. package/lib/components/dnd/gridDragPlanner.js +6 -2
  7. package/lib/components/settings/wrappers/contextual/StepSettingsDialog.js +16 -2
  8. package/lib/components/subModel/AddSubModelButton.js +15 -0
  9. package/lib/executor/FlowExecutor.js +31 -8
  10. package/lib/flowContext.js +4 -1
  11. package/lib/flowEngine.d.ts +19 -0
  12. package/lib/flowEngine.js +29 -1
  13. package/lib/runjs-context/registry.d.ts +1 -1
  14. package/lib/runjs-context/setup.js +19 -12
  15. package/lib/scheduler/ModelOperationScheduler.d.ts +5 -1
  16. package/lib/scheduler/ModelOperationScheduler.js +3 -2
  17. package/lib/types.d.ts +1 -1
  18. package/lib/utils/index.d.ts +2 -2
  19. package/lib/utils/index.js +4 -0
  20. package/lib/utils/parsePathnameToViewParams.js +1 -1
  21. package/lib/utils/schema-utils.d.ts +7 -1
  22. package/lib/utils/schema-utils.js +19 -0
  23. package/lib/views/FlowView.d.ts +7 -1
  24. package/lib/views/runViewBeforeClose.d.ts +10 -0
  25. package/lib/views/runViewBeforeClose.js +45 -0
  26. package/lib/views/useDialog.d.ts +2 -1
  27. package/lib/views/useDialog.js +20 -3
  28. package/lib/views/useDrawer.d.ts +2 -1
  29. package/lib/views/useDrawer.js +20 -3
  30. package/lib/views/usePage.d.ts +2 -1
  31. package/lib/views/usePage.js +10 -3
  32. package/package.json +5 -5
  33. package/src/JSRunner.ts +68 -4
  34. package/src/ViewScopedFlowEngine.ts +4 -0
  35. package/src/__tests__/JSRunner.test.ts +27 -1
  36. package/src/__tests__/runjsContext.test.ts +13 -0
  37. package/src/__tests__/runjsPreprocessDefault.test.ts +23 -0
  38. package/src/components/__tests__/gridDragPlanner.test.ts +88 -0
  39. package/src/components/dnd/gridDragPlanner.ts +8 -2
  40. package/src/components/settings/wrappers/contextual/StepSettingsDialog.tsx +18 -2
  41. package/src/components/subModel/AddSubModelButton.tsx +16 -0
  42. package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +50 -0
  43. package/src/executor/FlowExecutor.ts +34 -9
  44. package/src/executor/__tests__/flowExecutor.test.ts +57 -0
  45. package/src/flowContext.ts +5 -3
  46. package/src/flowEngine.ts +33 -1
  47. package/src/models/__tests__/dispatchEvent.when.test.ts +214 -0
  48. package/src/runjs-context/registry.ts +1 -1
  49. package/src/runjs-context/setup.ts +21 -12
  50. package/src/scheduler/ModelOperationScheduler.ts +14 -3
  51. package/src/types.ts +1 -0
  52. package/src/utils/__tests__/parsePathnameToViewParams.test.ts +7 -0
  53. package/src/utils/__tests__/utils.test.ts +62 -0
  54. package/src/utils/index.ts +2 -1
  55. package/src/utils/parsePathnameToViewParams.ts +2 -2
  56. package/src/utils/schema-utils.ts +30 -1
  57. package/src/views/FlowView.tsx +11 -1
  58. package/src/views/__tests__/runViewBeforeClose.test.ts +30 -0
  59. package/src/views/__tests__/useDialog.closeDestroy.test.tsx +13 -12
  60. package/src/views/runViewBeforeClose.ts +19 -0
  61. package/src/views/useDialog.tsx +25 -3
  62. package/src/views/useDrawer.tsx +25 -3
  63. package/src/views/usePage.tsx +12 -3
@@ -15,6 +15,7 @@ import {
15
15
  getSlotKey,
16
16
  resolveDropIntent,
17
17
  Point,
18
+ buildLayoutSnapshot,
18
19
  } from '../dnd/gridDragPlanner';
19
20
 
20
21
  const rect = { top: 0, left: 0, width: 100, height: 100 };
@@ -29,6 +30,93 @@ const createLayout = (
29
30
  rowOrder,
30
31
  });
31
32
 
33
+ const createDomRect = ({ top, left, width, height }: { top: number; left: number; width: number; height: number }) => {
34
+ return {
35
+ top,
36
+ left,
37
+ width,
38
+ height,
39
+ right: left + width,
40
+ bottom: top + height,
41
+ x: left,
42
+ y: top,
43
+ toJSON: () => ({}),
44
+ } as DOMRect;
45
+ };
46
+
47
+ const mockRect = (
48
+ element: Element,
49
+ rect: {
50
+ top: number;
51
+ left: number;
52
+ width: number;
53
+ height: number;
54
+ },
55
+ ) => {
56
+ Object.defineProperty(element, 'getBoundingClientRect', {
57
+ configurable: true,
58
+ value: () => createDomRect(rect),
59
+ });
60
+ };
61
+
62
+ describe('buildLayoutSnapshot', () => {
63
+ it('should ignore nested grid columns/items even when rowId is duplicated', () => {
64
+ const container = document.createElement('div');
65
+ const row = document.createElement('div');
66
+ row.setAttribute('data-grid-row-id', 'row-1');
67
+ container.appendChild(row);
68
+
69
+ const column = document.createElement('div');
70
+ column.setAttribute('data-grid-column-row-id', 'row-1');
71
+ column.setAttribute('data-grid-column-index', '0');
72
+ row.appendChild(column);
73
+
74
+ const item = document.createElement('div');
75
+ item.setAttribute('data-grid-item-row-id', 'row-1');
76
+ item.setAttribute('data-grid-column-index', '0');
77
+ item.setAttribute('data-grid-item-index', '0');
78
+ column.appendChild(item);
79
+
80
+ // 在外层 item 内构建一个嵌套 grid,并复用相同 rowId/columnIndex
81
+ const nestedRow = document.createElement('div');
82
+ nestedRow.setAttribute('data-grid-row-id', 'row-1');
83
+ item.appendChild(nestedRow);
84
+
85
+ const nestedColumn = document.createElement('div');
86
+ nestedColumn.setAttribute('data-grid-column-row-id', 'row-1');
87
+ nestedColumn.setAttribute('data-grid-column-index', '0');
88
+ nestedRow.appendChild(nestedColumn);
89
+
90
+ const nestedItem = document.createElement('div');
91
+ nestedItem.setAttribute('data-grid-item-row-id', 'row-1');
92
+ nestedItem.setAttribute('data-grid-column-index', '0');
93
+ nestedItem.setAttribute('data-grid-item-index', '0');
94
+ nestedColumn.appendChild(nestedItem);
95
+
96
+ mockRect(container, { top: 0, left: 0, width: 600, height: 600 });
97
+ mockRect(row, { top: 10, left: 10, width: 320, height: 120 });
98
+ mockRect(column, { top: 10, left: 10, width: 320, height: 120 });
99
+ mockRect(item, { top: 20, left: 20, width: 300, height: 80 });
100
+
101
+ // 嵌套 grid 给一个明显偏离的位置,用于判断是否被错误命中
102
+ mockRect(nestedRow, { top: 360, left: 360, width: 200, height: 120 });
103
+ mockRect(nestedColumn, { top: 360, left: 360, width: 200, height: 120 });
104
+ mockRect(nestedItem, { top: 370, left: 370, width: 180, height: 90 });
105
+
106
+ const snapshot = buildLayoutSnapshot({ container });
107
+ const columnEdgeSlots = snapshot.slots.filter((slot) => slot.type === 'column-edge');
108
+ const columnSlots = snapshot.slots.filter((slot) => slot.type === 'column');
109
+
110
+ // 外层单行单列单项应只有 6 个 slot:上/下 row-gap + 左/右 column-edge + before/after column
111
+ expect(snapshot.slots).toHaveLength(6);
112
+ expect(columnEdgeSlots).toHaveLength(2);
113
+ expect(columnSlots).toHaveLength(2);
114
+
115
+ // 不应混入嵌套 grid(其 top >= 360)
116
+ expect(snapshot.slots.every((slot) => slot.rect.top < 300)).toBe(true);
117
+ });
118
+ });
119
+
32
120
  describe('getSlotKey', () => {
33
121
  it('should generate unique key for column slot', () => {
34
122
  const slot: LayoutSlot = {
@@ -333,7 +333,10 @@ export const buildLayoutSnapshot = ({ container }: BuildLayoutSnapshotOptions):
333
333
 
334
334
  const columnElements = Array.from(
335
335
  container.querySelectorAll(`[data-grid-column-row-id="${rowId}"][data-grid-column-index]`),
336
- ) as HTMLElement[];
336
+ ).filter((el) => {
337
+ // 只保留当前 row 下的直接列,避免嵌套 Grid 中相同 rowId 的列混入
338
+ return (el as HTMLElement).closest('[data-grid-row-id]') === rowElement;
339
+ }) as HTMLElement[];
337
340
 
338
341
  const sortedColumns = columnElements.sort((a, b) => {
339
342
  const indexA = Number(a.dataset.gridColumnIndex || 0);
@@ -363,7 +366,10 @@ export const buildLayoutSnapshot = ({ container }: BuildLayoutSnapshotOptions):
363
366
 
364
367
  const itemElements = Array.from(
365
368
  columnElement.querySelectorAll(`[data-grid-item-row-id="${rowId}"][data-grid-column-index="${columnIndex}"]`),
366
- ) as HTMLElement[];
369
+ ).filter((el) => {
370
+ // 只保留当前 column 下的直接 item,避免命中更深层嵌套 column 的 item
371
+ return (el as HTMLElement).closest('[data-grid-column-row-id][data-grid-column-index]') === columnElement;
372
+ }) as HTMLElement[];
367
373
 
368
374
  const sortedItems = itemElements.sort((a, b) => {
369
375
  const indexA = Number(a.dataset.gridItemIndex || 0);
@@ -134,6 +134,17 @@ const openStepSettingsDialog = async ({
134
134
  };
135
135
 
136
136
  const openView = model.context.viewer[mode].bind(model.context.viewer);
137
+ const resolvedUiModeProps = toJS(uiModeProps) || {};
138
+ const { zIndex: uiModeZIndex, ...restUiModeProps } = resolvedUiModeProps;
139
+ const resolveDialogZIndex = (rawZIndex?: number) => {
140
+ const nextZIndex =
141
+ typeof model.context.viewer?.getNextZIndex === 'function'
142
+ ? model.context.viewer.getNextZIndex()
143
+ : (model.context.themeToken?.zIndexPopupBase || 1000) + 1;
144
+ const inputZIndex = Number(rawZIndex) || 0;
145
+ return Math.max(nextZIndex, inputZIndex);
146
+ };
147
+ const mergedZIndex = resolveDialogZIndex(uiModeZIndex);
137
148
 
138
149
  const form = createForm({
139
150
  initialValues: compileUiSchema(scopes, initialValues),
@@ -152,7 +163,8 @@ const openStepSettingsDialog = async ({
152
163
  title: dialogTitle || t(title),
153
164
  width: dialogWidth,
154
165
  destroyOnClose: true,
155
- ...toJS(uiModeProps),
166
+ ...restUiModeProps,
167
+ zIndex: mergedZIndex,
156
168
  // 透传 navigation,便于变量元信息根据真实视图栈推断父级弹窗
157
169
  inputArgs,
158
170
  onClose: () => {
@@ -165,7 +177,11 @@ const openStepSettingsDialog = async ({
165
177
  useEffect(() => {
166
178
  return autorun(() => {
167
179
  const dynamicProps = toJS(uiModeProps);
168
- currentDialog.update(dynamicProps);
180
+ const { zIndex, ...restDynamicProps } = dynamicProps || {};
181
+ currentDialog.update({
182
+ ...restDynamicProps,
183
+ zIndex: resolveDialogZIndex(zIndex),
184
+ });
169
185
  });
170
186
  }, []);
171
187
 
@@ -542,6 +542,22 @@ const AddSubModelButtonCore = function AddSubModelButton({
542
542
  [model, subModelKey, subModelType],
543
543
  );
544
544
 
545
+ React.useEffect(() => {
546
+ const handleSubModelChanged = () => {
547
+ setRefreshTick((x) => x + 1);
548
+ };
549
+
550
+ model.emitter?.on('onSubModelAdded', handleSubModelChanged);
551
+ model.emitter?.on('onSubModelRemoved', handleSubModelChanged);
552
+ model.emitter?.on('onSubModelReplaced', handleSubModelChanged);
553
+
554
+ return () => {
555
+ model.emitter?.off('onSubModelAdded', handleSubModelChanged);
556
+ model.emitter?.off('onSubModelRemoved', handleSubModelChanged);
557
+ model.emitter?.off('onSubModelReplaced', handleSubModelChanged);
558
+ };
559
+ }, [model]);
560
+
545
561
  // 点击处理逻辑
546
562
  const onClick = async (info: any) => {
547
563
  const clickedItem = info.originalItem || info;
@@ -995,6 +995,56 @@ describe('AddSubModelButton - toggle interactions', () => {
995
995
  const subModels = ((parent.subModels as any).items as FlowModel[]) || [];
996
996
  expect(subModels).toHaveLength(1);
997
997
  });
998
+
999
+ it('updates toggle state after external sub model removal', async () => {
1000
+ const engine = new FlowEngine();
1001
+ engine.flowSettings.forceEnable();
1002
+
1003
+ class ToggleParent extends FlowModel {}
1004
+ class ToggleChild extends FlowModel {}
1005
+
1006
+ engine.registerModels({ ToggleParent, ToggleChild });
1007
+ const parent = engine.createModel<ToggleParent>({ use: 'ToggleParent', uid: 'toggle-parent-external-remove' });
1008
+ const existing = engine.createModel<ToggleChild>({ use: 'ToggleChild', uid: 'toggle-child-external-remove' });
1009
+ parent.addSubModel('items', existing);
1010
+
1011
+ render(
1012
+ <FlowEngineProvider engine={engine}>
1013
+ <ConfigProvider>
1014
+ <App>
1015
+ <AddSubModelButton
1016
+ model={parent}
1017
+ subModelKey="items"
1018
+ items={[
1019
+ {
1020
+ key: 'toggle-child',
1021
+ label: 'Toggle Child',
1022
+ toggleable: true,
1023
+ useModel: 'ToggleChild',
1024
+ createModelOptions: { use: 'ToggleChild' },
1025
+ },
1026
+ ]}
1027
+ >
1028
+ Toggle Menu
1029
+ </AddSubModelButton>
1030
+ </App>
1031
+ </ConfigProvider>
1032
+ </FlowEngineProvider>,
1033
+ );
1034
+
1035
+ await act(async () => {
1036
+ await userEvent.click(screen.getByText('Toggle Menu'));
1037
+ });
1038
+
1039
+ await waitFor(() => expect(screen.getByText('Toggle Child')).toBeInTheDocument());
1040
+ await waitFor(() => expect(screen.getByRole('switch')).toHaveAttribute('aria-checked', 'true'));
1041
+
1042
+ await act(async () => {
1043
+ await existing.destroy();
1044
+ });
1045
+
1046
+ await waitFor(() => expect(screen.getByRole('switch')).toHaveAttribute('aria-checked', 'false'));
1047
+ });
998
1048
  });
999
1049
 
1000
1050
  // ========================
@@ -224,10 +224,12 @@ export class FlowExecutor {
224
224
  await this.emitModelEventIf(eventName, `flow:${flowKey}:step:${stepKey}:end`, {
225
225
  ...flowEventBasePayload,
226
226
  stepKey,
227
+ aborted: true,
227
228
  });
228
229
  await this.emitModelEventIf(eventName, `flow:${flowKey}:end`, {
229
230
  ...flowEventBasePayload,
230
231
  result: error,
232
+ aborted: true,
231
233
  });
232
234
  return Promise.resolve(error);
233
235
  }
@@ -316,9 +318,9 @@ export class FlowExecutor {
316
318
 
317
319
  // 记录本次 dispatchEvent 内注册的调度任务,用于在结束/错误后兜底清理未触发的任务
318
320
  const scheduledCancels: ScheduledCancel[] = [];
319
-
320
- // 组装执行函数(返回值用于缓存;beforeRender 返回 results:any[],其它返回 true)
321
- const execute = async () => {
321
+ // 组装执行函数(返回值用于缓存,包含事件结果与是否被 exitAll 中止)
322
+ const execute = async (): Promise<{ result: any[]; abortedByExitAll: boolean }> => {
323
+ let abortedByExitAll = false;
322
324
  if (sequential) {
323
325
  // 顺序执行:动态流(实例级)优先,其次静态流;各自组内再按 sort 升序,最后保持原始顺序稳定
324
326
  const flowsWithIndex = flowsToRun.map((f, i) => ({ f, i }));
@@ -351,7 +353,7 @@ export class FlowExecutor {
351
353
  .map((f) => [f.key, f] as const),
352
354
  );
353
355
  const scheduled = new Set<string>();
354
- const scheduleGroups = new Map<string, Array<{ flow: any; order: number }>>();
356
+ const scheduleGroups = new Map<string, Array<{ flow: any; order: number; shouldSkipOnAborted: boolean }>>();
355
357
  ordered.forEach((flow, indexInOrdered) => {
356
358
  const on = flow.on;
357
359
  const onObj = typeof on === 'object' ? (on as any) : undefined;
@@ -402,9 +404,11 @@ export class FlowExecutor {
402
404
  }
403
405
 
404
406
  if (!whenKey) return;
407
+ const shouldSkipOnAborted =
408
+ whenKey === `event:${eventName}:end` || phase === 'afterFlow' || phase === 'afterStep';
405
409
  scheduled.add(flow.key);
406
410
  const list = scheduleGroups.get(whenKey) || [];
407
- list.push({ flow, order: indexInOrdered });
411
+ list.push({ flow, order: indexInOrdered, shouldSkipOnAborted });
408
412
  scheduleGroups.set(whenKey, list);
409
413
  });
410
414
 
@@ -417,6 +421,14 @@ export class FlowExecutor {
417
421
  return a.order - b.order;
418
422
  });
419
423
  for (const it of sorted) {
424
+ const when = it.shouldSkipOnAborted
425
+ ? Object.assign(
426
+ (event: { type: string; aborted?: boolean }) => event.type === whenKey && event.aborted !== true,
427
+ {
428
+ __eventType: whenKey,
429
+ },
430
+ )
431
+ : (whenKey as any);
420
432
  const cancel = model.scheduleModelOperation(
421
433
  model.uid,
422
434
  async (m) => {
@@ -426,7 +438,7 @@ export class FlowExecutor {
426
438
  }
427
439
  results.push(res);
428
440
  },
429
- { when: whenKey as any },
441
+ { when },
430
442
  );
431
443
  scheduledCancels.push(cancel);
432
444
  }
@@ -441,12 +453,14 @@ export class FlowExecutor {
441
453
  const result = await this.runFlow(model, flow.key, inputArgs, runId, eventName);
442
454
  if (result instanceof FlowExitAllException) {
443
455
  logger.debug(`[FlowEngine.dispatchEvent] ${result.message}`);
456
+ abortedByExitAll = true;
444
457
  break; // 终止后续
445
458
  }
446
459
  results.push(result);
447
460
  } catch (error) {
448
461
  if (error instanceof FlowExitAllException) {
449
462
  logger.debug(`[FlowEngine.dispatchEvent] ${error.message}`);
463
+ abortedByExitAll = true;
450
464
  break; // 终止后续
451
465
  }
452
466
  logger.error(
@@ -456,7 +470,7 @@ export class FlowExecutor {
456
470
  throw error;
457
471
  }
458
472
  }
459
- return results;
473
+ return { result: results, abortedByExitAll };
460
474
  }
461
475
 
462
476
  // 并行
@@ -475,7 +489,11 @@ export class FlowExecutor {
475
489
  }
476
490
  }),
477
491
  );
478
- return results.filter((x) => x !== undefined);
492
+ const filteredResults = results.filter((x) => x !== undefined);
493
+ if (filteredResults.some((x) => x instanceof FlowExitAllException)) {
494
+ abortedByExitAll = true;
495
+ }
496
+ return { result: filteredResults, abortedByExitAll };
479
497
  };
480
498
 
481
499
  // 缓存键:按事件+scope 统一管理(beforeRender 也使用事件名 beforeRender)
@@ -489,7 +507,7 @@ export class FlowExecutor {
489
507
  : null;
490
508
 
491
509
  try {
492
- const result = await this.withApplyFlowCache(cacheKey, execute);
510
+ const { result, abortedByExitAll } = await this.withApplyFlowCache(cacheKey, execute);
493
511
  // 事件结束钩子
494
512
  try {
495
513
  await model.onDispatchEventEnd?.(eventName, options, inputArgs, result);
@@ -499,7 +517,14 @@ export class FlowExecutor {
499
517
  await this.emitModelEventIf(eventName, 'end', {
500
518
  ...eventBasePayload,
501
519
  result,
520
+ ...(abortedByExitAll ? { aborted: true } : {}),
502
521
  });
522
+ if (result && typeof result === 'object') {
523
+ Object.defineProperty(result, '__abortedByExitAll', {
524
+ value: abortedByExitAll,
525
+ configurable: true,
526
+ });
527
+ }
503
528
  return result;
504
529
  } catch (error) {
505
530
  // 进入错误钩子并记录
@@ -232,6 +232,37 @@ describe('FlowExecutor', () => {
232
232
  expect(calls.sort()).toEqual(['a', 'b']);
233
233
  });
234
234
 
235
+ it('dispatchEvent sequential exposes abortedByExitAll metadata on result array', async () => {
236
+ const flows = {
237
+ stopClose: {
238
+ on: { eventName: 'close' },
239
+ steps: {
240
+ only: {
241
+ handler: vi.fn().mockImplementation((ctx) => {
242
+ ctx.exit();
243
+ }),
244
+ },
245
+ },
246
+ },
247
+ afterClose: {
248
+ on: { eventName: 'close', phase: 'afterAllFlows' },
249
+ steps: {
250
+ only: {
251
+ handler: vi.fn(),
252
+ },
253
+ },
254
+ },
255
+ } satisfies Record<string, Omit<FlowDefinitionOptions, 'key'>>;
256
+
257
+ const model = createModelWithFlows('m-close-meta', flows);
258
+
259
+ const result = await engine.executor.dispatchEvent(model, 'close', {}, { sequential: true });
260
+
261
+ expect(Array.isArray(result)).toBe(true);
262
+ expect((result as any).__abortedByExitAll).toBe(true);
263
+ expect(flows.afterClose.steps.only.handler).not.toHaveBeenCalled();
264
+ });
265
+
235
266
  it('dispatchEvent sequential respects sort order and stops on errors', async () => {
236
267
  const calls: string[] = [];
237
268
  const mkFlow = (key: string, sort: number, opts?: { throw?: boolean }) => ({
@@ -288,6 +319,32 @@ describe('FlowExecutor', () => {
288
319
  expect(handler).toHaveBeenCalledTimes(2); // 每个 flow 各 1 次,共 2 次
289
320
  });
290
321
 
322
+ it("dispatchEvent('beforeRender') keeps aborted flag on end event when cache hits", async () => {
323
+ const handler = vi.fn().mockImplementation((ctx) => {
324
+ ctx.exitAll();
325
+ });
326
+ const flows = {
327
+ abortFlow: { steps: { s: { handler } } },
328
+ } satisfies Record<string, Omit<FlowDefinitionOptions, 'key'>>;
329
+ const model = createModelWithFlows('m-br-cache-aborted', flows);
330
+
331
+ const endEvents: any[] = [];
332
+ const onEnd = (payload: any) => {
333
+ endEvents.push(payload);
334
+ };
335
+ engine.emitter.on('model:event:beforeRender:end', onEnd);
336
+
337
+ await engine.executor.dispatchEvent(model, 'beforeRender', undefined, { sequential: true, useCache: true });
338
+ await engine.executor.dispatchEvent(model, 'beforeRender', undefined, { sequential: true, useCache: true });
339
+
340
+ engine.emitter.off('model:event:beforeRender:end', onEnd);
341
+
342
+ expect(handler).toHaveBeenCalledTimes(1);
343
+ expect(endEvents).toHaveLength(2);
344
+ expect(endEvents[0]?.aborted).toBe(true);
345
+ expect(endEvents[1]?.aborted).toBe(true);
346
+ });
347
+
291
348
  it('dispatchEvent supports sequential execution order and exitAll break', async () => {
292
349
  const calls: string[] = [];
293
350
  const mkFlow = (key: string, sort: number, opts?: { exitAll?: boolean }) => ({
@@ -27,7 +27,7 @@ import { ContextPathProxy } from './ContextPathProxy';
27
27
  import { DataSource, DataSourceManager } from './data-source';
28
28
  import { FlowEngine } from './flowEngine';
29
29
  import { FlowI18n } from './flowI18n';
30
- import { JSRunner, JSRunnerOptions } from './JSRunner';
30
+ import { JSRunner, JSRunnerOptions, shouldPreprocessRunJSTemplates } from './JSRunner';
31
31
  import type { FlowModel } from './models/flowModel';
32
32
  import type { ForkFlowModel } from './models/forkFlowModel';
33
33
  import { FlowResource, FlowSQLRepository } from './resources';
@@ -3035,8 +3035,10 @@ class BaseFlowEngineContext extends FlowContext {
3035
3035
  ...(runnerOptions || {}),
3036
3036
  globals: mergedGlobals,
3037
3037
  });
3038
- // Enable by default; use `preprocessTemplates: false` to explicitly disable.
3039
- const shouldPreprocessTemplates = preprocessTemplates !== false;
3038
+ const shouldPreprocessTemplates = shouldPreprocessRunJSTemplates({
3039
+ version: runnerOptions?.version,
3040
+ preprocessTemplates,
3041
+ });
3040
3042
  const jsCode = await prepareRunJsCode(String(code ?? ''), { preprocessTemplates: shouldPreprocessTemplates });
3041
3043
  return runner.run(jsCode);
3042
3044
  },
package/src/flowEngine.ts CHANGED
@@ -117,6 +117,13 @@ export class FlowEngine {
117
117
  private _previousEngine?: FlowEngine;
118
118
  private _nextEngine?: FlowEngine;
119
119
 
120
+ /**
121
+ * 视图销毁回调。由 useDrawer / useDialog 在创建弹窗视图时注册,
122
+ * 供外部(如 afterSuccess)通过引擎栈遍历来关闭多层弹窗。
123
+ * embed 视图(usePage)不注册此回调,因此 destroyView() 会自然跳过。
124
+ */
125
+ private _destroyView?: () => void;
126
+
120
127
  private _resources = new Map<string, typeof FlowResource>();
121
128
 
122
129
  /**
@@ -282,6 +289,28 @@ export class FlowEngine {
282
289
  }
283
290
  }
284
291
 
292
+ /**
293
+ * 注册视图销毁回调(由 useDrawer / useDialog 调用)。
294
+ */
295
+ public setDestroyView(fn: () => void): void {
296
+ this._destroyView = fn;
297
+ }
298
+
299
+ /**
300
+ * 关闭当前引擎关联的弹窗视图。
301
+ * 路由触发的弹窗会先 navigation.back() 清理 URL,再 destroy() 移除元素;
302
+ * 非路由弹窗直接 destroy()。
303
+ * embed 视图不注册回调,调用时返回 false 自动跳过。
304
+ * @returns 是否成功执行
305
+ */
306
+ public destroyView(): boolean {
307
+ if (this._destroyView) {
308
+ this._destroyView();
309
+ return true;
310
+ }
311
+ return false;
312
+ }
313
+
285
314
  // (已移除)getModelGlobal/forEachModelGlobal/getAllModelsGlobal:不再维护冗余全局遍历 API
286
315
 
287
316
  /**
@@ -959,6 +988,7 @@ export class FlowEngine {
959
988
  async loadOrCreateModel<T extends FlowModel = FlowModel>(
960
989
  options,
961
990
  extra?: {
991
+ skipSave?: boolean;
962
992
  delegateToParent?: boolean;
963
993
  delegate?: FlowContext;
964
994
  },
@@ -984,7 +1014,9 @@ export class FlowEngine {
984
1014
  model = this.createModel<T>(data as any, extra);
985
1015
  } else {
986
1016
  model = this.createModel<T>(options, extra);
987
- await model.save();
1017
+ if (!extra?.skipSave) {
1018
+ await model.save();
1019
+ }
988
1020
  }
989
1021
  if (model.parent) {
990
1022
  const subModel = model.parent.findSubModel(model.subKey, (m) => {