@nocobase/flow-engine 2.0.0-beta.2 → 2.0.0-beta.3
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.
|
@@ -361,6 +361,13 @@ export declare class FlowModel<Structure extends DefaultStructure = DefaultStruc
|
|
|
361
361
|
}): Promise<void>;
|
|
362
362
|
get translate(): any;
|
|
363
363
|
serialize(): Record<string, any>;
|
|
364
|
+
/**
|
|
365
|
+
* 复制当前模型实例为一个新的实例。
|
|
366
|
+
* 新实例及其所有子模型都会有新的 uid,且不保留 root model 的 parent 关系。
|
|
367
|
+
* 内部所有引用旧 uid 的地方(如 parentId, parentUid 等)都会被替换为对应的新 uid。
|
|
368
|
+
* @returns {FlowModel} 复制后的新模型实例
|
|
369
|
+
*/
|
|
370
|
+
clone<T extends FlowModel = this>(): T;
|
|
364
371
|
/**
|
|
365
372
|
* Opens the flow settings dialog for this flow model.
|
|
366
373
|
* @param options - Configuration options for opening flow settings, excluding the model property
|
package/lib/models/flowModel.js
CHANGED
|
@@ -1119,6 +1119,60 @@ const _FlowModel = class _FlowModel {
|
|
|
1119
1119
|
}
|
|
1120
1120
|
return data;
|
|
1121
1121
|
}
|
|
1122
|
+
/**
|
|
1123
|
+
* 复制当前模型实例为一个新的实例。
|
|
1124
|
+
* 新实例及其所有子模型都会有新的 uid,且不保留 root model 的 parent 关系。
|
|
1125
|
+
* 内部所有引用旧 uid 的地方(如 parentId, parentUid 等)都会被替换为对应的新 uid。
|
|
1126
|
+
* @returns {FlowModel} 复制后的新模型实例
|
|
1127
|
+
*/
|
|
1128
|
+
clone() {
|
|
1129
|
+
if (!this.flowEngine) {
|
|
1130
|
+
throw new Error("FlowEngine is not set on this model. Please set flowEngine before cloning.");
|
|
1131
|
+
}
|
|
1132
|
+
const serialized = this.serialize();
|
|
1133
|
+
const uidMap = /* @__PURE__ */ new Map();
|
|
1134
|
+
const collectUids = /* @__PURE__ */ __name((data) => {
|
|
1135
|
+
if (data.uid && typeof data.uid === "string") {
|
|
1136
|
+
uidMap.set(data.uid, (0, import_secure.uid)());
|
|
1137
|
+
}
|
|
1138
|
+
if (data.subModels) {
|
|
1139
|
+
for (const key in data.subModels) {
|
|
1140
|
+
const subModel = data.subModels[key];
|
|
1141
|
+
if (Array.isArray(subModel)) {
|
|
1142
|
+
subModel.forEach((item) => collectUids(item));
|
|
1143
|
+
} else if (subModel && typeof subModel === "object") {
|
|
1144
|
+
collectUids(subModel);
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}, "collectUids");
|
|
1149
|
+
collectUids(serialized);
|
|
1150
|
+
const replaceUidReferences = /* @__PURE__ */ __name((data, isRoot = false) => {
|
|
1151
|
+
if (data === null || data === void 0) {
|
|
1152
|
+
return data;
|
|
1153
|
+
}
|
|
1154
|
+
if (typeof data === "string") {
|
|
1155
|
+
return uidMap.get(data) ?? data;
|
|
1156
|
+
}
|
|
1157
|
+
if (Array.isArray(data)) {
|
|
1158
|
+
return data.map((item) => replaceUidReferences(item, false));
|
|
1159
|
+
}
|
|
1160
|
+
if (typeof data === "object") {
|
|
1161
|
+
const result = {};
|
|
1162
|
+
for (const key in data) {
|
|
1163
|
+
if (!Object.prototype.hasOwnProperty.call(data, key)) continue;
|
|
1164
|
+
if (isRoot && key === "parentId") {
|
|
1165
|
+
continue;
|
|
1166
|
+
}
|
|
1167
|
+
result[key] = replaceUidReferences(data[key], false);
|
|
1168
|
+
}
|
|
1169
|
+
return result;
|
|
1170
|
+
}
|
|
1171
|
+
return data;
|
|
1172
|
+
}, "replaceUidReferences");
|
|
1173
|
+
const clonedData = replaceUidReferences(serialized, true);
|
|
1174
|
+
return this.flowEngine.createModel(clonedData);
|
|
1175
|
+
}
|
|
1122
1176
|
/**
|
|
1123
1177
|
* Opens the flow settings dialog for this flow model.
|
|
1124
1178
|
* @param options - Configuration options for opening flow settings, excluding the model property
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/flow-engine",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
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.0-beta.
|
|
12
|
-
"@nocobase/shared": "2.0.0-beta.
|
|
11
|
+
"@nocobase/sdk": "2.0.0-beta.3",
|
|
12
|
+
"@nocobase/shared": "2.0.0-beta.3",
|
|
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": "AGPL-3.0",
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "6c178de2050651bcfcc0b2708de0246977eac510"
|
|
40
40
|
}
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
11
|
+
import { FlowEngine } from '../../flowEngine';
|
|
12
|
+
import { FlowModel } from '../flowModel';
|
|
13
|
+
|
|
14
|
+
describe('FlowModel.clone', () => {
|
|
15
|
+
let flowEngine: FlowEngine;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
flowEngine = new FlowEngine();
|
|
19
|
+
// Mock api for FlowEngineContext
|
|
20
|
+
(flowEngine.context as any).api = {
|
|
21
|
+
auth: {
|
|
22
|
+
role: 'admin',
|
|
23
|
+
locale: 'en-US',
|
|
24
|
+
token: 'mock-token',
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
vi.clearAllMocks();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should clone a simple model with a new uid', () => {
|
|
31
|
+
const original = flowEngine.createModel({
|
|
32
|
+
uid: 'original-uid',
|
|
33
|
+
use: 'FlowModel',
|
|
34
|
+
props: { title: 'Original Title' },
|
|
35
|
+
stepParams: { testFlow: { step1: { value: 'test' } } },
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const cloned = original.clone();
|
|
39
|
+
|
|
40
|
+
// uid should be different
|
|
41
|
+
expect(cloned.uid).not.toBe(original.uid);
|
|
42
|
+
|
|
43
|
+
// props should be the same
|
|
44
|
+
expect(cloned.props).toEqual(original.props);
|
|
45
|
+
|
|
46
|
+
// stepParams should be the same
|
|
47
|
+
expect(cloned.stepParams).toEqual(original.stepParams);
|
|
48
|
+
|
|
49
|
+
// should be registered in flowEngine
|
|
50
|
+
expect(flowEngine.getModel(cloned.uid)).toBe(cloned);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should clone model without parent relationship', () => {
|
|
54
|
+
const parent = flowEngine.createModel({
|
|
55
|
+
uid: 'parent-uid',
|
|
56
|
+
use: 'FlowModel',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const child = parent.addSubModel('children', {
|
|
60
|
+
use: 'FlowModel',
|
|
61
|
+
props: { name: 'child' },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const cloned = child.clone();
|
|
65
|
+
|
|
66
|
+
// cloned model should not have parent
|
|
67
|
+
expect(cloned.parent).toBeUndefined();
|
|
68
|
+
expect(cloned['_options'].parentId).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should clone model with subModels recursively', () => {
|
|
72
|
+
const parent = flowEngine.createModel({
|
|
73
|
+
uid: 'parent-uid',
|
|
74
|
+
use: 'FlowModel',
|
|
75
|
+
props: { name: 'parent' },
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const child1 = parent.addSubModel('items', {
|
|
79
|
+
use: 'FlowModel',
|
|
80
|
+
props: { name: 'child1' },
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const child2 = parent.addSubModel('items', {
|
|
84
|
+
use: 'FlowModel',
|
|
85
|
+
props: { name: 'child2' },
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const cloned = parent.clone();
|
|
89
|
+
|
|
90
|
+
// parent uid should be different
|
|
91
|
+
expect(cloned.uid).not.toBe(parent.uid);
|
|
92
|
+
|
|
93
|
+
// subModels should exist
|
|
94
|
+
expect(cloned.subModels['items']).toBeDefined();
|
|
95
|
+
expect(cloned.subModels['items']).toHaveLength(2);
|
|
96
|
+
|
|
97
|
+
// subModels uids should be different
|
|
98
|
+
const clonedChildren = cloned.subModels['items'] as FlowModel[];
|
|
99
|
+
expect(clonedChildren[0].uid).not.toBe(child1.uid);
|
|
100
|
+
expect(clonedChildren[1].uid).not.toBe(child2.uid);
|
|
101
|
+
|
|
102
|
+
// subModels props should be the same
|
|
103
|
+
expect(clonedChildren[0].props.name).toBe('child1');
|
|
104
|
+
expect(clonedChildren[1].props.name).toBe('child2');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should clone model with nested subModels', () => {
|
|
108
|
+
const root = flowEngine.createModel({
|
|
109
|
+
uid: 'root-uid',
|
|
110
|
+
use: 'FlowModel',
|
|
111
|
+
props: { level: 'root' },
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const level1 = root.addSubModel('children', {
|
|
115
|
+
use: 'FlowModel',
|
|
116
|
+
props: { level: 'level1' },
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const level2 = level1.addSubModel('children', {
|
|
120
|
+
use: 'FlowModel',
|
|
121
|
+
props: { level: 'level2' },
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const cloned = root.clone();
|
|
125
|
+
|
|
126
|
+
// All uids should be different
|
|
127
|
+
expect(cloned.uid).not.toBe(root.uid);
|
|
128
|
+
|
|
129
|
+
const clonedLevel1 = (cloned.subModels['children'] as FlowModel[])[0];
|
|
130
|
+
expect(clonedLevel1.uid).not.toBe(level1.uid);
|
|
131
|
+
expect(clonedLevel1.props.level).toBe('level1');
|
|
132
|
+
|
|
133
|
+
const clonedLevel2 = (clonedLevel1.subModels['children'] as FlowModel[])[0];
|
|
134
|
+
expect(clonedLevel2.uid).not.toBe(level2.uid);
|
|
135
|
+
expect(clonedLevel2.props.level).toBe('level2');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should clone model with object-type subModel', () => {
|
|
139
|
+
const parent = flowEngine.createModel({
|
|
140
|
+
uid: 'parent-uid',
|
|
141
|
+
use: 'FlowModel',
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const header = parent.setSubModel('header', {
|
|
145
|
+
use: 'FlowModel',
|
|
146
|
+
props: { title: 'Header' },
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const cloned = parent.clone();
|
|
150
|
+
|
|
151
|
+
// parent uid should be different
|
|
152
|
+
expect(cloned.uid).not.toBe(parent.uid);
|
|
153
|
+
|
|
154
|
+
// object-type subModel should exist with different uid
|
|
155
|
+
const clonedHeader = cloned.subModels['header'] as FlowModel;
|
|
156
|
+
expect(clonedHeader).toBeDefined();
|
|
157
|
+
expect(clonedHeader.uid).not.toBe(header.uid);
|
|
158
|
+
expect(clonedHeader.props.title).toBe('Header');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should preserve sortIndex when cloning', () => {
|
|
162
|
+
const model = flowEngine.createModel({
|
|
163
|
+
uid: 'test-uid',
|
|
164
|
+
use: 'FlowModel',
|
|
165
|
+
sortIndex: 5,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const cloned = model.clone();
|
|
169
|
+
|
|
170
|
+
expect(cloned.sortIndex).toBe(5);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should preserve stepParams when cloning', () => {
|
|
174
|
+
const model = flowEngine.createModel({
|
|
175
|
+
uid: 'test-uid',
|
|
176
|
+
use: 'FlowModel',
|
|
177
|
+
stepParams: {
|
|
178
|
+
flow1: { step1: { param1: 'value1' } },
|
|
179
|
+
flow2: { step2: { param2: 'value2' } },
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const cloned = model.clone();
|
|
184
|
+
|
|
185
|
+
expect(cloned.stepParams).toEqual({
|
|
186
|
+
flow1: { step1: { param1: 'value1' } },
|
|
187
|
+
flow2: { step2: { param2: 'value2' } },
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should throw error if flowEngine is not set', () => {
|
|
192
|
+
const model = flowEngine.createModel({
|
|
193
|
+
uid: 'test-uid',
|
|
194
|
+
use: 'FlowModel',
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Manually remove flowEngine to simulate edge case
|
|
198
|
+
(model as any).flowEngine = null;
|
|
199
|
+
|
|
200
|
+
expect(() => model.clone()).toThrow('FlowEngine is not set on this model. Please set flowEngine before cloning.');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('should clone model with correct type', () => {
|
|
204
|
+
class CustomModel extends FlowModel {
|
|
205
|
+
customMethod() {
|
|
206
|
+
return 'custom';
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
flowEngine.registerModels({ CustomModel });
|
|
211
|
+
|
|
212
|
+
const original = flowEngine.createModel<CustomModel>({
|
|
213
|
+
uid: 'custom-uid',
|
|
214
|
+
use: 'CustomModel',
|
|
215
|
+
props: { custom: true },
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const cloned = original.clone<CustomModel>();
|
|
219
|
+
|
|
220
|
+
expect(cloned).toBeInstanceOf(CustomModel);
|
|
221
|
+
expect(cloned.customMethod()).toBe('custom');
|
|
222
|
+
expect(cloned.uid).not.toBe(original.uid);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should clone model and all cloned models should be independent', () => {
|
|
226
|
+
const original = flowEngine.createModel({
|
|
227
|
+
uid: 'original-uid',
|
|
228
|
+
use: 'FlowModel',
|
|
229
|
+
props: { value: 1 },
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const cloned = original.clone();
|
|
233
|
+
|
|
234
|
+
// Modify original
|
|
235
|
+
original.setProps({ value: 2 });
|
|
236
|
+
|
|
237
|
+
// Cloned should not be affected
|
|
238
|
+
expect(cloned.props.value).toBe(1);
|
|
239
|
+
expect(original.props.value).toBe(2);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('should clone model with mixed subModels (array and object)', () => {
|
|
243
|
+
const parent = flowEngine.createModel({
|
|
244
|
+
uid: 'parent-uid',
|
|
245
|
+
use: 'FlowModel',
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// Add array-type subModels
|
|
249
|
+
parent.addSubModel('items', { use: 'FlowModel', props: { type: 'item1' } });
|
|
250
|
+
parent.addSubModel('items', { use: 'FlowModel', props: { type: 'item2' } });
|
|
251
|
+
|
|
252
|
+
// Add object-type subModel
|
|
253
|
+
parent.setSubModel('header', { use: 'FlowModel', props: { type: 'header' } });
|
|
254
|
+
parent.setSubModel('footer', { use: 'FlowModel', props: { type: 'footer' } });
|
|
255
|
+
|
|
256
|
+
const cloned = parent.clone();
|
|
257
|
+
|
|
258
|
+
// Check array-type subModels
|
|
259
|
+
const clonedItems = cloned.subModels['items'] as FlowModel[];
|
|
260
|
+
expect(clonedItems).toHaveLength(2);
|
|
261
|
+
expect(clonedItems[0].props.type).toBe('item1');
|
|
262
|
+
expect(clonedItems[1].props.type).toBe('item2');
|
|
263
|
+
|
|
264
|
+
// Check object-type subModels
|
|
265
|
+
const clonedHeader = cloned.subModels['header'] as FlowModel;
|
|
266
|
+
const clonedFooter = cloned.subModels['footer'] as FlowModel;
|
|
267
|
+
expect(clonedHeader.props.type).toBe('header');
|
|
268
|
+
expect(clonedFooter.props.type).toBe('footer');
|
|
269
|
+
|
|
270
|
+
// All uids should be unique
|
|
271
|
+
const originalItems = parent.subModels['items'] as FlowModel[];
|
|
272
|
+
expect(clonedItems[0].uid).not.toBe(originalItems[0].uid);
|
|
273
|
+
expect(clonedItems[1].uid).not.toBe(originalItems[1].uid);
|
|
274
|
+
expect(clonedHeader.uid).not.toBe((parent.subModels['header'] as FlowModel).uid);
|
|
275
|
+
expect(clonedFooter.uid).not.toBe((parent.subModels['footer'] as FlowModel).uid);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('should correctly remap parentId for subModels to new parent uid', () => {
|
|
279
|
+
const parent = flowEngine.createModel({
|
|
280
|
+
uid: 'parent-uid',
|
|
281
|
+
use: 'FlowModel',
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const child = parent.addSubModel('children', {
|
|
285
|
+
use: 'FlowModel',
|
|
286
|
+
props: { name: 'child' },
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
// Verify original relationship
|
|
290
|
+
expect(child.parentId).toBe(parent.uid);
|
|
291
|
+
|
|
292
|
+
const cloned = parent.clone();
|
|
293
|
+
const clonedChild = (cloned.subModels['children'] as FlowModel[])[0];
|
|
294
|
+
|
|
295
|
+
// Root model should not have parentId
|
|
296
|
+
expect(cloned.parentId).toBeUndefined();
|
|
297
|
+
|
|
298
|
+
// Child's parentId should be remapped to cloned parent's uid
|
|
299
|
+
expect(clonedChild.parentId).toBe(cloned.uid);
|
|
300
|
+
expect(clonedChild.parentId).not.toBe(parent.uid);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('should correctly remap parentId in deeply nested subModels', () => {
|
|
304
|
+
const root = flowEngine.createModel({
|
|
305
|
+
uid: 'root-uid',
|
|
306
|
+
use: 'FlowModel',
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const level1 = root.addSubModel('children', {
|
|
310
|
+
use: 'FlowModel',
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const level2 = level1.addSubModel('children', {
|
|
314
|
+
use: 'FlowModel',
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
const cloned = root.clone();
|
|
318
|
+
const clonedLevel1 = (cloned.subModels['children'] as FlowModel[])[0];
|
|
319
|
+
const clonedLevel2 = (clonedLevel1.subModels['children'] as FlowModel[])[0];
|
|
320
|
+
|
|
321
|
+
// Root should not have parentId
|
|
322
|
+
expect(cloned.parentId).toBeUndefined();
|
|
323
|
+
|
|
324
|
+
// Level1's parentId should point to cloned root
|
|
325
|
+
expect(clonedLevel1.parentId).toBe(cloned.uid);
|
|
326
|
+
|
|
327
|
+
// Level2's parentId should point to cloned level1
|
|
328
|
+
expect(clonedLevel2.parentId).toBe(clonedLevel1.uid);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('should replace uid references in props and other fields', () => {
|
|
332
|
+
const parent = flowEngine.createModel({
|
|
333
|
+
uid: 'parent-uid',
|
|
334
|
+
use: 'FlowModel',
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const child = parent.addSubModel('children', {
|
|
338
|
+
use: 'FlowModel',
|
|
339
|
+
props: {
|
|
340
|
+
// Store a reference to parent uid in props
|
|
341
|
+
targetUid: 'parent-uid',
|
|
342
|
+
relatedIds: ['parent-uid'],
|
|
343
|
+
nested: { refId: 'parent-uid' },
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
const cloned = parent.clone();
|
|
348
|
+
const clonedChild = (cloned.subModels['children'] as FlowModel[])[0];
|
|
349
|
+
|
|
350
|
+
// Props containing old uid references should be updated to new uids
|
|
351
|
+
expect(clonedChild.props.targetUid).toBe(cloned.uid);
|
|
352
|
+
expect(clonedChild.props.relatedIds[0]).toBe(cloned.uid);
|
|
353
|
+
expect(clonedChild.props.nested.refId).toBe(cloned.uid);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('should replace uid references in stepParams', () => {
|
|
357
|
+
const parent = flowEngine.createModel({
|
|
358
|
+
uid: 'parent-uid',
|
|
359
|
+
use: 'FlowModel',
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
const child = parent.addSubModel('children', {
|
|
363
|
+
use: 'FlowModel',
|
|
364
|
+
stepParams: {
|
|
365
|
+
someFlow: {
|
|
366
|
+
someStep: {
|
|
367
|
+
targetModelUid: 'parent-uid',
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
const cloned = parent.clone();
|
|
374
|
+
const clonedChild = (cloned.subModels['children'] as FlowModel[])[0];
|
|
375
|
+
|
|
376
|
+
// stepParams containing old uid references should be updated
|
|
377
|
+
expect(clonedChild.stepParams.someFlow.someStep.targetModelUid).toBe(cloned.uid);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it('should handle self-referencing uid in props', () => {
|
|
381
|
+
const model = flowEngine.createModel({
|
|
382
|
+
uid: 'self-uid',
|
|
383
|
+
use: 'FlowModel',
|
|
384
|
+
props: {
|
|
385
|
+
selfRef: 'self-uid',
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
const cloned = model.clone();
|
|
390
|
+
|
|
391
|
+
// Self-reference should be updated to new uid
|
|
392
|
+
expect(cloned.props.selfRef).toBe(cloned.uid);
|
|
393
|
+
expect(cloned.props.selfRef).not.toBe('self-uid');
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it('should not replace strings that are not uids', () => {
|
|
397
|
+
const model = flowEngine.createModel({
|
|
398
|
+
uid: 'model-uid',
|
|
399
|
+
use: 'FlowModel',
|
|
400
|
+
props: {
|
|
401
|
+
title: 'Some Title',
|
|
402
|
+
description: 'This is a description',
|
|
403
|
+
count: 42,
|
|
404
|
+
enabled: true,
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
const cloned = model.clone();
|
|
409
|
+
|
|
410
|
+
// Non-uid strings should remain unchanged
|
|
411
|
+
expect(cloned.props.title).toBe('Some Title');
|
|
412
|
+
expect(cloned.props.description).toBe('This is a description');
|
|
413
|
+
expect(cloned.props.count).toBe(42);
|
|
414
|
+
expect(cloned.props.enabled).toBe(true);
|
|
415
|
+
});
|
|
416
|
+
});
|
package/src/models/flowModel.tsx
CHANGED
|
@@ -1444,6 +1444,87 @@ export class FlowModel<Structure extends DefaultStructure = DefaultStructure> {
|
|
|
1444
1444
|
return data;
|
|
1445
1445
|
}
|
|
1446
1446
|
|
|
1447
|
+
/**
|
|
1448
|
+
* 复制当前模型实例为一个新的实例。
|
|
1449
|
+
* 新实例及其所有子模型都会有新的 uid,且不保留 root model 的 parent 关系。
|
|
1450
|
+
* 内部所有引用旧 uid 的地方(如 parentId, parentUid 等)都会被替换为对应的新 uid。
|
|
1451
|
+
* @returns {FlowModel} 复制后的新模型实例
|
|
1452
|
+
*/
|
|
1453
|
+
clone<T extends FlowModel = this>(): T {
|
|
1454
|
+
if (!this.flowEngine) {
|
|
1455
|
+
throw new Error('FlowEngine is not set on this model. Please set flowEngine before cloning.');
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
// 序列化当前实例
|
|
1459
|
+
const serialized = this.serialize();
|
|
1460
|
+
|
|
1461
|
+
// 第一步:收集所有 uid 并建立 oldUid -> newUid 的映射
|
|
1462
|
+
const uidMap = new Map<string, string>();
|
|
1463
|
+
|
|
1464
|
+
const collectUids = (data: Record<string, any>): void => {
|
|
1465
|
+
if (data.uid && typeof data.uid === 'string') {
|
|
1466
|
+
uidMap.set(data.uid, uid());
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
// 递归处理 subModels
|
|
1470
|
+
if (data.subModels) {
|
|
1471
|
+
for (const key in data.subModels) {
|
|
1472
|
+
const subModel = data.subModels[key];
|
|
1473
|
+
if (Array.isArray(subModel)) {
|
|
1474
|
+
subModel.forEach((item) => collectUids(item));
|
|
1475
|
+
} else if (subModel && typeof subModel === 'object') {
|
|
1476
|
+
collectUids(subModel);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
};
|
|
1481
|
+
|
|
1482
|
+
collectUids(serialized);
|
|
1483
|
+
|
|
1484
|
+
// 第二步:深度遍历并替换所有 uid 引用
|
|
1485
|
+
const replaceUidReferences = (data: any, isRoot = false): any => {
|
|
1486
|
+
if (data === null || data === undefined) {
|
|
1487
|
+
return data;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
// 如果是字符串,检查是否是需要替换的 uid
|
|
1491
|
+
if (typeof data === 'string') {
|
|
1492
|
+
return uidMap.get(data) ?? data;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
// 如果是数组,递归处理每个元素
|
|
1496
|
+
if (Array.isArray(data)) {
|
|
1497
|
+
return data.map((item) => replaceUidReferences(item, false));
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
// 如果是对象,递归处理每个属性
|
|
1501
|
+
if (typeof data === 'object') {
|
|
1502
|
+
const result: Record<string, any> = {};
|
|
1503
|
+
|
|
1504
|
+
for (const key in data) {
|
|
1505
|
+
if (!Object.prototype.hasOwnProperty.call(data, key)) continue;
|
|
1506
|
+
|
|
1507
|
+
// 只删除 root model 的 parentId
|
|
1508
|
+
if (isRoot && key === 'parentId') {
|
|
1509
|
+
continue;
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
result[key] = replaceUidReferences(data[key], false);
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
return result;
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
// 其他类型(number, boolean 等)直接返回
|
|
1519
|
+
return data;
|
|
1520
|
+
};
|
|
1521
|
+
|
|
1522
|
+
const clonedData = replaceUidReferences(serialized, true);
|
|
1523
|
+
|
|
1524
|
+
// 使用 flowEngine 创建新实例
|
|
1525
|
+
return this.flowEngine.createModel<T>(clonedData as any);
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1447
1528
|
/**
|
|
1448
1529
|
* Opens the flow settings dialog for this flow model.
|
|
1449
1530
|
* @param options - Configuration options for opening flow settings, excluding the model property
|