@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
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { HookType, NODE_CONDS_KEY, NODE_CONDS_RESULT_KEY, NODE_DISABLE_CODE_BLOCK_KEY, NODE_DISABLE_DATA_SOURCE_KEY } from "@tmagic/core";
|
|
2
|
+
//#region packages/editor/src/sdk/utils/props.ts
|
|
3
|
+
var arrayOptions = [{
|
|
4
|
+
text: "包含",
|
|
5
|
+
value: "include"
|
|
6
|
+
}, {
|
|
7
|
+
text: "不包含",
|
|
8
|
+
value: "not_include"
|
|
9
|
+
}];
|
|
10
|
+
var eqOptions = [{
|
|
11
|
+
text: "等于",
|
|
12
|
+
value: "="
|
|
13
|
+
}, {
|
|
14
|
+
text: "不等于",
|
|
15
|
+
value: "!="
|
|
16
|
+
}];
|
|
17
|
+
var numberOptions = [
|
|
18
|
+
{
|
|
19
|
+
text: "大于",
|
|
20
|
+
value: ">"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
text: "大于等于",
|
|
24
|
+
value: ">="
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
text: "小于",
|
|
28
|
+
value: "<"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
text: "小于等于",
|
|
32
|
+
value: "<="
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
text: "在范围内",
|
|
36
|
+
value: "between"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
text: "不在范围内",
|
|
40
|
+
value: "not_between"
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
var booleanOptions = [{
|
|
44
|
+
text: "是",
|
|
45
|
+
value: "is"
|
|
46
|
+
}, {
|
|
47
|
+
text: "不是",
|
|
48
|
+
value: "not"
|
|
49
|
+
}];
|
|
50
|
+
/** 按字段类型返回条件运算符选项(UI 与 typeMatch 校验共用) */
|
|
51
|
+
var getCondOpOptionsByFieldType = (type) => {
|
|
52
|
+
if (type === "array") return arrayOptions;
|
|
53
|
+
if (type === "boolean" || type === "null") return booleanOptions;
|
|
54
|
+
if (type === "number") return [...eqOptions, ...numberOptions];
|
|
55
|
+
if (type === "string") return [...arrayOptions, ...eqOptions];
|
|
56
|
+
return [
|
|
57
|
+
...arrayOptions,
|
|
58
|
+
...eqOptions,
|
|
59
|
+
...numberOptions
|
|
60
|
+
];
|
|
61
|
+
};
|
|
62
|
+
var styleTabConfig = {
|
|
63
|
+
title: "样式",
|
|
64
|
+
display: ({ services }) => !(services?.uiService?.get("showStylePanel") ?? true),
|
|
65
|
+
items: [{
|
|
66
|
+
name: "style",
|
|
67
|
+
labelWidth: "100px",
|
|
68
|
+
type: "style-setter",
|
|
69
|
+
items: [{ names: [
|
|
70
|
+
"display",
|
|
71
|
+
"flexDirection",
|
|
72
|
+
"justifyContent",
|
|
73
|
+
"alignItems",
|
|
74
|
+
"flexWrap",
|
|
75
|
+
"marginTop",
|
|
76
|
+
"marginRight",
|
|
77
|
+
"marginBottom",
|
|
78
|
+
"marginLeft",
|
|
79
|
+
"paddingTop",
|
|
80
|
+
"paddingRight",
|
|
81
|
+
"paddingBottom",
|
|
82
|
+
"paddingLeft",
|
|
83
|
+
"width",
|
|
84
|
+
"height",
|
|
85
|
+
"overflow",
|
|
86
|
+
"fontSize",
|
|
87
|
+
"lineHeight",
|
|
88
|
+
"fontWeight",
|
|
89
|
+
"color",
|
|
90
|
+
"textAlign",
|
|
91
|
+
"backgroundColor",
|
|
92
|
+
"backgroundImage",
|
|
93
|
+
"backgroundSize",
|
|
94
|
+
"backgroundPosition",
|
|
95
|
+
"backgroundRepeat",
|
|
96
|
+
"position",
|
|
97
|
+
"zIndex",
|
|
98
|
+
"top",
|
|
99
|
+
"right",
|
|
100
|
+
"bottom",
|
|
101
|
+
"left",
|
|
102
|
+
"borderRadius",
|
|
103
|
+
"borderTopWidth",
|
|
104
|
+
"borderTopStyle",
|
|
105
|
+
"borderTopColor",
|
|
106
|
+
"borderRightColor",
|
|
107
|
+
"borderRightWidth",
|
|
108
|
+
"borderRightStyle",
|
|
109
|
+
"borderRightColor",
|
|
110
|
+
"borderBottomWidth",
|
|
111
|
+
"borderBottomStyle",
|
|
112
|
+
"borderBottomColor",
|
|
113
|
+
"borderLeftStyle",
|
|
114
|
+
"borderLeftWidth",
|
|
115
|
+
"borderLeftColor",
|
|
116
|
+
"borderWidth",
|
|
117
|
+
"borderStyle",
|
|
118
|
+
"borderColor",
|
|
119
|
+
"opacity"
|
|
120
|
+
] }, {
|
|
121
|
+
name: "transform",
|
|
122
|
+
defaultValue: () => ({})
|
|
123
|
+
}]
|
|
124
|
+
}]
|
|
125
|
+
};
|
|
126
|
+
var eventTabConfig = {
|
|
127
|
+
title: "事件",
|
|
128
|
+
items: [{
|
|
129
|
+
name: "events",
|
|
130
|
+
src: "component",
|
|
131
|
+
labelWidth: "100px",
|
|
132
|
+
type: "event-select",
|
|
133
|
+
defaultValue: [],
|
|
134
|
+
rules: [{ typeMatch: true }]
|
|
135
|
+
}]
|
|
136
|
+
};
|
|
137
|
+
var advancedTabConfig = {
|
|
138
|
+
title: "高级",
|
|
139
|
+
items: [
|
|
140
|
+
{
|
|
141
|
+
name: NODE_DISABLE_CODE_BLOCK_KEY,
|
|
142
|
+
text: "禁用代码块",
|
|
143
|
+
type: "switch",
|
|
144
|
+
defaultValue: false,
|
|
145
|
+
extra: "开启后,配置的代码块将不会被执行"
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
name: NODE_DISABLE_DATA_SOURCE_KEY,
|
|
149
|
+
text: "禁用数据源",
|
|
150
|
+
type: "switch",
|
|
151
|
+
defaultValue: false,
|
|
152
|
+
extra: "开启后,组件内配置的数据源相关配置将不会被编译,显隐条件将失效"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "created",
|
|
156
|
+
text: "Created",
|
|
157
|
+
labelPosition: "top",
|
|
158
|
+
type: "code-select",
|
|
159
|
+
titleExtra: "组件初始化时执行",
|
|
160
|
+
rules: [{
|
|
161
|
+
typeMatch: true,
|
|
162
|
+
trigger: "change"
|
|
163
|
+
}, { validator: ({ value, callback }) => {
|
|
164
|
+
if (value && value.hookType !== HookType.CODE) return callback("hookType 必须是 code");
|
|
165
|
+
callback();
|
|
166
|
+
} }]
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
name: "mounted",
|
|
170
|
+
text: "Mounted",
|
|
171
|
+
labelPosition: "top",
|
|
172
|
+
type: "code-select",
|
|
173
|
+
titleExtra: "组件挂载到dom时执行",
|
|
174
|
+
rules: [{
|
|
175
|
+
typeMatch: true,
|
|
176
|
+
trigger: "change"
|
|
177
|
+
}, { validator: ({ value, callback }) => {
|
|
178
|
+
if (value && value.hookType !== HookType.CODE) return callback("hookType 必须是 code");
|
|
179
|
+
callback();
|
|
180
|
+
} }]
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
name: "display",
|
|
184
|
+
text: "Display",
|
|
185
|
+
titleExtra: "控制组件是否渲染,关系的代码块返回值为false时不渲染",
|
|
186
|
+
labelPosition: "top",
|
|
187
|
+
type: "code-select",
|
|
188
|
+
rules: [{
|
|
189
|
+
typeMatch: true,
|
|
190
|
+
trigger: "change"
|
|
191
|
+
}, { validator: ({ value, callback }) => {
|
|
192
|
+
if (value && value.hookType !== HookType.CODE) return callback("hookType 必须是 code");
|
|
193
|
+
callback();
|
|
194
|
+
} }]
|
|
195
|
+
}
|
|
196
|
+
]
|
|
197
|
+
};
|
|
198
|
+
var displayTabConfig = {
|
|
199
|
+
title: "显示条件",
|
|
200
|
+
display: (_state, { model }) => model.type !== "page",
|
|
201
|
+
items: [
|
|
202
|
+
{
|
|
203
|
+
name: NODE_CONDS_RESULT_KEY,
|
|
204
|
+
type: "select",
|
|
205
|
+
text: "条件成立时",
|
|
206
|
+
defaultValue: false,
|
|
207
|
+
options: [{
|
|
208
|
+
text: "显示",
|
|
209
|
+
value: false
|
|
210
|
+
}, {
|
|
211
|
+
text: "隐藏",
|
|
212
|
+
value: true
|
|
213
|
+
}],
|
|
214
|
+
extra: (_state, { model }) => `条件成立时${model[NODE_CONDS_RESULT_KEY] ? "隐藏" : "显示"},不成立时${model[NODE_CONDS_RESULT_KEY] ? "显示" : "隐藏"};<br />同一条件组内的所有条件配置同时成立时表示该条件组成立,任意一个条件组成立时表示条件成立(条件组内为且的关系,条件组间为或的关系);<br />条件为空时表示成立;`
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
text: "条件组配置",
|
|
218
|
+
static: true,
|
|
219
|
+
className: "display-conds-title"
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
type: "display-conds",
|
|
223
|
+
name: NODE_CONDS_KEY,
|
|
224
|
+
titlePrefix: "条件组",
|
|
225
|
+
defaultValue: [],
|
|
226
|
+
rules: [{ typeMatch: true }]
|
|
227
|
+
}
|
|
228
|
+
]
|
|
229
|
+
};
|
|
230
|
+
var fillConfig = (config = [], { labelWidth = "80px", disabledDataSource = false, disabledCodeBlock = false, copyText, notify } = {}) => {
|
|
231
|
+
const propsConfig = [];
|
|
232
|
+
if (!config.find((item) => "name" in item && item.name === "type")) propsConfig.push({
|
|
233
|
+
text: "type",
|
|
234
|
+
name: "type",
|
|
235
|
+
type: "hidden"
|
|
236
|
+
});
|
|
237
|
+
if (!config.find((item) => "name" in item && item.name === "id")) propsConfig.push({
|
|
238
|
+
name: "id",
|
|
239
|
+
text: "ID",
|
|
240
|
+
type: "text",
|
|
241
|
+
static: true,
|
|
242
|
+
append: {
|
|
243
|
+
type: "icon",
|
|
244
|
+
text: "https://vip.image.video.qpic.cn/vupload/20260615/36cf7e1781493669935.svg",
|
|
245
|
+
extra: "复制",
|
|
246
|
+
handler: (vm, { model }) => {
|
|
247
|
+
if (!copyText) return;
|
|
248
|
+
Promise.resolve(copyText(`${model.id}`)).then(() => {
|
|
249
|
+
notify?.("success", "已复制");
|
|
250
|
+
}).catch(() => {
|
|
251
|
+
notify?.("error", "复制失败");
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
if (!config.find((item) => "name" in item && item.name === "name")) propsConfig.push({
|
|
257
|
+
name: "name",
|
|
258
|
+
text: "组件名称"
|
|
259
|
+
});
|
|
260
|
+
const noCodeAdvancedTabItems = advancedTabConfig.items.filter((item) => "type" in item && item.type !== "code-select");
|
|
261
|
+
if (noCodeAdvancedTabItems.length > 0 && disabledCodeBlock) advancedTabConfig.items = noCodeAdvancedTabItems;
|
|
262
|
+
const tabConfig = {
|
|
263
|
+
type: "tab",
|
|
264
|
+
labelWidth,
|
|
265
|
+
className: "magic-right-panel-tabs-top",
|
|
266
|
+
items: [
|
|
267
|
+
{
|
|
268
|
+
title: "属性",
|
|
269
|
+
items: [...propsConfig, ...config]
|
|
270
|
+
},
|
|
271
|
+
{ ...styleTabConfig },
|
|
272
|
+
{ ...eventTabConfig }
|
|
273
|
+
]
|
|
274
|
+
};
|
|
275
|
+
if (!disabledCodeBlock) tabConfig.items.push({ ...advancedTabConfig });
|
|
276
|
+
else if (noCodeAdvancedTabItems.length > 0) tabConfig.items.push({ ...advancedTabConfig });
|
|
277
|
+
if (!disabledDataSource) tabConfig.items.push({ ...displayTabConfig });
|
|
278
|
+
return [tabConfig];
|
|
279
|
+
};
|
|
280
|
+
//#endregion
|
|
281
|
+
export { advancedTabConfig, arrayOptions, booleanOptions, displayTabConfig, eqOptions, eventTabConfig, fillConfig, getCondOpOptionsByFieldType, numberOptions, styleTabConfig };
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { editorNodeMergeCustomizer } from "../../utils/editor.js";
|
|
2
|
+
import { isPage, isPageFragment, isPageOrFragment, toLine, traverseNode } from "@tmagic/utils";
|
|
3
|
+
import { NodeType } from "@tmagic/core";
|
|
4
|
+
import { cloneDeep as cloneDeep$1, mergeWith } from "lodash-es";
|
|
5
|
+
import { builtInFields, registerBuiltInFields, validateForm } from "@tmagic/form/headless";
|
|
6
|
+
//#region packages/editor/src/sdk/validate/index.ts
|
|
7
|
+
/**
|
|
8
|
+
* @fileoverview DSL 校验:
|
|
9
|
+
*
|
|
10
|
+
* - 结构合法性:组件类型是否在组件列表中、父节点是否是容器、`canDropIn`、
|
|
11
|
+
* 页面片是否被禁用、id 是否重复、数据源类型是否可用;
|
|
12
|
+
* - 属性合法性:用 `@tmagic/form/headless` 的 `validateForm` 校验节点 / 数据源的表单配置。
|
|
13
|
+
*
|
|
14
|
+
* 模式见 {@link ValidateMode}:`strict` 抛 {@link DslValidationError},`record` 写入
|
|
15
|
+
* `editor.state.invalidNodeIds`,`off` 什么也不做。
|
|
16
|
+
*/
|
|
17
|
+
/** 校验不通过时抛出,`errors` 里是全部问题,调用方(如 agent)可据此修正后重试 */
|
|
18
|
+
var DslValidationError = class extends Error {
|
|
19
|
+
errors;
|
|
20
|
+
constructor(errors) {
|
|
21
|
+
super(`DSL 校验失败:${errors.map((item) => `${item.id !== void 0 ? `[${item.target}:${item.id}] ` : ""}${item.message}`).join(";")}`);
|
|
22
|
+
this.name = "DslValidationError";
|
|
23
|
+
this.errors = errors;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
/** 内置的、不需要出现在组件列表里也合法的节点类型 */
|
|
27
|
+
var BUILTIN_NODE_TYPES = /* @__PURE__ */ new Set([
|
|
28
|
+
NodeType.ROOT,
|
|
29
|
+
NodeType.PAGE,
|
|
30
|
+
NodeType.PAGE_FRAGMENT,
|
|
31
|
+
"container"
|
|
32
|
+
]);
|
|
33
|
+
/** 内置的数据源类型 */
|
|
34
|
+
var BUILTIN_DATA_SOURCE_TYPES = /* @__PURE__ */ new Set(["base", "http"]);
|
|
35
|
+
var BR_REG = /<br\s*\/?>/gi;
|
|
36
|
+
/** `validateForm` 返回的是以 `<br>` 分隔的文案,拆成单条问题 */
|
|
37
|
+
var splitErrorText = (error) => error.split(BR_REG).map((item) => item.trim()).filter(Boolean);
|
|
38
|
+
var toResult = (errors) => ({
|
|
39
|
+
valid: errors.length === 0,
|
|
40
|
+
errors
|
|
41
|
+
});
|
|
42
|
+
var structureIssue = (target, id, message, extra = {}) => ({
|
|
43
|
+
target,
|
|
44
|
+
id,
|
|
45
|
+
kind: "structure",
|
|
46
|
+
message,
|
|
47
|
+
...extra
|
|
48
|
+
});
|
|
49
|
+
var nodeIssue = (id, message, extra) => structureIssue("node", id, message, extra);
|
|
50
|
+
/** 节点及其所有子孙(先序) */
|
|
51
|
+
var flattenNode = (node) => {
|
|
52
|
+
const nodes = [];
|
|
53
|
+
traverseNode(node, (item) => {
|
|
54
|
+
nodes.push(item);
|
|
55
|
+
});
|
|
56
|
+
return nodes;
|
|
57
|
+
};
|
|
58
|
+
var isEmptyId = (id) => id === void 0 || id === null || id === "";
|
|
59
|
+
var DUPLICATE_ID_SUGGESTION = "请使用唯一的 id,或不传 id 由编辑器自动生成";
|
|
60
|
+
var createValidator = (ctx) => {
|
|
61
|
+
const mode = () => ctx.validateMode;
|
|
62
|
+
let builtInRegistered = false;
|
|
63
|
+
const ensureFields = () => {
|
|
64
|
+
if (builtInRegistered) return;
|
|
65
|
+
registerBuiltInFields(builtInFields);
|
|
66
|
+
builtInRegistered = true;
|
|
67
|
+
};
|
|
68
|
+
let cachedFormContext;
|
|
69
|
+
const formContext = () => cachedFormContext ??= {
|
|
70
|
+
services: {
|
|
71
|
+
editorService: ctx.editor,
|
|
72
|
+
propsService: ctx.props,
|
|
73
|
+
historyService: ctx.history,
|
|
74
|
+
dataSourceService: ctx.dataSource,
|
|
75
|
+
codeBlockService: ctx.codeBlock,
|
|
76
|
+
depService: ctx.dep,
|
|
77
|
+
storageService: ctx.storage,
|
|
78
|
+
eventsService: ctx.events,
|
|
79
|
+
componentListService: ctx.componentList
|
|
80
|
+
},
|
|
81
|
+
stage: null
|
|
82
|
+
};
|
|
83
|
+
const knownTypes = () => {
|
|
84
|
+
const set = /* @__PURE__ */ new Set();
|
|
85
|
+
for (const group of ctx.componentList.getList()) for (const item of group.items ?? []) set.add(toLine(item.type));
|
|
86
|
+
return set.size ? set : null;
|
|
87
|
+
};
|
|
88
|
+
const isKnownType = (type, known) => BUILTIN_NODE_TYPES.has(type) || BUILTIN_NODE_TYPES.has(toLine(type)) || !known || known.has(toLine(type));
|
|
89
|
+
/** 节点类型合法性:在组件列表中、没有被禁用的页面片 */
|
|
90
|
+
const checkNodeType = (node, known = knownTypes()) => {
|
|
91
|
+
const issues = [];
|
|
92
|
+
if (!isKnownType(node.type || "", known)) issues.push(nodeIssue(node.id, `组件类型「${node.type}」不在组件列表中`, {
|
|
93
|
+
path: "type",
|
|
94
|
+
suggestion: `可用类型:${[...known ?? []].join("、")}`
|
|
95
|
+
}));
|
|
96
|
+
if (isPageFragment(node) && ctx.editor.getConfig().disabledPageFragment) issues.push(nodeIssue(node.id, "当前配置禁用了页面片(disabledPageFragment)", { path: "type" }));
|
|
97
|
+
return issues;
|
|
98
|
+
};
|
|
99
|
+
/** 新增的节点 id 在这批节点内、或与当前 DSL 里已有节点重复 */
|
|
100
|
+
const findDuplicateIds = (nodes) => {
|
|
101
|
+
const issues = [];
|
|
102
|
+
const seen = /* @__PURE__ */ new Set();
|
|
103
|
+
for (const { id } of nodes.flatMap(flattenNode)) {
|
|
104
|
+
if (isEmptyId(id)) continue;
|
|
105
|
+
if (seen.has(id)) {
|
|
106
|
+
issues.push(nodeIssue(id, `节点 id「${id}」重复`, { suggestion: DUPLICATE_ID_SUGGESTION }));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
seen.add(id);
|
|
110
|
+
if (ctx.editor.getNodeById(id, false)) issues.push(nodeIssue(id, `节点 id「${id}」已存在于当前 DSL`, { suggestion: DUPLICATE_ID_SUGGESTION }));
|
|
111
|
+
}
|
|
112
|
+
return issues;
|
|
113
|
+
};
|
|
114
|
+
const canDropIn = (sourceIds, target, scene) => {
|
|
115
|
+
const fn = ctx.editor.getConfig().canDropIn;
|
|
116
|
+
if (!fn) return null;
|
|
117
|
+
const result = fn(sourceIds, target.id, scene);
|
|
118
|
+
if (result === false) return nodeIssue(target.id, `不允许放入容器「${target.id}」(canDropIn 返回 false)`);
|
|
119
|
+
if (typeof result !== "string" && typeof result !== "number") return null;
|
|
120
|
+
const redirected = ctx.editor.getNodeById(result, false);
|
|
121
|
+
if (redirected && Array.isArray(redirected.items)) return null;
|
|
122
|
+
return nodeIssue(target.id, `canDropIn 将容器「${target.id}」重定向到「${result}」,但该节点不存在或不是容器`);
|
|
123
|
+
};
|
|
124
|
+
/** 新增节点对父节点的要求 */
|
|
125
|
+
const checkAddParent = (node, parent) => {
|
|
126
|
+
if (!parent) return nodeIssue(node.id, "未找到父节点");
|
|
127
|
+
if (!Array.isArray(parent.items)) return nodeIssue(node.id, `父节点「${parent.id}」不是容器(没有 items)`);
|
|
128
|
+
if (isPageOrFragment(node)) return parent.type === NodeType.ROOT ? null : nodeIssue(node.id, "页面 / 页面片只能添加到 app 下");
|
|
129
|
+
if (parent.type === NodeType.ROOT) return nodeIssue(node.id, "app 下只能添加页面或页面片");
|
|
130
|
+
return canDropIn([], parent, "stage-add");
|
|
131
|
+
};
|
|
132
|
+
const checkAddStructure = (node, parent) => {
|
|
133
|
+
const known = knownTypes();
|
|
134
|
+
const issues = flattenNode(node).flatMap((item) => item.type ? checkNodeType(item, known) : [nodeIssue(item.id, "组件类型不能为空")]);
|
|
135
|
+
const parentIssue = checkAddParent(node, parent);
|
|
136
|
+
if (parentIssue) issues.push(parentIssue);
|
|
137
|
+
return issues;
|
|
138
|
+
};
|
|
139
|
+
/** 被移动的节点:必须存在,且不能移动到自己或子孙里 */
|
|
140
|
+
const checkMoveSource = (id, target) => {
|
|
141
|
+
const source = ctx.editor.getNodeById(id, false);
|
|
142
|
+
if (!source) return [nodeIssue(id, `要移动的节点「${id}」不存在`)];
|
|
143
|
+
if (isPageOrFragment(source)) return [];
|
|
144
|
+
return flattenNode(source).some((item) => item.id === target.id) ? [nodeIssue(id, `不能把节点「${id}」移动到它自己或子节点里`)] : [];
|
|
145
|
+
};
|
|
146
|
+
const checkMoveStructure = (sourceIds, target, scene) => {
|
|
147
|
+
if (!Array.isArray(target.items)) return [nodeIssue(target.id, `目标节点「${target.id}」不是容器(没有 items)`)];
|
|
148
|
+
const issues = sourceIds.flatMap((id) => checkMoveSource(id, target));
|
|
149
|
+
const targetIssue = target.type === NodeType.ROOT ? nodeIssue(target.id, "app 下只能放页面或页面片") : canDropIn(sourceIds, target, scene);
|
|
150
|
+
if (targetIssue) issues.push(targetIssue);
|
|
151
|
+
return issues;
|
|
152
|
+
};
|
|
153
|
+
const checkDataSourceStructure = (config) => {
|
|
154
|
+
const type = config.type || "base";
|
|
155
|
+
const known = /* @__PURE__ */ new Set([
|
|
156
|
+
...BUILTIN_DATA_SOURCE_TYPES,
|
|
157
|
+
...ctx.dataSource.get("datasourceTypeList").map((item) => toLine(item.type)),
|
|
158
|
+
...Object.keys(ctx.dataSource.get("configs"))
|
|
159
|
+
]);
|
|
160
|
+
if (known.has(toLine(type))) return [];
|
|
161
|
+
return [structureIssue("data-source", config.id, `数据源类型「${type}」不可用`, {
|
|
162
|
+
path: "type",
|
|
163
|
+
suggestion: `可用类型:${[...known].join("、")}`
|
|
164
|
+
})];
|
|
165
|
+
};
|
|
166
|
+
/** 用 `validateForm` 校验属性表单,配置不存在时不校验;校验本身出错也作为一条问题返回 */
|
|
167
|
+
const checkProps = async (target, id, values, getConfig, options = {}) => {
|
|
168
|
+
const toIssues = (messages) => messages.map((message) => ({
|
|
169
|
+
target,
|
|
170
|
+
id,
|
|
171
|
+
kind: "props",
|
|
172
|
+
message
|
|
173
|
+
}));
|
|
174
|
+
try {
|
|
175
|
+
const config = await getConfig();
|
|
176
|
+
if (!config?.length) return [];
|
|
177
|
+
ensureFields();
|
|
178
|
+
return toIssues(splitErrorText(await validateForm({
|
|
179
|
+
config,
|
|
180
|
+
typeMatchValid: true,
|
|
181
|
+
initValues: values,
|
|
182
|
+
context: formContext(),
|
|
183
|
+
...options
|
|
184
|
+
})));
|
|
185
|
+
} catch (e) {
|
|
186
|
+
return toIssues([`属性校验异常:${e?.message ?? e}`]);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const checkNodeProps = async (node) => {
|
|
190
|
+
if (!node.type || node.type === NodeType.ROOT) return [];
|
|
191
|
+
return checkProps("node", node.id, node, () => ctx.props.getPropsConfig(node.type, { node }), { useFieldTextInError: true });
|
|
192
|
+
};
|
|
193
|
+
const checkDataSourceProps = (config) => checkProps("data-source", config.id, config, () => ctx.dataSource.getFormConfig(config.type || "base"));
|
|
194
|
+
/** 已存在节点的结构校验:类型合法 */
|
|
195
|
+
const checkExistingNodeStructure = (node) => checkNodeType(node);
|
|
196
|
+
const validateNode = async (id) => {
|
|
197
|
+
const node = ctx.editor.getNodeById(id, false);
|
|
198
|
+
if (!node) return [nodeIssue(id, `节点「${id}」不存在`)];
|
|
199
|
+
const propsIssues = !isPage(node) || node.items ? await checkNodeProps(node) : [];
|
|
200
|
+
return [...checkExistingNodeStructure(node), ...propsIssues];
|
|
201
|
+
};
|
|
202
|
+
const validateDataSource = async (id) => {
|
|
203
|
+
const ds = ctx.dataSource.getDataSourceById(`${id}`);
|
|
204
|
+
if (!ds) return [structureIssue("data-source", id, `数据源「${id}」不存在`)];
|
|
205
|
+
return [...checkDataSourceStructure(ds, false), ...await checkDataSourceProps(ds)];
|
|
206
|
+
};
|
|
207
|
+
/** 按模式处理问题:strict 抛错;record 返回写入 invalidNodeIds 的提交函数 */
|
|
208
|
+
const handle = (issues, recordIds = []) => {
|
|
209
|
+
if (mode() === "strict") {
|
|
210
|
+
if (issues.length) throw new DslValidationError(issues);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (mode() !== "record") return void 0;
|
|
214
|
+
return () => {
|
|
215
|
+
for (const id of recordIds) ctx.editor.deleteInvalidNode(id, "props");
|
|
216
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
217
|
+
for (const issue of issues) {
|
|
218
|
+
if (issue.target !== "node" || issue.id === void 0) continue;
|
|
219
|
+
grouped.set(issue.id, [...grouped.get(issue.id) ?? [], issue.message]);
|
|
220
|
+
}
|
|
221
|
+
for (const [id, messages] of grouped) ctx.editor.setInvalidNode(id, "props", messages.join("<br>"));
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
/** update 的单个节点:类型不可变,并校验 update 实际会写入的结果 */
|
|
225
|
+
const checkUpdatedNode = async (node, replace) => {
|
|
226
|
+
const old = ctx.editor.getNodeById(node.id, false);
|
|
227
|
+
if (!old) return { issues: [nodeIssue(node.id, `节点「${node.id}」不存在`)] };
|
|
228
|
+
if (old.type === NodeType.ROOT) return { issues: [] };
|
|
229
|
+
const issues = [];
|
|
230
|
+
if (node.type && node.type !== old.type) issues.push(nodeIssue(node.id, `不允许修改节点类型(${old.type} -> ${node.type})`, { path: "type" }));
|
|
231
|
+
const merged = replace ? cloneDeep$1(node) : mergeWith(cloneDeep$1(ctx.reactivity.toRaw(old)), cloneDeep$1(node), editorNodeMergeCustomizer);
|
|
232
|
+
issues.push(...checkExistingNodeStructure(merged), ...await checkNodeProps(merged));
|
|
233
|
+
return {
|
|
234
|
+
id: node.id,
|
|
235
|
+
issues
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
return {
|
|
239
|
+
async node(id) {
|
|
240
|
+
return toResult(await validateNode(id));
|
|
241
|
+
},
|
|
242
|
+
async dataSource(id) {
|
|
243
|
+
return toResult(await validateDataSource(id));
|
|
244
|
+
},
|
|
245
|
+
async dsl() {
|
|
246
|
+
const root = ctx.editor.get("root");
|
|
247
|
+
if (!root) return toResult([structureIssue("dsl", void 0, "DSL 为空")]);
|
|
248
|
+
const ids = flattenNode(root).filter((node) => node.type !== NodeType.ROOT).map((node) => node.id);
|
|
249
|
+
const seen = /* @__PURE__ */ new Set();
|
|
250
|
+
const issues = ids.filter((id) => {
|
|
251
|
+
const duplicated = seen.has(id);
|
|
252
|
+
seen.add(id);
|
|
253
|
+
return duplicated;
|
|
254
|
+
}).map((id) => nodeIssue(id, `节点 id「${id}」重复`, { suggestion: DUPLICATE_ID_SUGGESTION }));
|
|
255
|
+
for (const id of seen) issues.push(...await validateNode(id));
|
|
256
|
+
for (const ds of ctx.dataSource.get("dataSources")) issues.push(...await validateDataSource(ds.id));
|
|
257
|
+
return toResult(issues);
|
|
258
|
+
},
|
|
259
|
+
checkNodeProps,
|
|
260
|
+
checkAddStructure,
|
|
261
|
+
checkMoveStructure,
|
|
262
|
+
checkDataSourceStructure,
|
|
263
|
+
async guardAdd(items) {
|
|
264
|
+
if (mode() === "off") return void 0;
|
|
265
|
+
const nodes = items.flatMap(({ node }) => flattenNode(node));
|
|
266
|
+
const issues = [...findDuplicateIds(items.map(({ node }) => node)), ...items.flatMap(({ node, parentNode }) => checkAddStructure(node, parentNode))];
|
|
267
|
+
for (const node of nodes) issues.push(...await checkNodeProps(node));
|
|
268
|
+
return handle(issues, nodes.map((node) => node.id));
|
|
269
|
+
},
|
|
270
|
+
async guardUpdate(nodes, { replace = false } = {}) {
|
|
271
|
+
if (mode() === "off") return void 0;
|
|
272
|
+
const results = await Promise.all(nodes.map((node) => checkUpdatedNode(node, replace)));
|
|
273
|
+
return handle(results.flatMap(({ issues }) => issues), results.flatMap(({ id }) => id === void 0 ? [] : [id]));
|
|
274
|
+
},
|
|
275
|
+
async guardMove(sourceIds, target, scene) {
|
|
276
|
+
if (mode() !== "strict") return;
|
|
277
|
+
handle(checkMoveStructure(sourceIds, target, scene));
|
|
278
|
+
},
|
|
279
|
+
guardDataSource(config, isNew) {
|
|
280
|
+
if (mode() !== "strict") return;
|
|
281
|
+
handle(checkDataSourceStructure(config, isNew));
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
//#endregion
|
|
286
|
+
export { DslValidationError, createValidator };
|