@nocobase/flow-engine 2.0.12 → 2.0.14
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.
- package/lib/ViewScopedFlowEngine.js +5 -1
- package/lib/components/dnd/gridDragPlanner.js +6 -2
- package/lib/components/subModel/AddSubModelButton.js +15 -0
- package/lib/flowEngine.d.ts +19 -0
- package/lib/flowEngine.js +29 -1
- package/lib/utils/parsePathnameToViewParams.js +1 -1
- package/lib/views/useDialog.js +10 -0
- package/lib/views/useDrawer.js +10 -0
- package/package.json +4 -4
- package/src/ViewScopedFlowEngine.ts +4 -0
- package/src/components/__tests__/gridDragPlanner.test.ts +88 -0
- package/src/components/dnd/gridDragPlanner.ts +8 -2
- package/src/components/subModel/AddSubModelButton.tsx +16 -0
- package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +50 -0
- package/src/flowEngine.ts +33 -1
- package/src/utils/__tests__/parsePathnameToViewParams.test.ts +7 -0
- package/src/utils/parsePathnameToViewParams.ts +2 -2
- package/src/views/__tests__/useDialog.closeDestroy.test.tsx +1 -0
- package/src/views/useDialog.tsx +14 -0
- package/src/views/useDrawer.tsx +14 -0
- package/src/views/usePage.tsx +1 -0
|
@@ -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);
|
|
@@ -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;
|
package/lib/flowEngine.d.ts
CHANGED
|
@@ -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
|
-
|
|
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) => {
|
|
@@ -92,7 +92,7 @@ const parsePathnameToViewParams = /* @__PURE__ */ __name((pathname) => {
|
|
|
92
92
|
} catch (_) {
|
|
93
93
|
parsed = decoded;
|
|
94
94
|
}
|
|
95
|
-
} else if (decoded &&
|
|
95
|
+
} else if (decoded && /^[^=&]+=[^=&]*(?:&[^=&]+=[^=&]*)*$/.test(decoded)) {
|
|
96
96
|
parsed = parseKeyValuePairs(decoded);
|
|
97
97
|
}
|
|
98
98
|
currentView.filterByTk = parsed;
|
package/lib/views/useDialog.js
CHANGED
|
@@ -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
|
() => {
|
package/lib/views/useDrawer.js
CHANGED
|
@@ -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.0.
|
|
3
|
+
"version": "2.0.14",
|
|
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.0.
|
|
12
|
-
"@nocobase/shared": "2.0.
|
|
11
|
+
"@nocobase/sdk": "2.0.14",
|
|
12
|
+
"@nocobase/shared": "2.0.14",
|
|
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": "
|
|
39
|
+
"gitHead": "b071059e085069874e61bf52412cc928936550b1"
|
|
40
40
|
}
|
|
@@ -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> = {
|
|
@@ -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
|
-
)
|
|
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
|
-
)
|
|
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);
|
|
@@ -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
|
// ========================
|
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
|
-
|
|
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) => {
|
|
@@ -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 &&
|
|
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
|
}),
|
package/src/views/useDialog.tsx
CHANGED
|
@@ -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
|
package/src/views/useDrawer.tsx
CHANGED
|
@@ -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
|
|
package/src/views/usePage.tsx
CHANGED
|
@@ -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
|
|