@tmagic/editor 1.4.6 → 1.4.8

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.
Files changed (158) hide show
  1. package/dist/style.css +1 -1
  2. package/dist/tmagic-editor.js +636 -589
  3. package/dist/tmagic-editor.umd.cjs +634 -586
  4. package/package.json +15 -11
  5. package/types/editorProps.d.ts +0 -7
  6. package/types/layouts/PropsPanel.vue.d.ts +9 -9
  7. package/types/layouts/sidebar/layer/use-click.d.ts +1 -1
  8. package/types/services/codeBlock.d.ts +2 -1
  9. package/types/services/dataSource.d.ts +2 -1
  10. package/types/services/dep.d.ts +12 -2
  11. package/types/services/editor.d.ts +22 -4
  12. package/types/services/props.d.ts +7 -6
  13. package/types/type.d.ts +6 -0
  14. package/types/utils/editor.d.ts +1 -0
  15. package/types/utils/idle-task.d.ts +14 -0
  16. package/types/utils/operator.d.ts +2 -2
  17. package/src/Editor.vue +0 -211
  18. package/src/components/CodeBlockEditor.vue +0 -268
  19. package/src/components/CodeParams.vue +0 -60
  20. package/src/components/ContentMenu.vue +0 -193
  21. package/src/components/FloatingBox.vue +0 -178
  22. package/src/components/Icon.vue +0 -27
  23. package/src/components/Resizer.vue +0 -23
  24. package/src/components/ScrollBar.vue +0 -151
  25. package/src/components/ScrollViewer.vue +0 -128
  26. package/src/components/SearchInput.vue +0 -37
  27. package/src/components/SplitView.vue +0 -186
  28. package/src/components/ToolButton.vue +0 -138
  29. package/src/components/Tree.vue +0 -71
  30. package/src/components/TreeNode.vue +0 -148
  31. package/src/editorProps.ts +0 -124
  32. package/src/fields/Code.vue +0 -50
  33. package/src/fields/CodeLink.vue +0 -80
  34. package/src/fields/CodeSelect.vue +0 -132
  35. package/src/fields/CodeSelectCol.vue +0 -139
  36. package/src/fields/DataSourceFieldSelect.vue +0 -149
  37. package/src/fields/DataSourceFields.vue +0 -323
  38. package/src/fields/DataSourceInput.vue +0 -325
  39. package/src/fields/DataSourceMethodSelect.vue +0 -153
  40. package/src/fields/DataSourceMethods.vue +0 -104
  41. package/src/fields/DataSourceMocks.vue +0 -255
  42. package/src/fields/DataSourceSelect.vue +0 -98
  43. package/src/fields/EventSelect.vue +0 -301
  44. package/src/fields/KeyValue.vue +0 -137
  45. package/src/fields/PageFragmentSelect.vue +0 -63
  46. package/src/fields/UISelect.vue +0 -135
  47. package/src/hooks/index.ts +0 -24
  48. package/src/hooks/use-code-block-edit.ts +0 -83
  49. package/src/hooks/use-data-source-edit.ts +0 -46
  50. package/src/hooks/use-data-source-method.ts +0 -100
  51. package/src/hooks/use-editor-content-height.ts +0 -26
  52. package/src/hooks/use-filter.ts +0 -55
  53. package/src/hooks/use-float-box.ts +0 -76
  54. package/src/hooks/use-getso.ts +0 -35
  55. package/src/hooks/use-next-float-box-position.ts +0 -29
  56. package/src/hooks/use-node-status.ts +0 -48
  57. package/src/hooks/use-stage.ts +0 -129
  58. package/src/hooks/use-window-rect.ts +0 -20
  59. package/src/icons/AppManageIcon.vue +0 -15
  60. package/src/icons/CenterIcon.vue +0 -13
  61. package/src/icons/CodeIcon.vue +0 -28
  62. package/src/icons/FolderMinusIcon.vue +0 -22
  63. package/src/icons/PinIcon.vue +0 -25
  64. package/src/icons/PinnedIcon.vue +0 -25
  65. package/src/index.ts +0 -118
  66. package/src/initService.ts +0 -447
  67. package/src/layouts/AddPageBox.vue +0 -55
  68. package/src/layouts/CodeEditor.vue +0 -226
  69. package/src/layouts/Framework.vue +0 -163
  70. package/src/layouts/NavMenu.vue +0 -200
  71. package/src/layouts/PropsPanel.vue +0 -130
  72. package/src/layouts/page-bar/AddButton.vue +0 -48
  73. package/src/layouts/page-bar/PageBar.vue +0 -159
  74. package/src/layouts/page-bar/PageBarScrollContainer.vue +0 -156
  75. package/src/layouts/page-bar/SwitchTypeButton.vue +0 -45
  76. package/src/layouts/sidebar/ComponentListPanel.vue +0 -134
  77. package/src/layouts/sidebar/Sidebar.vue +0 -278
  78. package/src/layouts/sidebar/code-block/CodeBlockList.vue +0 -142
  79. package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +0 -70
  80. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +0 -84
  81. package/src/layouts/sidebar/data-source/DataSourceList.vue +0 -160
  82. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +0 -106
  83. package/src/layouts/sidebar/layer/LayerMenu.vue +0 -128
  84. package/src/layouts/sidebar/layer/LayerNodeTool.vue +0 -32
  85. package/src/layouts/sidebar/layer/LayerPanel.vue +0 -119
  86. package/src/layouts/sidebar/layer/use-click.ts +0 -110
  87. package/src/layouts/sidebar/layer/use-drag.ts +0 -161
  88. package/src/layouts/sidebar/layer/use-keybinding.ts +0 -54
  89. package/src/layouts/sidebar/layer/use-node-status.ts +0 -114
  90. package/src/layouts/workspace/Breadcrumb.vue +0 -35
  91. package/src/layouts/workspace/Workspace.vue +0 -46
  92. package/src/layouts/workspace/viewer/NodeListMenu.vue +0 -115
  93. package/src/layouts/workspace/viewer/Stage.vue +0 -246
  94. package/src/layouts/workspace/viewer/StageOverlay.vue +0 -68
  95. package/src/layouts/workspace/viewer/ViewerMenu.vue +0 -145
  96. package/src/services/BaseService.ts +0 -230
  97. package/src/services/codeBlock.ts +0 -324
  98. package/src/services/componentList.ts +0 -58
  99. package/src/services/dataSource.ts +0 -209
  100. package/src/services/dep.ts +0 -80
  101. package/src/services/editor.ts +0 -1087
  102. package/src/services/events.ts +0 -93
  103. package/src/services/history.ts +0 -126
  104. package/src/services/keybinding.ts +0 -220
  105. package/src/services/props.ts +0 -265
  106. package/src/services/stageOverlay.ts +0 -212
  107. package/src/services/storage.ts +0 -155
  108. package/src/services/ui.ts +0 -156
  109. package/src/shims-vue.d.ts +0 -6
  110. package/src/theme/breadcrumb.scss +0 -6
  111. package/src/theme/code-block.scss +0 -6
  112. package/src/theme/code-editor.scss +0 -38
  113. package/src/theme/common/var.scss +0 -14
  114. package/src/theme/component-list-panel.scss +0 -95
  115. package/src/theme/content-menu.scss +0 -76
  116. package/src/theme/data-source-field.scss +0 -12
  117. package/src/theme/data-source-fields.scss +0 -13
  118. package/src/theme/data-source-input.scss +0 -18
  119. package/src/theme/data-source-methods.scss +0 -13
  120. package/src/theme/data-source.scss +0 -28
  121. package/src/theme/event.scss +0 -37
  122. package/src/theme/floating-box.scss +0 -32
  123. package/src/theme/framework.scss +0 -69
  124. package/src/theme/icon.scss +0 -12
  125. package/src/theme/index.scss +0 -10
  126. package/src/theme/key-value.scss +0 -20
  127. package/src/theme/layer-panel.scss +0 -26
  128. package/src/theme/layout.scss +0 -9
  129. package/src/theme/nav-menu.scss +0 -78
  130. package/src/theme/page-bar.scss +0 -89
  131. package/src/theme/page-fragment-select.scss +0 -14
  132. package/src/theme/props-panel.scss +0 -55
  133. package/src/theme/resizer.scss +0 -66
  134. package/src/theme/ruler.scss +0 -38
  135. package/src/theme/search-input.scss +0 -14
  136. package/src/theme/sidebar.scss +0 -79
  137. package/src/theme/stage.scss +0 -76
  138. package/src/theme/theme.scss +0 -27
  139. package/src/theme/tree.scss +0 -89
  140. package/src/theme/workspace.scss +0 -9
  141. package/src/type.ts +0 -746
  142. package/src/utils/compose.ts +0 -52
  143. package/src/utils/config.ts +0 -29
  144. package/src/utils/content-menu.ts +0 -95
  145. package/src/utils/data-source/formConfigs/base.ts +0 -30
  146. package/src/utils/data-source/formConfigs/http.ts +0 -60
  147. package/src/utils/data-source/index.ts +0 -236
  148. package/src/utils/editor.ts +0 -294
  149. package/src/utils/index.ts +0 -24
  150. package/src/utils/keybinding-config.ts +0 -125
  151. package/src/utils/logger.ts +0 -47
  152. package/src/utils/monaco-editor.ts +0 -8
  153. package/src/utils/operator.ts +0 -106
  154. package/src/utils/props.ts +0 -504
  155. package/src/utils/scroll-viewer.ts +0 -165
  156. package/src/utils/tree.ts +0 -15
  157. package/src/utils/undo-redo.ts +0 -76
  158. package/tsconfig.build.json +0 -16
@@ -1,324 +0,0 @@
1
- /*
2
- * Tencent is pleased to support the open source community by making TMagicEditor available.
3
- *
4
- * Copyright (C) 2023 THL A29 Limited, a Tencent company. 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
- import { reactive } from 'vue';
20
- import { cloneDeep, get, keys, pick } from 'lodash-es';
21
- import type { Writable } from 'type-fest';
22
-
23
- import { DepTargetType } from '@tmagic/dep';
24
- import type { ColumnConfig } from '@tmagic/form';
25
- import type { CodeBlockContent, CodeBlockDSL, Id, MNode } from '@tmagic/schema';
26
-
27
- import depService from '@editor/services/dep';
28
- import editorService from '@editor/services/editor';
29
- import storageService, { Protocol } from '@editor/services/storage';
30
- import type { AsyncHookPlugin, CodeState } from '@editor/type';
31
- import { CODE_DRAFT_STORAGE_KEY } from '@editor/type';
32
- import { getConfig } from '@editor/utils/config';
33
- import { COPY_CODE_STORAGE_KEY } from '@editor/utils/editor';
34
-
35
- import BaseService from './BaseService';
36
-
37
- const canUsePluginMethods = {
38
- async: ['setCodeDslById', 'setEditStatus', 'setCombineIds', 'setUndeleteableList', 'deleteCodeDslByIds'] as const,
39
- sync: ['setCodeDslByIdSync'],
40
- };
41
-
42
- type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
43
-
44
- class CodeBlock extends BaseService {
45
- private state = reactive<CodeState>({
46
- codeDsl: null,
47
- editable: true,
48
- combineIds: [],
49
- undeletableList: [],
50
- paramsColConfig: undefined,
51
- });
52
-
53
- constructor() {
54
- super([
55
- ...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
56
- ...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
57
- ]);
58
- }
59
-
60
- /**
61
- * 设置活动的代码块dsl数据源
62
- * @param {CodeBlockDSL} codeDsl 代码DSL
63
- * @returns {void}
64
- */
65
- public async setCodeDsl(codeDsl: CodeBlockDSL): Promise<void> {
66
- this.state.codeDsl = codeDsl;
67
- this.emit('code-dsl-change', this.state.codeDsl);
68
- }
69
-
70
- /**
71
- * 获取活动的代码块dsl数据源(默认从dsl中的codeBlocks字段读取)
72
- * 方法要支持钩子添加扩展,会被重写为异步方法,因此这里显示写为异步以提醒调用者需以异步形式调用
73
- * @param {boolean} forceRefresh 是否强制从活动dsl拉取刷新
74
- * @returns {CodeBlockDSL | null}
75
- */
76
- public getCodeDsl(): CodeBlockDSL | null {
77
- return this.state.codeDsl;
78
- }
79
-
80
- /**
81
- * 根据代码块id获取代码块内容
82
- * @param {Id} id 代码块id
83
- * @returns {CodeBlockContent | null}
84
- */
85
- public getCodeContentById(id: Id): CodeBlockContent | null {
86
- if (!id) return null;
87
- const totalCodeDsl = this.getCodeDsl();
88
- if (!totalCodeDsl) return null;
89
- return totalCodeDsl[id] ?? null;
90
- }
91
-
92
- /**
93
- * 设置代码块ID和代码内容到源dsl
94
- * @param {Id} id 代码块id
95
- * @param {CodeBlockContent} codeConfig 代码块内容配置信息
96
- * @returns {void}
97
- */
98
- public async setCodeDslById(id: Id, codeConfig: Partial<CodeBlockContent>): Promise<void> {
99
- this.setCodeDslByIdSync(id, codeConfig, true);
100
- }
101
-
102
- /**
103
- * 为了兼容历史原因
104
- * 设置代码块ID和代码内容到源dsl
105
- * @param {Id} id 代码块id
106
- * @param {CodeBlockContent} codeConfig 代码块内容配置信息
107
- * @param {boolean} force 是否强制写入,默认true
108
- * @returns {void}
109
- */
110
- public setCodeDslByIdSync(id: Id, codeConfig: Partial<CodeBlockContent>, force = true): void {
111
- const codeDsl = this.getCodeDsl();
112
-
113
- if (!codeDsl) {
114
- throw new Error('dsl中没有codeBlocks');
115
- }
116
- if (codeDsl[id] && !force) return;
117
-
118
- const codeConfigProcessed = cloneDeep(codeConfig);
119
- if (codeConfigProcessed.content) {
120
- // 在保存的时候转换代码内容
121
- const parseDSL = getConfig('parseDSL');
122
- if (typeof codeConfigProcessed.content === 'string') {
123
- codeConfigProcessed.content = parseDSL<(...args: any[]) => any>(codeConfigProcessed.content);
124
- }
125
- }
126
-
127
- const existContent = codeDsl[id] || {};
128
-
129
- codeDsl[id] = {
130
- ...existContent,
131
- ...codeConfigProcessed,
132
- };
133
-
134
- this.emit('addOrUpdate', id, codeDsl[id]);
135
- }
136
-
137
- /**
138
- * 根据代码块id数组获取代码dsl
139
- * @param {string[]} ids 代码块id数组
140
- * @returns {CodeBlockDSL}
141
- */
142
- public getCodeDslByIds(ids: string[]): CodeBlockDSL {
143
- const codeDsl = this.getCodeDsl();
144
- return pick(codeDsl, ids) as CodeBlockDSL;
145
- }
146
-
147
- /**
148
- * 获取编辑状态
149
- * @returns {boolean} 是否可编辑
150
- */
151
- public getEditStatus(): boolean {
152
- return this.state.editable;
153
- }
154
-
155
- /**
156
- * 设置编辑状态
157
- * @param {boolean} status 是否可编辑
158
- * @returns {void}
159
- */
160
- public async setEditStatus(status: boolean): Promise<void> {
161
- this.state.editable = status;
162
- }
163
-
164
- /**
165
- * 设置当前选中组件已关联绑定的代码块id数组
166
- * @param {string[]} ids 代码块id数组
167
- * @returns {void}
168
- */
169
- public async setCombineIds(ids: string[]): Promise<void> {
170
- this.state.combineIds = ids;
171
- }
172
-
173
- /**
174
- * 获取当前选中组件已关联绑定的代码块id数组
175
- * @returns {string[]}
176
- */
177
- public getCombineIds(): string[] {
178
- return this.state.combineIds;
179
- }
180
-
181
- /**
182
- * 获取不可删除列表
183
- * @returns {Id[]}
184
- */
185
- public getUndeletableList(): Id[] {
186
- return this.state.undeletableList;
187
- }
188
-
189
- /**
190
- * 设置不可删除列表:为业务逻辑预留的不可删除的代码块列表,由业务逻辑维护(如代码块上线后不可删除)
191
- * @param {Id[]} codeIds 代码块id数组
192
- * @returns {void}
193
- */
194
- public async setUndeleteableList(codeIds: Id[]): Promise<void> {
195
- this.state.undeletableList = codeIds;
196
- }
197
-
198
- /**
199
- * 设置代码草稿
200
- */
201
- public setCodeDraft(codeId: Id, content: string): void {
202
- globalThis.localStorage.setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
203
- }
204
-
205
- /**
206
- * 获取代码草稿
207
- */
208
- public getCodeDraft(codeId: Id): string | null {
209
- return globalThis.localStorage.getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
210
- }
211
-
212
- /**
213
- * 删除代码草稿
214
- */
215
- public removeCodeDraft(codeId: Id): void {
216
- globalThis.localStorage.removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
217
- }
218
-
219
- /**
220
- * 在dsl数据源中删除指定id的代码块
221
- * @param {Id[]} codeIds 需要删除的代码块id数组
222
- */
223
- public async deleteCodeDslByIds(codeIds: Id[]): Promise<void> {
224
- const currentDsl = await this.getCodeDsl();
225
-
226
- if (!currentDsl) return;
227
-
228
- codeIds.forEach((id) => {
229
- delete currentDsl[id];
230
-
231
- this.emit('remove', id);
232
- });
233
- }
234
-
235
- public setParamsColConfig(config: ColumnConfig): void {
236
- this.state.paramsColConfig = config;
237
- }
238
-
239
- public getParamsColConfig(): ColumnConfig | undefined {
240
- return this.state.paramsColConfig;
241
- }
242
-
243
- /**
244
- * 生成代码块唯一id
245
- * @returns {Id} 代码块唯一id
246
- */
247
- public async getUniqueId(): Promise<string> {
248
- const newId = `code_${Math.random().toString(10).substring(2).substring(0, 4)}`;
249
- // 判断是否重复
250
- const dsl = await this.getCodeDsl();
251
- const existedIds = keys(dsl);
252
- if (!existedIds.includes(newId)) return newId;
253
- return await this.getUniqueId();
254
- }
255
-
256
- /**
257
- * 复制时会带上组件关联的代码块
258
- * @param config 组件节点配置
259
- * @returns
260
- */
261
- public copyWithRelated(config: MNode | MNode[]): void {
262
- const copyNodes: MNode[] = Array.isArray(config) ? config : [config];
263
- // 关联的代码块也一并复制
264
- depService.clearByType(DepTargetType.RELATED_CODE_WHEN_COPY);
265
- depService.collect(copyNodes, true, DepTargetType.RELATED_CODE_WHEN_COPY);
266
- const customTarget = depService.getTarget(
267
- DepTargetType.RELATED_CODE_WHEN_COPY,
268
- DepTargetType.RELATED_CODE_WHEN_COPY,
269
- );
270
- const copyData: CodeBlockDSL = {};
271
- if (customTarget) {
272
- Object.keys(customTarget.deps).forEach((nodeId: Id) => {
273
- const node = editorService.getNodeById(nodeId);
274
- if (!node) return;
275
- customTarget!.deps[nodeId].keys.forEach((key) => {
276
- const relateCodeId = get(node, key);
277
- const isExist = Object.keys(copyData).find((codeId: Id) => codeId === relateCodeId);
278
- if (!isExist) {
279
- const relateCode = this.getCodeContentById(relateCodeId);
280
- if (relateCode) {
281
- copyData[relateCodeId] = relateCode;
282
- }
283
- }
284
- });
285
- });
286
- }
287
- storageService.setItem(COPY_CODE_STORAGE_KEY, copyData, {
288
- protocol: Protocol.OBJECT,
289
- });
290
- }
291
-
292
- /**
293
- * 粘贴代码块
294
- * @returns
295
- */
296
- public paste() {
297
- const codeDsl: CodeBlockDSL = storageService.getItem(COPY_CODE_STORAGE_KEY);
298
- Object.keys(codeDsl).forEach((codeId: Id) => {
299
- // 不覆盖同样id的代码块
300
- this.setCodeDslByIdSync(codeId, codeDsl[codeId], false);
301
- });
302
- }
303
-
304
- public resetState() {
305
- this.state.codeDsl = null;
306
- this.state.editable = true;
307
- this.state.combineIds = [];
308
- this.state.undeletableList = [];
309
- }
310
-
311
- public destroy(): void {
312
- this.resetState();
313
- this.removeAllListeners();
314
- this.removeAllPlugins();
315
- }
316
-
317
- public usePlugin(options: AsyncHookPlugin<AsyncMethodName, CodeBlock>): void {
318
- super.usePlugin(options);
319
- }
320
- }
321
-
322
- export type CodeBlockService = CodeBlock;
323
-
324
- export default new CodeBlock();
@@ -1,58 +0,0 @@
1
- /*
2
- * Tencent is pleased to support the open source community by making TMagicEditor available.
3
- *
4
- * Copyright (C) 2023 THL A29 Limited, a Tencent company. 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
- import { reactive } from 'vue';
20
-
21
- import type { ComponentGroup, ComponentGroupState } from '@editor/type';
22
-
23
- import BaseService from './BaseService';
24
-
25
- class ComponentList extends BaseService {
26
- private state = reactive<ComponentGroupState>({
27
- list: [],
28
- });
29
-
30
- constructor() {
31
- super([]);
32
- }
33
-
34
- /**
35
- * @param componentGroupList 组件列表配置
36
- */
37
- public setList(componentGroupList: ComponentGroup[]): void {
38
- this.state.list = componentGroupList;
39
- }
40
-
41
- public getList(): ComponentGroup[] {
42
- return this.state.list;
43
- }
44
-
45
- public resetState() {
46
- this.state.list = [];
47
- }
48
-
49
- public destroy() {
50
- this.resetState();
51
- this.removeAllListeners();
52
- this.removeAllPlugins();
53
- }
54
- }
55
-
56
- export type ComponentListService = ComponentList;
57
-
58
- export default new ComponentList();
@@ -1,209 +0,0 @@
1
- import { reactive } from 'vue';
2
- import { cloneDeep, get } from 'lodash-es';
3
- import { Writable } from 'type-fest';
4
-
5
- import type { EventOption } from '@tmagic/core';
6
- import { DepTargetType } from '@tmagic/dep';
7
- import type { FormConfig } from '@tmagic/form';
8
- import type { DataSourceSchema, Id, MNode } from '@tmagic/schema';
9
- import { guid, toLine } from '@tmagic/utils';
10
-
11
- import depService from '@editor/services/dep';
12
- import editorService from '@editor/services/editor';
13
- import storageService, { Protocol } from '@editor/services/storage';
14
- import type { DatasourceTypeOption, SyncHookPlugin } from '@editor/type';
15
- import { getFormConfig, getFormValue } from '@editor/utils/data-source';
16
- import { COPY_DS_STORAGE_KEY } from '@editor/utils/editor';
17
-
18
- import BaseService from './BaseService';
19
-
20
- interface State {
21
- datasourceTypeList: DatasourceTypeOption[];
22
- dataSources: DataSourceSchema[];
23
- editable: boolean;
24
- configs: Record<string, FormConfig>;
25
- values: Record<string, Partial<DataSourceSchema>>;
26
- events: Record<string, EventOption[]>;
27
- methods: Record<string, EventOption[]>;
28
- }
29
-
30
- type StateKey = keyof State;
31
-
32
- const canUsePluginMethods = {
33
- async: [],
34
- sync: [
35
- 'getFormConfig',
36
- 'setFormConfig',
37
- 'getFormValue',
38
- 'setFormValue',
39
- 'getFormEvent',
40
- 'setFormEvent',
41
- 'getFormMethod',
42
- 'setFormMethod',
43
- 'add',
44
- 'update',
45
- 'remove',
46
- 'createId',
47
- ] as const,
48
- };
49
-
50
- type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
51
-
52
- class DataSource extends BaseService {
53
- private state = reactive<State>({
54
- datasourceTypeList: [],
55
- dataSources: [],
56
- editable: true,
57
- configs: {},
58
- values: {},
59
- events: {},
60
- methods: {},
61
- });
62
-
63
- constructor() {
64
- super(canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })));
65
- }
66
-
67
- public set<K extends StateKey, T extends State[K]>(name: K, value: T) {
68
- this.state[name] = value;
69
- }
70
-
71
- public get<K extends StateKey>(name: K): State[K] {
72
- return this.state[name];
73
- }
74
-
75
- public getFormConfig(type = 'base') {
76
- return getFormConfig(toLine(type), this.get('configs'));
77
- }
78
-
79
- public setFormConfig(type: string, config: FormConfig) {
80
- this.get('configs')[toLine(type)] = config;
81
- }
82
-
83
- public getFormValue(type = 'base') {
84
- return getFormValue(toLine(type), this.get('values')[type]);
85
- }
86
-
87
- public setFormValue(type: string, value: Partial<DataSourceSchema>) {
88
- this.get('values')[toLine(type)] = value;
89
- }
90
-
91
- public getFormEvent(type = 'base') {
92
- return this.get('events')[toLine(type)] || [];
93
- }
94
-
95
- public setFormEvent(type: string, value: EventOption[] = []) {
96
- this.get('events')[toLine(type)] = value;
97
- }
98
-
99
- public getFormMethod(type = 'base') {
100
- return this.get('methods')[toLine(type)] || [];
101
- }
102
-
103
- public setFormMethod(type: string, value: EventOption[] = []) {
104
- this.get('methods')[toLine(type)] = value;
105
- }
106
-
107
- public add(config: DataSourceSchema) {
108
- const newConfig = {
109
- ...config,
110
- id: config.id && !this.getDataSourceById(config.id) ? config.id : this.createId(),
111
- };
112
-
113
- this.get('dataSources').push(newConfig);
114
-
115
- this.emit('add', newConfig);
116
-
117
- return newConfig;
118
- }
119
-
120
- public update(config: DataSourceSchema) {
121
- const dataSources = this.get('dataSources');
122
-
123
- const index = dataSources.findIndex((ds) => ds.id === config.id);
124
- dataSources[index] = cloneDeep(config);
125
-
126
- this.emit('update', config);
127
-
128
- return config;
129
- }
130
-
131
- public remove(id: string) {
132
- const dataSources = this.get('dataSources');
133
- const index = dataSources.findIndex((ds) => ds.id === id);
134
- dataSources.splice(index, 1);
135
-
136
- this.emit('remove', id);
137
- }
138
-
139
- public createId(): string {
140
- return `ds_${guid()}`;
141
- }
142
-
143
- public getDataSourceById(id: string) {
144
- return this.get('dataSources').find((ds) => ds.id === id);
145
- }
146
-
147
- public resetState() {
148
- this.set('dataSources', []);
149
- }
150
-
151
- public destroy() {
152
- this.removeAllListeners();
153
- this.resetState();
154
- this.removeAllPlugins();
155
- }
156
-
157
- public usePlugin(options: SyncHookPlugin<SyncMethodName, DataSource>): void {
158
- super.usePlugin(options);
159
- }
160
-
161
- /**
162
- * 复制时会带上组件关联的数据源
163
- * @param config 组件节点配置
164
- * @returns
165
- */
166
- public copyWithRelated(config: MNode | MNode[]): void {
167
- const copyNodes: MNode[] = Array.isArray(config) ? config : [config];
168
- depService.clearByType(DepTargetType.RELATED_DS_WHEN_COPY);
169
- depService.collect(copyNodes, true, DepTargetType.RELATED_DS_WHEN_COPY);
170
- const customTarget = depService.getTarget(DepTargetType.RELATED_DS_WHEN_COPY, DepTargetType.RELATED_DS_WHEN_COPY);
171
- const copyData: DataSourceSchema[] = [];
172
- if (customTarget) {
173
- Object.keys(customTarget.deps).forEach((nodeId: Id) => {
174
- const node = editorService.getNodeById(nodeId);
175
- if (!node) return;
176
- customTarget!.deps[nodeId].keys.forEach((key) => {
177
- const [relateDsId] = get(node, key);
178
- const isExist = copyData.find((dsItem) => dsItem.id === relateDsId);
179
- if (!isExist) {
180
- const relateDs = this.getDataSourceById(relateDsId);
181
- if (relateDs) {
182
- copyData.push(relateDs);
183
- }
184
- }
185
- });
186
- });
187
- }
188
- storageService.setItem(COPY_DS_STORAGE_KEY, copyData, {
189
- protocol: Protocol.OBJECT,
190
- });
191
- }
192
- /**
193
- * 粘贴数据源
194
- * @returns
195
- */
196
- public paste() {
197
- const dataSource: DataSourceSchema[] = storageService.getItem(COPY_DS_STORAGE_KEY);
198
- dataSource.forEach((item: DataSourceSchema) => {
199
- // 不覆盖同样id的数据源
200
- if (!this.getDataSourceById(item.id)) {
201
- this.add(item);
202
- }
203
- });
204
- }
205
- }
206
-
207
- export type DataSourceService = DataSource;
208
-
209
- export default new DataSource();
@@ -1,80 +0,0 @@
1
- /*
2
- * Tencent is pleased to support the open source community by making TMagicEditor available.
3
- *
4
- * Copyright (C) 2023 THL A29 Limited, a Tencent company. 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
- import { reactive } from 'vue';
19
-
20
- import { DepTargetType, type Target, Watcher } from '@tmagic/dep';
21
- import type { Id, MNode } from '@tmagic/schema';
22
-
23
- import BaseService from './BaseService';
24
-
25
- class Dep extends BaseService {
26
- private watcher = new Watcher({ initialTargets: reactive({}) });
27
-
28
- public removeTargets(type: string = DepTargetType.DEFAULT) {
29
- this.watcher.removeTargets(type);
30
-
31
- this.emit('remove-target');
32
- }
33
-
34
- public getTargets(type: string = DepTargetType.DEFAULT) {
35
- return this.watcher.getTargets(type);
36
- }
37
-
38
- public getTarget(id: Id, type: string = DepTargetType.DEFAULT) {
39
- return this.watcher.getTarget(id, type);
40
- }
41
-
42
- public addTarget(target: Target) {
43
- this.watcher.addTarget(target);
44
- this.emit('add-target', target);
45
- }
46
-
47
- public removeTarget(id: Id, type: string = DepTargetType.DEFAULT) {
48
- this.watcher.removeTarget(id, type);
49
- this.emit('remove-target');
50
- }
51
-
52
- public clearTargets() {
53
- this.watcher.clearTargets();
54
- }
55
-
56
- public collect(nodes: MNode[], deep = false, type?: DepTargetType) {
57
- this.watcher.collect(nodes, deep, type);
58
- this.emit('collected', nodes, deep);
59
- }
60
-
61
- public clear(nodes?: MNode[]) {
62
- return this.watcher.clear(nodes);
63
- }
64
-
65
- public clearByType(type: DepTargetType, nodes?: MNode[]) {
66
- return this.watcher.clearByType(type, nodes);
67
- }
68
-
69
- public hasTarget(id: Id, type: string = DepTargetType.DEFAULT) {
70
- return this.watcher.hasTarget(id, type);
71
- }
72
-
73
- public hasSpecifiedTypeTarget(type: string = DepTargetType.DEFAULT): boolean {
74
- return this.watcher.hasSpecifiedTypeTarget(type);
75
- }
76
- }
77
-
78
- export type DepService = Dep;
79
-
80
- export default new Dep();