@tmagic/editor 1.8.2 → 1.8.4-beta.1
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 +23 -25
- 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/headless.js +1 -2
- package/dist/es/hooks/use-stage.js +19 -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/NavMenuColumn.vue_vue_type_script_setup_true_lang.js +12 -2
- package/dist/es/layouts/page-bar/AddButton.vue_vue_type_script_setup_true_lang.js +4 -6
- 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/layouts/sidebar/ComponentListPanel.vue_vue_type_script_setup_true_lang.js +5 -1
- package/dist/es/layouts/workspace/viewer/Stage.vue_vue_type_script_setup_true_lang.js +3 -0
- 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} +27 -17
- 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/style.css +31 -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/dep/worker.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/style.css +31 -6
- package/dist/themes/magic-admin.css +31 -6
- package/dist/tmagic-editor-headless.umd.cjs +4690 -2891
- package/dist/tmagic-editor-sdk.umd.cjs +9911 -0
- package/dist/tmagic-editor.umd.cjs +14155 -13319
- package/package.json +13 -8
- package/src/Editor.vue +14 -9
- package/src/editorProps.ts +23 -0
- 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 +2 -1
- package/src/index.ts +11 -9
- package/src/initService.ts +59 -188
- package/src/layouts/Framework.vue +1 -1
- package/src/layouts/NavMenuColumn.vue +16 -4
- package/src/layouts/page-bar/AddButton.vue +5 -6
- package/src/layouts/props-panel/PropsPanel.vue +1 -1
- package/src/layouts/props-panel/use-style-panel.ts +1 -1
- package/src/layouts/sidebar/ComponentListPanel.vue +7 -1
- package/src/layouts/workspace/viewer/Stage.vue +5 -0
- 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} +29 -12
- 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/theme/nav-menu.scss +29 -2
- package/src/type.ts +33 -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 +797 -540
- package/types/sdk.d.ts +2594 -0
- /package/src/{services → sdk/services}/BaseService.ts +0 -0
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';
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { reactive } from 'vue';
|
|
20
19
|
import { cloneDeep, get, keys, pick } from 'lodash-es';
|
|
21
20
|
import type { Writable } from 'type-fest';
|
|
22
21
|
|
|
@@ -25,9 +24,6 @@ import { Target, Watcher } from '@tmagic/core';
|
|
|
25
24
|
import type { TableColumnConfig } from '@tmagic/form';
|
|
26
25
|
import { getValueByKeyPath, setValueByKeyPath } from '@tmagic/utils';
|
|
27
26
|
|
|
28
|
-
import editorService from '@editor/services/editor';
|
|
29
|
-
import historyService from '@editor/services/history';
|
|
30
|
-
import storageService, { Protocol } from '@editor/services/storage';
|
|
31
27
|
import type {
|
|
32
28
|
AsyncHookPlugin,
|
|
33
29
|
CodeBlockStepValue,
|
|
@@ -37,11 +33,13 @@ import type {
|
|
|
37
33
|
HistoryOpOptionsWithChangeRecords,
|
|
38
34
|
} from '@editor/type';
|
|
39
35
|
import { CODE_DRAFT_STORAGE_KEY } from '@editor/type';
|
|
40
|
-
import { getEditorConfig } from '@editor/utils/config';
|
|
41
36
|
import { COPY_CODE_STORAGE_KEY } from '@editor/utils/editor';
|
|
42
37
|
import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from '@editor/utils/history';
|
|
43
38
|
|
|
39
|
+
import type { SdkContext } from '../type';
|
|
40
|
+
|
|
44
41
|
import BaseService from './BaseService';
|
|
42
|
+
import { Protocol } from './WebStorage';
|
|
45
43
|
|
|
46
44
|
const canUsePluginMethods = {
|
|
47
45
|
async: ['setCodeDslById', 'setEditStatus', 'setCombineIds', 'setUndeleteableList', 'deleteCodeDslByIds'] as const,
|
|
@@ -51,13 +49,7 @@ const canUsePluginMethods = {
|
|
|
51
49
|
type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
|
|
52
50
|
|
|
53
51
|
class CodeBlock extends BaseService {
|
|
54
|
-
private state
|
|
55
|
-
codeDsl: null,
|
|
56
|
-
editable: true,
|
|
57
|
-
combineIds: [],
|
|
58
|
-
undeletableList: [],
|
|
59
|
-
paramsColConfig: undefined,
|
|
60
|
-
});
|
|
52
|
+
private state: CodeState;
|
|
61
53
|
|
|
62
54
|
/**
|
|
63
55
|
* 最近一次写入历史栈的代码块历史记录 uuid(单条写入场景:新增 / 更新)。
|
|
@@ -70,11 +62,18 @@ class CodeBlock extends BaseService {
|
|
|
70
62
|
*/
|
|
71
63
|
private lastDeletedHistoryIds: string[] = [];
|
|
72
64
|
|
|
73
|
-
constructor() {
|
|
65
|
+
constructor(private ctx: SdkContext) {
|
|
74
66
|
super([
|
|
75
67
|
...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
|
|
76
68
|
...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
|
|
77
69
|
]);
|
|
70
|
+
this.state = ctx.reactivity.reactive<CodeState>({
|
|
71
|
+
codeDsl: null,
|
|
72
|
+
editable: true,
|
|
73
|
+
combineIds: [],
|
|
74
|
+
undeletableList: [],
|
|
75
|
+
paramsColConfig: undefined,
|
|
76
|
+
});
|
|
78
77
|
}
|
|
79
78
|
|
|
80
79
|
/**
|
|
@@ -170,9 +169,9 @@ class CodeBlock extends BaseService {
|
|
|
170
169
|
const codeConfigProcessed = cloneDeep(codeConfig);
|
|
171
170
|
if (codeConfigProcessed.content) {
|
|
172
171
|
// 在保存的时候转换代码内容
|
|
173
|
-
|
|
174
|
-
if (typeof codeConfigProcessed.content === 'string') {
|
|
175
|
-
codeConfigProcessed.content = parseDSL
|
|
172
|
+
// 没有注入 parseDSL(如 Node 默认)时保持源码字符串,不会执行代码,导出的 DSL 里仍是字符串
|
|
173
|
+
if (typeof codeConfigProcessed.content === 'string' && this.ctx.options.parseDSL) {
|
|
174
|
+
codeConfigProcessed.content = this.ctx.parseDSL(codeConfigProcessed.content) as (...args: any[]) => any;
|
|
176
175
|
}
|
|
177
176
|
}
|
|
178
177
|
|
|
@@ -196,7 +195,7 @@ class CodeBlock extends BaseService {
|
|
|
196
195
|
historyDescription,
|
|
197
196
|
source: historySource,
|
|
198
197
|
});
|
|
199
|
-
this.lastPushedHistoryId = (step ?
|
|
198
|
+
this.lastPushedHistoryId = (step ? this.ctx.history.push('codeBlock', step, id) : null)?.uuid ?? null;
|
|
200
199
|
}
|
|
201
200
|
|
|
202
201
|
this.emit('addOrUpdate', id, codeDsl[id]);
|
|
@@ -268,21 +267,21 @@ class CodeBlock extends BaseService {
|
|
|
268
267
|
* 设置代码草稿
|
|
269
268
|
*/
|
|
270
269
|
public setCodeDraft(codeId: Id, content: string): void {
|
|
271
|
-
|
|
270
|
+
this.ctx.storage.getStorage().setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
|
|
272
271
|
}
|
|
273
272
|
|
|
274
273
|
/**
|
|
275
274
|
* 获取代码草稿
|
|
276
275
|
*/
|
|
277
276
|
public getCodeDraft(codeId: Id): string | null {
|
|
278
|
-
return
|
|
277
|
+
return this.ctx.storage.getStorage().getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
|
|
279
278
|
}
|
|
280
279
|
|
|
281
280
|
/**
|
|
282
281
|
* 删除代码草稿
|
|
283
282
|
*/
|
|
284
283
|
public removeCodeDraft(codeId: Id): void {
|
|
285
|
-
|
|
284
|
+
this.ctx.storage.getStorage().removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
|
|
286
285
|
}
|
|
287
286
|
|
|
288
287
|
/**
|
|
@@ -314,7 +313,7 @@ class CodeBlock extends BaseService {
|
|
|
314
313
|
historyDescription,
|
|
315
314
|
source: historySource,
|
|
316
315
|
});
|
|
317
|
-
const uuid = step ?
|
|
316
|
+
const uuid = step ? this.ctx.history.push('codeBlock', step, id)?.uuid : undefined;
|
|
318
317
|
if (uuid) this.lastDeletedHistoryIds.push(uuid);
|
|
319
318
|
}
|
|
320
319
|
|
|
@@ -390,7 +389,7 @@ class CodeBlock extends BaseService {
|
|
|
390
389
|
* @returns 撤销的 step;栈不存在或已无可撤销时返回 null
|
|
391
390
|
*/
|
|
392
391
|
public async undo(id: Id): Promise<CodeBlockStepValue | null> {
|
|
393
|
-
const step =
|
|
392
|
+
const step = this.ctx.history.undo('codeBlock', id);
|
|
394
393
|
if (!step) return null;
|
|
395
394
|
await this.applyHistoryStep(step, true);
|
|
396
395
|
return step;
|
|
@@ -402,7 +401,7 @@ class CodeBlock extends BaseService {
|
|
|
402
401
|
* @returns 重做的 step;栈不存在或已无可重做时返回 null
|
|
403
402
|
*/
|
|
404
403
|
public async redo(id: Id): Promise<CodeBlockStepValue | null> {
|
|
405
|
-
const step =
|
|
404
|
+
const step = this.ctx.history.redo('codeBlock', id);
|
|
406
405
|
if (!step) return null;
|
|
407
406
|
await this.applyHistoryStep(step, false);
|
|
408
407
|
return step;
|
|
@@ -410,12 +409,12 @@ class CodeBlock extends BaseService {
|
|
|
410
409
|
|
|
411
410
|
/** 是否可对指定代码块撤销。 */
|
|
412
411
|
public canUndo(id: Id): boolean {
|
|
413
|
-
return
|
|
412
|
+
return this.ctx.history.canUndo('codeBlock', id);
|
|
414
413
|
}
|
|
415
414
|
|
|
416
415
|
/** 是否可对指定代码块重做。 */
|
|
417
416
|
public canRedo(id: Id): boolean {
|
|
418
|
-
return
|
|
417
|
+
return this.ctx.history.canRedo('codeBlock', id);
|
|
419
418
|
}
|
|
420
419
|
|
|
421
420
|
/**
|
|
@@ -426,7 +425,7 @@ class CodeBlock extends BaseService {
|
|
|
426
425
|
* @returns 实际移动到的最终游标位置
|
|
427
426
|
*/
|
|
428
427
|
public async goto(id: Id, targetCursor: number): Promise<number> {
|
|
429
|
-
let cursor =
|
|
428
|
+
let cursor = this.ctx.history.getCursor('codeBlock', id);
|
|
430
429
|
const target = Math.max(0, targetCursor);
|
|
431
430
|
while (cursor > target) {
|
|
432
431
|
const step = await this.undo(id);
|
|
@@ -452,7 +451,7 @@ class CodeBlock extends BaseService {
|
|
|
452
451
|
* @returns 反向后产生的新 step;目标不存在 / 未应用时返回 null
|
|
453
452
|
*/
|
|
454
453
|
public async revert(id: Id, index: number): Promise<CodeBlockStepValue | null> {
|
|
455
|
-
const list =
|
|
454
|
+
const list = this.ctx.history.getStepList('codeBlock', id);
|
|
456
455
|
const entry = list[index];
|
|
457
456
|
if (!entry?.applied) return null;
|
|
458
457
|
// 更新类步骤(前后 content 都存在)必须带 changeRecords 才支持回滚,否则只能整内容替换,会冲掉后续无关变更。
|
|
@@ -473,7 +472,7 @@ class CodeBlock extends BaseService {
|
|
|
473
472
|
public async revertById(uuids: string[]): Promise<(CodeBlockStepValue | null)[]> {
|
|
474
473
|
const results: (CodeBlockStepValue | null)[] = [];
|
|
475
474
|
for (const uuid of uuids) {
|
|
476
|
-
const location =
|
|
475
|
+
const location = this.ctx.history.findStepLocationByUuid('codeBlock', uuid);
|
|
477
476
|
results.push(location ? await this.revert(location.id, location.index) : null);
|
|
478
477
|
}
|
|
479
478
|
return results;
|
|
@@ -513,7 +512,7 @@ class CodeBlock extends BaseService {
|
|
|
513
512
|
coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
|
|
514
513
|
|
|
515
514
|
Object.keys(customTarget.deps).forEach((nodeId: Id) => {
|
|
516
|
-
const node =
|
|
515
|
+
const node = this.ctx.editor.getNodeById(nodeId);
|
|
517
516
|
if (!node) return;
|
|
518
517
|
customTarget!.deps[nodeId].keys.forEach((key) => {
|
|
519
518
|
const relateCodeId = get(node, key);
|
|
@@ -527,7 +526,7 @@ class CodeBlock extends BaseService {
|
|
|
527
526
|
});
|
|
528
527
|
});
|
|
529
528
|
}
|
|
530
|
-
|
|
529
|
+
this.ctx.storage.setItem(COPY_CODE_STORAGE_KEY, copyData, {
|
|
531
530
|
protocol: Protocol.OBJECT,
|
|
532
531
|
});
|
|
533
532
|
}
|
|
@@ -537,7 +536,7 @@ class CodeBlock extends BaseService {
|
|
|
537
536
|
* @returns
|
|
538
537
|
*/
|
|
539
538
|
public paste() {
|
|
540
|
-
const codeDsl: CodeBlockDSL =
|
|
539
|
+
const codeDsl: CodeBlockDSL = this.ctx.storage.getItem(COPY_CODE_STORAGE_KEY);
|
|
541
540
|
Object.keys(codeDsl).forEach((codeId: Id) => {
|
|
542
541
|
// 不覆盖同样id的代码块
|
|
543
542
|
this.setCodeDslByIdSync(codeId, codeDsl[codeId], false);
|
|
@@ -575,13 +574,16 @@ class CodeBlock extends BaseService {
|
|
|
575
574
|
// 原本是新增 → revert 即删除
|
|
576
575
|
if (!oldSchema && newSchema) {
|
|
577
576
|
await this.deleteCodeDslByIds([id], { historyDescription, historySource: 'rollback' });
|
|
578
|
-
return
|
|
577
|
+
return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
|
|
579
578
|
}
|
|
580
579
|
|
|
581
580
|
// 原本是删除 → revert 即写回
|
|
582
581
|
if (oldSchema && !newSchema) {
|
|
583
|
-
this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, {
|
|
584
|
-
|
|
582
|
+
this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, {
|
|
583
|
+
historyDescription,
|
|
584
|
+
historySource: 'rollback',
|
|
585
|
+
});
|
|
586
|
+
return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
|
|
585
587
|
}
|
|
586
588
|
|
|
587
589
|
if (!oldSchema || !newSchema) return null;
|
|
@@ -605,11 +607,14 @@ class CodeBlock extends BaseService {
|
|
|
605
607
|
historyDescription,
|
|
606
608
|
historySource: 'rollback',
|
|
607
609
|
});
|
|
608
|
-
return
|
|
610
|
+
return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
|
|
609
611
|
}
|
|
610
612
|
|
|
611
|
-
this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, {
|
|
612
|
-
|
|
613
|
+
this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, {
|
|
614
|
+
historyDescription,
|
|
615
|
+
historySource: 'rollback',
|
|
616
|
+
});
|
|
617
|
+
return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
|
|
613
618
|
}
|
|
614
619
|
|
|
615
620
|
/**
|
|
@@ -680,4 +685,4 @@ class CodeBlock extends BaseService {
|
|
|
680
685
|
|
|
681
686
|
export type CodeBlockService = CodeBlock;
|
|
682
687
|
|
|
683
|
-
export default
|
|
688
|
+
export default CodeBlock;
|
|
@@ -16,19 +16,20 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { shallowReactive } from 'vue';
|
|
20
|
-
|
|
21
19
|
import type { ComponentGroup, ComponentGroupState } from '@editor/type';
|
|
22
20
|
|
|
21
|
+
import type { SdkContext } from '../type';
|
|
22
|
+
|
|
23
23
|
import BaseService from './BaseService';
|
|
24
24
|
|
|
25
25
|
class ComponentList extends BaseService {
|
|
26
|
-
private state
|
|
27
|
-
list: [],
|
|
28
|
-
});
|
|
26
|
+
private state: ComponentGroupState;
|
|
29
27
|
|
|
30
|
-
constructor() {
|
|
28
|
+
constructor(ctx: SdkContext) {
|
|
31
29
|
super([]);
|
|
30
|
+
this.state = ctx.reactivity.shallowReactive<ComponentGroupState>({
|
|
31
|
+
list: [],
|
|
32
|
+
});
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
/**
|
|
@@ -55,4 +56,4 @@ class ComponentList extends BaseService {
|
|
|
55
56
|
|
|
56
57
|
export type ComponentListService = ComponentList;
|
|
57
58
|
|
|
58
|
-
export default
|
|
59
|
+
export default ComponentList;
|