@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
|
@@ -0,0 +1,501 @@
|
|
|
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 DSL 校验:
|
|
21
|
+
*
|
|
22
|
+
* - 结构合法性:组件类型是否在组件列表中、父节点是否是容器、`canDropIn`、
|
|
23
|
+
* 页面片是否被禁用、id 是否重复、数据源类型是否可用;
|
|
24
|
+
* - 属性合法性:用 `@tmagic/form/headless` 的 `validateForm` 校验节点 / 数据源的表单配置。
|
|
25
|
+
*
|
|
26
|
+
* 模式见 {@link ValidateMode}:`strict` 抛 {@link DslValidationError},`record` 写入
|
|
27
|
+
* `editor.state.invalidNodeIds`,`off` 什么也不做。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { cloneDeep, mergeWith } from 'lodash-es';
|
|
31
|
+
|
|
32
|
+
import type { DataSourceSchema, Id, MContainer, MNode } from '@tmagic/core';
|
|
33
|
+
import { NodeType } from '@tmagic/core';
|
|
34
|
+
import type { FormConfig } from '@tmagic/form/headless';
|
|
35
|
+
import { builtInFields, registerBuiltInFields, validateForm } from '@tmagic/form/headless';
|
|
36
|
+
import { isPage, isPageFragment, isPageOrFragment, toLine, traverseNode } from '@tmagic/utils';
|
|
37
|
+
|
|
38
|
+
import type { CanDropInScene } from '@editor/type';
|
|
39
|
+
import { editorNodeMergeCustomizer } from '@editor/utils/editor';
|
|
40
|
+
|
|
41
|
+
import type { DslValidationIssue, DslValidationResult, SdkContext } from '../type';
|
|
42
|
+
|
|
43
|
+
/** 校验不通过时抛出,`errors` 里是全部问题,调用方(如 agent)可据此修正后重试 */
|
|
44
|
+
export class DslValidationError extends Error {
|
|
45
|
+
public readonly errors: DslValidationIssue[];
|
|
46
|
+
|
|
47
|
+
constructor(errors: DslValidationIssue[]) {
|
|
48
|
+
super(
|
|
49
|
+
`DSL 校验失败:${errors
|
|
50
|
+
.map((item) => `${item.id !== undefined ? `[${item.target}:${item.id}] ` : ''}${item.message}`)
|
|
51
|
+
.join(';')}`,
|
|
52
|
+
);
|
|
53
|
+
this.name = 'DslValidationError';
|
|
54
|
+
this.errors = errors;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 内置的、不需要出现在组件列表里也合法的节点类型 */
|
|
59
|
+
const BUILTIN_NODE_TYPES = new Set<string>([NodeType.ROOT, NodeType.PAGE, NodeType.PAGE_FRAGMENT, 'container']);
|
|
60
|
+
|
|
61
|
+
/** 内置的数据源类型 */
|
|
62
|
+
const BUILTIN_DATA_SOURCE_TYPES = new Set<string>(['base', 'http']);
|
|
63
|
+
|
|
64
|
+
const BR_REG = /<br\s*\/?>/gi;
|
|
65
|
+
|
|
66
|
+
/** `validateForm` 返回的是以 `<br>` 分隔的文案,拆成单条问题 */
|
|
67
|
+
const splitErrorText = (error: string): string[] =>
|
|
68
|
+
error
|
|
69
|
+
.split(BR_REG)
|
|
70
|
+
.map((item) => item.trim())
|
|
71
|
+
.filter(Boolean);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* `record` 模式下,校验结果要等到修改落进 DSL 之后再写入 `invalidNodeIds`(修改过程中会先清掉旧记录),
|
|
75
|
+
* 因此 guard 返回一个提交函数交给调用方在合适的时机调用;`strict` 抛错,`off` 返回空。
|
|
76
|
+
*/
|
|
77
|
+
export type GuardCommit = (() => void) | undefined;
|
|
78
|
+
|
|
79
|
+
export interface Validator {
|
|
80
|
+
/** 校验 DSL 中已有节点:结构(类型 / 父子关系)+ 属性表单 */
|
|
81
|
+
node(id: Id): Promise<DslValidationResult>;
|
|
82
|
+
/** 校验 DSL 中已有的数据源 */
|
|
83
|
+
dataSource(id: Id): Promise<DslValidationResult>;
|
|
84
|
+
/** 校验整份 DSL */
|
|
85
|
+
dsl(): Promise<DslValidationResult>;
|
|
86
|
+
|
|
87
|
+
/** 校验一个节点配置的属性表单(不要求它已经在 DSL 中) */
|
|
88
|
+
checkNodeProps(node: MNode): Promise<DslValidationIssue[]>;
|
|
89
|
+
/** 同步的结构校验:新增 */
|
|
90
|
+
checkAddStructure(node: MNode, parent: MContainer | null | undefined): DslValidationIssue[];
|
|
91
|
+
/** 同步的结构校验:移动 */
|
|
92
|
+
checkMoveStructure(sourceIds: Id[], target: MContainer, scene: CanDropInScene): DslValidationIssue[];
|
|
93
|
+
/** 同步的结构校验:数据源配置 */
|
|
94
|
+
checkDataSourceStructure(config: DataSourceSchema, isNew: boolean): DslValidationIssue[];
|
|
95
|
+
|
|
96
|
+
/** 供 service 在修改前调用:按 `validateMode` 抛出 / 记录 / 忽略。 */
|
|
97
|
+
guardAdd(items: { node: MNode; parentNode: MContainer }[]): Promise<GuardCommit>;
|
|
98
|
+
/** `replace` 与 `editor.update` 的同名选项一致:为 true 时整节点替换,否则与旧节点深合并 */
|
|
99
|
+
guardUpdate(nodes: MNode[], options?: { replace?: boolean }): Promise<GuardCommit>;
|
|
100
|
+
guardMove(sourceIds: Id[], target: MContainer, scene: CanDropInScene): Promise<void>;
|
|
101
|
+
guardDataSource(config: DataSourceSchema, isNew: boolean): void;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const toResult = (errors: DslValidationIssue[]): DslValidationResult => ({
|
|
105
|
+
valid: errors.length === 0,
|
|
106
|
+
errors,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const structureIssue = (
|
|
110
|
+
target: DslValidationIssue['target'],
|
|
111
|
+
id: Id | undefined,
|
|
112
|
+
message: string,
|
|
113
|
+
extra: Partial<DslValidationIssue> = {},
|
|
114
|
+
): DslValidationIssue => ({ target, id, kind: 'structure', message, ...extra });
|
|
115
|
+
|
|
116
|
+
const nodeIssue = (id: Id | undefined, message: string, extra?: Partial<DslValidationIssue>) =>
|
|
117
|
+
structureIssue('node', id, message, extra);
|
|
118
|
+
|
|
119
|
+
/** 节点及其所有子孙(先序) */
|
|
120
|
+
const flattenNode = (node: MNode): MNode[] => {
|
|
121
|
+
const nodes: MNode[] = [];
|
|
122
|
+
traverseNode(node, (item) => {
|
|
123
|
+
nodes.push(item);
|
|
124
|
+
});
|
|
125
|
+
return nodes;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const isEmptyId = (id: Id | undefined) => id === undefined || id === null || id === '';
|
|
129
|
+
|
|
130
|
+
const DUPLICATE_ID_SUGGESTION = '请使用唯一的 id,或不传 id 由编辑器自动生成';
|
|
131
|
+
|
|
132
|
+
export const createValidator = (ctx: SdkContext): Validator => {
|
|
133
|
+
const mode = () => ctx.validateMode;
|
|
134
|
+
|
|
135
|
+
let builtInRegistered = false;
|
|
136
|
+
const ensureFields = () => {
|
|
137
|
+
if (builtInRegistered) return;
|
|
138
|
+
registerBuiltInFields(builtInFields);
|
|
139
|
+
builtInRegistered = true;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
let cachedFormContext: object | undefined;
|
|
143
|
+
const formContext = () =>
|
|
144
|
+
(cachedFormContext ??= {
|
|
145
|
+
services: {
|
|
146
|
+
editorService: ctx.editor,
|
|
147
|
+
propsService: ctx.props,
|
|
148
|
+
historyService: ctx.history,
|
|
149
|
+
dataSourceService: ctx.dataSource,
|
|
150
|
+
codeBlockService: ctx.codeBlock,
|
|
151
|
+
depService: ctx.dep,
|
|
152
|
+
storageService: ctx.storage,
|
|
153
|
+
eventsService: ctx.events,
|
|
154
|
+
componentListService: ctx.componentList,
|
|
155
|
+
},
|
|
156
|
+
stage: null,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const knownTypes = (): Set<string> | null => {
|
|
160
|
+
const set = new Set<string>();
|
|
161
|
+
for (const group of ctx.componentList.getList()) {
|
|
162
|
+
for (const item of group.items ?? []) {
|
|
163
|
+
set.add(toLine(item.type));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// 没有登记组件列表时无从判断,不做类型校验
|
|
167
|
+
return set.size ? set : null;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const isKnownType = (type: string, known: Set<string> | null) =>
|
|
171
|
+
BUILTIN_NODE_TYPES.has(type) || BUILTIN_NODE_TYPES.has(toLine(type)) || !known || known.has(toLine(type));
|
|
172
|
+
|
|
173
|
+
/** 节点类型合法性:在组件列表中、没有被禁用的页面片 */
|
|
174
|
+
const checkNodeType = (node: MNode, known = knownTypes()): DslValidationIssue[] => {
|
|
175
|
+
const issues: DslValidationIssue[] = [];
|
|
176
|
+
|
|
177
|
+
if (!isKnownType(node.type || '', known)) {
|
|
178
|
+
issues.push(
|
|
179
|
+
nodeIssue(node.id, `组件类型「${node.type}」不在组件列表中`, {
|
|
180
|
+
path: 'type',
|
|
181
|
+
suggestion: `可用类型:${[...(known ?? [])].join('、')}`,
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (isPageFragment(node) && ctx.editor.getConfig().disabledPageFragment) {
|
|
187
|
+
issues.push(nodeIssue(node.id, '当前配置禁用了页面片(disabledPageFragment)', { path: 'type' }));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return issues;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/** 新增的节点 id 在这批节点内、或与当前 DSL 里已有节点重复 */
|
|
194
|
+
const findDuplicateIds = (nodes: MNode[]): DslValidationIssue[] => {
|
|
195
|
+
const issues: DslValidationIssue[] = [];
|
|
196
|
+
const seen = new Set<Id>();
|
|
197
|
+
|
|
198
|
+
for (const { id } of nodes.flatMap(flattenNode)) {
|
|
199
|
+
if (isEmptyId(id)) continue;
|
|
200
|
+
|
|
201
|
+
if (seen.has(id)) {
|
|
202
|
+
issues.push(nodeIssue(id, `节点 id「${id}」重复`, { suggestion: DUPLICATE_ID_SUGGESTION }));
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
seen.add(id);
|
|
207
|
+
if (ctx.editor.getNodeById(id, false)) {
|
|
208
|
+
issues.push(nodeIssue(id, `节点 id「${id}」已存在于当前 DSL`, { suggestion: DUPLICATE_ID_SUGGESTION }));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return issues;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const canDropIn = (sourceIds: Id[], target: MContainer, scene: CanDropInScene): DslValidationIssue | null => {
|
|
216
|
+
const fn = ctx.editor.getConfig().canDropIn;
|
|
217
|
+
if (!fn) return null;
|
|
218
|
+
|
|
219
|
+
const result = fn(sourceIds, target.id, scene);
|
|
220
|
+
if (result === false) {
|
|
221
|
+
return nodeIssue(target.id, `不允许放入容器「${target.id}」(canDropIn 返回 false)`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (typeof result !== 'string' && typeof result !== 'number') return null;
|
|
225
|
+
|
|
226
|
+
// 重定向:被重定向的目标必须存在,且同样是容器
|
|
227
|
+
const redirected = ctx.editor.getNodeById(result, false);
|
|
228
|
+
if (redirected && Array.isArray((redirected as MContainer).items)) return null;
|
|
229
|
+
|
|
230
|
+
return nodeIssue(target.id, `canDropIn 将容器「${target.id}」重定向到「${result}」,但该节点不存在或不是容器`);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/** 新增节点对父节点的要求 */
|
|
234
|
+
const checkAddParent = (node: MNode, parent: MContainer | null | undefined): DslValidationIssue | null => {
|
|
235
|
+
if (!parent) return nodeIssue(node.id, '未找到父节点');
|
|
236
|
+
if (!Array.isArray(parent.items)) return nodeIssue(node.id, `父节点「${parent.id}」不是容器(没有 items)`);
|
|
237
|
+
|
|
238
|
+
if (isPageOrFragment(node)) {
|
|
239
|
+
return parent.type === NodeType.ROOT ? null : nodeIssue(node.id, '页面 / 页面片只能添加到 app 下');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (parent.type === NodeType.ROOT) return nodeIssue(node.id, 'app 下只能添加页面或页面片');
|
|
243
|
+
|
|
244
|
+
return canDropIn([], parent, 'stage-add');
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const checkAddStructure: Validator['checkAddStructure'] = (node, parent) => {
|
|
248
|
+
const known = knownTypes();
|
|
249
|
+
|
|
250
|
+
const issues = flattenNode(node).flatMap((item) =>
|
|
251
|
+
item.type ? checkNodeType(item, known) : [nodeIssue(item.id, '组件类型不能为空')],
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const parentIssue = checkAddParent(node, parent);
|
|
255
|
+
if (parentIssue) issues.push(parentIssue);
|
|
256
|
+
|
|
257
|
+
return issues;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/** 被移动的节点:必须存在,且不能移动到自己或子孙里 */
|
|
261
|
+
const checkMoveSource = (id: Id, target: MContainer): DslValidationIssue[] => {
|
|
262
|
+
const source = ctx.editor.getNodeById(id, false);
|
|
263
|
+
if (!source) return [nodeIssue(id, `要移动的节点「${id}」不存在`)];
|
|
264
|
+
if (isPageOrFragment(source)) return [];
|
|
265
|
+
|
|
266
|
+
return flattenNode(source).some((item) => item.id === target.id)
|
|
267
|
+
? [nodeIssue(id, `不能把节点「${id}」移动到它自己或子节点里`)]
|
|
268
|
+
: [];
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const checkMoveStructure: Validator['checkMoveStructure'] = (sourceIds, target, scene) => {
|
|
272
|
+
if (!Array.isArray(target.items)) {
|
|
273
|
+
return [nodeIssue(target.id, `目标节点「${target.id}」不是容器(没有 items)`)];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const issues = sourceIds.flatMap((id) => checkMoveSource(id, target));
|
|
277
|
+
|
|
278
|
+
const targetIssue =
|
|
279
|
+
target.type === NodeType.ROOT
|
|
280
|
+
? nodeIssue(target.id, 'app 下只能放页面或页面片')
|
|
281
|
+
: canDropIn(sourceIds, target, scene);
|
|
282
|
+
if (targetIssue) issues.push(targetIssue);
|
|
283
|
+
|
|
284
|
+
return issues;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// add 在 id 冲突时会自动重新生成 id,不算错误,所以不区分是否新增
|
|
288
|
+
const checkDataSourceStructure: Validator['checkDataSourceStructure'] = (config) => {
|
|
289
|
+
const type = config.type || 'base';
|
|
290
|
+
|
|
291
|
+
const known = new Set<string>([
|
|
292
|
+
...BUILTIN_DATA_SOURCE_TYPES,
|
|
293
|
+
...ctx.dataSource.get('datasourceTypeList').map((item) => toLine(item.type)),
|
|
294
|
+
...Object.keys(ctx.dataSource.get('configs')),
|
|
295
|
+
]);
|
|
296
|
+
|
|
297
|
+
if (known.has(toLine(type))) return [];
|
|
298
|
+
|
|
299
|
+
return [
|
|
300
|
+
structureIssue('data-source', config.id, `数据源类型「${type}」不可用`, {
|
|
301
|
+
path: 'type',
|
|
302
|
+
suggestion: `可用类型:${[...known].join('、')}`,
|
|
303
|
+
}),
|
|
304
|
+
];
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/** 用 `validateForm` 校验属性表单,配置不存在时不校验;校验本身出错也作为一条问题返回 */
|
|
308
|
+
const checkProps = async (
|
|
309
|
+
target: 'node' | 'data-source',
|
|
310
|
+
id: Id,
|
|
311
|
+
values: MNode | DataSourceSchema,
|
|
312
|
+
getConfig: () => Promise<FormConfig | undefined> | FormConfig | undefined,
|
|
313
|
+
options: { useFieldTextInError?: boolean } = {},
|
|
314
|
+
): Promise<DslValidationIssue[]> => {
|
|
315
|
+
const toIssues = (messages: string[]): DslValidationIssue[] =>
|
|
316
|
+
messages.map((message) => ({ target, id, kind: 'props', message }));
|
|
317
|
+
|
|
318
|
+
try {
|
|
319
|
+
const config = await getConfig();
|
|
320
|
+
if (!config?.length) return [];
|
|
321
|
+
|
|
322
|
+
ensureFields();
|
|
323
|
+
|
|
324
|
+
const error = await validateForm({
|
|
325
|
+
config,
|
|
326
|
+
typeMatchValid: true,
|
|
327
|
+
initValues: values,
|
|
328
|
+
context: formContext() as any,
|
|
329
|
+
...options,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
return toIssues(splitErrorText(error));
|
|
333
|
+
} catch (e: any) {
|
|
334
|
+
return toIssues([`属性校验异常:${e?.message ?? e}`]);
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const checkNodeProps: Validator['checkNodeProps'] = async (node) => {
|
|
339
|
+
if (!node.type || node.type === NodeType.ROOT) return [];
|
|
340
|
+
|
|
341
|
+
return checkProps('node', node.id, node, () => ctx.props.getPropsConfig(node.type!, { node }), {
|
|
342
|
+
useFieldTextInError: true,
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const checkDataSourceProps = (config: DataSourceSchema) =>
|
|
347
|
+
checkProps('data-source', config.id, config, () => ctx.dataSource.getFormConfig(config.type || 'base'));
|
|
348
|
+
|
|
349
|
+
/** 已存在节点的结构校验:类型合法 */
|
|
350
|
+
const checkExistingNodeStructure = (node: MNode) => checkNodeType(node);
|
|
351
|
+
|
|
352
|
+
const validateNode = async (id: Id): Promise<DslValidationIssue[]> => {
|
|
353
|
+
const node = ctx.editor.getNodeById(id, false);
|
|
354
|
+
if (!node) return [nodeIssue(id, `节点「${id}」不存在`)];
|
|
355
|
+
|
|
356
|
+
const propsIssues = !isPage(node) || node.items ? await checkNodeProps(node) : [];
|
|
357
|
+
return [...checkExistingNodeStructure(node), ...propsIssues];
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const validateDataSource = async (id: Id): Promise<DslValidationIssue[]> => {
|
|
361
|
+
const ds = ctx.dataSource.getDataSourceById(`${id}`);
|
|
362
|
+
if (!ds) return [structureIssue('data-source', id, `数据源「${id}」不存在`)];
|
|
363
|
+
|
|
364
|
+
return [...checkDataSourceStructure(ds, false), ...(await checkDataSourceProps(ds))];
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
/** 按模式处理问题:strict 抛错;record 返回写入 invalidNodeIds 的提交函数 */
|
|
368
|
+
const handle = (issues: DslValidationIssue[], recordIds: Id[] = []): GuardCommit => {
|
|
369
|
+
if (mode() === 'strict') {
|
|
370
|
+
if (issues.length) throw new DslValidationError(issues);
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (mode() !== 'record') return undefined;
|
|
375
|
+
|
|
376
|
+
return () => {
|
|
377
|
+
for (const id of recordIds) {
|
|
378
|
+
ctx.editor.deleteInvalidNode(id, 'props');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const grouped = new Map<Id, string[]>();
|
|
382
|
+
for (const issue of issues) {
|
|
383
|
+
if (issue.target !== 'node' || issue.id === undefined) continue;
|
|
384
|
+
grouped.set(issue.id, [...(grouped.get(issue.id) ?? []), issue.message]);
|
|
385
|
+
}
|
|
386
|
+
for (const [id, messages] of grouped) {
|
|
387
|
+
ctx.editor.setInvalidNode(id, 'props', messages.join('<br>'));
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
/** update 的单个节点:类型不可变,并校验 update 实际会写入的结果 */
|
|
393
|
+
const checkUpdatedNode = async (
|
|
394
|
+
node: MNode,
|
|
395
|
+
replace: boolean,
|
|
396
|
+
): Promise<{ id?: Id; issues: DslValidationIssue[] }> => {
|
|
397
|
+
const old = ctx.editor.getNodeById(node.id, false);
|
|
398
|
+
if (!old) return { issues: [nodeIssue(node.id, `节点「${node.id}」不存在`)] };
|
|
399
|
+
if (old.type === NodeType.ROOT) return { issues: [] };
|
|
400
|
+
|
|
401
|
+
const issues: DslValidationIssue[] = [];
|
|
402
|
+
|
|
403
|
+
if (node.type && node.type !== old.type) {
|
|
404
|
+
issues.push(nodeIssue(node.id, `不允许修改节点类型(${old.type} -> ${node.type})`, { path: 'type' }));
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// 与 `Editor.doUpdate` 一致:replace 时整节点替换,否则深合并
|
|
408
|
+
const merged: MNode = replace
|
|
409
|
+
? cloneDeep(node)
|
|
410
|
+
: mergeWith(cloneDeep(ctx.reactivity.toRaw(old)), cloneDeep(node), editorNodeMergeCustomizer);
|
|
411
|
+
issues.push(...checkExistingNodeStructure(merged), ...(await checkNodeProps(merged)));
|
|
412
|
+
|
|
413
|
+
return { id: node.id, issues };
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
return {
|
|
417
|
+
async node(id) {
|
|
418
|
+
return toResult(await validateNode(id));
|
|
419
|
+
},
|
|
420
|
+
|
|
421
|
+
async dataSource(id) {
|
|
422
|
+
return toResult(await validateDataSource(id));
|
|
423
|
+
},
|
|
424
|
+
|
|
425
|
+
async dsl() {
|
|
426
|
+
const root = ctx.editor.get('root');
|
|
427
|
+
if (!root) return toResult([structureIssue('dsl', undefined, 'DSL 为空')]);
|
|
428
|
+
|
|
429
|
+
const ids = flattenNode(root)
|
|
430
|
+
.filter((node) => node.type !== NodeType.ROOT)
|
|
431
|
+
.map((node) => node.id);
|
|
432
|
+
|
|
433
|
+
// 重复 id:同一个 DSL 内出现两次
|
|
434
|
+
const seen = new Set<Id>();
|
|
435
|
+
const issues = ids
|
|
436
|
+
.filter((id) => {
|
|
437
|
+
const duplicated = seen.has(id);
|
|
438
|
+
seen.add(id);
|
|
439
|
+
return duplicated;
|
|
440
|
+
})
|
|
441
|
+
.map((id) => nodeIssue(id, `节点 id「${id}」重复`, { suggestion: DUPLICATE_ID_SUGGESTION }));
|
|
442
|
+
|
|
443
|
+
for (const id of seen) {
|
|
444
|
+
issues.push(...(await validateNode(id)));
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
for (const ds of ctx.dataSource.get('dataSources')) {
|
|
448
|
+
issues.push(...(await validateDataSource(ds.id)));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return toResult(issues);
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
checkNodeProps,
|
|
455
|
+
checkAddStructure,
|
|
456
|
+
checkMoveStructure,
|
|
457
|
+
checkDataSourceStructure,
|
|
458
|
+
|
|
459
|
+
async guardAdd(items) {
|
|
460
|
+
if (mode() === 'off') return undefined;
|
|
461
|
+
|
|
462
|
+
const nodes = items.flatMap(({ node }) => flattenNode(node));
|
|
463
|
+
|
|
464
|
+
const issues = [
|
|
465
|
+
...findDuplicateIds(items.map(({ node }) => node)),
|
|
466
|
+
...items.flatMap(({ node, parentNode }) => checkAddStructure(node, parentNode)),
|
|
467
|
+
];
|
|
468
|
+
|
|
469
|
+
for (const node of nodes) {
|
|
470
|
+
issues.push(...(await checkNodeProps(node)));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return handle(
|
|
474
|
+
issues,
|
|
475
|
+
nodes.map((node) => node.id),
|
|
476
|
+
);
|
|
477
|
+
},
|
|
478
|
+
|
|
479
|
+
async guardUpdate(nodes, { replace = false } = {}) {
|
|
480
|
+
if (mode() === 'off') return undefined;
|
|
481
|
+
|
|
482
|
+
const results = await Promise.all(nodes.map((node) => checkUpdatedNode(node, replace)));
|
|
483
|
+
|
|
484
|
+
return handle(
|
|
485
|
+
results.flatMap(({ issues }) => issues),
|
|
486
|
+
results.flatMap(({ id }) => (id === undefined ? [] : [id])),
|
|
487
|
+
);
|
|
488
|
+
},
|
|
489
|
+
|
|
490
|
+
// 移动 / 数据源只有结构问题,没有可挂载到节点属性上的错误,因此只在 strict 模式下生效
|
|
491
|
+
async guardMove(sourceIds, target, scene) {
|
|
492
|
+
if (mode() !== 'strict') return;
|
|
493
|
+
handle(checkMoveStructure(sourceIds, target, scene));
|
|
494
|
+
},
|
|
495
|
+
|
|
496
|
+
guardDataSource(config, isNew) {
|
|
497
|
+
if (mode() !== 'strict') return;
|
|
498
|
+
handle(checkDataSourceStructure(config, isNew));
|
|
499
|
+
},
|
|
500
|
+
};
|
|
501
|
+
};
|