@tmagic/editor 1.8.1 → 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 +17 -25
- package/dist/es/editorProps.js +3 -0
- 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/FormPanel.vue_vue_type_script_setup_true_lang.js +8 -6
- package/dist/es/layouts/props-panel/PropsPanel.vue_vue_type_script_setup_true_lang.js +4 -4
- 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 +14200 -13381
- package/package.json +12 -7
- package/src/Editor.vue +17 -10
- package/src/editorProps.ts +5 -2
- 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/FormPanel.vue +8 -5
- package/src/layouts/props-panel/PropsPanel.vue +8 -3
- 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/src/initService.ts
CHANGED
|
@@ -12,23 +12,22 @@ import type {
|
|
|
12
12
|
MPage,
|
|
13
13
|
MPageFragment,
|
|
14
14
|
} from '@tmagic/core';
|
|
15
|
-
import {
|
|
16
|
-
createCodeBlockTarget,
|
|
17
|
-
createDataSourceCondTarget,
|
|
18
|
-
createDataSourceMethodTarget,
|
|
19
|
-
createDataSourceTarget,
|
|
20
|
-
DepTargetType,
|
|
21
|
-
NODE_CONDS_KEY,
|
|
22
|
-
NodeType,
|
|
23
|
-
Target,
|
|
24
|
-
updateNode,
|
|
25
|
-
} from '@tmagic/core';
|
|
15
|
+
import { createCodeBlockTarget, DepTargetType, NodeType, Target, updateNode } from '@tmagic/core';
|
|
26
16
|
import { ChangeRecord } from '@tmagic/form';
|
|
27
17
|
import StageCore from '@tmagic/stage';
|
|
28
|
-
import { getDepNodeIds, getNodes, isPage
|
|
18
|
+
import { getDepNodeIds, getNodes, isPage } from '@tmagic/utils';
|
|
29
19
|
|
|
30
20
|
import PropsPanel from './layouts/PropsPanel.vue';
|
|
31
|
-
import {
|
|
21
|
+
import type { Reactivity } from './sdk/type';
|
|
22
|
+
import {
|
|
23
|
+
addDataSourceTargets,
|
|
24
|
+
classifyUpdatedNodes,
|
|
25
|
+
getDataSourceRecollectTypes,
|
|
26
|
+
removeDataSourceTargets,
|
|
27
|
+
removeTargetDeps,
|
|
28
|
+
resetDepTargets,
|
|
29
|
+
writeTargetDeps,
|
|
30
|
+
} from './sdk/utils/dep';
|
|
32
31
|
import { error as logError } from './utils/logger';
|
|
33
32
|
import { EditorProps } from './editorProps';
|
|
34
33
|
import { Services } from './type';
|
|
@@ -247,6 +246,16 @@ export const initServiceState = (
|
|
|
247
246
|
},
|
|
248
247
|
);
|
|
249
248
|
|
|
249
|
+
// 同步给 SDK 的结构合法性校验(浏览器端默认 validateMode 为 off,主动调用 `validate` 时才会用到)
|
|
250
|
+
watch(
|
|
251
|
+
() => [props.disabledPageFragment, props.canDropIn] as const,
|
|
252
|
+
([disabledPageFragment, canDropIn]) =>
|
|
253
|
+
editorService.setConfig({ disabledPageFragment: disabledPageFragment ?? false, canDropIn }),
|
|
254
|
+
{
|
|
255
|
+
immediate: true,
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
|
|
250
259
|
onBeforeUnmount(() => {
|
|
251
260
|
editorService.resetState();
|
|
252
261
|
historyService.resetState();
|
|
@@ -476,12 +485,6 @@ export const initServiceEvents = (
|
|
|
476
485
|
}
|
|
477
486
|
};
|
|
478
487
|
|
|
479
|
-
const initDataSourceDepTarget = (ds: DataSourceSchema) => {
|
|
480
|
-
depService.addTarget(createDataSourceTarget(ds, reactive({})));
|
|
481
|
-
depService.addTarget(createDataSourceMethodTarget(ds, reactive({})));
|
|
482
|
-
depService.addTarget(createDataSourceCondTarget(ds, reactive({})));
|
|
483
|
-
};
|
|
484
|
-
|
|
485
488
|
const rootChangeHandler = (value: MApp | null) => {
|
|
486
489
|
if (!value) return;
|
|
487
490
|
|
|
@@ -491,15 +494,12 @@ export const initServiceEvents = (
|
|
|
491
494
|
codeBlockService.setCodeDsl(value.codeBlocks);
|
|
492
495
|
dataSourceService.set('dataSources', value.dataSources);
|
|
493
496
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
for (const ds of dataSourceService.get('dataSources')) {
|
|
501
|
-
initDataSourceDepTarget(ds);
|
|
502
|
-
}
|
|
497
|
+
resetDepTargets(
|
|
498
|
+
depService,
|
|
499
|
+
value.codeBlocks,
|
|
500
|
+
dataSourceService.get('dataSources'),
|
|
501
|
+
reactive as Reactivity['reactive'],
|
|
502
|
+
);
|
|
503
503
|
|
|
504
504
|
if (Array.isArray(value.items)) {
|
|
505
505
|
updateStageDsl(value);
|
|
@@ -549,49 +549,7 @@ export const initServiceEvents = (
|
|
|
549
549
|
// 节点更新,收集依赖
|
|
550
550
|
// 仅当修改到数据源相关的才收集
|
|
551
551
|
const nodeUpdateHandler = (data: { newNode: MComponent; oldNode: MComponent; changeRecords?: ChangeRecord[] }[]) => {
|
|
552
|
-
const needRecollectNodes
|
|
553
|
-
const normalNodes: MComponent[] = [];
|
|
554
|
-
for (const { newNode, oldNode, changeRecords } of data) {
|
|
555
|
-
if (newNode.type === NodeType.ROOT) {
|
|
556
|
-
normalNodes.push(newNode);
|
|
557
|
-
} else if (changeRecords?.length) {
|
|
558
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
559
|
-
forChangeRecords: for (const record of changeRecords) {
|
|
560
|
-
if (!record.propPath) {
|
|
561
|
-
needRecollectNodes.push(newNode);
|
|
562
|
-
break forChangeRecords;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
// NODE_CONDS_KEY为显示条件key
|
|
566
|
-
if (
|
|
567
|
-
new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond`).test(record.propPath) ||
|
|
568
|
-
new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond.(\\d)+.value`).test(record.propPath) ||
|
|
569
|
-
record.propPath === NODE_CONDS_KEY ||
|
|
570
|
-
isValueIncludeDataSource(record.value)
|
|
571
|
-
) {
|
|
572
|
-
needRecollectNodes.push(newNode);
|
|
573
|
-
break forChangeRecords;
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
// 修改的key在收集的依赖中,则需要触发重新收集
|
|
577
|
-
for (const target of Object.values(depService.getTargets(DepTargetType.DATA_SOURCE))) {
|
|
578
|
-
if (!target.deps[newNode.id]) {
|
|
579
|
-
continue;
|
|
580
|
-
}
|
|
581
|
-
if (target.deps[newNode.id].keys.includes(record.propPath)) {
|
|
582
|
-
needRecollectNodes.push(newNode);
|
|
583
|
-
break forChangeRecords;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
normalNodes.push(newNode);
|
|
588
|
-
}
|
|
589
|
-
} else if (isIncludeDataSource(newNode, oldNode)) {
|
|
590
|
-
needRecollectNodes.push(newNode);
|
|
591
|
-
} else {
|
|
592
|
-
normalNodes.push(newNode);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
552
|
+
const { needRecollectNodes, normalNodes } = classifyUpdatedNodes({ dep: depService }, data);
|
|
595
553
|
|
|
596
554
|
if (needRecollectNodes.length) {
|
|
597
555
|
// 有数据源依赖,需要等依赖重新收集完才更新stage
|
|
@@ -603,13 +561,14 @@ export const initServiceEvents = (
|
|
|
603
561
|
handler();
|
|
604
562
|
} else {
|
|
605
563
|
updateStageNodes(normalNodes);
|
|
606
|
-
|
|
607
|
-
// 在上面判断是否需要收集数据源依赖中已经更新stage
|
|
608
|
-
Promise.all([
|
|
609
|
-
collectIdle(normalNodes, true, DepTargetType.CODE_BLOCK),
|
|
610
|
-
collectIdle(normalNodes, true, DepTargetType.DATA_SOURCE_METHOD),
|
|
611
|
-
]);
|
|
612
564
|
}
|
|
565
|
+
|
|
566
|
+
// 代码块、数据源方法依赖对所有被更新的节点都要收集:同一批里既改了数据源引用又改了方法时不能漏掉
|
|
567
|
+
const updatedNodes = [...needRecollectNodes, ...normalNodes];
|
|
568
|
+
Promise.all([
|
|
569
|
+
collectIdle(updatedNodes, true, DepTargetType.CODE_BLOCK),
|
|
570
|
+
collectIdle(updatedNodes, true, DepTargetType.DATA_SOURCE_METHOD),
|
|
571
|
+
]);
|
|
613
572
|
};
|
|
614
573
|
|
|
615
574
|
// 节点删除,清除对齐的依赖收集
|
|
@@ -634,7 +593,7 @@ export const initServiceEvents = (
|
|
|
634
593
|
|
|
635
594
|
const dataSourceAddHandler = (config: DataSourceSchema) => {
|
|
636
595
|
const handler = async () => {
|
|
637
|
-
|
|
596
|
+
addDataSourceTargets(depService, config, reactive as Reactivity['reactive']);
|
|
638
597
|
const app = await getTMagicApp();
|
|
639
598
|
|
|
640
599
|
if (!app?.dataSourceManager) {
|
|
@@ -669,84 +628,31 @@ export const initServiceEvents = (
|
|
|
669
628
|
ds.setData(config.mocks?.find((mock) => mock.useInEditor)?.data || ds.getDefaultData());
|
|
670
629
|
};
|
|
671
630
|
|
|
672
|
-
|
|
673
|
-
let isModifyField = false;
|
|
674
|
-
let isModifyMock = false;
|
|
675
|
-
let isModifyMethod = false;
|
|
676
|
-
for (const changeRecord of changeRecords) {
|
|
677
|
-
if (!changeRecord.propPath) {
|
|
678
|
-
continue;
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
isModifyField =
|
|
682
|
-
changeRecord.propPath === 'fields' ||
|
|
683
|
-
/fields.(\d)+.name/.test(changeRecord.propPath) ||
|
|
684
|
-
/fields.(\d)+.defaultValue/.test(changeRecord.propPath) ||
|
|
685
|
-
/fields.(\d)+$/.test(changeRecord.propPath);
|
|
686
|
-
|
|
687
|
-
isModifyMock = changeRecord.propPath === 'mocks';
|
|
688
|
-
|
|
689
|
-
isModifyMethod =
|
|
690
|
-
changeRecord.propPath === 'methods' ||
|
|
691
|
-
/methods.(\d)+.name/.test(changeRecord.propPath) ||
|
|
692
|
-
/methods.(\d)+$/.test(changeRecord.propPath);
|
|
693
|
-
|
|
694
|
-
needRecollectDep = isModifyField || isModifyMock || isModifyMethod;
|
|
695
|
-
|
|
696
|
-
if (needRecollectDep) {
|
|
697
|
-
break;
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
|
|
631
|
+
const types = getDataSourceRecollectTypes(changeRecords);
|
|
701
632
|
const root = editorService.get('root');
|
|
702
|
-
if (needRecollectDep) {
|
|
703
|
-
if (Array.isArray(root?.items)) {
|
|
704
|
-
depService.clearIdleTasks();
|
|
705
|
-
|
|
706
|
-
let collectIdlePromises: Promise<boolean>[] = [];
|
|
707
|
-
if (isModifyField) {
|
|
708
|
-
depService.removeTarget(config.id, DepTargetType.DATA_SOURCE);
|
|
709
|
-
depService.removeTarget(config.id, DepTargetType.DATA_SOURCE_COND);
|
|
710
633
|
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
collectIdle(root.items, true, DepTargetType.DATA_SOURCE),
|
|
716
|
-
collectIdle(root.items, true, DepTargetType.DATA_SOURCE_COND),
|
|
717
|
-
];
|
|
718
|
-
} else if (isModifyMock) {
|
|
719
|
-
depService.removeTarget(config.id, DepTargetType.DATA_SOURCE);
|
|
634
|
+
if (!types) {
|
|
635
|
+
if (root?.dataSources) updateDsData();
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
720
638
|
|
|
721
|
-
|
|
639
|
+
if (!Array.isArray(root?.items)) return;
|
|
722
640
|
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
641
|
+
depService.clearIdleTasks();
|
|
642
|
+
removeDataSourceTargets(depService, config.id, types);
|
|
643
|
+
addDataSourceTargets(depService, config, reactive as Reactivity['reactive'], types);
|
|
726
644
|
|
|
727
|
-
|
|
645
|
+
const collectIdlePromises = types.map((type) => collectIdle(root.items, true, type));
|
|
728
646
|
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
const results = await Promise.all(collectIdlePromises);
|
|
733
|
-
if (!results.every(Boolean)) return;
|
|
734
|
-
|
|
735
|
-
updateDataSourceSchema();
|
|
736
|
-
await updateDsData();
|
|
737
|
-
updateStageNodes(root.items);
|
|
738
|
-
};
|
|
739
|
-
handler();
|
|
740
|
-
}
|
|
741
|
-
} else if (root?.dataSources) {
|
|
742
|
-
updateDsData();
|
|
743
|
-
}
|
|
744
|
-
};
|
|
647
|
+
const handler = async () => {
|
|
648
|
+
const results = await Promise.all(collectIdlePromises);
|
|
649
|
+
if (!results.every(Boolean)) return;
|
|
745
650
|
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
651
|
+
updateDataSourceSchema();
|
|
652
|
+
await updateDsData();
|
|
653
|
+
updateStageNodes(root.items);
|
|
654
|
+
};
|
|
655
|
+
handler();
|
|
750
656
|
};
|
|
751
657
|
|
|
752
658
|
const dataSourceRemoveHandler = (id: string) => {
|
|
@@ -773,7 +679,7 @@ export const initServiceEvents = (
|
|
|
773
679
|
app?.dataSourceManager?.removeDataSource(id);
|
|
774
680
|
|
|
775
681
|
updateStageNodes(nodes);
|
|
776
|
-
|
|
682
|
+
removeDataSourceTargets(depService, id);
|
|
777
683
|
};
|
|
778
684
|
|
|
779
685
|
handler();
|
|
@@ -799,45 +705,10 @@ export const initServiceEvents = (
|
|
|
799
705
|
codeBlockService.on('addOrUpdate', codeBlockAddOrUpdateHandler);
|
|
800
706
|
codeBlockService.on('remove', codeBlockRemoveHandler);
|
|
801
707
|
|
|
802
|
-
const targetAddHandler = (target: Target) =>
|
|
803
|
-
const root = editorService.get('root');
|
|
804
|
-
if (!root) return;
|
|
708
|
+
const targetAddHandler = (target: Target) => writeTargetDeps(editorService.get('root'), target);
|
|
805
709
|
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
root.dataSourceDeps = {};
|
|
809
|
-
}
|
|
810
|
-
root.dataSourceDeps[target.id] = target.deps;
|
|
811
|
-
} else if (target.type === DepTargetType.DATA_SOURCE_COND) {
|
|
812
|
-
if (!root.dataSourceCondDeps) {
|
|
813
|
-
root.dataSourceCondDeps = {};
|
|
814
|
-
}
|
|
815
|
-
root.dataSourceCondDeps[target.id] = target.deps;
|
|
816
|
-
} else if (target.type === DepTargetType.DATA_SOURCE_METHOD) {
|
|
817
|
-
if (!root.dataSourceMethodDeps) {
|
|
818
|
-
root.dataSourceMethodDeps = {};
|
|
819
|
-
}
|
|
820
|
-
root.dataSourceMethodDeps[target.id] = target.deps;
|
|
821
|
-
}
|
|
822
|
-
};
|
|
823
|
-
|
|
824
|
-
const targetRemoveHandler = (id: string | number, type: DepTargetType) => {
|
|
825
|
-
const root = editorService.get('root');
|
|
826
|
-
|
|
827
|
-
if (!root) return;
|
|
828
|
-
|
|
829
|
-
if (root.dataSourceDeps && type === DepTargetType.DATA_SOURCE) {
|
|
830
|
-
delete root.dataSourceDeps[id];
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
if (root.dataSourceCondDeps && type === DepTargetType.DATA_SOURCE_COND) {
|
|
834
|
-
delete root.dataSourceCondDeps[id];
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
if (root.dataSourceMethodDeps && type === DepTargetType.DATA_SOURCE_METHOD) {
|
|
838
|
-
delete root.dataSourceMethodDeps[id];
|
|
839
|
-
}
|
|
840
|
-
};
|
|
710
|
+
const targetRemoveHandler = (id: string | number, type: DepTargetType) =>
|
|
711
|
+
removeTargetDeps(editorService.get('root'), id, type);
|
|
841
712
|
|
|
842
713
|
depService.on('add-target', targetAddHandler);
|
|
843
714
|
depService.on('remove-target', targetRemoveHandler);
|
|
@@ -76,7 +76,7 @@ import type { MPage, MPageFragment } from '@tmagic/core';
|
|
|
76
76
|
|
|
77
77
|
import SplitView from '@editor/components/SplitView.vue';
|
|
78
78
|
import { useServices } from '@editor/hooks/use-services';
|
|
79
|
-
import { Protocol } from '@editor/services/
|
|
79
|
+
import { Protocol } from '@editor/services/sdk-instance';
|
|
80
80
|
import type { FrameworkSlots, GetColumnWidth, PageBarSortOptions } from '@editor/type';
|
|
81
81
|
import { getEditorConfig } from '@editor/utils/config';
|
|
82
82
|
import {
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
:size="propsPanelSize"
|
|
13
13
|
:init-values="values"
|
|
14
14
|
:config="config"
|
|
15
|
-
:type-match-valid="
|
|
15
|
+
:type-match-valid="enablePropsFormValidate"
|
|
16
16
|
:context="formContext"
|
|
17
|
-
:validate-on-init="
|
|
17
|
+
:validate-on-init="enablePropsFormValidate"
|
|
18
18
|
@change="submit"
|
|
19
19
|
@error="errorHandler"
|
|
20
20
|
></MForm>
|
|
@@ -86,7 +86,10 @@ const emit = defineEmits<{
|
|
|
86
86
|
unmounted: [];
|
|
87
87
|
}>();
|
|
88
88
|
|
|
89
|
-
const enablePropsFormValidate = inject(
|
|
89
|
+
const enablePropsFormValidate = inject(
|
|
90
|
+
ENABLE_PROPS_FORM_VALIDATE,
|
|
91
|
+
computed(() => false),
|
|
92
|
+
);
|
|
90
93
|
|
|
91
94
|
const services = useServices();
|
|
92
95
|
const { uiService } = services;
|
|
@@ -116,7 +119,7 @@ const submit = async (v: FormValue, eventData: ContainerChangeEventData) => {
|
|
|
116
119
|
// 校验成功:正常更新节点(第三个参数 error 为空,表示清除该来源的错误记录)
|
|
117
120
|
emit('submit', values, eventData);
|
|
118
121
|
} catch (e: any) {
|
|
119
|
-
if (enablePropsFormValidate) {
|
|
122
|
+
if (enablePropsFormValidate.value) {
|
|
120
123
|
// 启用校验联动:校验失败时仍以当前表单值更新节点,并把错误信息一并抛给上层记录
|
|
121
124
|
emit('submit', v, eventData, e);
|
|
122
125
|
} else {
|
|
@@ -146,7 +149,7 @@ const formatValidateErrorHtml = (error: string): string =>
|
|
|
146
149
|
const saveCode = async (values: any) => {
|
|
147
150
|
const newValues = props.codeValueKey ? { [props.codeValueKey]: values } : values;
|
|
148
151
|
|
|
149
|
-
if (!enablePropsFormValidate) {
|
|
152
|
+
if (!enablePropsFormValidate.value) {
|
|
150
153
|
// 未启用校验联动:保持原行为,直接提交源码保存的值
|
|
151
154
|
emit('submit', newValues);
|
|
152
155
|
return;
|
|
@@ -66,7 +66,7 @@ import MIcon from '@editor/components/Icon.vue';
|
|
|
66
66
|
import Resizer from '@editor/components/Resizer.vue';
|
|
67
67
|
import { ENABLE_PROPS_FORM_VALIDATE } from '@editor/editorProps';
|
|
68
68
|
import { useServices } from '@editor/hooks/use-services';
|
|
69
|
-
import { Protocol } from '@editor/services/
|
|
69
|
+
import { Protocol } from '@editor/services/sdk-instance';
|
|
70
70
|
import type { NodeInvalidSource, PropsPanelSlots } from '@editor/type';
|
|
71
71
|
import { styleTabConfig } from '@editor/utils';
|
|
72
72
|
import { PROPS_PANEL_WIDTH_STORAGE_KEY } from '@editor/utils/const';
|
|
@@ -93,7 +93,10 @@ const emit = defineEmits<{
|
|
|
93
93
|
|
|
94
94
|
const { editorService, uiService, propsService, storageService } = useServices();
|
|
95
95
|
|
|
96
|
-
const enablePropsFormValidate = inject(
|
|
96
|
+
const enablePropsFormValidate = inject(
|
|
97
|
+
ENABLE_PROPS_FORM_VALIDATE,
|
|
98
|
+
computed(() => false),
|
|
99
|
+
);
|
|
97
100
|
|
|
98
101
|
const values = ref<FormValue>({});
|
|
99
102
|
// ts类型应该是FormConfig, 但是打包时会出错,所以暂时用any
|
|
@@ -205,7 +208,9 @@ const submit = async (
|
|
|
205
208
|
replace,
|
|
206
209
|
// 启用校验联动时,仅校验失败(error 存在)才把错误信息随更新传入 editorService 记录;
|
|
207
210
|
// 其余情况(含表单校验成功、CodeEditor 源码保存)不携带 invalidInfo,由 editorService 在执行 update 时统一清除该节点错误。
|
|
208
|
-
...(enablePropsFormValidate && error
|
|
211
|
+
...(enablePropsFormValidate.value && error
|
|
212
|
+
? { invalidInfo: { id: newValue.id, source, error: error?.message } }
|
|
213
|
+
: {}),
|
|
209
214
|
});
|
|
210
215
|
} catch (e: any) {
|
|
211
216
|
emit('submit-error', e);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { computed, type Ref, watch } from 'vue';
|
|
2
2
|
|
|
3
|
-
import { Protocol } from '@editor/services/
|
|
3
|
+
import { Protocol } from '@editor/services/sdk-instance';
|
|
4
4
|
import { Services } from '@editor/type';
|
|
5
5
|
import { RIGHT_COLUMN_WIDTH_STORAGE_KEY } from '@editor/utils/const';
|
|
6
6
|
|
package/src/sdk/index.ts
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2025 Tencent. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @fileoverview `@tmagic/editor/sdk`:不依赖 DOM 的 DSL 操作 SDK。
|
|
21
|
+
*
|
|
22
|
+
* 每次调用 {@link createDslSdk} 得到一组独立的 service(editor / history / props / dataSource /
|
|
23
|
+
* codeBlock / dep / storage / events / componentList),可在 Node 中运行。浏览器端的
|
|
24
|
+
* `editorService` 等单例就是用同一个工厂创建的默认实例。
|
|
25
|
+
*
|
|
26
|
+
* @module @tmagic/editor/sdk
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { nextTick, reactive, shallowReactive, toRaw } from 'vue';
|
|
30
|
+
import { cloneDeep, isEmpty } from 'lodash-es';
|
|
31
|
+
|
|
32
|
+
import type { MApp } from '@tmagic/core';
|
|
33
|
+
|
|
34
|
+
import type { PersistedHistoryState } from '@editor/type';
|
|
35
|
+
|
|
36
|
+
import CodeBlock from './services/CodeBlock';
|
|
37
|
+
import ComponentList from './services/ComponentList';
|
|
38
|
+
import DataSource from './services/DataSource';
|
|
39
|
+
import Dep from './services/Dep';
|
|
40
|
+
import Editor from './services/Editor';
|
|
41
|
+
import Events from './services/Events';
|
|
42
|
+
import History from './services/History';
|
|
43
|
+
import Props from './services/Props';
|
|
44
|
+
import WebStorage from './services/WebStorage';
|
|
45
|
+
import type { Reactivity, SdkContext, SdkOptions } from './type';
|
|
46
|
+
import { createValidator, type Validator } from './validate';
|
|
47
|
+
import { bindWiring, whenWiringSettled, type WiringHandle } from './wiring';
|
|
48
|
+
|
|
49
|
+
const SERVICE_NAMES = [
|
|
50
|
+
'editor',
|
|
51
|
+
'props',
|
|
52
|
+
'history',
|
|
53
|
+
'dataSource',
|
|
54
|
+
'codeBlock',
|
|
55
|
+
'dep',
|
|
56
|
+
'storage',
|
|
57
|
+
'events',
|
|
58
|
+
'componentList',
|
|
59
|
+
] as const;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Node 下默认的 DSL 反序列化:序列化出来的内容是被括号包起来的 JSON(函数、正则等需要自己传入 `parseDSL`)。
|
|
63
|
+
* 刻意不使用 `eval`,避免执行调用方传入的历史快照里的任意代码。
|
|
64
|
+
*/
|
|
65
|
+
export const defaultParseDSL = (code: string): any => {
|
|
66
|
+
const text = code.trim();
|
|
67
|
+
const body = text.startsWith('(') && text.endsWith(')') ? text.slice(1, -1) : text;
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(body);
|
|
70
|
+
} catch (e: any) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`默认的 parseDSL 只能解析 JSON,无法还原含函数 / undefined 等的内容(${e?.message ?? e});请在 createDslSdk 的 parseDSL 选项中传入自己的实现`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export interface DslSdk {
|
|
78
|
+
readonly editor: Editor;
|
|
79
|
+
readonly history: History;
|
|
80
|
+
readonly props: Props;
|
|
81
|
+
readonly dataSource: DataSource;
|
|
82
|
+
readonly codeBlock: CodeBlock;
|
|
83
|
+
readonly dep: Dep;
|
|
84
|
+
readonly storage: WebStorage;
|
|
85
|
+
readonly events: Events;
|
|
86
|
+
readonly componentList: ComponentList;
|
|
87
|
+
readonly validate: Validator;
|
|
88
|
+
readonly ctx: SdkContext;
|
|
89
|
+
|
|
90
|
+
/** 登记表(组件属性配置等,其中一部分是异步填充的)全部就绪后 resolve */
|
|
91
|
+
ready: Promise<void>;
|
|
92
|
+
|
|
93
|
+
/** 导出当前 DSL(深拷贝,含依赖收集结果) */
|
|
94
|
+
getDsl(): MApp | null;
|
|
95
|
+
/** 整体替换 DSL,会按 `set root` 的语义写入历史记录 */
|
|
96
|
+
setDsl(dsl: MApp | null): Promise<void>;
|
|
97
|
+
/** 绑定依赖收集连线;`autoWiring` 为 true 时创建时已绑定。返回解绑函数 */
|
|
98
|
+
bindWiring(): () => void;
|
|
99
|
+
/** 导出历史快照,下次 `createDslSdk({ history })` 时传回即可恢复 */
|
|
100
|
+
serializeHistory(): PersistedHistoryState;
|
|
101
|
+
destroy(): void;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type Services = Pick<
|
|
105
|
+
SdkContext,
|
|
106
|
+
'editor' | 'props' | 'history' | 'dataSource' | 'codeBlock' | 'dep' | 'storage' | 'events' | 'componentList'
|
|
107
|
+
>;
|
|
108
|
+
|
|
109
|
+
/** 把 options 里的登记表写入各 service,返回需要等待的异步任务(如属性表单配置) */
|
|
110
|
+
const registerOptions = (
|
|
111
|
+
{ editor, props, events, componentList, dataSource }: Services,
|
|
112
|
+
options: SdkOptions,
|
|
113
|
+
): Promise<unknown>[] => {
|
|
114
|
+
if (options.componentGroupList) componentList.setList(options.componentGroupList);
|
|
115
|
+
if (options.datasourceList) dataSource.set('datasourceTypeList', options.datasourceList);
|
|
116
|
+
|
|
117
|
+
props.setDisabledCodeBlock(options.disabledCodeBlock ?? false);
|
|
118
|
+
props.setDisabledDataSource(options.disabledDataSource ?? false);
|
|
119
|
+
|
|
120
|
+
const pending = [props.setPropsConfigs(options.propsConfigs ?? {}), props.setPropsValues(options.propsValues ?? {})];
|
|
121
|
+
|
|
122
|
+
const eventMethodList = Object.entries(options.eventMethodList ?? {});
|
|
123
|
+
events.setEvents(Object.fromEntries(eventMethodList.map(([type, item]) => [type, item.events])));
|
|
124
|
+
events.setMethods(Object.fromEntries(eventMethodList.map(([type, item]) => [type, item.methods])));
|
|
125
|
+
|
|
126
|
+
for (const [type, config] of Object.entries(options.datasourceConfigs ?? {})) {
|
|
127
|
+
dataSource.setFormConfig(type, config);
|
|
128
|
+
}
|
|
129
|
+
for (const [type, value] of Object.entries(options.datasourceValues ?? {})) {
|
|
130
|
+
dataSource.setFormValue(type, value);
|
|
131
|
+
}
|
|
132
|
+
for (const [type, item] of Object.entries(options.datasourceEventMethodList ?? {})) {
|
|
133
|
+
dataSource.setFormEvent(type, item.events);
|
|
134
|
+
dataSource.setFormMethod(type, item.methods);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
editor.set('disabledMultiSelect', options.disabledMultiSelect ?? false);
|
|
138
|
+
editor.set('alwaysMultiSelect', options.alwaysMultiSelect ?? false);
|
|
139
|
+
editor.setConfig({
|
|
140
|
+
disabledPageFragment: options.disabledPageFragment ?? false,
|
|
141
|
+
canDropIn: options.canDropIn,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return pending;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 创建一个相互连线、互相独立的 DSL SDK 实例。
|
|
149
|
+
*
|
|
150
|
+
* @param options 见 {@link SdkOptions}
|
|
151
|
+
*/
|
|
152
|
+
export const createDslSdk = (options: SdkOptions = {}): DslSdk => {
|
|
153
|
+
const reactivity: Reactivity = {
|
|
154
|
+
// Vue 的 reactive 返回 Reactive<T>,与接口中的 T 类型不完全一致,这里做断言
|
|
155
|
+
|
|
156
|
+
reactive: reactive as Reactivity['reactive'],
|
|
157
|
+
|
|
158
|
+
shallowReactive: shallowReactive as Reactivity['shallowReactive'],
|
|
159
|
+
toRaw,
|
|
160
|
+
nextTick,
|
|
161
|
+
...options.reactivity,
|
|
162
|
+
};
|
|
163
|
+
const parseDSL = options.parseDSL ?? defaultParseDSL;
|
|
164
|
+
|
|
165
|
+
// service 之间互相访问用懒 getter,构造期互不依赖,从而解开 editor <-> props / history 的循环
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
167
|
+
const services = {} as { -readonly [K in keyof Services]: Services[K] };
|
|
168
|
+
|
|
169
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
170
|
+
const ctx = {
|
|
171
|
+
reactivity,
|
|
172
|
+
options,
|
|
173
|
+
validateMode: options.validateMode ?? 'strict',
|
|
174
|
+
parseDSL: (code: string) => parseDSL(code),
|
|
175
|
+
ready: Promise.resolve(),
|
|
176
|
+
} as SdkContext;
|
|
177
|
+
|
|
178
|
+
for (const name of SERVICE_NAMES) {
|
|
179
|
+
Object.defineProperty(ctx, name, { get: () => services[name], enumerable: true });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
services.storage = new WebStorage(ctx, options.storage);
|
|
183
|
+
services.history = new History(ctx);
|
|
184
|
+
services.props = new Props(ctx);
|
|
185
|
+
services.events = new Events(ctx);
|
|
186
|
+
services.componentList = new ComponentList(ctx);
|
|
187
|
+
services.dep = new Dep(ctx);
|
|
188
|
+
services.codeBlock = new CodeBlock(ctx);
|
|
189
|
+
services.dataSource = new DataSource(ctx);
|
|
190
|
+
services.editor = new Editor(ctx);
|
|
191
|
+
|
|
192
|
+
const { editor, history } = services;
|
|
193
|
+
|
|
194
|
+
const validate = createValidator(ctx);
|
|
195
|
+
ctx.validator = validate;
|
|
196
|
+
|
|
197
|
+
const pending = registerOptions(services, options);
|
|
198
|
+
|
|
199
|
+
let wiring: WiringHandle | null = null;
|
|
200
|
+
const unbind = () => {
|
|
201
|
+
wiring?.unbind();
|
|
202
|
+
wiring = null;
|
|
203
|
+
};
|
|
204
|
+
const bind = () => {
|
|
205
|
+
wiring ??= bindWiring(ctx);
|
|
206
|
+
return unbind;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
if (options.autoWiring ?? true) {
|
|
210
|
+
bind();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 先恢复历史再设置 root:恢复出的初始基线不会被 set root 重复创建
|
|
214
|
+
if (options.history) {
|
|
215
|
+
history.restore(options.history);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const setDsl = async (dsl: MApp | null) => {
|
|
219
|
+
editor.set('root', dsl ? cloneDeep(dsl) : null);
|
|
220
|
+
await whenWiringSettled(ctx);
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
if (options.dsl) {
|
|
224
|
+
pending.push(setDsl(options.dsl));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
ctx.ready = Promise.all(pending).then(() => undefined);
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
...services,
|
|
231
|
+
validate,
|
|
232
|
+
ctx,
|
|
233
|
+
get ready() {
|
|
234
|
+
return ctx.ready;
|
|
235
|
+
},
|
|
236
|
+
getDsl() {
|
|
237
|
+
const root = reactivity.toRaw(editor.get('root'));
|
|
238
|
+
return root && !isEmpty(root) ? cloneDeep(root) : null;
|
|
239
|
+
},
|
|
240
|
+
setDsl,
|
|
241
|
+
bindWiring: bind,
|
|
242
|
+
serializeHistory: () => history.serialize({ jsonSafe: true }),
|
|
243
|
+
destroy() {
|
|
244
|
+
unbind();
|
|
245
|
+
for (const service of Object.values(services)) {
|
|
246
|
+
service.destroy();
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export { default as BaseService } from './services/BaseService';
|
|
253
|
+
export { default as CodeBlockService } from './services/CodeBlock';
|
|
254
|
+
export { default as ComponentListService } from './services/ComponentList';
|
|
255
|
+
export { default as DataSourceService } from './services/DataSource';
|
|
256
|
+
export { default as DepService } from './services/Dep';
|
|
257
|
+
export { default as EditorService } from './services/Editor';
|
|
258
|
+
export { default as EventsService } from './services/Events';
|
|
259
|
+
export { default as HistoryService } from './services/History';
|
|
260
|
+
export { default as PropsService } from './services/Props';
|
|
261
|
+
export { default as StorageService, MemoryStorage, Protocol } from './services/WebStorage';
|
|
262
|
+
export * from './type';
|
|
263
|
+
export * from './validate';
|
|
264
|
+
export * from './wiring';
|