@tmagic/editor 1.8.2 → 1.8.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.
- package/dist/es/Editor.vue_vue_type_script_setup_true_lang.js +16 -24
- package/dist/es/fields/CondOpSelect.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceFieldSelect/Index.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceInput.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/configs.js +1 -1
- package/dist/es/fields/configs/codeSelect.js +5 -7
- package/dist/es/fields/configs/displayConds.js +8 -9
- package/dist/es/fields/configs/eventSelect.js +30 -27
- package/dist/es/hooks/use-stage.js +18 -18
- package/dist/es/index.js +9 -16
- package/dist/es/initService.js +33 -106
- package/dist/es/layouts/Framework.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/props-panel/PropsPanel.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/layouts/props-panel/use-style-panel.js +1 -1
- package/dist/es/sdk/index.js +151 -0
- package/dist/es/{services → sdk/services}/BaseService.js +1 -1
- package/dist/es/{services/codeBlock.js → sdk/services/CodeBlock.js} +37 -40
- package/dist/es/{services/componentList.js → sdk/services/ComponentList.js} +5 -6
- package/dist/es/{services/dataSource.js → sdk/services/DataSource.js} +38 -37
- package/dist/es/{services/dep.js → sdk/services/Dep.js} +24 -14
- package/dist/es/{services/editor.js → sdk/services/Editor.js} +124 -87
- package/dist/es/{services/events.js → sdk/services/Events.js} +13 -13
- package/dist/es/{services/history.js → sdk/services/History.js} +86 -33
- package/dist/es/{services/props.js → sdk/services/Props.js} +25 -26
- package/dist/es/{services/storage.js → sdk/services/WebStorage.js} +31 -8
- package/dist/es/sdk/utils/dep.js +100 -0
- package/dist/es/sdk/utils/operator.js +70 -0
- package/dist/es/sdk/utils/position.js +8 -0
- package/dist/es/sdk/utils/props.js +281 -0
- package/dist/es/sdk/validate/index.js +286 -0
- package/dist/es/sdk/wiring.js +163 -0
- package/dist/es/sdk.js +15 -0
- package/dist/es/services/keybinding.js +21 -21
- package/dist/es/services/sdk-instance.js +37 -0
- package/dist/es/services/stageOverlay.js +14 -14
- package/dist/es/services/ui.js +6 -6
- package/dist/es/utils/data-source/formConfigs/base.js +1 -1
- package/dist/es/utils/data-source/formConfigs/http.js +1 -1
- package/dist/es/utils/editor.js +2 -2
- package/dist/es/utils/event.js +25 -21
- package/dist/es/utils/operator.js +11 -57
- package/dist/es/utils/props.js +12 -282
- package/dist/es/utils/type-match-rules.js +10 -12
- package/dist/tmagic-editor-headless.umd.cjs +4690 -2891
- package/dist/tmagic-editor-sdk.umd.cjs +9911 -0
- package/dist/tmagic-editor.umd.cjs +14126 -13312
- package/package.json +12 -7
- package/src/Editor.vue +11 -9
- package/src/fields/configs/codeSelect.ts +6 -9
- package/src/fields/configs/displayConds.ts +10 -9
- package/src/fields/configs/eventSelect.ts +40 -25
- package/src/hooks/use-stage.ts +1 -1
- package/src/index.ts +11 -9
- package/src/initService.ts +59 -188
- package/src/layouts/Framework.vue +1 -1
- package/src/layouts/props-panel/PropsPanel.vue +1 -1
- package/src/layouts/props-panel/use-style-panel.ts +1 -1
- package/src/sdk/index.ts +264 -0
- package/src/{services/codeBlock.ts → sdk/services/CodeBlock.ts} +43 -38
- package/src/{services/componentList.ts → sdk/services/ComponentList.ts} +8 -7
- package/src/{services/dataSource.ts → sdk/services/DataSource.ts} +38 -32
- package/src/{services/dep.ts → sdk/services/Dep.ts} +24 -11
- package/src/{services/editor.ts → sdk/services/Editor.ts} +175 -86
- package/src/{services/events.ts → sdk/services/Events.ts} +15 -12
- package/src/{services/history.ts → sdk/services/History.ts} +100 -28
- package/src/{services/props.ts → sdk/services/Props.ts} +25 -25
- package/src/{services/storage.ts → sdk/services/WebStorage.ts} +41 -7
- package/src/sdk/type.ts +191 -0
- package/src/sdk/utils/dep.ts +196 -0
- package/src/sdk/utils/operator.ts +114 -0
- package/src/sdk/utils/position.ts +23 -0
- package/src/sdk/utils/props.ts +368 -0
- package/src/sdk/validate/index.ts +501 -0
- package/src/sdk/wiring.ts +211 -0
- package/src/sdk.ts +19 -0
- package/src/services/keybinding.ts +2 -2
- package/src/services/sdk-instance.ts +66 -0
- package/src/services/stageOverlay.ts +2 -2
- package/src/services/ui.ts +3 -3
- package/src/type.ts +9 -9
- package/src/utils/data-source/formConfigs/base.ts +1 -1
- package/src/utils/data-source/formConfigs/http.ts +1 -1
- package/src/utils/dep/collect-worker-client.ts +9 -6
- package/src/utils/dep/worker.ts +5 -0
- package/src/utils/editor.ts +2 -1
- package/src/utils/event.ts +36 -18
- package/src/utils/operator.ts +21 -87
- package/src/utils/props.ts +17 -332
- package/src/utils/type-match-rules.ts +1 -3
- package/types/index.d.ts +742 -528
- package/types/sdk.d.ts +2572 -0
- /package/src/{services → sdk/services}/BaseService.ts +0 -0
package/dist/es/initService.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { isIncludeDataSource } from "./utils/editor.js";
|
|
2
1
|
import { error } from "./utils/logger.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { addDataSourceTargets, classifyUpdatedNodes, getDataSourceRecollectTypes, removeDataSourceTargets, removeTargetDeps, resetDepTargets, writeTargetDeps } from "./sdk/utils/dep.js";
|
|
3
|
+
import { getDepNodeIds, getNodes, isPage } from "@tmagic/utils";
|
|
4
|
+
import { DepTargetType, NodeType, createCodeBlockTarget, updateNode } from "@tmagic/core";
|
|
5
5
|
import { nextTick, onBeforeUnmount, reactive, toRaw, watch } from "vue";
|
|
6
6
|
import { cloneDeep as cloneDeep$1 } from "lodash-es";
|
|
7
7
|
//#region packages/editor/src/initService.ts
|
|
@@ -66,6 +66,10 @@ var initServiceState = (props, { editorService, historyService, componentListSer
|
|
|
66
66
|
watch(() => props.stageRect, (stageRect) => stageRect && uiService.set("stageRect", stageRect), { immediate: true });
|
|
67
67
|
watch(() => props.disabledCodeBlock, (disabledCodeBlock) => propsService.setDisabledCodeBlock(disabledCodeBlock ?? false), { immediate: true });
|
|
68
68
|
watch(() => props.disabledDataSource, (disabledDataSource) => propsService.setDisabledDataSource(disabledDataSource ?? false), { immediate: true });
|
|
69
|
+
watch(() => [props.disabledPageFragment, props.canDropIn], ([disabledPageFragment, canDropIn]) => editorService.setConfig({
|
|
70
|
+
disabledPageFragment: disabledPageFragment ?? false,
|
|
71
|
+
canDropIn
|
|
72
|
+
}), { immediate: true });
|
|
69
73
|
onBeforeUnmount(() => {
|
|
70
74
|
editorService.resetState();
|
|
71
75
|
historyService.resetState();
|
|
@@ -198,20 +202,13 @@ var initServiceEvents = (props, emit, { editorService, codeBlockService, dataSou
|
|
|
198
202
|
runtime?.updateRootConfig?.(dsl);
|
|
199
203
|
}
|
|
200
204
|
};
|
|
201
|
-
const initDataSourceDepTarget = (ds) => {
|
|
202
|
-
depService.addTarget(createDataSourceTarget(ds, reactive({})));
|
|
203
|
-
depService.addTarget(createDataSourceMethodTarget(ds, reactive({})));
|
|
204
|
-
depService.addTarget(createDataSourceCondTarget(ds, reactive({})));
|
|
205
|
-
};
|
|
206
205
|
const rootChangeHandler = (value) => {
|
|
207
206
|
if (!value) return;
|
|
208
207
|
value.codeBlocks = value.codeBlocks || {};
|
|
209
208
|
value.dataSources = value.dataSources || [];
|
|
210
209
|
codeBlockService.setCodeDsl(value.codeBlocks);
|
|
211
210
|
dataSourceService.set("dataSources", value.dataSources);
|
|
212
|
-
depService.
|
|
213
|
-
for (const [id, code] of Object.entries(value.codeBlocks)) depService.addTarget(createCodeBlockTarget(id, code));
|
|
214
|
-
for (const ds of dataSourceService.get("dataSources")) initDataSourceDepTarget(ds);
|
|
211
|
+
resetDepTargets(depService, value.codeBlocks, dataSourceService.get("dataSources"), reactive);
|
|
215
212
|
if (Array.isArray(value.items)) updateStageDsl(value);
|
|
216
213
|
else {
|
|
217
214
|
depService.clear();
|
|
@@ -239,29 +236,7 @@ var initServiceEvents = (props, emit, { editorService, codeBlockService, dataSou
|
|
|
239
236
|
});
|
|
240
237
|
};
|
|
241
238
|
const nodeUpdateHandler = (data) => {
|
|
242
|
-
const needRecollectNodes =
|
|
243
|
-
const normalNodes = [];
|
|
244
|
-
for (const { newNode, oldNode, changeRecords } of data) if (newNode.type === NodeType.ROOT) normalNodes.push(newNode);
|
|
245
|
-
else if (changeRecords?.length) forChangeRecords: for (const record of changeRecords) {
|
|
246
|
-
if (!record.propPath) {
|
|
247
|
-
needRecollectNodes.push(newNode);
|
|
248
|
-
break forChangeRecords;
|
|
249
|
-
}
|
|
250
|
-
if (new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond`).test(record.propPath) || new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond.(\\d)+.value`).test(record.propPath) || record.propPath === NODE_CONDS_KEY || isValueIncludeDataSource(record.value)) {
|
|
251
|
-
needRecollectNodes.push(newNode);
|
|
252
|
-
break forChangeRecords;
|
|
253
|
-
}
|
|
254
|
-
for (const target of Object.values(depService.getTargets(DepTargetType.DATA_SOURCE))) {
|
|
255
|
-
if (!target.deps[newNode.id]) continue;
|
|
256
|
-
if (target.deps[newNode.id].keys.includes(record.propPath)) {
|
|
257
|
-
needRecollectNodes.push(newNode);
|
|
258
|
-
break forChangeRecords;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
normalNodes.push(newNode);
|
|
262
|
-
}
|
|
263
|
-
else if (isIncludeDataSource(newNode, oldNode)) needRecollectNodes.push(newNode);
|
|
264
|
-
else normalNodes.push(newNode);
|
|
239
|
+
const { needRecollectNodes, normalNodes } = classifyUpdatedNodes({ dep: depService }, data);
|
|
265
240
|
if (needRecollectNodes.length) {
|
|
266
241
|
const handler = async () => {
|
|
267
242
|
if (!await collectIdle(needRecollectNodes, true, DepTargetType.DATA_SOURCE)) return;
|
|
@@ -269,10 +244,9 @@ var initServiceEvents = (props, emit, { editorService, codeBlockService, dataSou
|
|
|
269
244
|
updateStageNodes(needRecollectNodes);
|
|
270
245
|
};
|
|
271
246
|
handler();
|
|
272
|
-
} else
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
247
|
+
} else updateStageNodes(normalNodes);
|
|
248
|
+
const updatedNodes = [...needRecollectNodes, ...normalNodes];
|
|
249
|
+
Promise.all([collectIdle(updatedNodes, true, DepTargetType.CODE_BLOCK), collectIdle(updatedNodes, true, DepTargetType.DATA_SOURCE_METHOD)]);
|
|
276
250
|
};
|
|
277
251
|
const nodeRemoveHandler = (nodes) => {
|
|
278
252
|
depService.clear(nodes);
|
|
@@ -289,7 +263,7 @@ var initServiceEvents = (props, emit, { editorService, codeBlockService, dataSou
|
|
|
289
263
|
editorService.on("update", nodeUpdateHandler);
|
|
290
264
|
const dataSourceAddHandler = (config) => {
|
|
291
265
|
const handler = async () => {
|
|
292
|
-
|
|
266
|
+
addDataSourceTargets(depService, config, reactive);
|
|
293
267
|
const app = await getTMagicApp();
|
|
294
268
|
if (!app?.dataSourceManager) return;
|
|
295
269
|
app.dataSourceManager.addDataSource(config);
|
|
@@ -307,52 +281,24 @@ var initServiceEvents = (props, emit, { editorService, codeBlockService, dataSou
|
|
|
307
281
|
ds.setFields(config.fields);
|
|
308
282
|
ds.setData(config.mocks?.find((mock) => mock.useInEditor)?.data || ds.getDefaultData());
|
|
309
283
|
};
|
|
310
|
-
|
|
311
|
-
let isModifyField = false;
|
|
312
|
-
let isModifyMock = false;
|
|
313
|
-
let isModifyMethod = false;
|
|
314
|
-
for (const changeRecord of changeRecords) {
|
|
315
|
-
if (!changeRecord.propPath) continue;
|
|
316
|
-
isModifyField = changeRecord.propPath === "fields" || /fields.(\d)+.name/.test(changeRecord.propPath) || /fields.(\d)+.defaultValue/.test(changeRecord.propPath) || /fields.(\d)+$/.test(changeRecord.propPath);
|
|
317
|
-
isModifyMock = changeRecord.propPath === "mocks";
|
|
318
|
-
isModifyMethod = changeRecord.propPath === "methods" || /methods.(\d)+.name/.test(changeRecord.propPath) || /methods.(\d)+$/.test(changeRecord.propPath);
|
|
319
|
-
needRecollectDep = isModifyField || isModifyMock || isModifyMethod;
|
|
320
|
-
if (needRecollectDep) break;
|
|
321
|
-
}
|
|
284
|
+
const types = getDataSourceRecollectTypes(changeRecords);
|
|
322
285
|
const root = editorService.get("root");
|
|
323
|
-
if (
|
|
324
|
-
if (
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
depService.addTarget(createDataSourceMethodTarget(config, reactive({})));
|
|
340
|
-
collectIdlePromises = [collectIdle(root.items, true, DepTargetType.DATA_SOURCE_METHOD)];
|
|
341
|
-
}
|
|
342
|
-
const handler = async () => {
|
|
343
|
-
if (!(await Promise.all(collectIdlePromises)).every(Boolean)) return;
|
|
344
|
-
updateDataSourceSchema();
|
|
345
|
-
await updateDsData();
|
|
346
|
-
updateStageNodes(root.items);
|
|
347
|
-
};
|
|
348
|
-
handler();
|
|
349
|
-
}
|
|
350
|
-
} else if (root?.dataSources) updateDsData();
|
|
351
|
-
};
|
|
352
|
-
const removeDataSourceTarget = (id) => {
|
|
353
|
-
depService.removeTarget(id, DepTargetType.DATA_SOURCE);
|
|
354
|
-
depService.removeTarget(id, DepTargetType.DATA_SOURCE_COND);
|
|
355
|
-
depService.removeTarget(id, DepTargetType.DATA_SOURCE_METHOD);
|
|
286
|
+
if (!types) {
|
|
287
|
+
if (root?.dataSources) updateDsData();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (!Array.isArray(root?.items)) return;
|
|
291
|
+
depService.clearIdleTasks();
|
|
292
|
+
removeDataSourceTargets(depService, config.id, types);
|
|
293
|
+
addDataSourceTargets(depService, config, reactive, types);
|
|
294
|
+
const collectIdlePromises = types.map((type) => collectIdle(root.items, true, type));
|
|
295
|
+
const handler = async () => {
|
|
296
|
+
if (!(await Promise.all(collectIdlePromises)).every(Boolean)) return;
|
|
297
|
+
updateDataSourceSchema();
|
|
298
|
+
await updateDsData();
|
|
299
|
+
updateStageNodes(root.items);
|
|
300
|
+
};
|
|
301
|
+
handler();
|
|
356
302
|
};
|
|
357
303
|
const dataSourceRemoveHandler = (id) => {
|
|
358
304
|
const root = editorService.get("root");
|
|
@@ -368,7 +314,7 @@ var initServiceEvents = (props, emit, { editorService, codeBlockService, dataSou
|
|
|
368
314
|
updateDataSourceSchema();
|
|
369
315
|
(await getTMagicApp())?.dataSourceManager?.removeDataSource(id);
|
|
370
316
|
updateStageNodes(nodes);
|
|
371
|
-
|
|
317
|
+
removeDataSourceTargets(depService, id);
|
|
372
318
|
};
|
|
373
319
|
handler();
|
|
374
320
|
};
|
|
@@ -387,27 +333,8 @@ var initServiceEvents = (props, emit, { editorService, codeBlockService, dataSou
|
|
|
387
333
|
};
|
|
388
334
|
codeBlockService.on("addOrUpdate", codeBlockAddOrUpdateHandler);
|
|
389
335
|
codeBlockService.on("remove", codeBlockRemoveHandler);
|
|
390
|
-
const targetAddHandler = (target) =>
|
|
391
|
-
|
|
392
|
-
if (!root) return;
|
|
393
|
-
if (target.type === DepTargetType.DATA_SOURCE) {
|
|
394
|
-
if (!root.dataSourceDeps) root.dataSourceDeps = {};
|
|
395
|
-
root.dataSourceDeps[target.id] = target.deps;
|
|
396
|
-
} else if (target.type === DepTargetType.DATA_SOURCE_COND) {
|
|
397
|
-
if (!root.dataSourceCondDeps) root.dataSourceCondDeps = {};
|
|
398
|
-
root.dataSourceCondDeps[target.id] = target.deps;
|
|
399
|
-
} else if (target.type === DepTargetType.DATA_SOURCE_METHOD) {
|
|
400
|
-
if (!root.dataSourceMethodDeps) root.dataSourceMethodDeps = {};
|
|
401
|
-
root.dataSourceMethodDeps[target.id] = target.deps;
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
const targetRemoveHandler = (id, type) => {
|
|
405
|
-
const root = editorService.get("root");
|
|
406
|
-
if (!root) return;
|
|
407
|
-
if (root.dataSourceDeps && type === DepTargetType.DATA_SOURCE) delete root.dataSourceDeps[id];
|
|
408
|
-
if (root.dataSourceCondDeps && type === DepTargetType.DATA_SOURCE_COND) delete root.dataSourceCondDeps[id];
|
|
409
|
-
if (root.dataSourceMethodDeps && type === DepTargetType.DATA_SOURCE_METHOD) delete root.dataSourceMethodDeps[id];
|
|
410
|
-
};
|
|
336
|
+
const targetAddHandler = (target) => writeTargetDeps(editorService.get("root"), target);
|
|
337
|
+
const targetRemoveHandler = (id, type) => removeTargetDeps(editorService.get("root"), id, type);
|
|
411
338
|
depService.on("add-target", targetAddHandler);
|
|
412
339
|
depService.on("remove-target", targetRemoveHandler);
|
|
413
340
|
depService.on("ds-collected", dsDepCollectedHandler);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getEditorConfig } from "../utils/config.js";
|
|
2
|
-
import { Protocol } from "../services/
|
|
2
|
+
import { Protocol } from "../sdk/services/WebStorage.js";
|
|
3
3
|
import { LEFT_COLUMN_WIDTH_STORAGE_KEY, RIGHT_COLUMN_WIDTH_STORAGE_KEY } from "../utils/const.js";
|
|
4
4
|
import { useServices } from "../hooks/use-services.js";
|
|
5
5
|
import SplitView_default from "../components/SplitView.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Protocol } from "../../sdk/services/WebStorage.js";
|
|
2
|
+
import { styleTabConfig } from "../../sdk/utils/props.js";
|
|
3
3
|
import { PROPS_PANEL_WIDTH_STORAGE_KEY } from "../../utils/const.js";
|
|
4
4
|
import { useServices } from "../../hooks/use-services.js";
|
|
5
5
|
import Resizer_default from "../../components/Resizer.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Protocol } from "../../services/
|
|
1
|
+
import { Protocol } from "../../sdk/services/WebStorage.js";
|
|
2
2
|
import { RIGHT_COLUMN_WIDTH_STORAGE_KEY } from "../../utils/const.js";
|
|
3
3
|
import { computed, watch } from "vue";
|
|
4
4
|
//#region packages/editor/src/layouts/props-panel/use-style-panel.ts
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import BaseService from "./services/BaseService.js";
|
|
2
|
+
import { MemoryStorage, Protocol, WebStorage } from "./services/WebStorage.js";
|
|
3
|
+
import CodeBlock from "./services/CodeBlock.js";
|
|
4
|
+
import ComponentList from "./services/ComponentList.js";
|
|
5
|
+
import DataSource from "./services/DataSource.js";
|
|
6
|
+
import Dep from "./services/Dep.js";
|
|
7
|
+
import Editor from "./services/Editor.js";
|
|
8
|
+
import Events from "./services/Events.js";
|
|
9
|
+
import History from "./services/History.js";
|
|
10
|
+
import Props from "./services/Props.js";
|
|
11
|
+
import { DslValidationError, createValidator } from "./validate/index.js";
|
|
12
|
+
import { classifyUpdatedNodes } from "./utils/dep.js";
|
|
13
|
+
import { bindWiring, whenWiringSettled } from "./wiring.js";
|
|
14
|
+
import { nextTick, reactive, shallowReactive, toRaw } from "vue";
|
|
15
|
+
import { cloneDeep, isEmpty } from "lodash-es";
|
|
16
|
+
//#region packages/editor/src/sdk/index.ts
|
|
17
|
+
/**
|
|
18
|
+
* @fileoverview `@tmagic/editor/sdk`:不依赖 DOM 的 DSL 操作 SDK。
|
|
19
|
+
*
|
|
20
|
+
* 每次调用 {@link createDslSdk} 得到一组独立的 service(editor / history / props / dataSource /
|
|
21
|
+
* codeBlock / dep / storage / events / componentList),可在 Node 中运行。浏览器端的
|
|
22
|
+
* `editorService` 等单例就是用同一个工厂创建的默认实例。
|
|
23
|
+
*
|
|
24
|
+
* @module @tmagic/editor/sdk
|
|
25
|
+
*/
|
|
26
|
+
var SERVICE_NAMES = [
|
|
27
|
+
"editor",
|
|
28
|
+
"props",
|
|
29
|
+
"history",
|
|
30
|
+
"dataSource",
|
|
31
|
+
"codeBlock",
|
|
32
|
+
"dep",
|
|
33
|
+
"storage",
|
|
34
|
+
"events",
|
|
35
|
+
"componentList"
|
|
36
|
+
];
|
|
37
|
+
/**
|
|
38
|
+
* Node 下默认的 DSL 反序列化:序列化出来的内容是被括号包起来的 JSON(函数、正则等需要自己传入 `parseDSL`)。
|
|
39
|
+
* 刻意不使用 `eval`,避免执行调用方传入的历史快照里的任意代码。
|
|
40
|
+
*/
|
|
41
|
+
var defaultParseDSL = (code) => {
|
|
42
|
+
const text = code.trim();
|
|
43
|
+
const body = text.startsWith("(") && text.endsWith(")") ? text.slice(1, -1) : text;
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(body);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
throw new Error(`默认的 parseDSL 只能解析 JSON,无法还原含函数 / undefined 等的内容(${e?.message ?? e});请在 createDslSdk 的 parseDSL 选项中传入自己的实现`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/** 把 options 里的登记表写入各 service,返回需要等待的异步任务(如属性表单配置) */
|
|
51
|
+
var registerOptions = ({ editor, props, events, componentList, dataSource }, options) => {
|
|
52
|
+
if (options.componentGroupList) componentList.setList(options.componentGroupList);
|
|
53
|
+
if (options.datasourceList) dataSource.set("datasourceTypeList", options.datasourceList);
|
|
54
|
+
props.setDisabledCodeBlock(options.disabledCodeBlock ?? false);
|
|
55
|
+
props.setDisabledDataSource(options.disabledDataSource ?? false);
|
|
56
|
+
const pending = [props.setPropsConfigs(options.propsConfigs ?? {}), props.setPropsValues(options.propsValues ?? {})];
|
|
57
|
+
const eventMethodList = Object.entries(options.eventMethodList ?? {});
|
|
58
|
+
events.setEvents(Object.fromEntries(eventMethodList.map(([type, item]) => [type, item.events])));
|
|
59
|
+
events.setMethods(Object.fromEntries(eventMethodList.map(([type, item]) => [type, item.methods])));
|
|
60
|
+
for (const [type, config] of Object.entries(options.datasourceConfigs ?? {})) dataSource.setFormConfig(type, config);
|
|
61
|
+
for (const [type, value] of Object.entries(options.datasourceValues ?? {})) dataSource.setFormValue(type, value);
|
|
62
|
+
for (const [type, item] of Object.entries(options.datasourceEventMethodList ?? {})) {
|
|
63
|
+
dataSource.setFormEvent(type, item.events);
|
|
64
|
+
dataSource.setFormMethod(type, item.methods);
|
|
65
|
+
}
|
|
66
|
+
editor.set("disabledMultiSelect", options.disabledMultiSelect ?? false);
|
|
67
|
+
editor.set("alwaysMultiSelect", options.alwaysMultiSelect ?? false);
|
|
68
|
+
editor.setConfig({
|
|
69
|
+
disabledPageFragment: options.disabledPageFragment ?? false,
|
|
70
|
+
canDropIn: options.canDropIn
|
|
71
|
+
});
|
|
72
|
+
return pending;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* 创建一个相互连线、互相独立的 DSL SDK 实例。
|
|
76
|
+
*
|
|
77
|
+
* @param options 见 {@link SdkOptions}
|
|
78
|
+
*/
|
|
79
|
+
var createDslSdk = (options = {}) => {
|
|
80
|
+
const reactivity = {
|
|
81
|
+
reactive,
|
|
82
|
+
shallowReactive,
|
|
83
|
+
toRaw,
|
|
84
|
+
nextTick,
|
|
85
|
+
...options.reactivity
|
|
86
|
+
};
|
|
87
|
+
const parseDSL = options.parseDSL ?? defaultParseDSL;
|
|
88
|
+
const services = {};
|
|
89
|
+
const ctx = {
|
|
90
|
+
reactivity,
|
|
91
|
+
options,
|
|
92
|
+
validateMode: options.validateMode ?? "strict",
|
|
93
|
+
parseDSL: (code) => parseDSL(code),
|
|
94
|
+
ready: Promise.resolve()
|
|
95
|
+
};
|
|
96
|
+
for (const name of SERVICE_NAMES) Object.defineProperty(ctx, name, {
|
|
97
|
+
get: () => services[name],
|
|
98
|
+
enumerable: true
|
|
99
|
+
});
|
|
100
|
+
services.storage = new WebStorage(ctx, options.storage);
|
|
101
|
+
services.history = new History(ctx);
|
|
102
|
+
services.props = new Props(ctx);
|
|
103
|
+
services.events = new Events(ctx);
|
|
104
|
+
services.componentList = new ComponentList(ctx);
|
|
105
|
+
services.dep = new Dep(ctx);
|
|
106
|
+
services.codeBlock = new CodeBlock(ctx);
|
|
107
|
+
services.dataSource = new DataSource(ctx);
|
|
108
|
+
services.editor = new Editor(ctx);
|
|
109
|
+
const { editor, history } = services;
|
|
110
|
+
const validate = createValidator(ctx);
|
|
111
|
+
ctx.validator = validate;
|
|
112
|
+
const pending = registerOptions(services, options);
|
|
113
|
+
let wiring = null;
|
|
114
|
+
const unbind = () => {
|
|
115
|
+
wiring?.unbind();
|
|
116
|
+
wiring = null;
|
|
117
|
+
};
|
|
118
|
+
const bind = () => {
|
|
119
|
+
wiring ??= bindWiring(ctx);
|
|
120
|
+
return unbind;
|
|
121
|
+
};
|
|
122
|
+
if (options.autoWiring ?? true) bind();
|
|
123
|
+
if (options.history) history.restore(options.history);
|
|
124
|
+
const setDsl = async (dsl) => {
|
|
125
|
+
editor.set("root", dsl ? cloneDeep(dsl) : null);
|
|
126
|
+
await whenWiringSettled(ctx);
|
|
127
|
+
};
|
|
128
|
+
if (options.dsl) pending.push(setDsl(options.dsl));
|
|
129
|
+
ctx.ready = Promise.all(pending).then(() => void 0);
|
|
130
|
+
return {
|
|
131
|
+
...services,
|
|
132
|
+
validate,
|
|
133
|
+
ctx,
|
|
134
|
+
get ready() {
|
|
135
|
+
return ctx.ready;
|
|
136
|
+
},
|
|
137
|
+
getDsl() {
|
|
138
|
+
const root = reactivity.toRaw(editor.get("root"));
|
|
139
|
+
return root && !isEmpty(root) ? cloneDeep(root) : null;
|
|
140
|
+
},
|
|
141
|
+
setDsl,
|
|
142
|
+
bindWiring: bind,
|
|
143
|
+
serializeHistory: () => history.serialize({ jsonSafe: true }),
|
|
144
|
+
destroy() {
|
|
145
|
+
unbind();
|
|
146
|
+
for (const service of Object.values(services)) service.destroy();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
//#endregion
|
|
151
|
+
export { BaseService, CodeBlock as CodeBlockService, ComponentList as ComponentListService, DataSource as DataSourceService, Dep as DepService, DslValidationError, Editor as EditorService, Events as EventsService, History as HistoryService, MemoryStorage, Props as PropsService, Protocol, WebStorage as StorageService, bindWiring, classifyUpdatedNodes, createDslSdk, createValidator, defaultParseDSL, whenWiringSettled };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from "events";
|
|
2
|
-
//#region packages/editor/src/services/BaseService.ts
|
|
2
|
+
//#region packages/editor/src/sdk/services/BaseService.ts
|
|
3
3
|
var methodName = (prefix, name) => `${prefix}${name[0].toUpperCase()}${name.substring(1)}`;
|
|
4
4
|
var isError = (error) => Object.prototype.toString.call(error) === "[object Error]";
|
|
5
5
|
var doAction = (args, scope, sourceMethod, beforeMethodName, afterMethodName) => {
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import { CODE_DRAFT_STORAGE_KEY } from "
|
|
2
|
-
import {
|
|
1
|
+
import { CODE_DRAFT_STORAGE_KEY } from "../../type.js";
|
|
2
|
+
import { COPY_CODE_STORAGE_KEY } from "../../utils/editor.js";
|
|
3
|
+
import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from "../../utils/history.js";
|
|
3
4
|
import BaseService from "./BaseService.js";
|
|
4
|
-
import {
|
|
5
|
-
import history_default from "./history.js";
|
|
6
|
-
import storage_default, { Protocol } from "./storage.js";
|
|
7
|
-
import { COPY_CODE_STORAGE_KEY } from "../utils/editor.js";
|
|
8
|
-
import editor_default from "./editor.js";
|
|
5
|
+
import { Protocol } from "./WebStorage.js";
|
|
9
6
|
import { getValueByKeyPath, setValueByKeyPath } from "@tmagic/utils";
|
|
10
7
|
import { Target, Watcher } from "@tmagic/core";
|
|
11
|
-
import { reactive } from "vue";
|
|
12
8
|
import { cloneDeep as cloneDeep$1, get, keys, pick } from "lodash-es";
|
|
13
|
-
//#region packages/editor/src/services/
|
|
9
|
+
//#region packages/editor/src/sdk/services/CodeBlock.ts
|
|
14
10
|
var canUsePluginMethods = {
|
|
15
11
|
async: [
|
|
16
12
|
"setCodeDslById",
|
|
@@ -22,13 +18,8 @@ var canUsePluginMethods = {
|
|
|
22
18
|
sync: ["setCodeDslByIdSync"]
|
|
23
19
|
};
|
|
24
20
|
var CodeBlock = class extends BaseService {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
editable: true,
|
|
28
|
-
combineIds: [],
|
|
29
|
-
undeletableList: [],
|
|
30
|
-
paramsColConfig: void 0
|
|
31
|
-
});
|
|
21
|
+
ctx;
|
|
22
|
+
state;
|
|
32
23
|
/**
|
|
33
24
|
* 最近一次写入历史栈的代码块历史记录 uuid(单条写入场景:新增 / 更新)。
|
|
34
25
|
* 供 setCodeDslById(Sync)AndGetHistoryId 取回,普通方法不读取它。
|
|
@@ -39,7 +30,7 @@ var CodeBlock = class extends BaseService {
|
|
|
39
30
|
* 在 deleteCodeDslByIds 入口处重置,供 deleteCodeDslByIdsAndGetHistoryId 取回。
|
|
40
31
|
*/
|
|
41
32
|
lastDeletedHistoryIds = [];
|
|
42
|
-
constructor() {
|
|
33
|
+
constructor(ctx) {
|
|
43
34
|
super([...canUsePluginMethods.async.map((methodName) => ({
|
|
44
35
|
name: methodName,
|
|
45
36
|
isAsync: true
|
|
@@ -47,6 +38,14 @@ var CodeBlock = class extends BaseService {
|
|
|
47
38
|
name: methodName,
|
|
48
39
|
isAsync: false
|
|
49
40
|
}))]);
|
|
41
|
+
this.ctx = ctx;
|
|
42
|
+
this.state = ctx.reactivity.reactive({
|
|
43
|
+
codeDsl: null,
|
|
44
|
+
editable: true,
|
|
45
|
+
combineIds: [],
|
|
46
|
+
undeletableList: [],
|
|
47
|
+
paramsColConfig: void 0
|
|
48
|
+
});
|
|
50
49
|
}
|
|
51
50
|
/**
|
|
52
51
|
* 设置活动的代码块dsl数据源
|
|
@@ -113,8 +112,7 @@ var CodeBlock = class extends BaseService {
|
|
|
113
112
|
if (codeDsl[id] && !force) return;
|
|
114
113
|
const codeConfigProcessed = cloneDeep$1(codeConfig);
|
|
115
114
|
if (codeConfigProcessed.content) {
|
|
116
|
-
|
|
117
|
-
if (typeof codeConfigProcessed.content === "string") codeConfigProcessed.content = parseDSL(codeConfigProcessed.content);
|
|
115
|
+
if (typeof codeConfigProcessed.content === "string" && this.ctx.options.parseDSL) codeConfigProcessed.content = this.ctx.parseDSL(codeConfigProcessed.content);
|
|
118
116
|
}
|
|
119
117
|
const oldContent = codeDsl[id] ? cloneDeep$1(codeDsl[id]) : null;
|
|
120
118
|
codeDsl[id] = {
|
|
@@ -130,7 +128,7 @@ var CodeBlock = class extends BaseService {
|
|
|
130
128
|
historyDescription,
|
|
131
129
|
source: historySource
|
|
132
130
|
});
|
|
133
|
-
this.lastPushedHistoryId = (step ?
|
|
131
|
+
this.lastPushedHistoryId = (step ? this.ctx.history.push("codeBlock", step, id) : null)?.uuid ?? null;
|
|
134
132
|
}
|
|
135
133
|
this.emit("addOrUpdate", id, codeDsl[id]);
|
|
136
134
|
this.emit("change", this.getCodeDsl());
|
|
@@ -193,19 +191,19 @@ var CodeBlock = class extends BaseService {
|
|
|
193
191
|
* 设置代码草稿
|
|
194
192
|
*/
|
|
195
193
|
setCodeDraft(codeId, content) {
|
|
196
|
-
|
|
194
|
+
this.ctx.storage.getStorage().setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
|
|
197
195
|
}
|
|
198
196
|
/**
|
|
199
197
|
* 获取代码草稿
|
|
200
198
|
*/
|
|
201
199
|
getCodeDraft(codeId) {
|
|
202
|
-
return
|
|
200
|
+
return this.ctx.storage.getStorage().getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
|
|
203
201
|
}
|
|
204
202
|
/**
|
|
205
203
|
* 删除代码草稿
|
|
206
204
|
*/
|
|
207
205
|
removeCodeDraft(codeId) {
|
|
208
|
-
|
|
206
|
+
this.ctx.storage.getStorage().removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
|
|
209
207
|
}
|
|
210
208
|
/**
|
|
211
209
|
* 在dsl数据源中删除指定id的代码块
|
|
@@ -227,7 +225,7 @@ var CodeBlock = class extends BaseService {
|
|
|
227
225
|
historyDescription,
|
|
228
226
|
source: historySource
|
|
229
227
|
});
|
|
230
|
-
const uuid = step ?
|
|
228
|
+
const uuid = step ? this.ctx.history.push("codeBlock", step, id)?.uuid : void 0;
|
|
231
229
|
if (uuid) this.lastDeletedHistoryIds.push(uuid);
|
|
232
230
|
}
|
|
233
231
|
this.emit("remove", id);
|
|
@@ -289,7 +287,7 @@ var CodeBlock = class extends BaseService {
|
|
|
289
287
|
* @returns 撤销的 step;栈不存在或已无可撤销时返回 null
|
|
290
288
|
*/
|
|
291
289
|
async undo(id) {
|
|
292
|
-
const step =
|
|
290
|
+
const step = this.ctx.history.undo("codeBlock", id);
|
|
293
291
|
if (!step) return null;
|
|
294
292
|
await this.applyHistoryStep(step, true);
|
|
295
293
|
return step;
|
|
@@ -300,18 +298,18 @@ var CodeBlock = class extends BaseService {
|
|
|
300
298
|
* @returns 重做的 step;栈不存在或已无可重做时返回 null
|
|
301
299
|
*/
|
|
302
300
|
async redo(id) {
|
|
303
|
-
const step =
|
|
301
|
+
const step = this.ctx.history.redo("codeBlock", id);
|
|
304
302
|
if (!step) return null;
|
|
305
303
|
await this.applyHistoryStep(step, false);
|
|
306
304
|
return step;
|
|
307
305
|
}
|
|
308
306
|
/** 是否可对指定代码块撤销。 */
|
|
309
307
|
canUndo(id) {
|
|
310
|
-
return
|
|
308
|
+
return this.ctx.history.canUndo("codeBlock", id);
|
|
311
309
|
}
|
|
312
310
|
/** 是否可对指定代码块重做。 */
|
|
313
311
|
canRedo(id) {
|
|
314
|
-
return
|
|
312
|
+
return this.ctx.history.canRedo("codeBlock", id);
|
|
315
313
|
}
|
|
316
314
|
/**
|
|
317
315
|
* 跳转指定代码块的历史栈到目标游标。语义同 editor.gotoPageStep。
|
|
@@ -321,7 +319,7 @@ var CodeBlock = class extends BaseService {
|
|
|
321
319
|
* @returns 实际移动到的最终游标位置
|
|
322
320
|
*/
|
|
323
321
|
async goto(id, targetCursor) {
|
|
324
|
-
let cursor =
|
|
322
|
+
let cursor = this.ctx.history.getCursor("codeBlock", id);
|
|
325
323
|
const target = Math.max(0, targetCursor);
|
|
326
324
|
while (cursor > target) {
|
|
327
325
|
if (!await this.undo(id)) break;
|
|
@@ -344,7 +342,7 @@ var CodeBlock = class extends BaseService {
|
|
|
344
342
|
* @returns 反向后产生的新 step;目标不存在 / 未应用时返回 null
|
|
345
343
|
*/
|
|
346
344
|
async revert(id, index) {
|
|
347
|
-
const entry =
|
|
345
|
+
const entry = this.ctx.history.getStepList("codeBlock", id)[index];
|
|
348
346
|
if (!entry?.applied) return null;
|
|
349
347
|
const { oldSchema, newSchema, changeRecords } = entry.step.diff?.[0] ?? {};
|
|
350
348
|
if (oldSchema && newSchema && !changeRecords?.length) return null;
|
|
@@ -361,7 +359,7 @@ var CodeBlock = class extends BaseService {
|
|
|
361
359
|
async revertById(uuids) {
|
|
362
360
|
const results = [];
|
|
363
361
|
for (const uuid of uuids) {
|
|
364
|
-
const location =
|
|
362
|
+
const location = this.ctx.history.findStepLocationByUuid("codeBlock", uuid);
|
|
365
363
|
results.push(location ? await this.revert(location.id, location.index) : null);
|
|
366
364
|
}
|
|
367
365
|
return results;
|
|
@@ -390,7 +388,7 @@ var CodeBlock = class extends BaseService {
|
|
|
390
388
|
coperWatcher.addTarget(customTarget);
|
|
391
389
|
coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
|
|
392
390
|
Object.keys(customTarget.deps).forEach((nodeId) => {
|
|
393
|
-
const node =
|
|
391
|
+
const node = this.ctx.editor.getNodeById(nodeId);
|
|
394
392
|
if (!node) return;
|
|
395
393
|
customTarget.deps[nodeId].keys.forEach((key) => {
|
|
396
394
|
const relateCodeId = get(node, key);
|
|
@@ -401,14 +399,14 @@ var CodeBlock = class extends BaseService {
|
|
|
401
399
|
});
|
|
402
400
|
});
|
|
403
401
|
}
|
|
404
|
-
|
|
402
|
+
this.ctx.storage.setItem(COPY_CODE_STORAGE_KEY, copyData, { protocol: Protocol.OBJECT });
|
|
405
403
|
}
|
|
406
404
|
/**
|
|
407
405
|
* 粘贴代码块
|
|
408
406
|
* @returns
|
|
409
407
|
*/
|
|
410
408
|
paste() {
|
|
411
|
-
const codeDsl =
|
|
409
|
+
const codeDsl = this.ctx.storage.getItem(COPY_CODE_STORAGE_KEY);
|
|
412
410
|
Object.keys(codeDsl).forEach((codeId) => {
|
|
413
411
|
this.setCodeDslByIdSync(codeId, codeDsl[codeId], false);
|
|
414
412
|
});
|
|
@@ -439,14 +437,14 @@ var CodeBlock = class extends BaseService {
|
|
|
439
437
|
historyDescription,
|
|
440
438
|
historySource: "rollback"
|
|
441
439
|
});
|
|
442
|
-
return
|
|
440
|
+
return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
|
|
443
441
|
}
|
|
444
442
|
if (oldSchema && !newSchema) {
|
|
445
443
|
this.setCodeDslByIdSync(id, cloneDeep$1(oldSchema), true, {
|
|
446
444
|
historyDescription,
|
|
447
445
|
historySource: "rollback"
|
|
448
446
|
});
|
|
449
|
-
return
|
|
447
|
+
return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
|
|
450
448
|
}
|
|
451
449
|
if (!oldSchema || !newSchema) return null;
|
|
452
450
|
if (changeRecords?.length) {
|
|
@@ -467,13 +465,13 @@ var CodeBlock = class extends BaseService {
|
|
|
467
465
|
historyDescription,
|
|
468
466
|
historySource: "rollback"
|
|
469
467
|
});
|
|
470
|
-
return
|
|
468
|
+
return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
|
|
471
469
|
}
|
|
472
470
|
this.setCodeDslByIdSync(id, cloneDeep$1(oldSchema), true, {
|
|
473
471
|
historyDescription,
|
|
474
472
|
historySource: "rollback"
|
|
475
473
|
});
|
|
476
|
-
return
|
|
474
|
+
return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
|
|
477
475
|
}
|
|
478
476
|
/**
|
|
479
477
|
* 把一条历史 step 应用到当前代码块服务上。
|
|
@@ -526,6 +524,5 @@ var CodeBlock = class extends BaseService {
|
|
|
526
524
|
this.setCodeDslByIdSync(id, cloneDeep$1(sourceForValues), true, { doNotPushHistory: true });
|
|
527
525
|
}
|
|
528
526
|
};
|
|
529
|
-
var codeBlock_default = new CodeBlock();
|
|
530
527
|
//#endregion
|
|
531
|
-
export {
|
|
528
|
+
export { CodeBlock as default };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import BaseService from "./BaseService.js";
|
|
2
|
-
|
|
3
|
-
//#region packages/editor/src/services/componentList.ts
|
|
2
|
+
//#region packages/editor/src/sdk/services/ComponentList.ts
|
|
4
3
|
var ComponentList = class extends BaseService {
|
|
5
|
-
state
|
|
6
|
-
constructor() {
|
|
4
|
+
state;
|
|
5
|
+
constructor(ctx) {
|
|
7
6
|
super([]);
|
|
7
|
+
this.state = ctx.reactivity.shallowReactive({ list: [] });
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* @param componentGroupList 组件列表配置
|
|
@@ -24,6 +24,5 @@ var ComponentList = class extends BaseService {
|
|
|
24
24
|
this.removeAllPlugins();
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
|
-
var componentList_default = new ComponentList();
|
|
28
27
|
//#endregion
|
|
29
|
-
export {
|
|
28
|
+
export { ComponentList as default };
|