@tmagic/editor 1.5.4 → 1.5.5
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/dist/tmagic-editor.js +218 -190
- package/dist/tmagic-editor.umd.cjs +218 -190
- package/package.json +7 -7
- package/src/initService.ts +264 -233
- package/src/services/dep.ts +3 -3
- package/src/utils/idle-task.ts +1 -1
package/src/initService.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { onBeforeUnmount, reactive, toRaw, watch } from 'vue';
|
|
2
2
|
import { cloneDeep } from 'lodash-es';
|
|
3
3
|
|
|
4
4
|
import type TMagicCore from '@tmagic/core';
|
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
EventOption,
|
|
9
9
|
Id,
|
|
10
10
|
MApp,
|
|
11
|
-
|
|
11
|
+
MComponent,
|
|
12
12
|
MPage,
|
|
13
13
|
MPageFragment,
|
|
14
14
|
} from '@tmagic/core';
|
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
Target,
|
|
23
23
|
} from '@tmagic/core';
|
|
24
24
|
import { ChangeRecord } from '@tmagic/form';
|
|
25
|
-
import { getNodes, isPage, isValueIncludeDataSource
|
|
25
|
+
import { getNodes, isPage, isValueIncludeDataSource } from '@tmagic/utils';
|
|
26
26
|
|
|
27
27
|
import PropsPanel from './layouts/PropsPanel.vue';
|
|
28
28
|
import { isIncludeDataSource } from './utils/editor';
|
|
@@ -106,11 +106,12 @@ export const initServiceState = (
|
|
|
106
106
|
const eventsList: Record<string, EventOption[]> = {};
|
|
107
107
|
const methodsList: Record<string, EventOption[]> = {};
|
|
108
108
|
|
|
109
|
-
eventMethodList
|
|
110
|
-
Object.keys(eventMethodList)
|
|
109
|
+
if (eventMethodList) {
|
|
110
|
+
for (const type of Object.keys(eventMethodList)) {
|
|
111
111
|
eventsList[type] = eventMethodList[type].events;
|
|
112
112
|
methodsList[type] = eventMethodList[type].methods;
|
|
113
|
-
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
114
115
|
|
|
115
116
|
eventsService.setEvents(eventsList);
|
|
116
117
|
eventsService.setMethods(methodsList);
|
|
@@ -123,10 +124,13 @@ export const initServiceState = (
|
|
|
123
124
|
watch(
|
|
124
125
|
() => props.datasourceConfigs,
|
|
125
126
|
(configs) => {
|
|
126
|
-
configs
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
127
|
+
if (!configs) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const [key, value] of Object.entries(configs)) {
|
|
132
|
+
dataSourceService.setFormConfig(key, value);
|
|
133
|
+
}
|
|
130
134
|
},
|
|
131
135
|
{
|
|
132
136
|
immediate: true,
|
|
@@ -136,10 +140,13 @@ export const initServiceState = (
|
|
|
136
140
|
watch(
|
|
137
141
|
() => props.datasourceValues,
|
|
138
142
|
(values) => {
|
|
139
|
-
values
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
+
if (!values) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (const [key, value] of Object.entries(values)) {
|
|
148
|
+
dataSourceService.setFormValue(key, value);
|
|
149
|
+
}
|
|
143
150
|
},
|
|
144
151
|
{
|
|
145
152
|
immediate: true,
|
|
@@ -152,18 +159,20 @@ export const initServiceState = (
|
|
|
152
159
|
const eventsList: Record<string, EventOption[]> = {};
|
|
153
160
|
const methodsList: Record<string, EventOption[]> = {};
|
|
154
161
|
|
|
155
|
-
eventMethodList
|
|
156
|
-
Object.keys(eventMethodList)
|
|
162
|
+
if (eventMethodList) {
|
|
163
|
+
for (const type of Object.keys(eventMethodList)) {
|
|
157
164
|
eventsList[type] = eventMethodList[type].events;
|
|
158
165
|
methodsList[type] = eventMethodList[type].methods;
|
|
159
|
-
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
160
168
|
|
|
161
|
-
|
|
169
|
+
for (const [key, value] of Object.entries(eventsList)) {
|
|
162
170
|
dataSourceService.setFormEvent(key, value);
|
|
163
|
-
}
|
|
164
|
-
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
for (const [key, value] of Object.entries(methodsList)) {
|
|
165
174
|
dataSourceService.setFormMethod(key, value);
|
|
166
|
-
}
|
|
175
|
+
}
|
|
167
176
|
},
|
|
168
177
|
{
|
|
169
178
|
immediate: true,
|
|
@@ -203,74 +212,8 @@ export const initServiceEvents = (
|
|
|
203
212
|
((event: 'update:modelValue', value: MApp | null) => void),
|
|
204
213
|
{ editorService, codeBlockService, dataSourceService, depService }: Services,
|
|
205
214
|
) => {
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
value.codeBlocks = value.codeBlocks || {};
|
|
210
|
-
value.dataSources = value.dataSources || [];
|
|
211
|
-
|
|
212
|
-
codeBlockService.setCodeDsl(value.codeBlocks);
|
|
213
|
-
dataSourceService.set('dataSources', value.dataSources);
|
|
214
|
-
|
|
215
|
-
depService.removeTargets(DepTargetType.CODE_BLOCK);
|
|
216
|
-
|
|
217
|
-
Object.entries(value.codeBlocks).forEach(([id, code]) => {
|
|
218
|
-
depService.addTarget(createCodeBlockTarget(id, code));
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
dataSourceService.get('dataSources').forEach((ds) => {
|
|
222
|
-
initDataSourceDepTarget(ds);
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
if (Array.isArray(value.items)) {
|
|
226
|
-
depService.clearIdleTasks();
|
|
227
|
-
collectIdle(value.items, true);
|
|
228
|
-
} else {
|
|
229
|
-
depService.clear();
|
|
230
|
-
delete value.dataSourceDeps;
|
|
231
|
-
delete value.dataSourceCondDeps;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const nodeId = editorService.get('node')?.id || props.defaultSelected;
|
|
235
|
-
let node;
|
|
236
|
-
if (nodeId) {
|
|
237
|
-
node = editorService.getNodeById(nodeId);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (node && node !== value) {
|
|
241
|
-
await editorService.select(node.id);
|
|
242
|
-
} else if (value.items?.length) {
|
|
243
|
-
await editorService.select(value.items[0]);
|
|
244
|
-
} else if (value.id) {
|
|
245
|
-
editorService.set('nodes', [value]);
|
|
246
|
-
editorService.set('parent', null);
|
|
247
|
-
editorService.set('page', null);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
if (toRaw(value) !== toRaw(preValue)) {
|
|
251
|
-
emit('update:modelValue', value);
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
const stage = computed(() => editorService.get('stage'));
|
|
256
|
-
|
|
257
|
-
watch(stage, (stage) => {
|
|
258
|
-
if (!stage) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
stage.on('rerender', () => {
|
|
263
|
-
const node = editorService.get('node');
|
|
264
|
-
|
|
265
|
-
if (!node) return;
|
|
266
|
-
|
|
267
|
-
collectIdle([node], true);
|
|
268
|
-
updateStage([node]);
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
const getApp = () => {
|
|
273
|
-
const renderer = stage.value?.renderer;
|
|
215
|
+
const getTMagicApp = () => {
|
|
216
|
+
const renderer = editorService.get('stage')?.renderer;
|
|
274
217
|
if (!renderer) {
|
|
275
218
|
return undefined;
|
|
276
219
|
}
|
|
@@ -280,6 +223,7 @@ export const initServiceEvents = (
|
|
|
280
223
|
}
|
|
281
224
|
|
|
282
225
|
return new Promise<TMagicCore | undefined>((resolve) => {
|
|
226
|
+
// 设置 10s 超时
|
|
283
227
|
const timeout = globalThis.setTimeout(() => {
|
|
284
228
|
resolve(undefined);
|
|
285
229
|
}, 10000);
|
|
@@ -293,127 +237,50 @@ export const initServiceEvents = (
|
|
|
293
237
|
});
|
|
294
238
|
};
|
|
295
239
|
|
|
296
|
-
const
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
if (root && app?.dsl) {
|
|
301
|
-
app.dsl.dataSourceDeps = root.dataSourceDeps;
|
|
302
|
-
app.dsl.dataSources = root.dataSources;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
if (root?.dataSources) {
|
|
306
|
-
app?.dataSourceManager?.updateSchema(root.dataSources);
|
|
240
|
+
const updateStageNodes = (nodes: MComponent[]) => {
|
|
241
|
+
for (const node of nodes) {
|
|
242
|
+
updateStageNode(node);
|
|
307
243
|
}
|
|
308
244
|
};
|
|
309
245
|
|
|
310
|
-
const
|
|
246
|
+
const updateStageNode = (node: MComponent) => {
|
|
311
247
|
const root = editorService.get('root');
|
|
312
248
|
if (!root) return;
|
|
313
249
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (target.type === DepTargetType.DATA_SOURCE_COND) {
|
|
322
|
-
if (!root.dataSourceCondDeps) {
|
|
323
|
-
root.dataSourceCondDeps = {};
|
|
324
|
-
}
|
|
325
|
-
root.dataSourceCondDeps[target.id] = target.deps;
|
|
326
|
-
}
|
|
250
|
+
return editorService.get('stage')?.update({
|
|
251
|
+
config: cloneDeep(node),
|
|
252
|
+
parentId: editorService.getParentById(node.id)?.id,
|
|
253
|
+
root: cloneDeep(root),
|
|
254
|
+
});
|
|
327
255
|
};
|
|
328
256
|
|
|
329
|
-
const
|
|
257
|
+
const updateDataSourceSchema = async () => {
|
|
330
258
|
const root = editorService.get('root');
|
|
259
|
+
const app = await getTMagicApp();
|
|
331
260
|
|
|
332
|
-
if (!root)
|
|
261
|
+
if (!app || !root) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
333
264
|
|
|
334
|
-
if (
|
|
335
|
-
|
|
265
|
+
if (app.dsl) {
|
|
266
|
+
app.dsl.dataSources = root.dataSources;
|
|
336
267
|
}
|
|
337
268
|
|
|
338
|
-
if (root.
|
|
339
|
-
|
|
269
|
+
if (root.dataSources) {
|
|
270
|
+
app.dataSourceManager?.updateSchema(root.dataSources);
|
|
340
271
|
}
|
|
341
272
|
};
|
|
342
273
|
|
|
343
|
-
|
|
344
|
-
* 修改dsl后会执行依赖收集并调用此方法
|
|
345
|
-
* 所以这个方法是会被执行两次,当时updateStage应当时一次,所以通过inDeps参数来区分调用时机
|
|
346
|
-
* inDeps为true是则更新依赖收集到的节点,否则则更新没有依赖的节点
|
|
347
|
-
* @param nodes 需要更新的节点
|
|
348
|
-
* @param inDeps 是否依赖收集完成事件中执行的
|
|
349
|
-
* @returns
|
|
350
|
-
*/
|
|
351
|
-
const updateStage = (nodes: MNode[], inDeps = false) => {
|
|
274
|
+
const dsDepCollectedHandler = async () => {
|
|
352
275
|
const root = editorService.get('root');
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
const update = (node: MNode) =>
|
|
356
|
-
stage.value?.update({
|
|
357
|
-
config: cloneDeep(node),
|
|
358
|
-
parentId: editorService.getParentById(node.id)?.id,
|
|
359
|
-
root: cloneDeep(root),
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
nodes.forEach((node) => {
|
|
363
|
-
const inDepsNodeId: Id[] = [];
|
|
364
|
-
const deps = Object.values(root.dataSourceDeps || {});
|
|
365
|
-
deps.forEach((dep) => {
|
|
366
|
-
Object.keys(dep).forEach((id) => {
|
|
367
|
-
inDepsNodeId.push(id);
|
|
368
|
-
});
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
if (inDeps) {
|
|
372
|
-
if (inDepsNodeId.includes(node.id)) {
|
|
373
|
-
update(node);
|
|
374
|
-
}
|
|
375
|
-
} else {
|
|
376
|
-
if (!inDepsNodeId.includes(node.id)) {
|
|
377
|
-
update(node);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
});
|
|
381
|
-
};
|
|
276
|
+
const app = await getTMagicApp();
|
|
382
277
|
|
|
383
|
-
|
|
384
|
-
const root = editorService.get('root');
|
|
385
|
-
if (!root) return;
|
|
386
|
-
const app = await getApp();
|
|
387
|
-
if (app?.dsl) {
|
|
278
|
+
if (root && app?.dsl) {
|
|
388
279
|
app.dsl.dataSourceDeps = root.dataSourceDeps;
|
|
389
280
|
}
|
|
390
|
-
if (deep) {
|
|
391
|
-
nodes.forEach((node) => {
|
|
392
|
-
traverseNode<MNode>(
|
|
393
|
-
node,
|
|
394
|
-
(node) => {
|
|
395
|
-
updateStage([node], true);
|
|
396
|
-
},
|
|
397
|
-
[],
|
|
398
|
-
true,
|
|
399
|
-
);
|
|
400
|
-
});
|
|
401
|
-
} else {
|
|
402
|
-
updateStage(nodes, true);
|
|
403
|
-
}
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
depService.on('add-target', targetAddHandler);
|
|
407
|
-
depService.on('remove-target', targetRemoveHandler);
|
|
408
|
-
depService.on('ds-collected', dsDepCollectedHandler);
|
|
409
|
-
|
|
410
|
-
const initDataSourceDepTarget = (ds: DataSourceSchema) => {
|
|
411
|
-
depService.addTarget(createDataSourceTarget(ds, reactive({})));
|
|
412
|
-
depService.addTarget(createDataSourceMethodTarget(ds, reactive({})));
|
|
413
|
-
depService.addTarget(createDataSourceCondTarget(ds, reactive({})));
|
|
414
281
|
};
|
|
415
282
|
|
|
416
|
-
const collectIdle = (nodes:
|
|
283
|
+
const collectIdle = (nodes: MComponent[], deep: boolean, type?: DepTargetType) =>
|
|
417
284
|
Promise.all(
|
|
418
285
|
nodes.map((node) => {
|
|
419
286
|
let pageId: Id | undefined;
|
|
@@ -424,63 +291,163 @@ export const initServiceEvents = (
|
|
|
424
291
|
const info = editorService.getNodeInfo(node.id);
|
|
425
292
|
pageId = info.page?.id;
|
|
426
293
|
}
|
|
427
|
-
return depService.collectIdle([node], { pageId }, deep);
|
|
294
|
+
return depService.collectIdle([node], { pageId }, deep, type);
|
|
428
295
|
}),
|
|
429
296
|
);
|
|
430
297
|
|
|
298
|
+
watch(
|
|
299
|
+
() => editorService.get('stage'),
|
|
300
|
+
(stage) => {
|
|
301
|
+
if (!stage) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
stage.on('rerender', async () => {
|
|
306
|
+
const node = editorService.get('node');
|
|
307
|
+
|
|
308
|
+
if (!node) return;
|
|
309
|
+
|
|
310
|
+
await collectIdle([node], true, DepTargetType.DATA_SOURCE);
|
|
311
|
+
updateStageNode(node);
|
|
312
|
+
});
|
|
313
|
+
},
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
const initDataSourceDepTarget = (ds: DataSourceSchema) => {
|
|
317
|
+
depService.addTarget(createDataSourceTarget(ds, reactive({})));
|
|
318
|
+
depService.addTarget(createDataSourceMethodTarget(ds, reactive({})));
|
|
319
|
+
depService.addTarget(createDataSourceCondTarget(ds, reactive({})));
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const rootChangeHandler = async (value: MApp | null, preValue?: MApp | null) => {
|
|
323
|
+
if (!value) return;
|
|
324
|
+
|
|
325
|
+
value.codeBlocks = value.codeBlocks || {};
|
|
326
|
+
value.dataSources = value.dataSources || [];
|
|
327
|
+
|
|
328
|
+
codeBlockService.setCodeDsl(value.codeBlocks);
|
|
329
|
+
dataSourceService.set('dataSources', value.dataSources);
|
|
330
|
+
|
|
331
|
+
depService.removeTargets(DepTargetType.CODE_BLOCK);
|
|
332
|
+
|
|
333
|
+
for (const [id, code] of Object.entries(value.codeBlocks)) {
|
|
334
|
+
depService.addTarget(createCodeBlockTarget(id, code));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
for (const ds of dataSourceService.get('dataSources')) {
|
|
338
|
+
initDataSourceDepTarget(ds);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (Array.isArray(value.items)) {
|
|
342
|
+
depService.clearIdleTasks();
|
|
343
|
+
collectIdle(value.items, true).then(() => {
|
|
344
|
+
updateStageNodes(value.items);
|
|
345
|
+
});
|
|
346
|
+
} else {
|
|
347
|
+
depService.clear();
|
|
348
|
+
delete value.dataSourceDeps;
|
|
349
|
+
delete value.dataSourceCondDeps;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const nodeId = editorService.get('node')?.id || props.defaultSelected;
|
|
353
|
+
let node;
|
|
354
|
+
if (nodeId) {
|
|
355
|
+
node = editorService.getNodeById(nodeId);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (node && node !== value) {
|
|
359
|
+
await editorService.select(node.id);
|
|
360
|
+
} else if (value.items?.length) {
|
|
361
|
+
await editorService.select(value.items[0]);
|
|
362
|
+
} else if (value.id) {
|
|
363
|
+
editorService.set('nodes', [value]);
|
|
364
|
+
editorService.set('parent', null);
|
|
365
|
+
editorService.set('page', null);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (toRaw(value) !== toRaw(preValue)) {
|
|
369
|
+
emit('update:modelValue', value);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
|
|
431
373
|
// 新增节点,收集依赖
|
|
432
|
-
const nodeAddHandler = (nodes:
|
|
433
|
-
collectIdle(nodes, true);
|
|
374
|
+
const nodeAddHandler = async (nodes: MComponent[]) => {
|
|
375
|
+
await collectIdle(nodes, true);
|
|
434
376
|
|
|
435
|
-
|
|
377
|
+
updateStageNodes(nodes);
|
|
436
378
|
};
|
|
437
379
|
|
|
438
380
|
// 节点更新,收集依赖
|
|
439
381
|
// 仅当修改到数据源相关的才收集
|
|
440
|
-
const nodeUpdateHandler = (
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
382
|
+
const nodeUpdateHandler = async (
|
|
383
|
+
data: { newNode: MComponent; oldNode: MComponent; changeRecords?: ChangeRecord[] }[],
|
|
384
|
+
) => {
|
|
385
|
+
const needRecollectNodes: MComponent[] = [];
|
|
386
|
+
const normalNodes: MComponent[] = [];
|
|
387
|
+
for (const { newNode, oldNode, changeRecords } of data) {
|
|
444
388
|
if (changeRecords?.length) {
|
|
445
|
-
|
|
389
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
390
|
+
forChangeRecords: for (const record of changeRecords) {
|
|
391
|
+
if (!record.propPath) {
|
|
392
|
+
needRecollectNodes.push(newNode);
|
|
393
|
+
break forChangeRecords;
|
|
394
|
+
}
|
|
395
|
+
|
|
446
396
|
// NODE_CONDS_KEY为显示条件key
|
|
447
397
|
if (
|
|
448
|
-
!record.propPath ||
|
|
449
398
|
new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond`).test(record.propPath) ||
|
|
450
399
|
new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond.(\\d)+.value`).test(record.propPath) ||
|
|
451
400
|
record.propPath === NODE_CONDS_KEY ||
|
|
452
401
|
isValueIncludeDataSource(record.value)
|
|
453
402
|
) {
|
|
454
403
|
needRecollectNodes.push(newNode);
|
|
455
|
-
|
|
456
|
-
normalNodes.push(newNode);
|
|
404
|
+
break forChangeRecords;
|
|
457
405
|
}
|
|
406
|
+
|
|
407
|
+
// 修改的key在收集的依赖中,则需要触发重新收集
|
|
408
|
+
for (const target of Object.values(depService.getTargets(DepTargetType.DATA_SOURCE))) {
|
|
409
|
+
if (!target.deps[newNode.id]) {
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
if (target.deps[newNode.id].keys.includes(record.propPath)) {
|
|
413
|
+
needRecollectNodes.push(newNode);
|
|
414
|
+
break forChangeRecords;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
normalNodes.push(newNode);
|
|
458
419
|
}
|
|
459
420
|
} else if (isIncludeDataSource(newNode, oldNode)) {
|
|
460
421
|
needRecollectNodes.push(newNode);
|
|
461
422
|
} else {
|
|
462
423
|
normalNodes.push(newNode);
|
|
463
424
|
}
|
|
464
|
-
}
|
|
425
|
+
}
|
|
465
426
|
|
|
466
427
|
if (needRecollectNodes.length) {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
428
|
+
// 有数据源依赖,需要等依赖重新收集完才更新stage
|
|
429
|
+
await collectIdle(needRecollectNodes, true, DepTargetType.DATA_SOURCE);
|
|
430
|
+
await collectIdle(needRecollectNodes, true, DepTargetType.DATA_SOURCE_COND);
|
|
431
|
+
updateStageNodes(needRecollectNodes);
|
|
432
|
+
} else {
|
|
433
|
+
updateStageNodes(normalNodes);
|
|
434
|
+
// 在上面判断是否需要收集数据源依赖中已经更新stage
|
|
435
|
+
Promise.all([
|
|
436
|
+
collectIdle(normalNodes, true, DepTargetType.CODE_BLOCK),
|
|
437
|
+
collectIdle(normalNodes, true, DepTargetType.DATA_SOURCE_METHOD),
|
|
438
|
+
]);
|
|
471
439
|
}
|
|
472
440
|
};
|
|
473
441
|
|
|
474
442
|
// 节点删除,清除对齐的依赖收集
|
|
475
|
-
const nodeRemoveHandler = (nodes:
|
|
443
|
+
const nodeRemoveHandler = (nodes: MComponent[]) => {
|
|
476
444
|
depService.clear(nodes);
|
|
477
445
|
};
|
|
478
446
|
|
|
479
447
|
// 由于历史记录变化是更新整个page,所以历史记录变化时,需要重新收集依赖
|
|
480
|
-
const historyChangeHandler = (page: MPage | MPageFragment) => {
|
|
481
|
-
collectIdle([page], true)
|
|
482
|
-
|
|
483
|
-
});
|
|
448
|
+
const historyChangeHandler = async (page: MPage | MPageFragment) => {
|
|
449
|
+
await collectIdle([page], true);
|
|
450
|
+
updateStageNode(page);
|
|
484
451
|
};
|
|
485
452
|
|
|
486
453
|
editorService.on('history-change', historyChangeHandler);
|
|
@@ -489,25 +456,9 @@ export const initServiceEvents = (
|
|
|
489
456
|
editorService.on('remove', nodeRemoveHandler);
|
|
490
457
|
editorService.on('update', nodeUpdateHandler);
|
|
491
458
|
|
|
492
|
-
const codeBlockAddOrUpdateHandler = (id: Id, codeBlock: CodeBlockContent) => {
|
|
493
|
-
if (depService.hasTarget(id, DepTargetType.CODE_BLOCK)) {
|
|
494
|
-
depService.getTarget(id, DepTargetType.CODE_BLOCK)!.name = codeBlock.name;
|
|
495
|
-
return;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
depService.addTarget(createCodeBlockTarget(id, codeBlock));
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
const codeBlockRemoveHandler = (id: Id) => {
|
|
502
|
-
depService.removeTarget(id, DepTargetType.CODE_BLOCK);
|
|
503
|
-
};
|
|
504
|
-
|
|
505
|
-
codeBlockService.on('addOrUpdate', codeBlockAddOrUpdateHandler);
|
|
506
|
-
codeBlockService.on('remove', codeBlockRemoveHandler);
|
|
507
|
-
|
|
508
459
|
const dataSourceAddHandler = async (config: DataSourceSchema) => {
|
|
509
460
|
initDataSourceDepTarget(config);
|
|
510
|
-
const app = await
|
|
461
|
+
const app = await getTMagicApp();
|
|
511
462
|
app?.dataSourceManager?.addDataSource(config);
|
|
512
463
|
};
|
|
513
464
|
|
|
@@ -516,19 +467,28 @@ export const initServiceEvents = (
|
|
|
516
467
|
{ changeRecords }: { changeRecords: ChangeRecord[] },
|
|
517
468
|
) => {
|
|
518
469
|
let needRecollectDep = false;
|
|
470
|
+
let isModifyField = false;
|
|
471
|
+
let isModifyMock = false;
|
|
472
|
+
let isModifyMethod = false;
|
|
519
473
|
for (const changeRecord of changeRecords) {
|
|
520
474
|
if (!changeRecord.propPath) {
|
|
521
475
|
continue;
|
|
522
476
|
}
|
|
523
477
|
|
|
524
|
-
|
|
478
|
+
isModifyField =
|
|
525
479
|
changeRecord.propPath === 'fields' ||
|
|
526
|
-
changeRecord.propPath === 'methods' ||
|
|
527
480
|
/fields.(\d)+.name/.test(changeRecord.propPath) ||
|
|
528
|
-
/fields.(\d)+$/.test(changeRecord.propPath)
|
|
481
|
+
/fields.(\d)+$/.test(changeRecord.propPath);
|
|
482
|
+
|
|
483
|
+
isModifyMock = changeRecord.propPath === 'mocks';
|
|
484
|
+
|
|
485
|
+
isModifyMethod =
|
|
486
|
+
changeRecord.propPath === 'methods' ||
|
|
529
487
|
/methods.(\d)+.name/.test(changeRecord.propPath) ||
|
|
530
488
|
/methods.(\d)+$/.test(changeRecord.propPath);
|
|
531
489
|
|
|
490
|
+
needRecollectDep = isModifyField || isModifyMock || isModifyMethod;
|
|
491
|
+
|
|
532
492
|
if (needRecollectDep) {
|
|
533
493
|
break;
|
|
534
494
|
}
|
|
@@ -542,12 +502,24 @@ export const initServiceEvents = (
|
|
|
542
502
|
removeDataSourceTarget(config.id);
|
|
543
503
|
initDataSourceDepTarget(config);
|
|
544
504
|
|
|
545
|
-
|
|
505
|
+
let collectIdlePromises: Promise<void[]>[] = [];
|
|
506
|
+
if (isModifyField) {
|
|
507
|
+
collectIdlePromises = [
|
|
508
|
+
collectIdle(root.items, true, DepTargetType.DATA_SOURCE),
|
|
509
|
+
collectIdle(root.items, true, DepTargetType.DATA_SOURCE_COND),
|
|
510
|
+
];
|
|
511
|
+
} else if (isModifyMock) {
|
|
512
|
+
collectIdlePromises = [collectIdle(root.items, true, DepTargetType.DATA_SOURCE)];
|
|
513
|
+
} else if (isModifyMethod) {
|
|
514
|
+
collectIdlePromises = [collectIdle(root.items, true, DepTargetType.DATA_SOURCE_METHOD)];
|
|
515
|
+
}
|
|
516
|
+
Promise.all(collectIdlePromises).then(() => {
|
|
546
517
|
updateDataSourceSchema();
|
|
518
|
+
updateStageNodes(root.items);
|
|
547
519
|
});
|
|
548
520
|
}
|
|
549
521
|
} else if (root?.dataSources) {
|
|
550
|
-
const app = await
|
|
522
|
+
const app = await getTMagicApp();
|
|
551
523
|
app?.dataSourceManager?.updateSchema(root.dataSources);
|
|
552
524
|
}
|
|
553
525
|
};
|
|
@@ -562,8 +534,14 @@ export const initServiceEvents = (
|
|
|
562
534
|
const root = editorService.get('root');
|
|
563
535
|
const nodeIds = Object.keys(root?.dataSourceDeps?.[id] || {});
|
|
564
536
|
const nodes = getNodes(nodeIds, root?.items);
|
|
565
|
-
|
|
537
|
+
|
|
538
|
+
Promise.all([
|
|
539
|
+
collectIdle(nodes, false, DepTargetType.DATA_SOURCE),
|
|
540
|
+
collectIdle(nodes, false, DepTargetType.DATA_SOURCE_COND),
|
|
541
|
+
collectIdle(nodes, false, DepTargetType.DATA_SOURCE_METHOD),
|
|
542
|
+
]).then(() => {
|
|
566
543
|
updateDataSourceSchema();
|
|
544
|
+
updateStageNodes(nodes);
|
|
567
545
|
});
|
|
568
546
|
|
|
569
547
|
removeDataSourceTarget(id);
|
|
@@ -573,6 +551,59 @@ export const initServiceEvents = (
|
|
|
573
551
|
dataSourceService.on('update', dataSourceUpdateHandler);
|
|
574
552
|
dataSourceService.on('remove', dataSourceRemoveHandler);
|
|
575
553
|
|
|
554
|
+
const codeBlockAddOrUpdateHandler = (id: Id, codeBlock: CodeBlockContent) => {
|
|
555
|
+
if (depService.hasTarget(id, DepTargetType.CODE_BLOCK)) {
|
|
556
|
+
depService.getTarget(id, DepTargetType.CODE_BLOCK)!.name = codeBlock.name;
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
depService.addTarget(createCodeBlockTarget(id, codeBlock));
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
const codeBlockRemoveHandler = (id: Id) => {
|
|
564
|
+
depService.removeTarget(id, DepTargetType.CODE_BLOCK);
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
codeBlockService.on('addOrUpdate', codeBlockAddOrUpdateHandler);
|
|
568
|
+
codeBlockService.on('remove', codeBlockRemoveHandler);
|
|
569
|
+
|
|
570
|
+
const targetAddHandler = (target: Target) => {
|
|
571
|
+
const root = editorService.get('root');
|
|
572
|
+
if (!root) return;
|
|
573
|
+
|
|
574
|
+
if (target.type === DepTargetType.DATA_SOURCE) {
|
|
575
|
+
if (!root.dataSourceDeps) {
|
|
576
|
+
root.dataSourceDeps = {};
|
|
577
|
+
}
|
|
578
|
+
root.dataSourceDeps[target.id] = target.deps;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (target.type === DepTargetType.DATA_SOURCE_COND) {
|
|
582
|
+
if (!root.dataSourceCondDeps) {
|
|
583
|
+
root.dataSourceCondDeps = {};
|
|
584
|
+
}
|
|
585
|
+
root.dataSourceCondDeps[target.id] = target.deps;
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const targetRemoveHandler = (id: string | number) => {
|
|
590
|
+
const root = editorService.get('root');
|
|
591
|
+
|
|
592
|
+
if (!root) return;
|
|
593
|
+
|
|
594
|
+
if (root.dataSourceDeps) {
|
|
595
|
+
delete root.dataSourceDeps[id];
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (root.dataSourceCondDeps) {
|
|
599
|
+
delete root.dataSourceCondDeps[id];
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
depService.on('add-target', targetAddHandler);
|
|
604
|
+
depService.on('remove-target', targetRemoveHandler);
|
|
605
|
+
depService.on('ds-collected', dsDepCollectedHandler);
|
|
606
|
+
|
|
576
607
|
onBeforeUnmount(() => {
|
|
577
608
|
depService.off('add-target', targetAddHandler);
|
|
578
609
|
depService.off('remove-target', targetRemoveHandler);
|