@tmagic/editor 1.4.8 → 1.4.9
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/package.json +11 -11
- package/src/Editor.vue +211 -0
- package/src/components/CodeBlockEditor.vue +268 -0
- package/src/components/CodeParams.vue +60 -0
- package/src/components/ContentMenu.vue +193 -0
- package/src/components/FloatingBox.vue +178 -0
- package/src/components/Icon.vue +27 -0
- package/src/components/Resizer.vue +23 -0
- package/src/components/ScrollBar.vue +151 -0
- package/src/components/ScrollViewer.vue +128 -0
- package/src/components/SearchInput.vue +37 -0
- package/src/components/SplitView.vue +186 -0
- package/src/components/ToolButton.vue +138 -0
- package/src/components/Tree.vue +71 -0
- package/src/components/TreeNode.vue +150 -0
- package/src/editorProps.ts +117 -0
- package/src/fields/Code.vue +50 -0
- package/src/fields/CodeLink.vue +80 -0
- package/src/fields/CodeSelect.vue +132 -0
- package/src/fields/CodeSelectCol.vue +139 -0
- package/src/fields/DataSourceFieldSelect.vue +149 -0
- package/src/fields/DataSourceFields.vue +323 -0
- package/src/fields/DataSourceInput.vue +339 -0
- package/src/fields/DataSourceMethodSelect.vue +153 -0
- package/src/fields/DataSourceMethods.vue +104 -0
- package/src/fields/DataSourceMocks.vue +255 -0
- package/src/fields/DataSourceSelect.vue +98 -0
- package/src/fields/EventSelect.vue +375 -0
- package/src/fields/KeyValue.vue +137 -0
- package/src/fields/PageFragmentSelect.vue +63 -0
- package/src/fields/UISelect.vue +135 -0
- package/src/hooks/index.ts +24 -0
- package/src/hooks/use-code-block-edit.ts +83 -0
- package/src/hooks/use-data-source-edit.ts +46 -0
- package/src/hooks/use-data-source-method.ts +100 -0
- package/src/hooks/use-editor-content-height.ts +26 -0
- package/src/hooks/use-filter.ts +55 -0
- package/src/hooks/use-float-box.ts +76 -0
- package/src/hooks/use-getso.ts +35 -0
- package/src/hooks/use-next-float-box-position.ts +29 -0
- package/src/hooks/use-node-status.ts +48 -0
- package/src/hooks/use-stage.ts +129 -0
- package/src/hooks/use-window-rect.ts +20 -0
- package/src/icons/AppManageIcon.vue +15 -0
- package/src/icons/CenterIcon.vue +13 -0
- package/src/icons/CodeIcon.vue +28 -0
- package/src/icons/FolderMinusIcon.vue +22 -0
- package/src/icons/PinIcon.vue +25 -0
- package/src/icons/PinnedIcon.vue +25 -0
- package/src/index.ts +118 -0
- package/src/initService.ts +448 -0
- package/src/layouts/AddPageBox.vue +55 -0
- package/src/layouts/CodeEditor.vue +226 -0
- package/src/layouts/Framework.vue +163 -0
- package/src/layouts/NavMenu.vue +200 -0
- package/src/layouts/PropsPanel.vue +131 -0
- package/src/layouts/page-bar/AddButton.vue +48 -0
- package/src/layouts/page-bar/PageBar.vue +159 -0
- package/src/layouts/page-bar/PageBarScrollContainer.vue +156 -0
- package/src/layouts/page-bar/SwitchTypeButton.vue +45 -0
- package/src/layouts/sidebar/ComponentListPanel.vue +134 -0
- package/src/layouts/sidebar/Sidebar.vue +278 -0
- package/src/layouts/sidebar/code-block/CodeBlockList.vue +160 -0
- package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +70 -0
- package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +84 -0
- package/src/layouts/sidebar/data-source/DataSourceList.vue +172 -0
- package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +106 -0
- package/src/layouts/sidebar/layer/LayerMenu.vue +128 -0
- package/src/layouts/sidebar/layer/LayerNodeTool.vue +32 -0
- package/src/layouts/sidebar/layer/LayerPanel.vue +119 -0
- package/src/layouts/sidebar/layer/use-click.ts +113 -0
- package/src/layouts/sidebar/layer/use-drag.ts +167 -0
- package/src/layouts/sidebar/layer/use-keybinding.ts +54 -0
- package/src/layouts/sidebar/layer/use-node-status.ts +114 -0
- package/src/layouts/workspace/Breadcrumb.vue +35 -0
- package/src/layouts/workspace/Workspace.vue +46 -0
- package/src/layouts/workspace/viewer/NodeListMenu.vue +115 -0
- package/src/layouts/workspace/viewer/Stage.vue +246 -0
- package/src/layouts/workspace/viewer/StageOverlay.vue +68 -0
- package/src/layouts/workspace/viewer/ViewerMenu.vue +145 -0
- package/src/services/BaseService.ts +230 -0
- package/src/services/codeBlock.ts +327 -0
- package/src/services/componentList.ts +58 -0
- package/src/services/dataSource.ts +216 -0
- package/src/services/dep.ts +152 -0
- package/src/services/editor.ts +1135 -0
- package/src/services/events.ts +93 -0
- package/src/services/history.ts +126 -0
- package/src/services/keybinding.ts +220 -0
- package/src/services/props.ts +286 -0
- package/src/services/stageOverlay.ts +212 -0
- package/src/services/storage.ts +155 -0
- package/src/services/ui.ts +156 -0
- package/src/shims-vue.d.ts +6 -0
- package/src/theme/breadcrumb.scss +6 -0
- package/src/theme/code-block.scss +6 -0
- package/src/theme/code-editor.scss +38 -0
- package/src/theme/common/var.scss +14 -0
- package/src/theme/component-list-panel.scss +95 -0
- package/src/theme/content-menu.scss +76 -0
- package/src/theme/data-source-field.scss +12 -0
- package/src/theme/data-source-fields.scss +13 -0
- package/src/theme/data-source-input.scss +18 -0
- package/src/theme/data-source-methods.scss +13 -0
- package/src/theme/data-source.scss +28 -0
- package/src/theme/event.scss +37 -0
- package/src/theme/floating-box.scss +32 -0
- package/src/theme/framework.scss +69 -0
- package/src/theme/icon.scss +12 -0
- package/src/theme/index.scss +10 -0
- package/src/theme/key-value.scss +20 -0
- package/src/theme/layer-panel.scss +26 -0
- package/src/theme/layout.scss +9 -0
- package/src/theme/nav-menu.scss +78 -0
- package/src/theme/page-bar.scss +89 -0
- package/src/theme/page-fragment-select.scss +14 -0
- package/src/theme/props-panel.scss +55 -0
- package/src/theme/resizer.scss +66 -0
- package/src/theme/ruler.scss +38 -0
- package/src/theme/search-input.scss +14 -0
- package/src/theme/sidebar.scss +79 -0
- package/src/theme/stage.scss +76 -0
- package/src/theme/theme.scss +27 -0
- package/src/theme/tree.scss +89 -0
- package/src/theme/workspace.scss +9 -0
- package/src/type.ts +749 -0
- package/src/utils/compose.ts +52 -0
- package/src/utils/config.ts +29 -0
- package/src/utils/content-menu.ts +95 -0
- package/src/utils/data-source/formConfigs/base.ts +30 -0
- package/src/utils/data-source/formConfigs/http.ts +60 -0
- package/src/utils/data-source/index.ts +243 -0
- package/src/utils/editor.ts +313 -0
- package/src/utils/idle-task.ts +72 -0
- package/src/utils/index.ts +24 -0
- package/src/utils/keybinding-config.ts +125 -0
- package/src/utils/logger.ts +47 -0
- package/src/utils/monaco-editor.ts +8 -0
- package/src/utils/operator.ts +106 -0
- package/src/utils/props.ts +504 -0
- package/src/utils/scroll-viewer.ts +165 -0
- package/src/utils/tree.ts +15 -0
- package/src/utils/undo-redo.ts +76 -0
|
@@ -0,0 +1,286 @@
|
|
|
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 {
|
|
30
|
+
AsyncHookPlugin,
|
|
31
|
+
PropsFormConfigFunction,
|
|
32
|
+
PropsFormValueFunction,
|
|
33
|
+
PropsState,
|
|
34
|
+
SyncHookPlugin,
|
|
35
|
+
} from '@editor/type';
|
|
36
|
+
import { fillConfig } from '@editor/utils/props';
|
|
37
|
+
|
|
38
|
+
import BaseService from './BaseService';
|
|
39
|
+
|
|
40
|
+
const canUsePluginMethods = {
|
|
41
|
+
async: [
|
|
42
|
+
'setPropsConfig',
|
|
43
|
+
'getPropsConfig',
|
|
44
|
+
'setPropsValue',
|
|
45
|
+
'getPropsValue',
|
|
46
|
+
'fillConfig',
|
|
47
|
+
'getDefaultPropsValue',
|
|
48
|
+
] as const,
|
|
49
|
+
sync: ['createId', 'setNewItemId'] as const,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
|
|
53
|
+
type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
|
|
54
|
+
|
|
55
|
+
class Props extends BaseService {
|
|
56
|
+
private state = reactive<PropsState>({
|
|
57
|
+
propsConfigMap: {},
|
|
58
|
+
propsValueMap: {},
|
|
59
|
+
relateIdMap: {},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
constructor() {
|
|
63
|
+
super([
|
|
64
|
+
...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
|
|
65
|
+
...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public setPropsConfigs(configs: Record<string, FormConfig | PropsFormConfigFunction>) {
|
|
70
|
+
Object.keys(configs).forEach((type: string) => {
|
|
71
|
+
this.setPropsConfig(toLine(type), configs[type]);
|
|
72
|
+
});
|
|
73
|
+
this.emit('props-configs-change');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public async fillConfig(config: FormConfig, labelWidth?: string) {
|
|
77
|
+
return fillConfig(config, typeof labelWidth !== 'function' ? labelWidth : '80px');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public async setPropsConfig(type: string, config: FormConfig | PropsFormConfigFunction) {
|
|
81
|
+
let c = config;
|
|
82
|
+
if (typeof config === 'function') {
|
|
83
|
+
c = config({ editorService });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this.state.propsConfigMap[toLine(type)] = await this.fillConfig(Array.isArray(c) ? c : [c]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 获取指点类型的组件属性表单配置
|
|
91
|
+
* @param type 组件类型
|
|
92
|
+
* @returns 组件属性表单配置
|
|
93
|
+
*/
|
|
94
|
+
public async getPropsConfig(type: string): Promise<FormConfig> {
|
|
95
|
+
if (type === 'area') {
|
|
96
|
+
return await this.getPropsConfig('button');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return cloneDeep(this.state.propsConfigMap[toLine(type)] || (await this.fillConfig([])));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public setPropsValues(values: Record<string, Partial<MNode> | PropsFormValueFunction>) {
|
|
103
|
+
Object.keys(values).forEach((type: string) => {
|
|
104
|
+
this.setPropsValue(toLine(type), values[type]);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 为指点类型组件设置组件初始值
|
|
110
|
+
* @param type 组件类型
|
|
111
|
+
* @param value 组件初始值
|
|
112
|
+
*/
|
|
113
|
+
public async setPropsValue(type: string, value: Partial<MNode> | PropsFormValueFunction) {
|
|
114
|
+
let v = value;
|
|
115
|
+
if (typeof value === 'function') {
|
|
116
|
+
v = value({ editorService });
|
|
117
|
+
}
|
|
118
|
+
this.state.propsValueMap[toLine(type)] = v;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 获取指定类型的组件初始值
|
|
123
|
+
* @param type 组件类型
|
|
124
|
+
* @returns 组件初始值
|
|
125
|
+
*/
|
|
126
|
+
public async getPropsValue(componentType: string, { inputEvent, ...defaultValue }: Record<string, any> = {}) {
|
|
127
|
+
const type = toLine(componentType);
|
|
128
|
+
|
|
129
|
+
if (type === 'area') {
|
|
130
|
+
const value = (await this.getPropsValue('button')) as MComponent;
|
|
131
|
+
value.className = 'action-area';
|
|
132
|
+
value.text = '';
|
|
133
|
+
if (value.style) {
|
|
134
|
+
value.style.backgroundColor = 'rgba(255, 255, 255, 0)';
|
|
135
|
+
}
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const id = this.createId(type);
|
|
140
|
+
const defaultPropsValue = this.getDefaultPropsValue(type);
|
|
141
|
+
const data = this.setNewItemId(
|
|
142
|
+
cloneDeep({
|
|
143
|
+
type,
|
|
144
|
+
...defaultValue,
|
|
145
|
+
} as any),
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
id,
|
|
150
|
+
...defaultPropsValue,
|
|
151
|
+
...mergeWith({}, cloneDeep(this.state.propsValueMap[type] || {}), data),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public createId(type: string | number): string {
|
|
156
|
+
return `${type}_${guid()}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 将组件与组件的子元素配置中的id都设置成一个新的ID
|
|
161
|
+
* 如果没有相同ID并且force为false则保持不变
|
|
162
|
+
* @param {Object} config 组件配置
|
|
163
|
+
* @param {Boolean} force 是否强制设置新的ID
|
|
164
|
+
*/
|
|
165
|
+
/* eslint no-param-reassign: ["error", { "props": false }] */
|
|
166
|
+
public setNewItemId(config: MNode, force = true) {
|
|
167
|
+
if (force || editorService.getNodeById(config.id)) {
|
|
168
|
+
const newId = this.createId(config.type || 'component');
|
|
169
|
+
this.setRelateId(config.id, newId);
|
|
170
|
+
config.id = newId;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (config.items && Array.isArray(config.items)) {
|
|
174
|
+
for (const item of config.items) {
|
|
175
|
+
this.setNewItemId(item);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return config;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 获取默认属性配置
|
|
184
|
+
* @param type 组件类型
|
|
185
|
+
* @returns Object
|
|
186
|
+
*/
|
|
187
|
+
public getDefaultPropsValue(type: string) {
|
|
188
|
+
return ['page', 'container'].includes(type)
|
|
189
|
+
? {
|
|
190
|
+
type,
|
|
191
|
+
layout: 'absolute',
|
|
192
|
+
style: {},
|
|
193
|
+
name: type,
|
|
194
|
+
items: [],
|
|
195
|
+
}
|
|
196
|
+
: {
|
|
197
|
+
type,
|
|
198
|
+
style: {},
|
|
199
|
+
name: type,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public resetState() {
|
|
204
|
+
this.state.propsConfigMap = {};
|
|
205
|
+
this.state.propsValueMap = {};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 替换关联ID
|
|
210
|
+
* @param originConfigs 原组件配置
|
|
211
|
+
* @param targetConfigs 待替换的组件配置
|
|
212
|
+
*/
|
|
213
|
+
public replaceRelateId(originConfigs: MNode[], targetConfigs: MNode[], collectorOptions: TargetOptions) {
|
|
214
|
+
const relateIdMap = this.getRelateIdMap();
|
|
215
|
+
|
|
216
|
+
if (Object.keys(relateIdMap).length === 0) return;
|
|
217
|
+
|
|
218
|
+
const target = new Target({
|
|
219
|
+
...collectorOptions,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const coperWatcher = new Watcher();
|
|
223
|
+
|
|
224
|
+
coperWatcher.addTarget(target);
|
|
225
|
+
coperWatcher.collect(originConfigs, {}, true, collectorOptions.type);
|
|
226
|
+
|
|
227
|
+
originConfigs.forEach((config: MNode) => {
|
|
228
|
+
const newId = relateIdMap[config.id];
|
|
229
|
+
const path = getNodePath(newId, targetConfigs);
|
|
230
|
+
const targetConfig = path[path.length - 1];
|
|
231
|
+
|
|
232
|
+
if (!targetConfig) return;
|
|
233
|
+
|
|
234
|
+
target.deps[config.id]?.keys?.forEach((fullKey) => {
|
|
235
|
+
const relateOriginId = getValueByKeyPath(fullKey, config);
|
|
236
|
+
const relateTargetId = relateIdMap[relateOriginId];
|
|
237
|
+
if (!relateTargetId) return;
|
|
238
|
+
setValueByKeyPath(fullKey, relateTargetId, targetConfig);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// 递归items
|
|
242
|
+
if (config.items && Array.isArray(config.items)) {
|
|
243
|
+
this.replaceRelateId(config.items, targetConfigs, collectorOptions);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 清除setNewItemId前后映射关系
|
|
250
|
+
*/
|
|
251
|
+
public clearRelateId() {
|
|
252
|
+
this.state.relateIdMap = {};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
public destroy() {
|
|
256
|
+
this.resetState();
|
|
257
|
+
this.removeAllListeners();
|
|
258
|
+
this.removeAllPlugins();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
public usePlugin(options: AsyncHookPlugin<AsyncMethodName, Props> & SyncHookPlugin<SyncMethodName, Props>): void {
|
|
262
|
+
super.usePlugin(options);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* 获取setNewItemId前后映射关系
|
|
267
|
+
* @param oldId 原组件ID
|
|
268
|
+
* @returns 新旧ID映射
|
|
269
|
+
*/
|
|
270
|
+
private getRelateIdMap() {
|
|
271
|
+
return this.state.relateIdMap;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 记录setNewItemId前后映射关系
|
|
276
|
+
* @param oldId 原组件ID
|
|
277
|
+
* @param newId 分配的新ID
|
|
278
|
+
*/
|
|
279
|
+
private setRelateId(oldId: Id, newId: Id) {
|
|
280
|
+
this.state.relateIdMap[oldId] = newId;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export type PropsService = Props;
|
|
285
|
+
|
|
286
|
+
export default new Props();
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { reactive } from 'vue';
|
|
2
|
+
import type { Writable } from 'type-fest';
|
|
3
|
+
|
|
4
|
+
import StageCore from '@tmagic/stage';
|
|
5
|
+
|
|
6
|
+
import { useStage } from '@editor/hooks/use-stage';
|
|
7
|
+
import BaseService from '@editor/services//BaseService';
|
|
8
|
+
import editorService from '@editor/services//editor';
|
|
9
|
+
import type { StageOptions, StageOverlayState, SyncHookPlugin } from '@editor/type';
|
|
10
|
+
|
|
11
|
+
const canUsePluginMethods = {
|
|
12
|
+
async: [],
|
|
13
|
+
sync: ['openOverlay', 'closeOverlay', 'updateOverlay', 'createStage'] as const,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
|
|
17
|
+
|
|
18
|
+
class StageOverlay extends BaseService {
|
|
19
|
+
private state: StageOverlayState = reactive({
|
|
20
|
+
wrapDiv: document.createElement('div'),
|
|
21
|
+
sourceEl: null,
|
|
22
|
+
contentEl: null,
|
|
23
|
+
stage: null,
|
|
24
|
+
stageOptions: null,
|
|
25
|
+
wrapWidth: 0,
|
|
26
|
+
wrapHeight: 0,
|
|
27
|
+
stageOverlayVisible: false,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
constructor() {
|
|
31
|
+
super(canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })));
|
|
32
|
+
|
|
33
|
+
this.get('wrapDiv').classList.add('tmagic-editor-sub-stage-wrap');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public get<K extends keyof StageOverlayState>(name: K): StageOverlayState[K] {
|
|
37
|
+
return this.state[name];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public set<K extends keyof StageOverlayState, T extends StageOverlayState[K]>(name: K, value: T) {
|
|
41
|
+
this.state[name] = value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public openOverlay(el: HTMLElement | null) {
|
|
45
|
+
const stageOptions = this.get('stageOptions');
|
|
46
|
+
if (!el || !stageOptions) return;
|
|
47
|
+
|
|
48
|
+
this.set('sourceEl', el);
|
|
49
|
+
|
|
50
|
+
this.createContentEl();
|
|
51
|
+
|
|
52
|
+
this.set('stageOverlayVisible', true);
|
|
53
|
+
|
|
54
|
+
editorService.on('update', this.updateHandler);
|
|
55
|
+
editorService.on('add', this.addHandler);
|
|
56
|
+
editorService.on('remove', this.removeHandler);
|
|
57
|
+
editorService.on('drag-to', this.updateHandler);
|
|
58
|
+
editorService.on('move-layer', this.updateHandler);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public closeOverlay() {
|
|
62
|
+
this.set('stageOverlayVisible', false);
|
|
63
|
+
const subStage = this.get('stage');
|
|
64
|
+
const wrapDiv = this.get('wrapDiv');
|
|
65
|
+
subStage?.destroy();
|
|
66
|
+
wrapDiv.remove();
|
|
67
|
+
|
|
68
|
+
this.set('stage', null);
|
|
69
|
+
this.set('sourceEl', null);
|
|
70
|
+
this.set('contentEl', null);
|
|
71
|
+
|
|
72
|
+
editorService.off('update', this.updateHandler);
|
|
73
|
+
editorService.off('add', this.addHandler);
|
|
74
|
+
editorService.off('remove', this.removeHandler);
|
|
75
|
+
editorService.off('drag-to', this.updateHandler);
|
|
76
|
+
editorService.off('move-layer', this.updateHandler);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public updateOverlay() {
|
|
80
|
+
const sourceEl = this.get('sourceEl');
|
|
81
|
+
|
|
82
|
+
if (!sourceEl) return;
|
|
83
|
+
|
|
84
|
+
const { scrollWidth, scrollHeight } = sourceEl;
|
|
85
|
+
|
|
86
|
+
this.set('wrapWidth', scrollWidth);
|
|
87
|
+
this.set('wrapHeight', scrollHeight);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public createStage(stageOptions: StageOptions = {}) {
|
|
91
|
+
return useStage({
|
|
92
|
+
...stageOptions,
|
|
93
|
+
zoom: 1,
|
|
94
|
+
runtimeUrl: '',
|
|
95
|
+
autoScrollIntoView: false,
|
|
96
|
+
render: async (stage: StageCore) => {
|
|
97
|
+
this.copyDocumentElement();
|
|
98
|
+
|
|
99
|
+
const rootEls = stage.renderer.getDocument()?.body.children;
|
|
100
|
+
if (rootEls) {
|
|
101
|
+
Array.from(rootEls).forEach((element) => {
|
|
102
|
+
if (['SCRIPT', 'STYLE'].includes(element.tagName)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
element.remove();
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const wrapDiv = this.get('wrapDiv');
|
|
110
|
+
|
|
111
|
+
await this.render();
|
|
112
|
+
|
|
113
|
+
return wrapDiv;
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public usePlugin(options: SyncHookPlugin<SyncMethodName, StageOverlay>): void {
|
|
119
|
+
super.usePlugin(options);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private createContentEl() {
|
|
123
|
+
const sourceEl = this.get('sourceEl');
|
|
124
|
+
if (!sourceEl) return;
|
|
125
|
+
|
|
126
|
+
const contentEl = sourceEl.cloneNode(true) as HTMLElement;
|
|
127
|
+
this.set('contentEl', contentEl);
|
|
128
|
+
|
|
129
|
+
contentEl.style.position = 'static';
|
|
130
|
+
contentEl.style.overflow = 'visible';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private copyDocumentElement() {
|
|
134
|
+
const subStage = this.get('stage');
|
|
135
|
+
const stage = editorService.get('stage');
|
|
136
|
+
|
|
137
|
+
const doc = subStage?.renderer.getDocument();
|
|
138
|
+
const documentElement = stage?.renderer.getDocument()?.documentElement;
|
|
139
|
+
|
|
140
|
+
if (doc && documentElement) {
|
|
141
|
+
doc.replaceChild(documentElement.cloneNode(true), doc.documentElement);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private async render() {
|
|
146
|
+
this.createContentEl();
|
|
147
|
+
|
|
148
|
+
const contentEl = this.get('contentEl');
|
|
149
|
+
const sourceEl = this.get('sourceEl');
|
|
150
|
+
const wrapDiv = this.get('wrapDiv');
|
|
151
|
+
const subStage = this.get('stage');
|
|
152
|
+
const stageOptions = this.get('stageOptions');
|
|
153
|
+
|
|
154
|
+
if (!contentEl) return;
|
|
155
|
+
|
|
156
|
+
wrapDiv.style.cssText = `
|
|
157
|
+
width: ${sourceEl?.scrollWidth}px;
|
|
158
|
+
height: ${sourceEl?.scrollHeight}px;
|
|
159
|
+
background-color: #fff;
|
|
160
|
+
`;
|
|
161
|
+
|
|
162
|
+
Array.from(wrapDiv.children).forEach((element) => {
|
|
163
|
+
element.remove();
|
|
164
|
+
});
|
|
165
|
+
wrapDiv.appendChild(contentEl);
|
|
166
|
+
|
|
167
|
+
setTimeout(() => {
|
|
168
|
+
subStage?.renderer.contentWindow?.magic.onPageElUpdate(wrapDiv);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
if (await stageOptions?.canSelect?.(contentEl)) {
|
|
172
|
+
subStage?.select(contentEl.id);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private updateHandler = () => {
|
|
177
|
+
setTimeout(() => {
|
|
178
|
+
this.render();
|
|
179
|
+
this.updateOverlay();
|
|
180
|
+
|
|
181
|
+
this.updateSelectStatus();
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
private addHandler = () => {
|
|
186
|
+
this.render();
|
|
187
|
+
this.updateOverlay();
|
|
188
|
+
|
|
189
|
+
this.updateSelectStatus();
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
private removeHandler = () => {
|
|
193
|
+
this.render();
|
|
194
|
+
this.updateOverlay();
|
|
195
|
+
|
|
196
|
+
this.updateSelectStatus();
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
private updateSelectStatus() {
|
|
200
|
+
const subStage = this.get('stage');
|
|
201
|
+
const nodes = editorService.get('nodes');
|
|
202
|
+
if (nodes.length > 1) {
|
|
203
|
+
subStage?.multiSelect(nodes.map((n) => n.id));
|
|
204
|
+
} else {
|
|
205
|
+
subStage?.select(nodes[0].id);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export type StageOverlayService = StageOverlay;
|
|
211
|
+
|
|
212
|
+
export default new StageOverlay();
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import serialize from 'serialize-javascript';
|
|
2
|
+
import type { Writable } from 'type-fest';
|
|
3
|
+
|
|
4
|
+
import type { SyncHookPlugin } from '@editor/type';
|
|
5
|
+
import { getConfig } from '@editor/utils/config';
|
|
6
|
+
|
|
7
|
+
import BaseService from './BaseService';
|
|
8
|
+
|
|
9
|
+
interface Options {
|
|
10
|
+
namespace?: string;
|
|
11
|
+
protocol?: Protocol;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export enum Protocol {
|
|
15
|
+
OBJECT = 'object',
|
|
16
|
+
JSON = 'json',
|
|
17
|
+
STRING = 'string',
|
|
18
|
+
NUMBER = 'number',
|
|
19
|
+
BOOLEAN = 'boolean',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const canUsePluginMethods = {
|
|
23
|
+
async: [],
|
|
24
|
+
sync: ['getStorage', 'getNamespace', 'clear', 'getItem', 'removeItem', 'setItem'] as const,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 数据存储服务
|
|
31
|
+
*/
|
|
32
|
+
export class WebStorage extends BaseService {
|
|
33
|
+
private storage: Storage = globalThis.localStorage;
|
|
34
|
+
private namespace = 'tmagic';
|
|
35
|
+
|
|
36
|
+
constructor() {
|
|
37
|
+
super(canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 获取数据存储对象,可以通过
|
|
42
|
+
* const storageService = new StorageService();
|
|
43
|
+
* storageService.usePlugin({
|
|
44
|
+
* // 替换存储对象为 sessionStorage
|
|
45
|
+
* async afterGetStorage(): Promise<Storage> {
|
|
46
|
+
* return window.sessionStorage;
|
|
47
|
+
* },
|
|
48
|
+
* });
|
|
49
|
+
*/
|
|
50
|
+
public getStorage(): Storage {
|
|
51
|
+
return this.storage;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public getNamespace(): string {
|
|
55
|
+
return this.namespace;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 清理,支持storageService.usePlugin
|
|
60
|
+
*/
|
|
61
|
+
public clear(): void {
|
|
62
|
+
const storage = this.getStorage();
|
|
63
|
+
storage.clear();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 获取存储项,支持storageService.usePlugin
|
|
67
|
+
*/
|
|
68
|
+
public getItem(key: string, options: Options = {}): any {
|
|
69
|
+
const storage = this.getStorage();
|
|
70
|
+
const namespace = this.getNamespace();
|
|
71
|
+
const { protocol = options.protocol, item } = this.getValueAndProtocol(
|
|
72
|
+
storage.getItem(`${options.namespace || namespace}:${key}`),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if (item === null) return null;
|
|
76
|
+
|
|
77
|
+
switch (protocol) {
|
|
78
|
+
case Protocol.OBJECT:
|
|
79
|
+
return getConfig('parseDSL')(`(${item})`);
|
|
80
|
+
case Protocol.JSON:
|
|
81
|
+
return JSON.parse(item);
|
|
82
|
+
case Protocol.NUMBER:
|
|
83
|
+
return Number(item);
|
|
84
|
+
case Protocol.BOOLEAN:
|
|
85
|
+
return Boolean(item);
|
|
86
|
+
default:
|
|
87
|
+
return item;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 获取指定索引位置的key
|
|
92
|
+
*/
|
|
93
|
+
public key(index: number): string | null {
|
|
94
|
+
const storage = this.getStorage();
|
|
95
|
+
return storage.key(index);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 移除存储项,支持storageService.usePlugin
|
|
100
|
+
*/
|
|
101
|
+
public removeItem(key: string, options: Options = {}): void {
|
|
102
|
+
const storage = this.getStorage();
|
|
103
|
+
const namespace = this.getNamespace();
|
|
104
|
+
storage.removeItem(`${options.namespace || namespace}:${key}`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 设置存储项,支持storageService.usePlugin
|
|
109
|
+
*/
|
|
110
|
+
public setItem(key: string, value: any, options: Options = {}): void {
|
|
111
|
+
const storage = this.getStorage();
|
|
112
|
+
const namespace = this.getNamespace();
|
|
113
|
+
let item = value;
|
|
114
|
+
const protocol = options.protocol ? `${options.protocol}:` : '';
|
|
115
|
+
if (typeof value === Protocol.STRING || typeof value === Protocol.NUMBER) {
|
|
116
|
+
item = `${protocol}${value}`;
|
|
117
|
+
} else {
|
|
118
|
+
item = `${protocol}${serialize(value)}`;
|
|
119
|
+
}
|
|
120
|
+
storage.setItem(`${options.namespace || namespace}:${key}`, item);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public destroy() {
|
|
124
|
+
this.removeAllListeners();
|
|
125
|
+
this.removeAllPlugins();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public usePlugin(options: SyncHookPlugin<SyncMethodName, WebStorage>): void {
|
|
129
|
+
super.usePlugin(options);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private getValueAndProtocol(value: string | null) {
|
|
133
|
+
let protocol = '';
|
|
134
|
+
|
|
135
|
+
if (value === null) {
|
|
136
|
+
return {
|
|
137
|
+
item: value,
|
|
138
|
+
protocol,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const item = value.replace(new RegExp(`^(${Object.values(Protocol).join('|')})(:)(.+)`), (_$0, $1, _$2, $3) => {
|
|
143
|
+
protocol = $1;
|
|
144
|
+
return $3;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
protocol,
|
|
149
|
+
item,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export type StorageService = WebStorage;
|
|
155
|
+
export default new WebStorage();
|