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