@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,93 +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 } from 'lodash-es';
21
-
22
- import { DEFAULT_EVENTS, DEFAULT_METHODS, EventOption } from '@tmagic/core';
23
- import { toLine } from '@tmagic/utils';
24
-
25
- import type { ComponentGroup } from '@editor/type';
26
-
27
- import BaseService from './BaseService';
28
-
29
- let eventMap: Record<string, EventOption[]> = reactive({});
30
- let methodMap: Record<string, EventOption[]> = reactive({});
31
-
32
- class Events extends BaseService {
33
- constructor() {
34
- super([]);
35
- }
36
-
37
- public init(componentGroupList: ComponentGroup[]) {
38
- componentGroupList.forEach((group) => {
39
- group.items.forEach((element) => {
40
- const type = toLine(element.type);
41
- if (!this.getEvent(type)) {
42
- this.setEvent(type, DEFAULT_EVENTS);
43
- }
44
- if (!this.getMethod(type)) {
45
- this.setMethod(type, DEFAULT_METHODS);
46
- }
47
- });
48
- });
49
- }
50
-
51
- public setEvents(events: Record<string, EventOption[]>) {
52
- Object.keys(events).forEach((type: string) => {
53
- this.setEvent(toLine(type), events[type] || []);
54
- });
55
- }
56
-
57
- public setEvent(type: string, events: EventOption[]) {
58
- eventMap[toLine(type)] = [...DEFAULT_EVENTS, ...events];
59
- }
60
-
61
- public getEvent(type: string): EventOption[] {
62
- return cloneDeep(eventMap[toLine(type)] || DEFAULT_EVENTS);
63
- }
64
-
65
- public setMethods(methods: Record<string, EventOption[]>) {
66
- Object.keys(methods).forEach((type: string) => {
67
- this.setMethod(toLine(type), methods[type] || []);
68
- });
69
- }
70
-
71
- public setMethod(type: string, method: EventOption[]) {
72
- methodMap[toLine(type)] = [...DEFAULT_METHODS, ...method];
73
- }
74
-
75
- public getMethod(type: string) {
76
- return cloneDeep(methodMap[toLine(type)] || DEFAULT_METHODS);
77
- }
78
-
79
- public resetState() {
80
- eventMap = reactive({});
81
- methodMap = reactive({});
82
- }
83
-
84
- public destroy() {
85
- this.resetState();
86
- this.removeAllListeners();
87
- this.removeAllPlugins();
88
- }
89
- }
90
-
91
- export type EventsService = Events;
92
-
93
- export default new Events();
@@ -1,126 +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 { MPage, MPageFragment } from '@tmagic/schema';
22
-
23
- import type { HistoryState, StepValue } from '@editor/type';
24
- import { UndoRedo } from '@editor/utils/undo-redo';
25
-
26
- import BaseService from './BaseService';
27
-
28
- class History extends BaseService {
29
- public state = reactive<HistoryState>({
30
- pageSteps: {},
31
- pageId: undefined,
32
- canRedo: false,
33
- canUndo: false,
34
- });
35
-
36
- constructor() {
37
- super([]);
38
-
39
- this.on('change', this.setCanUndoRedo);
40
- }
41
-
42
- public reset() {
43
- this.state.pageSteps = {};
44
- this.resetPage();
45
- }
46
-
47
- public resetPage() {
48
- this.state.pageId = undefined;
49
- this.state.canRedo = false;
50
- this.state.canUndo = false;
51
- }
52
-
53
- public changePage(page: MPage | MPageFragment): void {
54
- if (!page) return;
55
-
56
- this.state.pageId = page.id;
57
-
58
- if (!this.state.pageSteps[this.state.pageId]) {
59
- const undoRedo = new UndoRedo<StepValue>();
60
-
61
- undoRedo.pushElement({
62
- data: page,
63
- modifiedNodeIds: new Map(),
64
- nodeId: page.id,
65
- });
66
-
67
- this.state.pageSteps[this.state.pageId] = undoRedo;
68
- }
69
-
70
- this.setCanUndoRedo();
71
-
72
- this.emit('page-change', this.state.pageSteps[this.state.pageId]);
73
- }
74
-
75
- public resetState(): void {
76
- this.state.pageId = undefined;
77
- this.state.pageSteps = {};
78
- this.state.canRedo = false;
79
- this.state.canUndo = false;
80
- }
81
-
82
- public push(state: StepValue): StepValue | null {
83
- const undoRedo = this.getUndoRedo();
84
- if (!undoRedo) return null;
85
- undoRedo.pushElement(state);
86
- this.emit('change', state);
87
- return state;
88
- }
89
-
90
- public undo(): StepValue | null {
91
- const undoRedo = this.getUndoRedo();
92
- if (!undoRedo) return null;
93
- const state = undoRedo.undo();
94
- this.emit('change', state);
95
- return state;
96
- }
97
-
98
- public redo(): StepValue | null {
99
- const undoRedo = this.getUndoRedo();
100
- if (!undoRedo) return null;
101
- const state = undoRedo.redo();
102
- this.emit('change', state);
103
- return state;
104
- }
105
-
106
- public destroy(): void {
107
- this.resetState();
108
- this.removeAllListeners();
109
- this.removeAllPlugins();
110
- }
111
-
112
- private getUndoRedo() {
113
- if (!this.state.pageId) return null;
114
- return this.state.pageSteps[this.state.pageId];
115
- }
116
-
117
- private setCanUndoRedo(): void {
118
- const undoRedo = this.getUndoRedo();
119
- this.state.canRedo = undoRedo?.canRedo() || false;
120
- this.state.canUndo = undoRedo?.canUndo() || false;
121
- }
122
- }
123
-
124
- export type HistoryService = History;
125
-
126
- export default new History();
@@ -1,220 +0,0 @@
1
- import KeyController, { KeyControllerEvent } from 'keycon';
2
-
3
- import { isPage, isPageFragment } from '@tmagic/utils';
4
-
5
- import { KeyBindingCacheItem, KeyBindingCommand, KeyBindingItem } from '@editor/type';
6
-
7
- import BaseService from './BaseService';
8
- import editorService from './editor';
9
- import uiService from './ui';
10
-
11
- class Keybinding extends BaseService {
12
- public ctrlKey = /mac os x/.test(navigator.userAgent.toLowerCase()) ? 'meta' : 'ctrl';
13
-
14
- private controllers = new Map<string, KeyController>();
15
-
16
- private bindingList: KeyBindingCacheItem[] = [];
17
-
18
- private commands: Record<KeyBindingCommand | string, (e: KeyboardEvent) => void | Promise<void>> = {
19
- [KeyBindingCommand.DELETE_NODE]: () => {
20
- const nodes = editorService.get('nodes');
21
-
22
- if (!nodes || isPage(nodes[0]) || isPageFragment(nodes[0])) return;
23
- editorService.remove(nodes);
24
- },
25
- [KeyBindingCommand.COPY_NODE]: () => {
26
- const nodes = editorService.get('nodes');
27
- nodes && editorService.copy(nodes);
28
- },
29
- [KeyBindingCommand.CUT_NODE]: () => {
30
- const nodes = editorService.get('nodes');
31
-
32
- if (!nodes || isPage(nodes[0]) || isPageFragment(nodes[0])) return;
33
- editorService.copy(nodes);
34
- editorService.remove(nodes);
35
- },
36
- [KeyBindingCommand.PASTE_NODE]: () => {
37
- const nodes = editorService.get('nodes');
38
- nodes && editorService.paste({ offsetX: 10, offsetY: 10 });
39
- },
40
- [KeyBindingCommand.UNDO]: () => {
41
- editorService.undo();
42
- },
43
- [KeyBindingCommand.REDO]: () => {
44
- editorService.redo();
45
- },
46
- [KeyBindingCommand.ZOOM_IN]: () => {
47
- uiService.zoom(0.1);
48
- },
49
- [KeyBindingCommand.ZOOM_OUT]: () => {
50
- uiService.zoom(-0.1);
51
- },
52
- [KeyBindingCommand.ZOOM_RESET]: () => {
53
- uiService.set('zoom', 1);
54
- },
55
- [KeyBindingCommand.ZOOM_FIT]: async () => {
56
- uiService.set('zoom', await uiService.calcZoom());
57
- },
58
- [KeyBindingCommand.MOVE_UP_1]: () => {
59
- editorService.move(0, -1);
60
- },
61
- [KeyBindingCommand.MOVE_DOWN_1]: () => {
62
- editorService.move(0, 1);
63
- },
64
- [KeyBindingCommand.MOVE_LEFT_1]: () => {
65
- editorService.move(-1, 0);
66
- },
67
- [KeyBindingCommand.MOVE_RIGHT_1]: () => {
68
- editorService.move(1, 0);
69
- },
70
- [KeyBindingCommand.MOVE_UP_10]: () => {
71
- editorService.move(0, -10);
72
- },
73
- [KeyBindingCommand.MOVE_DOWN_10]: () => {
74
- editorService.move(0, 10);
75
- },
76
- [KeyBindingCommand.MOVE_LEFT_10]: () => {
77
- editorService.move(-10, 0);
78
- },
79
- [KeyBindingCommand.MOVE_RIGHT_10]: () => {
80
- editorService.move(10, 0);
81
- },
82
- [KeyBindingCommand.SWITCH_NODE]: () => {
83
- editorService.selectNextNode();
84
- },
85
- };
86
-
87
- public registerCommand(command: string, handler: (e: KeyboardEvent) => void | Promise<void>) {
88
- this.commands[command] = handler;
89
- }
90
-
91
- /**
92
- * @deprecated
93
- */
94
- public registeCommand(command: string, handler: (e: KeyboardEvent) => void | Promise<void>) {
95
- this.registerCommand(command, handler);
96
- }
97
-
98
- public unregisterCommand(command: string) {
99
- delete this.commands[command];
100
- }
101
-
102
- /**
103
- * @deprecated
104
- */
105
- public unregisteCommand(command: string) {
106
- this.unregisterCommand(command);
107
- }
108
-
109
- public registerEl(name: string, el?: HTMLElement) {
110
- if (name !== 'global' && !el) {
111
- throw new Error('只有name为global可以不传el');
112
- }
113
-
114
- const keycon = new KeyController(el);
115
- this.controllers.set(name, keycon);
116
- this.bind(name);
117
- }
118
-
119
- /**
120
- * @deprecated
121
- */
122
- public registeEl(name: string, el?: HTMLElement) {
123
- this.registerEl(name, el);
124
- }
125
-
126
- public unregisterEl(name: string) {
127
- this.controllers.get(name)?.destroy();
128
- this.controllers.delete(name);
129
- this.bindingList.forEach((item) => {
130
- item.bound = false;
131
- });
132
- }
133
-
134
- /**
135
- * @deprecated
136
- */
137
- public unregisteEl(name: string) {
138
- this.unregisterEl(name);
139
- }
140
-
141
- public register(maps: KeyBindingItem[]) {
142
- for (const keybindingItem of maps) {
143
- const { command, keybinding, when } = keybindingItem;
144
-
145
- for (const [type = '', eventType = 'keydown'] of when) {
146
- const cacheItem: KeyBindingCacheItem = { type, command, keybinding, eventType, bound: false };
147
-
148
- this.bindingList.push(cacheItem);
149
- }
150
- }
151
-
152
- this.bind();
153
- }
154
-
155
- /**
156
- * @deprecated
157
- */
158
- public registe(map: KeyBindingItem[]) {
159
- this.register(map);
160
- }
161
-
162
- public reset() {
163
- this.controllers.forEach((keycon) => {
164
- keycon.destroy();
165
- });
166
- this.controllers.clear();
167
- this.bindingList = [];
168
- }
169
-
170
- public destroy() {
171
- this.reset();
172
- }
173
-
174
- private bind(name?: string) {
175
- for (const item of this.bindingList) {
176
- const { type, eventType, command, keybinding, bound } = item;
177
-
178
- if (name && name !== type) {
179
- continue;
180
- }
181
-
182
- if (bound) {
183
- continue;
184
- }
185
-
186
- const keycon = this.controllers.get(type);
187
- if (!keycon) {
188
- continue;
189
- }
190
-
191
- const handler = (e: KeyControllerEvent) => {
192
- e.inputEvent.preventDefault();
193
-
194
- this.commands[command]?.(e.inputEvent);
195
- };
196
- this.getKeyconKeys(keybinding).forEach((keys) => {
197
- if (keys[0]) {
198
- keycon[eventType](keys, handler);
199
- } else {
200
- keycon[eventType](handler);
201
- }
202
- });
203
-
204
- item.bound = true;
205
- }
206
- }
207
-
208
- private getKeyconKeys(keybinding: string | string[] = '') {
209
- const splitKey = (key: string) => key.split('+').map((k) => (k === 'ctrl' ? this.ctrlKey : k));
210
-
211
- if (Array.isArray(keybinding)) {
212
- return keybinding.map((key) => splitKey(key));
213
- }
214
- return [splitKey(keybinding)];
215
- }
216
- }
217
-
218
- export type KeybindingService = Keybinding;
219
-
220
- export default new Keybinding();
@@ -1,265 +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, mergeWith } from 'lodash-es';
21
- import { Writable } from 'type-fest';
22
-
23
- import { DepTargetType } from '@tmagic/dep';
24
- import type { FormConfig } from '@tmagic/form';
25
- import type { Id, MComponent, MNode } from '@tmagic/schema';
26
- import { getNodePath, getValueByKeyPath, guid, setValueByKeyPath, toLine } from '@tmagic/utils';
27
-
28
- import depService from '@editor/services/dep';
29
- import editorService from '@editor/services/editor';
30
- import type { AsyncHookPlugin, PropsState, SyncHookPlugin } from '@editor/type';
31
- import { fillConfig } from '@editor/utils/props';
32
-
33
- import BaseService from './BaseService';
34
-
35
- const canUsePluginMethods = {
36
- async: [
37
- 'setPropsConfig',
38
- 'getPropsConfig',
39
- 'setPropsValue',
40
- 'getPropsValue',
41
- 'fillConfig',
42
- 'getDefaultPropsValue',
43
- ] as const,
44
- sync: ['createId', 'setNewItemId'] as const,
45
- };
46
-
47
- type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
48
- type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
49
-
50
- class Props extends BaseService {
51
- private state = reactive<PropsState>({
52
- propsConfigMap: {},
53
- propsValueMap: {},
54
- relateIdMap: {},
55
- });
56
-
57
- constructor() {
58
- super([
59
- ...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
60
- ...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
61
- ]);
62
- }
63
-
64
- public setPropsConfigs(configs: Record<string, FormConfig>) {
65
- Object.keys(configs).forEach((type: string) => {
66
- this.setPropsConfig(toLine(type), configs[type]);
67
- });
68
- this.emit('props-configs-change');
69
- }
70
-
71
- public async fillConfig(config: FormConfig, labelWidth?: string) {
72
- return fillConfig(config, typeof labelWidth !== 'function' ? labelWidth : '80px');
73
- }
74
-
75
- public async setPropsConfig(type: string, config: FormConfig) {
76
- this.state.propsConfigMap[toLine(type)] = await this.fillConfig(Array.isArray(config) ? config : [config]);
77
- }
78
-
79
- /**
80
- * 获取指点类型的组件属性表单配置
81
- * @param type 组件类型
82
- * @returns 组件属性表单配置
83
- */
84
- public async getPropsConfig(type: string): Promise<FormConfig> {
85
- if (type === 'area') {
86
- return await this.getPropsConfig('button');
87
- }
88
-
89
- return cloneDeep(this.state.propsConfigMap[toLine(type)] || (await this.fillConfig([])));
90
- }
91
-
92
- public setPropsValues(values: Record<string, Partial<MNode>>) {
93
- Object.keys(values).forEach((type: string) => {
94
- this.setPropsValue(toLine(type), values[type]);
95
- });
96
- }
97
-
98
- /**
99
- * 为指点类型组件设置组件初始值
100
- * @param type 组件类型
101
- * @param value 组件初始值
102
- */
103
- public async setPropsValue(type: string, value: Partial<MNode>) {
104
- this.state.propsValueMap[toLine(type)] = value;
105
- }
106
-
107
- /**
108
- * 获取指定类型的组件初始值
109
- * @param type 组件类型
110
- * @returns 组件初始值
111
- */
112
- public async getPropsValue(componentType: string, { inputEvent, ...defaultValue }: Record<string, any> = {}) {
113
- const type = toLine(componentType);
114
-
115
- if (type === 'area') {
116
- const value = (await this.getPropsValue('button')) as MComponent;
117
- value.className = 'action-area';
118
- value.text = '';
119
- if (value.style) {
120
- value.style.backgroundColor = 'rgba(255, 255, 255, 0)';
121
- }
122
- return value;
123
- }
124
-
125
- const id = this.createId(type);
126
- const defaultPropsValue = this.getDefaultPropsValue(type);
127
- const data = this.setNewItemId(
128
- cloneDeep({
129
- type,
130
- ...defaultValue,
131
- } as any),
132
- );
133
-
134
- return {
135
- id,
136
- ...defaultPropsValue,
137
- ...mergeWith({}, cloneDeep(this.state.propsValueMap[type] || {}), data),
138
- };
139
- }
140
-
141
- public createId(type: string | number): string {
142
- return `${type}_${guid()}`;
143
- }
144
-
145
- /**
146
- * 将组件与组件的子元素配置中的id都设置成一个新的ID
147
- * 如果没有相同ID并且force为false则保持不变
148
- * @param {Object} config 组件配置
149
- * @param {Boolean} force 是否强制设置新的ID
150
- */
151
- /* eslint no-param-reassign: ["error", { "props": false }] */
152
- public setNewItemId(config: MNode, force = true) {
153
- if (force || editorService.getNodeById(config.id)) {
154
- const newId = this.createId(config.type || 'component');
155
- this.setRelateId(config.id, newId);
156
- config.id = newId;
157
- }
158
-
159
- if (config.items && Array.isArray(config.items)) {
160
- for (const item of config.items) {
161
- this.setNewItemId(item);
162
- }
163
- }
164
-
165
- return config;
166
- }
167
-
168
- /**
169
- * 获取默认属性配置
170
- * @param type 组件类型
171
- * @returns Object
172
- */
173
- public getDefaultPropsValue(type: string) {
174
- return ['page', 'container'].includes(type)
175
- ? {
176
- type,
177
- layout: 'absolute',
178
- style: {},
179
- name: type,
180
- items: [],
181
- }
182
- : {
183
- type,
184
- style: {},
185
- name: type,
186
- };
187
- }
188
-
189
- public resetState() {
190
- this.state.propsConfigMap = {};
191
- this.state.propsValueMap = {};
192
- }
193
-
194
- /**
195
- * 替换关联ID
196
- * @param originConfigs 原组件配置
197
- * @param targetConfigs 待替换的组件配置
198
- */
199
- public replaceRelateId(originConfigs: MNode[], targetConfigs: MNode[]) {
200
- const relateIdMap = this.getRelateIdMap();
201
- if (Object.keys(relateIdMap).length === 0) return;
202
- depService.clearByType(DepTargetType.RELATED_COMP_WHEN_COPY);
203
- depService.collect(originConfigs, true, DepTargetType.RELATED_COMP_WHEN_COPY);
204
- const target = depService.getTarget(DepTargetType.RELATED_COMP_WHEN_COPY, DepTargetType.RELATED_COMP_WHEN_COPY);
205
- if (!target) return;
206
- originConfigs.forEach((config: MNode) => {
207
- const newId = relateIdMap[config.id];
208
- const path = getNodePath(newId, targetConfigs);
209
- const targetConfig = path[path.length - 1];
210
-
211
- if (!targetConfig) return;
212
-
213
- target.deps[config.id]?.keys?.forEach((fullKey) => {
214
- const relateOriginId = getValueByKeyPath(fullKey, config);
215
- const relateTargetId = relateIdMap[relateOriginId];
216
- if (!relateTargetId) return;
217
- setValueByKeyPath(fullKey, relateTargetId, targetConfig);
218
- });
219
-
220
- // 递归items
221
- if (config.items && Array.isArray(config.items)) {
222
- this.replaceRelateId(config.items, targetConfigs);
223
- }
224
- });
225
- }
226
-
227
- /**
228
- * 清除setNewItemId前后映射关系
229
- */
230
- public clearRelateId() {
231
- this.state.relateIdMap = {};
232
- }
233
-
234
- public destroy() {
235
- this.resetState();
236
- this.removeAllListeners();
237
- this.removeAllPlugins();
238
- }
239
-
240
- public usePlugin(options: AsyncHookPlugin<AsyncMethodName, Props> & SyncHookPlugin<SyncMethodName, Props>): void {
241
- super.usePlugin(options);
242
- }
243
-
244
- /**
245
- * 获取setNewItemId前后映射关系
246
- * @param oldId 原组件ID
247
- * @returns 新旧ID映射
248
- */
249
- private getRelateIdMap() {
250
- return this.state.relateIdMap;
251
- }
252
-
253
- /**
254
- * 记录setNewItemId前后映射关系
255
- * @param oldId 原组件ID
256
- * @param newId 分配的新ID
257
- */
258
- private setRelateId(oldId: Id, newId: Id) {
259
- this.state.relateIdMap[oldId] = newId;
260
- }
261
- }
262
-
263
- export type PropsService = Props;
264
-
265
- export default new Props();