@tmagic/editor 1.4.7 → 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 (152) hide show
  1. package/dist/style.css +1 -1
  2. package/dist/tmagic-editor.js +314 -452
  3. package/dist/tmagic-editor.umd.cjs +313 -450
  4. package/package.json +15 -11
  5. package/types/layouts/sidebar/layer/use-click.d.ts +1 -1
  6. package/types/services/editor.d.ts +19 -2
  7. package/types/services/props.d.ts +5 -5
  8. package/types/type.d.ts +6 -0
  9. package/types/utils/editor.d.ts +1 -0
  10. package/src/Editor.vue +0 -211
  11. package/src/components/CodeBlockEditor.vue +0 -268
  12. package/src/components/CodeParams.vue +0 -60
  13. package/src/components/ContentMenu.vue +0 -193
  14. package/src/components/FloatingBox.vue +0 -178
  15. package/src/components/Icon.vue +0 -27
  16. package/src/components/Resizer.vue +0 -23
  17. package/src/components/ScrollBar.vue +0 -151
  18. package/src/components/ScrollViewer.vue +0 -128
  19. package/src/components/SearchInput.vue +0 -37
  20. package/src/components/SplitView.vue +0 -186
  21. package/src/components/ToolButton.vue +0 -138
  22. package/src/components/Tree.vue +0 -71
  23. package/src/components/TreeNode.vue +0 -150
  24. package/src/editorProps.ts +0 -117
  25. package/src/fields/Code.vue +0 -50
  26. package/src/fields/CodeLink.vue +0 -80
  27. package/src/fields/CodeSelect.vue +0 -132
  28. package/src/fields/CodeSelectCol.vue +0 -139
  29. package/src/fields/DataSourceFieldSelect.vue +0 -149
  30. package/src/fields/DataSourceFields.vue +0 -323
  31. package/src/fields/DataSourceInput.vue +0 -331
  32. package/src/fields/DataSourceMethodSelect.vue +0 -153
  33. package/src/fields/DataSourceMethods.vue +0 -104
  34. package/src/fields/DataSourceMocks.vue +0 -255
  35. package/src/fields/DataSourceSelect.vue +0 -98
  36. package/src/fields/EventSelect.vue +0 -375
  37. package/src/fields/KeyValue.vue +0 -137
  38. package/src/fields/PageFragmentSelect.vue +0 -63
  39. package/src/fields/UISelect.vue +0 -135
  40. package/src/hooks/index.ts +0 -24
  41. package/src/hooks/use-code-block-edit.ts +0 -83
  42. package/src/hooks/use-data-source-edit.ts +0 -46
  43. package/src/hooks/use-data-source-method.ts +0 -100
  44. package/src/hooks/use-editor-content-height.ts +0 -26
  45. package/src/hooks/use-filter.ts +0 -55
  46. package/src/hooks/use-float-box.ts +0 -76
  47. package/src/hooks/use-getso.ts +0 -35
  48. package/src/hooks/use-next-float-box-position.ts +0 -29
  49. package/src/hooks/use-node-status.ts +0 -48
  50. package/src/hooks/use-stage.ts +0 -129
  51. package/src/hooks/use-window-rect.ts +0 -20
  52. package/src/icons/AppManageIcon.vue +0 -15
  53. package/src/icons/CenterIcon.vue +0 -13
  54. package/src/icons/CodeIcon.vue +0 -28
  55. package/src/icons/FolderMinusIcon.vue +0 -22
  56. package/src/icons/PinIcon.vue +0 -25
  57. package/src/icons/PinnedIcon.vue +0 -25
  58. package/src/index.ts +0 -118
  59. package/src/initService.ts +0 -443
  60. package/src/layouts/AddPageBox.vue +0 -55
  61. package/src/layouts/CodeEditor.vue +0 -226
  62. package/src/layouts/Framework.vue +0 -163
  63. package/src/layouts/NavMenu.vue +0 -200
  64. package/src/layouts/PropsPanel.vue +0 -130
  65. package/src/layouts/page-bar/AddButton.vue +0 -48
  66. package/src/layouts/page-bar/PageBar.vue +0 -159
  67. package/src/layouts/page-bar/PageBarScrollContainer.vue +0 -156
  68. package/src/layouts/page-bar/SwitchTypeButton.vue +0 -45
  69. package/src/layouts/sidebar/ComponentListPanel.vue +0 -134
  70. package/src/layouts/sidebar/Sidebar.vue +0 -278
  71. package/src/layouts/sidebar/code-block/CodeBlockList.vue +0 -160
  72. package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +0 -70
  73. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +0 -84
  74. package/src/layouts/sidebar/data-source/DataSourceList.vue +0 -172
  75. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +0 -106
  76. package/src/layouts/sidebar/layer/LayerMenu.vue +0 -128
  77. package/src/layouts/sidebar/layer/LayerNodeTool.vue +0 -32
  78. package/src/layouts/sidebar/layer/LayerPanel.vue +0 -119
  79. package/src/layouts/sidebar/layer/use-click.ts +0 -110
  80. package/src/layouts/sidebar/layer/use-drag.ts +0 -161
  81. package/src/layouts/sidebar/layer/use-keybinding.ts +0 -54
  82. package/src/layouts/sidebar/layer/use-node-status.ts +0 -114
  83. package/src/layouts/workspace/Breadcrumb.vue +0 -35
  84. package/src/layouts/workspace/Workspace.vue +0 -46
  85. package/src/layouts/workspace/viewer/NodeListMenu.vue +0 -115
  86. package/src/layouts/workspace/viewer/Stage.vue +0 -246
  87. package/src/layouts/workspace/viewer/StageOverlay.vue +0 -68
  88. package/src/layouts/workspace/viewer/ViewerMenu.vue +0 -145
  89. package/src/services/BaseService.ts +0 -230
  90. package/src/services/codeBlock.ts +0 -327
  91. package/src/services/componentList.ts +0 -58
  92. package/src/services/dataSource.ts +0 -216
  93. package/src/services/dep.ts +0 -152
  94. package/src/services/editor.ts +0 -1096
  95. package/src/services/events.ts +0 -93
  96. package/src/services/history.ts +0 -126
  97. package/src/services/keybinding.ts +0 -220
  98. package/src/services/props.ts +0 -271
  99. package/src/services/stageOverlay.ts +0 -212
  100. package/src/services/storage.ts +0 -155
  101. package/src/services/ui.ts +0 -156
  102. package/src/shims-vue.d.ts +0 -6
  103. package/src/theme/breadcrumb.scss +0 -6
  104. package/src/theme/code-block.scss +0 -6
  105. package/src/theme/code-editor.scss +0 -38
  106. package/src/theme/common/var.scss +0 -14
  107. package/src/theme/component-list-panel.scss +0 -95
  108. package/src/theme/content-menu.scss +0 -76
  109. package/src/theme/data-source-field.scss +0 -12
  110. package/src/theme/data-source-fields.scss +0 -13
  111. package/src/theme/data-source-input.scss +0 -18
  112. package/src/theme/data-source-methods.scss +0 -13
  113. package/src/theme/data-source.scss +0 -28
  114. package/src/theme/event.scss +0 -37
  115. package/src/theme/floating-box.scss +0 -32
  116. package/src/theme/framework.scss +0 -69
  117. package/src/theme/icon.scss +0 -12
  118. package/src/theme/index.scss +0 -10
  119. package/src/theme/key-value.scss +0 -20
  120. package/src/theme/layer-panel.scss +0 -26
  121. package/src/theme/layout.scss +0 -9
  122. package/src/theme/nav-menu.scss +0 -78
  123. package/src/theme/page-bar.scss +0 -89
  124. package/src/theme/page-fragment-select.scss +0 -14
  125. package/src/theme/props-panel.scss +0 -55
  126. package/src/theme/resizer.scss +0 -66
  127. package/src/theme/ruler.scss +0 -38
  128. package/src/theme/search-input.scss +0 -14
  129. package/src/theme/sidebar.scss +0 -79
  130. package/src/theme/stage.scss +0 -76
  131. package/src/theme/theme.scss +0 -27
  132. package/src/theme/tree.scss +0 -89
  133. package/src/theme/workspace.scss +0 -9
  134. package/src/type.ts +0 -746
  135. package/src/utils/compose.ts +0 -52
  136. package/src/utils/config.ts +0 -29
  137. package/src/utils/content-menu.ts +0 -95
  138. package/src/utils/data-source/formConfigs/base.ts +0 -30
  139. package/src/utils/data-source/formConfigs/http.ts +0 -60
  140. package/src/utils/data-source/index.ts +0 -243
  141. package/src/utils/editor.ts +0 -294
  142. package/src/utils/idle-task.ts +0 -72
  143. package/src/utils/index.ts +0 -24
  144. package/src/utils/keybinding-config.ts +0 -125
  145. package/src/utils/logger.ts +0 -47
  146. package/src/utils/monaco-editor.ts +0 -8
  147. package/src/utils/operator.ts +0 -106
  148. package/src/utils/props.ts +0 -504
  149. package/src/utils/scroll-viewer.ts +0 -165
  150. package/src/utils/tree.ts +0 -15
  151. package/src/utils/undo-redo.ts +0 -76
  152. 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,271 +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 { Target, type TargetOptions, Watcher } 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 editorService from '@editor/services/editor';
29
- import type { AsyncHookPlugin, PropsState, SyncHookPlugin } from '@editor/type';
30
- import { fillConfig } from '@editor/utils/props';
31
-
32
- import BaseService from './BaseService';
33
-
34
- const canUsePluginMethods = {
35
- async: [
36
- 'setPropsConfig',
37
- 'getPropsConfig',
38
- 'setPropsValue',
39
- 'getPropsValue',
40
- 'fillConfig',
41
- 'getDefaultPropsValue',
42
- ] as const,
43
- sync: ['createId', 'setNewItemId'] as const,
44
- };
45
-
46
- type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
47
- type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
48
-
49
- class Props extends BaseService {
50
- private state = reactive<PropsState>({
51
- propsConfigMap: {},
52
- propsValueMap: {},
53
- relateIdMap: {},
54
- });
55
-
56
- constructor() {
57
- super([
58
- ...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
59
- ...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
60
- ]);
61
- }
62
-
63
- public setPropsConfigs(configs: Record<string, FormConfig>) {
64
- Object.keys(configs).forEach((type: string) => {
65
- this.setPropsConfig(toLine(type), configs[type]);
66
- });
67
- this.emit('props-configs-change');
68
- }
69
-
70
- public async fillConfig(config: FormConfig, labelWidth?: string) {
71
- return fillConfig(config, typeof labelWidth !== 'function' ? labelWidth : '80px');
72
- }
73
-
74
- public async setPropsConfig(type: string, config: FormConfig) {
75
- this.state.propsConfigMap[toLine(type)] = await this.fillConfig(Array.isArray(config) ? config : [config]);
76
- }
77
-
78
- /**
79
- * 获取指点类型的组件属性表单配置
80
- * @param type 组件类型
81
- * @returns 组件属性表单配置
82
- */
83
- public async getPropsConfig(type: string): Promise<FormConfig> {
84
- if (type === 'area') {
85
- return await this.getPropsConfig('button');
86
- }
87
-
88
- return cloneDeep(this.state.propsConfigMap[toLine(type)] || (await this.fillConfig([])));
89
- }
90
-
91
- public setPropsValues(values: Record<string, Partial<MNode>>) {
92
- Object.keys(values).forEach((type: string) => {
93
- this.setPropsValue(toLine(type), values[type]);
94
- });
95
- }
96
-
97
- /**
98
- * 为指点类型组件设置组件初始值
99
- * @param type 组件类型
100
- * @param value 组件初始值
101
- */
102
- public async setPropsValue(type: string, value: Partial<MNode>) {
103
- this.state.propsValueMap[toLine(type)] = value;
104
- }
105
-
106
- /**
107
- * 获取指定类型的组件初始值
108
- * @param type 组件类型
109
- * @returns 组件初始值
110
- */
111
- public async getPropsValue(componentType: string, { inputEvent, ...defaultValue }: Record<string, any> = {}) {
112
- const type = toLine(componentType);
113
-
114
- if (type === 'area') {
115
- const value = (await this.getPropsValue('button')) as MComponent;
116
- value.className = 'action-area';
117
- value.text = '';
118
- if (value.style) {
119
- value.style.backgroundColor = 'rgba(255, 255, 255, 0)';
120
- }
121
- return value;
122
- }
123
-
124
- const id = this.createId(type);
125
- const defaultPropsValue = this.getDefaultPropsValue(type);
126
- const data = this.setNewItemId(
127
- cloneDeep({
128
- type,
129
- ...defaultValue,
130
- } as any),
131
- );
132
-
133
- return {
134
- id,
135
- ...defaultPropsValue,
136
- ...mergeWith({}, cloneDeep(this.state.propsValueMap[type] || {}), data),
137
- };
138
- }
139
-
140
- public createId(type: string | number): string {
141
- return `${type}_${guid()}`;
142
- }
143
-
144
- /**
145
- * 将组件与组件的子元素配置中的id都设置成一个新的ID
146
- * 如果没有相同ID并且force为false则保持不变
147
- * @param {Object} config 组件配置
148
- * @param {Boolean} force 是否强制设置新的ID
149
- */
150
- /* eslint no-param-reassign: ["error", { "props": false }] */
151
- public setNewItemId(config: MNode, force = true) {
152
- if (force || editorService.getNodeById(config.id)) {
153
- const newId = this.createId(config.type || 'component');
154
- this.setRelateId(config.id, newId);
155
- config.id = newId;
156
- }
157
-
158
- if (config.items && Array.isArray(config.items)) {
159
- for (const item of config.items) {
160
- this.setNewItemId(item);
161
- }
162
- }
163
-
164
- return config;
165
- }
166
-
167
- /**
168
- * 获取默认属性配置
169
- * @param type 组件类型
170
- * @returns Object
171
- */
172
- public getDefaultPropsValue(type: string) {
173
- return ['page', 'container'].includes(type)
174
- ? {
175
- type,
176
- layout: 'absolute',
177
- style: {},
178
- name: type,
179
- items: [],
180
- }
181
- : {
182
- type,
183
- style: {},
184
- name: type,
185
- };
186
- }
187
-
188
- public resetState() {
189
- this.state.propsConfigMap = {};
190
- this.state.propsValueMap = {};
191
- }
192
-
193
- /**
194
- * 替换关联ID
195
- * @param originConfigs 原组件配置
196
- * @param targetConfigs 待替换的组件配置
197
- */
198
- public replaceRelateId(originConfigs: MNode[], targetConfigs: MNode[], collectorOptions: TargetOptions) {
199
- const relateIdMap = this.getRelateIdMap();
200
-
201
- if (Object.keys(relateIdMap).length === 0) return;
202
-
203
- const target = new Target({
204
- ...collectorOptions,
205
- });
206
-
207
- const coperWatcher = new Watcher();
208
-
209
- coperWatcher.addTarget(target);
210
- coperWatcher.collect(originConfigs, {}, true, collectorOptions.type);
211
-
212
- originConfigs.forEach((config: MNode) => {
213
- const newId = relateIdMap[config.id];
214
- const path = getNodePath(newId, targetConfigs);
215
- const targetConfig = path[path.length - 1];
216
-
217
- if (!targetConfig) return;
218
-
219
- target.deps[config.id]?.keys?.forEach((fullKey) => {
220
- const relateOriginId = getValueByKeyPath(fullKey, config);
221
- const relateTargetId = relateIdMap[relateOriginId];
222
- if (!relateTargetId) return;
223
- setValueByKeyPath(fullKey, relateTargetId, targetConfig);
224
- });
225
-
226
- // 递归items
227
- if (config.items && Array.isArray(config.items)) {
228
- this.replaceRelateId(config.items, targetConfigs, collectorOptions);
229
- }
230
- });
231
- }
232
-
233
- /**
234
- * 清除setNewItemId前后映射关系
235
- */
236
- public clearRelateId() {
237
- this.state.relateIdMap = {};
238
- }
239
-
240
- public destroy() {
241
- this.resetState();
242
- this.removeAllListeners();
243
- this.removeAllPlugins();
244
- }
245
-
246
- public usePlugin(options: AsyncHookPlugin<AsyncMethodName, Props> & SyncHookPlugin<SyncMethodName, Props>): void {
247
- super.usePlugin(options);
248
- }
249
-
250
- /**
251
- * 获取setNewItemId前后映射关系
252
- * @param oldId 原组件ID
253
- * @returns 新旧ID映射
254
- */
255
- private getRelateIdMap() {
256
- return this.state.relateIdMap;
257
- }
258
-
259
- /**
260
- * 记录setNewItemId前后映射关系
261
- * @param oldId 原组件ID
262
- * @param newId 分配的新ID
263
- */
264
- private setRelateId(oldId: Id, newId: Id) {
265
- this.state.relateIdMap[oldId] = newId;
266
- }
267
- }
268
-
269
- export type PropsService = Props;
270
-
271
- export default new Props();