@nocobase/flow-engine 2.1.0-beta.7 → 2.1.0-beta.8

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 (35) hide show
  1. package/lib/JSRunner.d.ts +10 -1
  2. package/lib/JSRunner.js +50 -5
  3. package/lib/ViewScopedFlowEngine.js +5 -1
  4. package/lib/components/dnd/gridDragPlanner.js +6 -2
  5. package/lib/components/settings/wrappers/contextual/StepSettingsDialog.js +16 -2
  6. package/lib/components/subModel/AddSubModelButton.js +15 -0
  7. package/lib/flowContext.js +4 -1
  8. package/lib/flowEngine.d.ts +19 -0
  9. package/lib/flowEngine.js +29 -1
  10. package/lib/runjs-context/registry.d.ts +1 -1
  11. package/lib/runjs-context/setup.js +19 -12
  12. package/lib/utils/parsePathnameToViewParams.js +1 -1
  13. package/lib/views/useDialog.js +10 -0
  14. package/lib/views/useDrawer.js +10 -0
  15. package/package.json +4 -4
  16. package/src/JSRunner.ts +68 -4
  17. package/src/ViewScopedFlowEngine.ts +4 -0
  18. package/src/__tests__/JSRunner.test.ts +27 -1
  19. package/src/__tests__/runjsContext.test.ts +13 -0
  20. package/src/__tests__/runjsPreprocessDefault.test.ts +23 -0
  21. package/src/components/__tests__/gridDragPlanner.test.ts +88 -0
  22. package/src/components/dnd/gridDragPlanner.ts +8 -2
  23. package/src/components/settings/wrappers/contextual/StepSettingsDialog.tsx +18 -2
  24. package/src/components/subModel/AddSubModelButton.tsx +16 -0
  25. package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +50 -0
  26. package/src/flowContext.ts +5 -3
  27. package/src/flowEngine.ts +33 -1
  28. package/src/runjs-context/registry.ts +1 -1
  29. package/src/runjs-context/setup.ts +21 -12
  30. package/src/utils/__tests__/parsePathnameToViewParams.test.ts +7 -0
  31. package/src/utils/parsePathnameToViewParams.ts +2 -2
  32. package/src/views/__tests__/useDialog.closeDestroy.test.tsx +1 -0
  33. package/src/views/useDialog.tsx +14 -0
  34. package/src/views/useDrawer.tsx +14 -0
  35. package/src/views/usePage.tsx +1 -0
package/lib/JSRunner.d.ts CHANGED
@@ -13,11 +13,20 @@ export interface JSRunnerOptions {
13
13
  version?: string;
14
14
  /**
15
15
  * Enable RunJS template compatibility preprocessing for `{{ ... }}`.
16
- * When enabled via `ctx.runjs(code, vars, { preprocessTemplates: true })` (default),
16
+ * When enabled (or falling back to version default),
17
17
  * the code will be rewritten to call `ctx.resolveJsonTemplate(...)` at runtime.
18
18
  */
19
19
  preprocessTemplates?: boolean;
20
20
  }
21
+ /**
22
+ * Decide whether RunJS `{{ ... }}` compatibility preprocessing should run.
23
+ *
24
+ * Priority:
25
+ * 1. Explicit `preprocessTemplates` option always wins.
26
+ * 2. Otherwise, `version === 'v2'` disables preprocessing.
27
+ * 3. Fallback keeps v1-compatible behavior (enabled).
28
+ */
29
+ export declare function shouldPreprocessRunJSTemplates(options?: Pick<JSRunnerOptions, 'preprocessTemplates' | 'version'>): boolean;
21
30
  export declare class JSRunner {
22
31
  private globals;
23
32
  private timeoutMs;
package/lib/JSRunner.js CHANGED
@@ -27,11 +27,53 @@ var __copyProps = (to, from, except, desc) => {
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
  var JSRunner_exports = {};
29
29
  __export(JSRunner_exports, {
30
- JSRunner: () => JSRunner
30
+ JSRunner: () => JSRunner,
31
+ shouldPreprocessRunJSTemplates: () => shouldPreprocessRunJSTemplates
31
32
  });
32
33
  module.exports = __toCommonJS(JSRunner_exports);
33
34
  var import_ses = require("ses");
34
35
  var import_exceptions = require("./utils/exceptions");
36
+ function shouldPreprocessRunJSTemplates(options) {
37
+ if (typeof (options == null ? void 0 : options.preprocessTemplates) === "boolean") {
38
+ return options.preprocessTemplates;
39
+ }
40
+ return (options == null ? void 0 : options.version) !== "v2";
41
+ }
42
+ __name(shouldPreprocessRunJSTemplates, "shouldPreprocessRunJSTemplates");
43
+ const BARE_CTX_TEMPLATE_RE = /(^|[=(:,[\s)])(\{\{\s*(ctx(?:\.|\[|\?\.)[^}]*)\s*\}\})/m;
44
+ function extractDeprecatedCtxTemplateUsage(code) {
45
+ const src = String(code || "");
46
+ const m = src.match(BARE_CTX_TEMPLATE_RE);
47
+ if (!m) return null;
48
+ const placeholder = String(m[2] || "").trim();
49
+ const expression = String(m[3] || "").trim();
50
+ if (!placeholder || !expression) return null;
51
+ return { placeholder, expression };
52
+ }
53
+ __name(extractDeprecatedCtxTemplateUsage, "extractDeprecatedCtxTemplateUsage");
54
+ function shouldHintCtxTemplateSyntax(err, usage) {
55
+ const isSyntaxError = err instanceof SyntaxError || String((err == null ? void 0 : err.name) || "") === "SyntaxError";
56
+ if (!isSyntaxError) return false;
57
+ if (!usage) return false;
58
+ const msg = String((err == null ? void 0 : err.message) || err || "");
59
+ return /unexpected token/i.test(msg);
60
+ }
61
+ __name(shouldHintCtxTemplateSyntax, "shouldHintCtxTemplateSyntax");
62
+ function toCtxTemplateSyntaxHintError(err, usage) {
63
+ const hint = `"${usage.placeholder}" has been deprecated and cannot be used as executable RunJS syntax. Use await ctx.getVar("${usage.expression}") instead, or keep "${usage.placeholder}" as a plain string.`;
64
+ const out = new SyntaxError(hint);
65
+ try {
66
+ out.cause = err;
67
+ } catch (_) {
68
+ }
69
+ try {
70
+ out.__runjsHideLocation = true;
71
+ out.stack = `${out.name}: ${out.message}`;
72
+ } catch (_) {
73
+ }
74
+ return out;
75
+ }
76
+ __name(toCtxTemplateSyntaxHintError, "toCtxTemplateSyntaxHintError");
35
77
  const _JSRunner = class _JSRunner {
36
78
  globals;
37
79
  timeoutMs;
@@ -111,11 +153,13 @@ const _JSRunner = class _JSRunner {
111
153
  if (err instanceof import_exceptions.FlowExitAllException) {
112
154
  throw err;
113
155
  }
114
- console.error(err);
156
+ const usage = extractDeprecatedCtxTemplateUsage(code);
157
+ const outErr = shouldHintCtxTemplateSyntax(err, usage) && usage ? toCtxTemplateSyntaxHintError(err, usage) : err;
158
+ console.error(outErr);
115
159
  return {
116
160
  success: false,
117
- error: err,
118
- timeout: err.message === "Execution timed out"
161
+ error: outErr,
162
+ timeout: (outErr == null ? void 0 : outErr.message) === "Execution timed out"
119
163
  };
120
164
  }
121
165
  }
@@ -124,5 +168,6 @@ __name(_JSRunner, "JSRunner");
124
168
  let JSRunner = _JSRunner;
125
169
  // Annotate the CommonJS export names for ESM import in node:
126
170
  0 && (module.exports = {
127
- JSRunner
171
+ JSRunner,
172
+ shouldPreprocessRunJSTemplates
128
173
  });
@@ -65,7 +65,11 @@ function createViewScopedEngine(parent) {
65
65
  "_previousEngine",
66
66
  "_nextEngine",
67
67
  // getModel 需要在本地执行以确保全局查找时正确遍历整个引擎栈
68
- "getModel"
68
+ "getModel",
69
+ // 视图销毁回调需要在本地存储,每个视图引擎有自己的销毁逻辑
70
+ "_destroyView",
71
+ "setDestroyView",
72
+ "destroyView"
69
73
  ]);
70
74
  const handler = {
71
75
  get(target, prop, receiver) {
@@ -220,7 +220,9 @@ const buildLayoutSnapshot = /* @__PURE__ */ __name(({ container }) => {
220
220
  }
221
221
  const columnElements = Array.from(
222
222
  container.querySelectorAll(`[data-grid-column-row-id="${rowId}"][data-grid-column-index]`)
223
- );
223
+ ).filter((el) => {
224
+ return el.closest("[data-grid-row-id]") === rowElement;
225
+ });
224
226
  const sortedColumns = columnElements.sort((a, b) => {
225
227
  const indexA = Number(a.dataset.gridColumnIndex || 0);
226
228
  const indexB = Number(b.dataset.gridColumnIndex || 0);
@@ -245,7 +247,9 @@ const buildLayoutSnapshot = /* @__PURE__ */ __name(({ container }) => {
245
247
  });
246
248
  const itemElements = Array.from(
247
249
  columnElement.querySelectorAll(`[data-grid-item-row-id="${rowId}"][data-grid-column-index="${columnIndex}"]`)
248
- );
250
+ ).filter((el) => {
251
+ return el.closest("[data-grid-column-row-id][data-grid-column-index]") === columnElement;
252
+ });
249
253
  const sortedItems = itemElements.sort((a, b) => {
250
254
  const indexA = Number(a.dataset.gridItemIndex || 0);
251
255
  const indexB = Number(b.dataset.gridItemIndex || 0);
@@ -126,6 +126,15 @@ const openStepSettingsDialog = /* @__PURE__ */ __name(async ({
126
126
  }
127
127
  };
128
128
  const openView = model.context.viewer[mode].bind(model.context.viewer);
129
+ const resolvedUiModeProps = (0, import_reactive.toJS)(uiModeProps) || {};
130
+ const { zIndex: uiModeZIndex, ...restUiModeProps } = resolvedUiModeProps;
131
+ const resolveDialogZIndex = /* @__PURE__ */ __name((rawZIndex) => {
132
+ var _a2, _b2;
133
+ const nextZIndex = typeof ((_a2 = model.context.viewer) == null ? void 0 : _a2.getNextZIndex) === "function" ? model.context.viewer.getNextZIndex() : (((_b2 = model.context.themeToken) == null ? void 0 : _b2.zIndexPopupBase) || 1e3) + 1;
134
+ const inputZIndex = Number(rawZIndex) || 0;
135
+ return Math.max(nextZIndex, inputZIndex);
136
+ }, "resolveDialogZIndex");
137
+ const mergedZIndex = resolveDialogZIndex(uiModeZIndex);
129
138
  const form = (0, import_core.createForm)({
130
139
  initialValues: (0, import_utils.compileUiSchema)(scopes, initialValues)
131
140
  });
@@ -141,7 +150,8 @@ const openStepSettingsDialog = /* @__PURE__ */ __name(async ({
141
150
  title: dialogTitle || t(title),
142
151
  width: dialogWidth,
143
152
  destroyOnClose: true,
144
- ...(0, import_reactive.toJS)(uiModeProps),
153
+ ...restUiModeProps,
154
+ zIndex: mergedZIndex,
145
155
  // 透传 navigation,便于变量元信息根据真实视图栈推断父级弹窗
146
156
  inputArgs,
147
157
  onClose: /* @__PURE__ */ __name(() => {
@@ -155,7 +165,11 @@ const openStepSettingsDialog = /* @__PURE__ */ __name(async ({
155
165
  (0, import_react2.useEffect)(() => {
156
166
  return (0, import_reactive.autorun)(() => {
157
167
  const dynamicProps = (0, import_reactive.toJS)(uiModeProps);
158
- currentDialog.update(dynamicProps);
168
+ const { zIndex, ...restDynamicProps } = dynamicProps || {};
169
+ currentDialog.update({
170
+ ...restDynamicProps,
171
+ zIndex: resolveDialogZIndex(zIndex)
172
+ });
159
173
  });
160
174
  }, []);
161
175
  const compiledFormSchema = (0, import_utils.compileUiSchema)(scopes, formSchema);
@@ -376,6 +376,21 @@ const AddSubModelButtonCore = /* @__PURE__ */ __name(function AddSubModelButton(
376
376
  }),
377
377
  [model, subModelKey, subModelType]
378
378
  );
379
+ import_react.default.useEffect(() => {
380
+ var _a, _b, _c;
381
+ const handleSubModelChanged = /* @__PURE__ */ __name(() => {
382
+ setRefreshTick((x) => x + 1);
383
+ }, "handleSubModelChanged");
384
+ (_a = model.emitter) == null ? void 0 : _a.on("onSubModelAdded", handleSubModelChanged);
385
+ (_b = model.emitter) == null ? void 0 : _b.on("onSubModelRemoved", handleSubModelChanged);
386
+ (_c = model.emitter) == null ? void 0 : _c.on("onSubModelReplaced", handleSubModelChanged);
387
+ return () => {
388
+ var _a2, _b2, _c2;
389
+ (_a2 = model.emitter) == null ? void 0 : _a2.off("onSubModelAdded", handleSubModelChanged);
390
+ (_b2 = model.emitter) == null ? void 0 : _b2.off("onSubModelRemoved", handleSubModelChanged);
391
+ (_c2 = model.emitter) == null ? void 0 : _c2.off("onSubModelReplaced", handleSubModelChanged);
392
+ };
393
+ }, [model]);
379
394
  const onClick = /* @__PURE__ */ __name(async (info) => {
380
395
  const clickedItem = info.originalItem || info;
381
396
  const item = clickedItem.originalItem || clickedItem;
@@ -2222,7 +2222,10 @@ const _BaseFlowEngineContext = class _BaseFlowEngineContext extends FlowContext
2222
2222
  ...runnerOptions || {},
2223
2223
  globals: mergedGlobals
2224
2224
  });
2225
- const shouldPreprocessTemplates = preprocessTemplates !== false;
2225
+ const shouldPreprocessTemplates = (0, import_JSRunner.shouldPreprocessRunJSTemplates)({
2226
+ version: runnerOptions == null ? void 0 : runnerOptions.version,
2227
+ preprocessTemplates
2228
+ });
2226
2229
  const jsCode = await (0, import_utils.prepareRunJsCode)(String(code ?? ""), { preprocessTemplates: shouldPreprocessTemplates });
2227
2230
  return runner.run(jsCode);
2228
2231
  }
@@ -83,6 +83,12 @@ export declare class FlowEngine {
83
83
  */
84
84
  private _previousEngine?;
85
85
  private _nextEngine?;
86
+ /**
87
+ * 视图销毁回调。由 useDrawer / useDialog 在创建弹窗视图时注册,
88
+ * 供外部(如 afterSuccess)通过引擎栈遍历来关闭多层弹窗。
89
+ * embed 视图(usePage)不注册此回调,因此 destroyView() 会自然跳过。
90
+ */
91
+ private _destroyView?;
86
92
  private _resources;
87
93
  /**
88
94
  * Data change registry used to coordinate "refresh on active" across view-scoped engines.
@@ -151,6 +157,18 @@ export declare class FlowEngine {
151
157
  * 将当前引擎从栈中移除并修复相邻指针(用于视图关闭时)。
152
158
  */
153
159
  unlinkFromStack(): void;
160
+ /**
161
+ * 注册视图销毁回调(由 useDrawer / useDialog 调用)。
162
+ */
163
+ setDestroyView(fn: () => void): void;
164
+ /**
165
+ * 关闭当前引擎关联的弹窗视图。
166
+ * 路由触发的弹窗会先 navigation.back() 清理 URL,再 destroy() 移除元素;
167
+ * 非路由弹窗直接 destroy()。
168
+ * embed 视图不注册回调,调用时返回 false 自动跳过。
169
+ * @returns 是否成功执行
170
+ */
171
+ destroyView(): boolean;
154
172
  /**
155
173
  * Get the flow engine context object.
156
174
  * @returns {FlowEngineContext} Flow engine context
@@ -333,6 +351,7 @@ export declare class FlowEngine {
333
351
  * @returns {Promise<T | null>} Model instance or null
334
352
  */
335
353
  loadOrCreateModel<T extends FlowModel = FlowModel>(options: any, extra?: {
354
+ skipSave?: boolean;
336
355
  delegateToParent?: boolean;
337
356
  delegate?: FlowContext;
338
357
  }): Promise<T | null>;
package/lib/flowEngine.js CHANGED
@@ -119,6 +119,12 @@ const _FlowEngine = class _FlowEngine {
119
119
  */
120
120
  __publicField(this, "_previousEngine");
121
121
  __publicField(this, "_nextEngine");
122
+ /**
123
+ * 视图销毁回调。由 useDrawer / useDialog 在创建弹窗视图时注册,
124
+ * 供外部(如 afterSuccess)通过引擎栈遍历来关闭多层弹窗。
125
+ * embed 视图(usePage)不注册此回调,因此 destroyView() 会自然跳过。
126
+ */
127
+ __publicField(this, "_destroyView");
122
128
  __publicField(this, "_resources", /* @__PURE__ */ new Map());
123
129
  /**
124
130
  * Data change registry used to coordinate "refresh on active" across view-scoped engines.
@@ -258,6 +264,26 @@ const _FlowEngine = class _FlowEngine {
258
264
  prev._nextEngine = void 0;
259
265
  }
260
266
  }
267
+ /**
268
+ * 注册视图销毁回调(由 useDrawer / useDialog 调用)。
269
+ */
270
+ setDestroyView(fn) {
271
+ this._destroyView = fn;
272
+ }
273
+ /**
274
+ * 关闭当前引擎关联的弹窗视图。
275
+ * 路由触发的弹窗会先 navigation.back() 清理 URL,再 destroy() 移除元素;
276
+ * 非路由弹窗直接 destroy()。
277
+ * embed 视图不注册回调,调用时返回 false 自动跳过。
278
+ * @returns 是否成功执行
279
+ */
280
+ destroyView() {
281
+ if (this._destroyView) {
282
+ this._destroyView();
283
+ return true;
284
+ }
285
+ return false;
286
+ }
261
287
  // (已移除)getModelGlobal/forEachModelGlobal/getAllModelsGlobal:不再维护冗余全局遍历 API
262
288
  /**
263
289
  * Get the flow engine context object.
@@ -829,7 +855,9 @@ const _FlowEngine = class _FlowEngine {
829
855
  model = this.createModel(data, extra);
830
856
  } else {
831
857
  model = this.createModel(options, extra);
832
- await model.save();
858
+ if (!(extra == null ? void 0 : extra.skipSave)) {
859
+ await model.save();
860
+ }
833
861
  }
834
862
  if (model.parent) {
835
863
  const subModel = model.parent.findSubModel(model.subKey, (m2) => {
@@ -6,7 +6,7 @@
6
6
  * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
- export type RunJSVersion = 'v1' | (string & {});
9
+ export type RunJSVersion = 'v1' | 'v2' | (string & {});
10
10
  export type RunJSContextCtor = new (delegate: any) => any;
11
11
  export type RunJSContextMeta = {
12
12
  scenes?: string[];
@@ -67,19 +67,26 @@ async function setupRunJSContexts() {
67
67
  import("./contexts/JSRecordActionRunJSContext"),
68
68
  import("./contexts/JSCollectionActionRunJSContext")
69
69
  ]);
70
- const v1 = "v1";
71
- import_registry.RunJSContextRegistry.register(v1, "*", import_flowContext.FlowRunJSContext);
72
- import_registry.RunJSContextRegistry.register(v1, "JSBlockModel", JSBlockRunJSContext, { scenes: ["block"] });
73
- import_registry.RunJSContextRegistry.register(v1, "JSFieldModel", JSFieldRunJSContext, { scenes: ["detail"] });
74
- import_registry.RunJSContextRegistry.register(v1, "JSEditableFieldModel", JSEditableFieldRunJSContext, { scenes: ["form"] });
75
- import_registry.RunJSContextRegistry.register(v1, "JSItemModel", JSItemRunJSContext, { scenes: ["form"] });
76
- import_registry.RunJSContextRegistry.register(v1, "JSColumnModel", JSColumnRunJSContext, { scenes: ["table"] });
77
- import_registry.RunJSContextRegistry.register(v1, "FormJSFieldItemModel", FormJSFieldItemRunJSContext, { scenes: ["form"] });
78
- import_registry.RunJSContextRegistry.register(v1, "JSRecordActionModel", JSRecordActionRunJSContext, { scenes: ["table"] });
79
- import_registry.RunJSContextRegistry.register(v1, "JSCollectionActionModel", JSCollectionActionRunJSContext, { scenes: ["table"] });
80
- await (0, import_contributions.applyRunJSContextContributions)(v1);
70
+ const registerBuiltins = /* @__PURE__ */ __name((version) => {
71
+ import_registry.RunJSContextRegistry.register(version, "*", import_flowContext.FlowRunJSContext);
72
+ import_registry.RunJSContextRegistry.register(version, "JSBlockModel", JSBlockRunJSContext, { scenes: ["block"] });
73
+ import_registry.RunJSContextRegistry.register(version, "JSFieldModel", JSFieldRunJSContext, { scenes: ["detail"] });
74
+ import_registry.RunJSContextRegistry.register(version, "JSEditableFieldModel", JSEditableFieldRunJSContext, { scenes: ["form"] });
75
+ import_registry.RunJSContextRegistry.register(version, "JSItemModel", JSItemRunJSContext, { scenes: ["form"] });
76
+ import_registry.RunJSContextRegistry.register(version, "JSColumnModel", JSColumnRunJSContext, { scenes: ["table"] });
77
+ import_registry.RunJSContextRegistry.register(version, "FormJSFieldItemModel", FormJSFieldItemRunJSContext, { scenes: ["form"] });
78
+ import_registry.RunJSContextRegistry.register(version, "JSRecordActionModel", JSRecordActionRunJSContext, { scenes: ["table"] });
79
+ import_registry.RunJSContextRegistry.register(version, "JSCollectionActionModel", JSCollectionActionRunJSContext, {
80
+ scenes: ["table"]
81
+ });
82
+ }, "registerBuiltins");
83
+ const versions = ["v1", "v2"];
84
+ for (const version of versions) {
85
+ registerBuiltins(version);
86
+ await (0, import_contributions.applyRunJSContextContributions)(version);
87
+ (0, import_contributions.markRunJSContextsSetupDone)(version);
88
+ }
81
89
  done = true;
82
- (0, import_contributions.markRunJSContextsSetupDone)(v1);
83
90
  }
84
91
  __name(setupRunJSContexts, "setupRunJSContexts");
85
92
  // Annotate the CommonJS export names for ESM import in node:
@@ -92,7 +92,7 @@ const parsePathnameToViewParams = /* @__PURE__ */ __name((pathname) => {
92
92
  } catch (_) {
93
93
  parsed = decoded;
94
94
  }
95
- } else if (decoded && decoded.includes("=") && decoded.includes("&")) {
95
+ } else if (decoded && /^[^=&]+=[^=&]*(?:&[^=&]+=[^=&]*)*$/.test(decoded)) {
96
96
  parsed = parseKeyValuePairs(decoded);
97
97
  }
98
98
  currentView.filterByTk = parsed;
@@ -103,12 +103,15 @@ function useDialog() {
103
103
  } else {
104
104
  ctx.addDelegate(flowContext.engine.context);
105
105
  }
106
+ let destroyed = false;
106
107
  const currentDialog = {
107
108
  type: "dialog",
108
109
  inputArgs: config.inputArgs || {},
109
110
  preventClose: !!config.preventClose,
110
111
  destroy: /* @__PURE__ */ __name((result) => {
111
112
  var _a2, _b2, _c2, _d;
113
+ if (destroyed) return;
114
+ destroyed = true;
112
115
  (_a2 = config.onClose) == null ? void 0 : _a2.call(config);
113
116
  (_b2 = dialogRef.current) == null ? void 0 : _b2.destroy();
114
117
  closeFunc == null ? void 0 : closeFunc();
@@ -154,6 +157,13 @@ function useDialog() {
154
157
  get: /* @__PURE__ */ __name(() => currentDialog, "get"),
155
158
  resolveOnServer: (0, import_variablesParams.createViewRecordResolveOnServer)(ctx, () => (0, import_variablesParams.getViewRecordFromParent)(flowContext, ctx))
156
159
  });
160
+ scopedEngine.setDestroyView(() => {
161
+ var _a2, _b2;
162
+ if (config.triggerByRouter && ((_b2 = (_a2 = config.inputArgs) == null ? void 0 : _a2.navigation) == null ? void 0 : _b2.back)) {
163
+ config.inputArgs.navigation.back();
164
+ }
165
+ currentDialog.destroy();
166
+ });
157
167
  (0, import_createViewMeta.registerPopupVariable)(ctx, currentDialog);
158
168
  const DialogWithContext = (0, import__.observer)(
159
169
  () => {
@@ -122,12 +122,15 @@ function useDrawer() {
122
122
  } else {
123
123
  ctx.addDelegate(flowContext.engine.context);
124
124
  }
125
+ let destroyed = false;
125
126
  const currentDrawer = {
126
127
  type: "drawer",
127
128
  inputArgs: config.inputArgs || {},
128
129
  preventClose: !!config.preventClose,
129
130
  destroy: /* @__PURE__ */ __name((result) => {
130
131
  var _a2, _b2, _c, _d;
132
+ if (destroyed) return;
133
+ destroyed = true;
131
134
  (_a2 = config.onClose) == null ? void 0 : _a2.call(config);
132
135
  (_b2 = drawerRef.current) == null ? void 0 : _b2.destroy();
133
136
  closeFunc == null ? void 0 : closeFunc();
@@ -173,6 +176,13 @@ function useDrawer() {
173
176
  get: /* @__PURE__ */ __name(() => currentDrawer, "get"),
174
177
  resolveOnServer: (0, import_variablesParams.createViewRecordResolveOnServer)(ctx, () => (0, import_variablesParams.getViewRecordFromParent)(flowContext, ctx))
175
178
  });
179
+ scopedEngine.setDestroyView(() => {
180
+ var _a2, _b2;
181
+ if (config.triggerByRouter && ((_b2 = (_a2 = config.inputArgs) == null ? void 0 : _a2.navigation) == null ? void 0 : _b2.back)) {
182
+ config.inputArgs.navigation.back();
183
+ }
184
+ currentDrawer.destroy();
185
+ });
176
186
  (0, import_createViewMeta.registerPopupVariable)(ctx, currentDrawer);
177
187
  const DrawerWithContext = React.memo(
178
188
  (0, import__.observer)((props) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/flow-engine",
3
- "version": "2.1.0-beta.7",
3
+ "version": "2.1.0-beta.8",
4
4
  "private": false,
5
5
  "description": "A standalone flow engine for NocoBase, managing workflows, models, and actions.",
6
6
  "main": "lib/index.js",
@@ -8,8 +8,8 @@
8
8
  "dependencies": {
9
9
  "@formily/antd-v5": "1.x",
10
10
  "@formily/reactive": "2.x",
11
- "@nocobase/sdk": "2.1.0-beta.7",
12
- "@nocobase/shared": "2.1.0-beta.7",
11
+ "@nocobase/sdk": "2.1.0-beta.8",
12
+ "@nocobase/shared": "2.1.0-beta.8",
13
13
  "ahooks": "^3.7.2",
14
14
  "dayjs": "^1.11.9",
15
15
  "dompurify": "^3.0.2",
@@ -36,5 +36,5 @@
36
36
  ],
37
37
  "author": "NocoBase Team",
38
38
  "license": "Apache-2.0",
39
- "gitHead": "da7dfef2b6d6854988a56119463c8c38e3221e79"
39
+ "gitHead": "5099d561c5467292414c1e77ad6bad3730d97344"
40
40
  }
package/src/JSRunner.ts CHANGED
@@ -16,12 +16,74 @@ export interface JSRunnerOptions {
16
16
  version?: string;
17
17
  /**
18
18
  * Enable RunJS template compatibility preprocessing for `{{ ... }}`.
19
- * When enabled via `ctx.runjs(code, vars, { preprocessTemplates: true })` (default),
19
+ * When enabled (or falling back to version default),
20
20
  * the code will be rewritten to call `ctx.resolveJsonTemplate(...)` at runtime.
21
21
  */
22
22
  preprocessTemplates?: boolean;
23
23
  }
24
24
 
25
+ /**
26
+ * Decide whether RunJS `{{ ... }}` compatibility preprocessing should run.
27
+ *
28
+ * Priority:
29
+ * 1. Explicit `preprocessTemplates` option always wins.
30
+ * 2. Otherwise, `version === 'v2'` disables preprocessing.
31
+ * 3. Fallback keeps v1-compatible behavior (enabled).
32
+ */
33
+ export function shouldPreprocessRunJSTemplates(
34
+ options?: Pick<JSRunnerOptions, 'preprocessTemplates' | 'version'>,
35
+ ): boolean {
36
+ if (typeof options?.preprocessTemplates === 'boolean') {
37
+ return options.preprocessTemplates;
38
+ }
39
+ return options?.version !== 'v2';
40
+ }
41
+
42
+ // Heuristic: detect likely bare `{{ctx.xxx}}` usage in executable positions (not quoted string literals).
43
+ const BARE_CTX_TEMPLATE_RE = /(^|[=(:,[\s)])(\{\{\s*(ctx(?:\.|\[|\?\.)[^}]*)\s*\}\})/m;
44
+
45
+ function extractDeprecatedCtxTemplateUsage(code: string): { placeholder: string; expression: string } | null {
46
+ const src = String(code || '');
47
+ const m = src.match(BARE_CTX_TEMPLATE_RE);
48
+ if (!m) return null;
49
+ const placeholder = String(m[2] || '').trim();
50
+ const expression = String(m[3] || '').trim();
51
+ if (!placeholder || !expression) return null;
52
+ return { placeholder, expression };
53
+ }
54
+
55
+ function shouldHintCtxTemplateSyntax(err: any, usage: { placeholder: string; expression: string } | null): boolean {
56
+ const isSyntaxError = err instanceof SyntaxError || String((err as any)?.name || '') === 'SyntaxError';
57
+ if (!isSyntaxError) return false;
58
+ if (!usage) return false;
59
+ const msg = String((err as any)?.message || err || '');
60
+ return /unexpected token/i.test(msg);
61
+ }
62
+
63
+ function toCtxTemplateSyntaxHintError(
64
+ err: any,
65
+ usage: {
66
+ placeholder: string;
67
+ expression: string;
68
+ },
69
+ ): Error {
70
+ const hint = `"${usage.placeholder}" has been deprecated and cannot be used as executable RunJS syntax. Use await ctx.getVar("${usage.expression}") instead, or keep "${usage.placeholder}" as a plain string.`;
71
+ const out = new SyntaxError(hint);
72
+ try {
73
+ (out as any).cause = err;
74
+ } catch (_) {
75
+ // ignore
76
+ }
77
+ try {
78
+ // Hint-only error: avoid leaking internal bundle line numbers from stack parsers in preview UI.
79
+ (out as any).__runjsHideLocation = true;
80
+ out.stack = `${out.name}: ${out.message}`;
81
+ } catch (_) {
82
+ // ignore
83
+ }
84
+ return out;
85
+ }
86
+
25
87
  export class JSRunner {
26
88
  private globals: Record<string, any>;
27
89
  private timeoutMs: number;
@@ -118,11 +180,13 @@ export class JSRunner {
118
180
  if (err instanceof FlowExitAllException) {
119
181
  throw err;
120
182
  }
121
- console.error(err);
183
+ const usage = extractDeprecatedCtxTemplateUsage(code);
184
+ const outErr = shouldHintCtxTemplateSyntax(err, usage) && usage ? toCtxTemplateSyntaxHintError(err, usage) : err;
185
+ console.error(outErr);
122
186
  return {
123
187
  success: false,
124
- error: err,
125
- timeout: err.message === 'Execution timed out',
188
+ error: outErr,
189
+ timeout: (outErr as any)?.message === 'Execution timed out',
126
190
  };
127
191
  }
128
192
  }
@@ -62,6 +62,10 @@ export function createViewScopedEngine(parent: FlowEngine): FlowEngine {
62
62
  '_nextEngine',
63
63
  // getModel 需要在本地执行以确保全局查找时正确遍历整个引擎栈
64
64
  'getModel',
65
+ // 视图销毁回调需要在本地存储,每个视图引擎有自己的销毁逻辑
66
+ '_destroyView',
67
+ 'setDestroyView',
68
+ 'destroyView',
65
69
  ]);
66
70
 
67
71
  const handler: ProxyHandler<FlowEngine> = {
@@ -8,7 +8,7 @@
8
8
  */
9
9
 
10
10
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
11
- import { JSRunner } from '../JSRunner';
11
+ import { JSRunner, shouldPreprocessRunJSTemplates } from '../JSRunner';
12
12
  import { createSafeWindow } from '../utils';
13
13
 
14
14
  describe('JSRunner', () => {
@@ -30,6 +30,18 @@ describe('JSRunner', () => {
30
30
  vi.restoreAllMocks();
31
31
  });
32
32
 
33
+ it('shouldPreprocessRunJSTemplates: explicit option has highest priority', () => {
34
+ expect(shouldPreprocessRunJSTemplates({ version: 'v2', preprocessTemplates: true })).toBe(true);
35
+ expect(shouldPreprocessRunJSTemplates({ version: 'v1', preprocessTemplates: false })).toBe(false);
36
+ });
37
+
38
+ it('shouldPreprocessRunJSTemplates: falls back to version policy', () => {
39
+ expect(shouldPreprocessRunJSTemplates({ version: 'v1' })).toBe(true);
40
+ expect(shouldPreprocessRunJSTemplates({ version: 'v2' })).toBe(false);
41
+ expect(shouldPreprocessRunJSTemplates({})).toBe(true);
42
+ expect(shouldPreprocessRunJSTemplates()).toBe(true);
43
+ });
44
+
33
45
  it('executes simple code and returns value', async () => {
34
46
  const runner = new JSRunner();
35
47
  const result = await runner.run('return 1 + 2 + 3');
@@ -152,6 +164,20 @@ describe('JSRunner', () => {
152
164
  expect((result.error as Error).message).toBe('Execution timed out');
153
165
  });
154
166
 
167
+ it('returns friendly hint when bare {{ctx.xxx}} appears in syntax error', async () => {
168
+ const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
169
+ const runner = new JSRunner();
170
+ const result = await runner.run('const z = {{ctx.user.id}}');
171
+ expect(result.success).toBe(false);
172
+ expect(result.error).toBeInstanceOf(SyntaxError);
173
+ const msg = String((result.error as any)?.message || '');
174
+ expect(msg).toContain('"{{ctx.user.id}}" has been deprecated');
175
+ expect(msg).toContain('await ctx.getVar("ctx.user.id")');
176
+ expect(msg).not.toContain('(at ');
177
+ expect((result.error as any)?.__runjsHideLocation).toBe(true);
178
+ expect(spy).toHaveBeenCalled();
179
+ });
180
+
155
181
  it('skips execution when URL contains skipRunJs=true', async () => {
156
182
  // 模拟预览模式下通过 URL 参数跳过代码执行
157
183
  if (typeof window !== 'undefined' && typeof window.history?.pushState === 'function') {
@@ -29,6 +29,10 @@ describe('flowRunJSContext registry and doc', () => {
29
29
  expect(RunJSContextRegistry['resolve']('v1' as any, '*')).toBeTruthy();
30
30
  });
31
31
 
32
+ it('should register v2 mapping', () => {
33
+ expect(RunJSContextRegistry['resolve']('v2' as any, '*')).toBeTruthy();
34
+ });
35
+
32
36
  it('should register all context types', () => {
33
37
  const contextTypes = [
34
38
  'JSBlockModel',
@@ -44,12 +48,20 @@ describe('flowRunJSContext registry and doc', () => {
44
48
  const ctor = RunJSContextRegistry['resolve']('v1' as any, modelClass);
45
49
  expect(ctor).toBeTruthy();
46
50
  });
51
+
52
+ contextTypes.forEach((modelClass) => {
53
+ const ctor = RunJSContextRegistry['resolve']('v2' as any, modelClass);
54
+ expect(ctor).toBeTruthy();
55
+ });
47
56
  });
48
57
 
49
58
  it('should expose scene metadata for contexts', () => {
50
59
  expect(getRunJSScenesForModel('JSBlockModel', 'v1')).toEqual(['block']);
51
60
  expect(getRunJSScenesForModel('JSFieldModel', 'v1')).toEqual(['detail']);
61
+ expect(getRunJSScenesForModel('JSBlockModel', 'v2')).toEqual(['block']);
62
+ expect(getRunJSScenesForModel('JSFieldModel', 'v2')).toEqual(['detail']);
52
63
  expect(getRunJSScenesForModel('UnknownModel', 'v1')).toEqual([]);
64
+ expect(getRunJSScenesForModel('UnknownModel', 'v2')).toEqual([]);
53
65
  });
54
66
 
55
67
  it('should only execute once (idempotent)', async () => {
@@ -175,6 +187,7 @@ describe('flowRunJSContext registry and doc', () => {
175
187
  const ctx = new FlowContext();
176
188
  ctx.defineProperty('model', { value: { constructor: { name: 'JSColumnModel' } } });
177
189
  expect(getRunJSScenesForContext(ctx as any, { version: 'v1' })).toEqual(['table']);
190
+ expect(getRunJSScenesForContext(ctx as any, { version: 'v2' })).toEqual(['table']);
178
191
  });
179
192
 
180
193
  it('JSBlockModel context should have element property in doc', () => {
@@ -36,6 +36,29 @@ describe('ctx.runjs preprocessTemplates default', () => {
36
36
  expect(r.value).toBe('{{ctx.user.id}}');
37
37
  });
38
38
 
39
+ it('disables template preprocess by default for version v2', async () => {
40
+ const engine = new FlowEngine();
41
+ const ctx = engine.context as any;
42
+ ctx.defineProperty('user', { value: { id: 123 } });
43
+
44
+ const r = await ctx.runjs('return "{{ctx.user.id}}";', undefined, { version: 'v2' });
45
+ expect(r.success).toBe(true);
46
+ expect(r.value).toBe('{{ctx.user.id}}');
47
+ });
48
+
49
+ it('keeps explicit preprocessTemplates override higher priority than version', async () => {
50
+ const engine = new FlowEngine();
51
+ const ctx = engine.context as any;
52
+ ctx.defineProperty('user', { value: { id: 123 } });
53
+
54
+ const r = await ctx.runjs('return "{{ctx.user.id}}";', undefined, {
55
+ version: 'v2',
56
+ preprocessTemplates: true,
57
+ });
58
+ expect(r.success).toBe(true);
59
+ expect(r.value).toBe('123');
60
+ });
61
+
39
62
  it('does not double-preprocess already prepared code', async () => {
40
63
  const engine = new FlowEngine();
41
64
  const ctx = engine.context as any;
@@ -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
  // ========================
@@ -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) => {
@@ -10,7 +10,7 @@
10
10
  // 为避免在模块初始化阶段引入 FlowContext(从而触发循环依赖),不要在顶层导入各类 RunJSContext。
11
11
  // 在需要默认映射时(首次 resolve)再使用 createRequire 同步加载对应模块。
12
12
 
13
- export type RunJSVersion = 'v1' | (string & {});
13
+ export type RunJSVersion = 'v1' | 'v2' | (string & {});
14
14
  export type RunJSContextCtor = new (delegate: any) => any;
15
15
  export type RunJSContextMeta = {
16
16
  scenes?: string[];
@@ -41,17 +41,26 @@ export async function setupRunJSContexts() {
41
41
  import('./contexts/JSCollectionActionRunJSContext'),
42
42
  ]);
43
43
 
44
- const v1 = 'v1';
45
- RunJSContextRegistry.register(v1, '*', FlowRunJSContext);
46
- RunJSContextRegistry.register(v1, 'JSBlockModel', JSBlockRunJSContext, { scenes: ['block'] });
47
- RunJSContextRegistry.register(v1, 'JSFieldModel', JSFieldRunJSContext, { scenes: ['detail'] });
48
- RunJSContextRegistry.register(v1, 'JSEditableFieldModel', JSEditableFieldRunJSContext, { scenes: ['form'] });
49
- RunJSContextRegistry.register(v1, 'JSItemModel', JSItemRunJSContext, { scenes: ['form'] });
50
- RunJSContextRegistry.register(v1, 'JSColumnModel', JSColumnRunJSContext, { scenes: ['table'] });
51
- RunJSContextRegistry.register(v1, 'FormJSFieldItemModel', FormJSFieldItemRunJSContext, { scenes: ['form'] });
52
- RunJSContextRegistry.register(v1, 'JSRecordActionModel', JSRecordActionRunJSContext, { scenes: ['table'] });
53
- RunJSContextRegistry.register(v1, 'JSCollectionActionModel', JSCollectionActionRunJSContext, { scenes: ['table'] });
54
- await applyRunJSContextContributions(v1);
44
+ const registerBuiltins = (version: 'v1' | 'v2') => {
45
+ RunJSContextRegistry.register(version, '*', FlowRunJSContext);
46
+ RunJSContextRegistry.register(version, 'JSBlockModel', JSBlockRunJSContext, { scenes: ['block'] });
47
+ RunJSContextRegistry.register(version, 'JSFieldModel', JSFieldRunJSContext, { scenes: ['detail'] });
48
+ RunJSContextRegistry.register(version, 'JSEditableFieldModel', JSEditableFieldRunJSContext, { scenes: ['form'] });
49
+ RunJSContextRegistry.register(version, 'JSItemModel', JSItemRunJSContext, { scenes: ['form'] });
50
+ RunJSContextRegistry.register(version, 'JSColumnModel', JSColumnRunJSContext, { scenes: ['table'] });
51
+ RunJSContextRegistry.register(version, 'FormJSFieldItemModel', FormJSFieldItemRunJSContext, { scenes: ['form'] });
52
+ RunJSContextRegistry.register(version, 'JSRecordActionModel', JSRecordActionRunJSContext, { scenes: ['table'] });
53
+ RunJSContextRegistry.register(version, 'JSCollectionActionModel', JSCollectionActionRunJSContext, {
54
+ scenes: ['table'],
55
+ });
56
+ };
57
+
58
+ const versions: Array<'v1' | 'v2'> = ['v1', 'v2'];
59
+ for (const version of versions) {
60
+ registerBuiltins(version);
61
+ await applyRunJSContextContributions(version);
62
+ markRunJSContextsSetupDone(version);
63
+ }
64
+
55
65
  done = true;
56
- markRunJSContextsSetupDone(v1);
57
66
  }
@@ -109,6 +109,13 @@ describe('parsePathnameToViewParams', () => {
109
109
  expect(result).toEqual([{ viewUid: 'xxx', filterByTk: { id: '1', tenant: 'ac' } }]);
110
110
  });
111
111
 
112
+ test('should parse filterByTk from single key-value encoded segment into object', () => {
113
+ const kv = encodeURIComponent('id=1');
114
+ const path = `/admin/xxx/filterbytk/${kv}`;
115
+ const result = parsePathnameToViewParams(path);
116
+ expect(result).toEqual([{ viewUid: 'xxx', filterByTk: { id: '1' } }]);
117
+ });
118
+
112
119
  test('should parse filterByTk from JSON object segment', () => {
113
120
  const json = encodeURIComponent('{"id":"1","tenant":"ac"}');
114
121
  const path = `/admin/xxx/filterbytk/${json}`;
@@ -116,8 +116,8 @@ export const parsePathnameToViewParams = (pathname: string): ViewParam[] => {
116
116
  // 解析失败,按字符串保留
117
117
  parsed = decoded;
118
118
  }
119
- } else if (decoded && decoded.includes('=') && decoded.includes('&')) {
120
- // 形如 a=b&c=d 的整体段
119
+ } else if (decoded && /^[^=&]+=[^=&]*(?:&[^=&]+=[^=&]*)*$/.test(decoded)) {
120
+ // 形如 a=b 或 a=b&c=d 的整体段
121
121
  parsed = parseKeyValuePairs(decoded);
122
122
  }
123
123
  currentView.filterByTk = parsed;
@@ -26,6 +26,7 @@ vi.mock('../../ViewScopedFlowEngine', () => ({
26
26
  createViewScopedEngine: (engine) => ({
27
27
  context: new FlowContext(),
28
28
  unlinkFromStack: vi.fn(),
29
+ setDestroyView: vi.fn(),
29
30
  // mimic real view stack linkage: previousEngine points to the last engine in chain
30
31
  previousEngine: (engine as any)?.nextEngine || engine,
31
32
  }),
@@ -89,12 +89,17 @@ export function useDialog() {
89
89
  ctx.addDelegate(flowContext.engine.context);
90
90
  }
91
91
 
92
+ // 幂等保护:防止 FlowPage 路由清理时二次调用 destroy
93
+ let destroyed = false;
94
+
92
95
  // 构造 currentDialog 实例
93
96
  const currentDialog = {
94
97
  type: 'dialog' as const,
95
98
  inputArgs: config.inputArgs || {},
96
99
  preventClose: !!config.preventClose,
97
100
  destroy: (result?: any) => {
101
+ if (destroyed) return;
102
+ destroyed = true;
98
103
  config.onClose?.();
99
104
  dialogRef.current?.destroy();
100
105
  closeFunc?.();
@@ -140,6 +145,15 @@ export function useDialog() {
140
145
  get: () => currentDialog,
141
146
  resolveOnServer: createViewRecordResolveOnServer(ctx, () => getViewRecordFromParent(flowContext, ctx)),
142
147
  });
148
+ // 注册视图销毁回调,供外部(如 afterSuccess)通过引擎栈遍历来关闭多层弹窗。
149
+ // 对路由触发的弹窗:先 navigation.back() 清理 URL(replace 方式),再 destroy() 立即移除元素;
150
+ // 对非路由弹窗:直接 destroy()。destroy() 已做幂等保护,FlowPage 后续清理不会重复执行。
151
+ scopedEngine.setDestroyView(() => {
152
+ if (config.triggerByRouter && config.inputArgs?.navigation?.back) {
153
+ config.inputArgs.navigation.back();
154
+ }
155
+ currentDialog.destroy();
156
+ });
143
157
  // 顶层 popup 变量:弹窗记录/数据源/上级弹窗链(去重封装)
144
158
  registerPopupVariable(ctx, currentDialog);
145
159
  // 内部组件,在 Provider 内部计算 content
@@ -118,12 +118,17 @@ export function useDrawer() {
118
118
  ctx.addDelegate(flowContext.engine.context);
119
119
  }
120
120
 
121
+ // 幂等保护:防止 FlowPage 路由清理时二次调用 destroy
122
+ let destroyed = false;
123
+
121
124
  // 构造 currentDrawer 实例
122
125
  const currentDrawer = {
123
126
  type: 'drawer' as const,
124
127
  inputArgs: config.inputArgs || {},
125
128
  preventClose: !!config.preventClose,
126
129
  destroy: (result?: any) => {
130
+ if (destroyed) return;
131
+ destroyed = true;
127
132
  config.onClose?.();
128
133
  drawerRef.current?.destroy();
129
134
  closeFunc?.();
@@ -169,6 +174,15 @@ export function useDrawer() {
169
174
  get: () => currentDrawer,
170
175
  resolveOnServer: createViewRecordResolveOnServer(ctx, () => getViewRecordFromParent(flowContext, ctx)),
171
176
  });
177
+ // 注册视图销毁回调,供外部(如 afterSuccess)通过引擎栈遍历来关闭多层弹窗。
178
+ // 对路由触发的弹窗:先 navigation.back() 清理 URL(replace 方式),再 destroy() 立即移除元素;
179
+ // 对非路由弹窗:直接 destroy()。destroy() 已做幂等保护,FlowPage 后续清理不会重复执行。
180
+ scopedEngine.setDestroyView(() => {
181
+ if (config.triggerByRouter && config.inputArgs?.navigation?.back) {
182
+ config.inputArgs.navigation.back();
183
+ }
184
+ currentDrawer.destroy();
185
+ });
172
186
  // 顶层 popup 变量:弹窗记录/数据源/上级弹窗链(去重封装)
173
187
  registerPopupVariable(ctx, currentDrawer);
174
188
 
@@ -178,6 +178,7 @@ export function usePage() {
178
178
  // 仅当访问关联字段或前端无本地记录数据时,才交给服务端解析
179
179
  resolveOnServer: createViewRecordResolveOnServer(ctx, () => getViewRecordFromParent(flowContext, ctx)),
180
180
  });
181
+ // embed 视图不注册 destroyView,afterSuccess 关闭弹窗时自然跳过
181
182
  // 顶层 popup 变量:弹窗记录/数据源/上级弹窗链(去重封装)
182
183
  registerPopupVariable(ctx, currentPage);
183
184