@tmagic/editor 1.4.8 → 1.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/style.css +26 -0
- package/dist/tmagic-editor.js +3213 -2820
- package/dist/tmagic-editor.umd.cjs +3237 -2839
- package/package.json +14 -12
- package/src/Editor.vue +212 -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 +120 -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/CondOpSelect.vue +87 -0
- package/src/fields/DataSourceFieldSelect/FieldSelect.vue +152 -0
- package/src/fields/DataSourceFieldSelect/Index.vue +168 -0
- package/src/fields/DataSourceFields.vue +329 -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/DisplayConds.vue +145 -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 +124 -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 +165 -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 +165 -0
- package/src/layouts/page-bar/PageBarScrollContainer.vue +192 -0
- package/src/layouts/page-bar/PageList.vue +55 -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 +157 -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-select.scss +16 -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 +102 -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 +28 -0
- package/src/theme/tree.scss +89 -0
- package/src/theme/workspace.scss +9 -0
- package/src/type.ts +780 -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 +249 -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 +407 -0
- package/src/utils/scroll-viewer.ts +165 -0
- package/src/utils/tree.ts +15 -0
- package/src/utils/undo-redo.ts +76 -0
- package/types/editorProps.d.ts +3 -1
- package/types/fields/CondOpSelect.vue.d.ts +17 -0
- package/types/fields/DataSourceFieldSelect/FieldSelect.vue.d.ts +36 -0
- package/types/fields/{DataSourceFieldSelect.vue.d.ts → DataSourceFieldSelect/Index.vue.d.ts} +1 -1
- package/types/fields/DisplayConds.vue.d.ts +32 -0
- package/types/index.d.ts +3 -1
- package/types/layouts/Framework.vue.d.ts +3 -1
- package/types/layouts/PropsPanel.vue.d.ts +21 -21
- package/types/layouts/page-bar/PageBar.vue.d.ts +6 -0
- package/types/layouts/page-bar/PageBarScrollContainer.vue.d.ts +3 -0
- package/types/layouts/page-bar/PageList.vue.d.ts +25 -0
- package/types/services/ui.d.ts +1 -0
- package/types/type.d.ts +30 -4
- package/types/utils/data-source/index.d.ts +1 -0
- package/types/utils/props.d.ts +12 -0
|
@@ -0,0 +1,313 @@
|
|
|
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 serialize from 'serialize-javascript';
|
|
20
|
+
|
|
21
|
+
import type { Id, MApp, MContainer, MNode, MPage, MPageFragment } from '@tmagic/schema';
|
|
22
|
+
import { NodeType } from '@tmagic/schema';
|
|
23
|
+
import type StageCore from '@tmagic/stage';
|
|
24
|
+
import { calcValueByFontsize, getNodePath, isNumber, isPage, isPageFragment, isPop } from '@tmagic/utils';
|
|
25
|
+
|
|
26
|
+
import { Layout } from '@editor/type';
|
|
27
|
+
export const COPY_STORAGE_KEY = '$MagicEditorCopyData';
|
|
28
|
+
export const COPY_CODE_STORAGE_KEY = '$MagicEditorCopyCode';
|
|
29
|
+
export const COPY_DS_STORAGE_KEY = '$MagicEditorCopyDataSource';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 获取所有页面配置
|
|
33
|
+
* @param root DSL跟节点
|
|
34
|
+
* @returns 所有页面配置
|
|
35
|
+
*/
|
|
36
|
+
export const getPageList = (root?: MApp | null): MPage[] => {
|
|
37
|
+
if (!root) return [];
|
|
38
|
+
if (!Array.isArray(root.items)) return [];
|
|
39
|
+
return root.items.filter((item) => isPage(item)) as MPage[];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const getPageFragmentList = (root?: MApp | null): MPageFragment[] => {
|
|
43
|
+
if (!root) return [];
|
|
44
|
+
if (!Array.isArray(root.items)) return [];
|
|
45
|
+
return root.items.filter((item) => isPageFragment(item)) as MPageFragment[];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 获取所有页面名称
|
|
50
|
+
* @param pages 所有页面配置
|
|
51
|
+
* @returns 所有页面名称
|
|
52
|
+
*/
|
|
53
|
+
export const getPageNameList = (pages: (MPage | MPageFragment)[]): string[] =>
|
|
54
|
+
pages.map((page) => page.name || 'index');
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 新增页面时,生成页面名称
|
|
58
|
+
* @param {Object} pageNameList 所有页面名称
|
|
59
|
+
* @returns {string}
|
|
60
|
+
*/
|
|
61
|
+
export const generatePageName = (pageNameList: string[], type: NodeType.PAGE | NodeType.PAGE_FRAGMENT): string => {
|
|
62
|
+
let pageLength = pageNameList.length;
|
|
63
|
+
|
|
64
|
+
if (!pageLength) return `${type}_index`;
|
|
65
|
+
|
|
66
|
+
let pageName = `${type}_${pageLength}`;
|
|
67
|
+
while (pageNameList.includes(pageName)) {
|
|
68
|
+
pageLength += 1;
|
|
69
|
+
pageName = `${type}_${pageLength}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return pageName;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 新增页面时,生成页面名称
|
|
77
|
+
* @param {Object} app 所有页面配置
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
export const generatePageNameByApp = (app: MApp, type: NodeType.PAGE | NodeType.PAGE_FRAGMENT): string =>
|
|
81
|
+
generatePageName(getPageNameList(type === 'page' ? getPageList(app) : getPageFragmentList(app)), type);
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {Object} node
|
|
85
|
+
* @returns {boolean}
|
|
86
|
+
*/
|
|
87
|
+
export const isFixed = (node: MNode): boolean => node.style?.position === 'fixed';
|
|
88
|
+
|
|
89
|
+
export const getNodeIndex = (id: Id, parent: MContainer | MApp): number => {
|
|
90
|
+
const items = parent?.items || [];
|
|
91
|
+
return items.findIndex((item: MNode) => `${item.id}` === `${id}`);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const getRelativeStyle = (style: Record<string, any> = {}): Record<string, any> => ({
|
|
95
|
+
...style,
|
|
96
|
+
position: 'relative',
|
|
97
|
+
top: 0,
|
|
98
|
+
left: 0,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const getMiddleTop = (node: MNode, parentNode: MNode, stage: StageCore | null) => {
|
|
102
|
+
let height = node.style?.height || 0;
|
|
103
|
+
|
|
104
|
+
if (!stage || typeof node.style?.top !== 'undefined' || !parentNode.style) return node.style?.top;
|
|
105
|
+
|
|
106
|
+
if (!isNumber(height)) {
|
|
107
|
+
height = 0;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const { height: parentHeight } = parentNode.style;
|
|
111
|
+
// wrapperHeight 是未 calcValue的高度, 所以要将其calcValueByFontsize一下, 否则在pad or pc端计算的结果有误
|
|
112
|
+
const { scrollTop = 0, wrapperHeight } = stage.mask;
|
|
113
|
+
const wrapperHeightDeal = calcValueByFontsize(stage.renderer.getDocument()!, wrapperHeight);
|
|
114
|
+
const scrollTopDeal = calcValueByFontsize(stage.renderer.getDocument()!, scrollTop);
|
|
115
|
+
if (isPage(parentNode)) {
|
|
116
|
+
return (wrapperHeightDeal - height) / 2 + scrollTopDeal;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 如果容器的元素高度大于当前视口高度的2倍, 添加的元素居中位置也会看不见, 所以要取最小值计算
|
|
120
|
+
return (Math.min(parentHeight, wrapperHeightDeal) - height) / 2;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const getInitPositionStyle = (style: Record<string, any> = {}, layout: Layout) => {
|
|
124
|
+
if (layout === Layout.ABSOLUTE) {
|
|
125
|
+
const newStyle: Record<string, any> = {
|
|
126
|
+
...style,
|
|
127
|
+
position: 'absolute',
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
if (typeof newStyle.left === 'undefined' && typeof newStyle.right === 'undefined') {
|
|
131
|
+
newStyle.left = 0;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return newStyle;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (layout === Layout.RELATIVE) {
|
|
138
|
+
return getRelativeStyle(style);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return style;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export const setChildrenLayout = (node: MContainer, layout: Layout) => {
|
|
145
|
+
node.items?.forEach((child: MNode) => {
|
|
146
|
+
setLayout(child, layout);
|
|
147
|
+
});
|
|
148
|
+
return node;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const setLayout = (node: MNode, layout: Layout) => {
|
|
152
|
+
if (isPop(node)) return;
|
|
153
|
+
|
|
154
|
+
const style = node.style || {};
|
|
155
|
+
|
|
156
|
+
// 是 fixed 不做处理
|
|
157
|
+
if (style.position === 'fixed') return;
|
|
158
|
+
|
|
159
|
+
if (layout !== Layout.RELATIVE) {
|
|
160
|
+
style.position = 'absolute';
|
|
161
|
+
} else {
|
|
162
|
+
node.style = getRelativeStyle(style);
|
|
163
|
+
node.style.right = 'auto';
|
|
164
|
+
node.style.bottom = 'auto';
|
|
165
|
+
}
|
|
166
|
+
return node;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const change2Fixed = (node: MNode, root: MApp) => {
|
|
170
|
+
const path = getNodePath(node.id, root.items);
|
|
171
|
+
const offset = {
|
|
172
|
+
left: 0,
|
|
173
|
+
top: 0,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
path.forEach((value) => {
|
|
177
|
+
offset.left = offset.left + globalThis.parseFloat(value.style?.left || 0);
|
|
178
|
+
offset.top = offset.top + globalThis.parseFloat(value.style?.top || 0);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
...(node.style || {}),
|
|
183
|
+
...offset,
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export const Fixed2Other = async (
|
|
188
|
+
node: MNode,
|
|
189
|
+
root: MApp,
|
|
190
|
+
getLayout: (parent: MNode, node?: MNode) => Promise<Layout>,
|
|
191
|
+
) => {
|
|
192
|
+
const path = getNodePath(node.id, root.items);
|
|
193
|
+
const cur = path.pop();
|
|
194
|
+
const offset = {
|
|
195
|
+
left: cur?.style?.left || 0,
|
|
196
|
+
top: cur?.style?.top || 0,
|
|
197
|
+
right: '',
|
|
198
|
+
bottom: '',
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
path.forEach((value) => {
|
|
202
|
+
offset.left = offset.left - globalThis.parseFloat(value.style?.left || 0);
|
|
203
|
+
offset.top = offset.top - globalThis.parseFloat(value.style?.top || 0);
|
|
204
|
+
});
|
|
205
|
+
const style = node.style || {};
|
|
206
|
+
|
|
207
|
+
const parent = path.pop();
|
|
208
|
+
if (!parent) {
|
|
209
|
+
return getRelativeStyle(style);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const layout = await getLayout(parent);
|
|
213
|
+
if (layout !== Layout.RELATIVE) {
|
|
214
|
+
return {
|
|
215
|
+
...style,
|
|
216
|
+
...offset,
|
|
217
|
+
position: 'absolute',
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return getRelativeStyle(style);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
export const getGuideLineFromCache = (key: string): number[] => {
|
|
225
|
+
if (!key) return [];
|
|
226
|
+
|
|
227
|
+
const guideLineCacheData = globalThis.localStorage.getItem(key);
|
|
228
|
+
if (guideLineCacheData) {
|
|
229
|
+
try {
|
|
230
|
+
return JSON.parse(guideLineCacheData) || [];
|
|
231
|
+
} catch (e) {
|
|
232
|
+
console.error(e);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return [];
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const fixNodeLeft = (config: MNode, parent: MContainer, doc?: Document) => {
|
|
240
|
+
if (!doc || !config.style || !isNumber(config.style.left)) return config.style?.left;
|
|
241
|
+
|
|
242
|
+
const el = doc.getElementById(`${config.id}`);
|
|
243
|
+
const parentEl = doc.getElementById(`${parent.id}`);
|
|
244
|
+
|
|
245
|
+
const left = Number(config.style?.left) || 0;
|
|
246
|
+
if (el && parentEl) {
|
|
247
|
+
const calcParentOffsetWidth = calcValueByFontsize(doc, parentEl.offsetWidth);
|
|
248
|
+
const calcElOffsetWidth = calcValueByFontsize(doc, el.offsetWidth);
|
|
249
|
+
if (calcElOffsetWidth + left > calcParentOffsetWidth) {
|
|
250
|
+
return calcParentOffsetWidth - calcElOffsetWidth;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return config.style.left;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export const fixNodePosition = (config: MNode, parent: MContainer, stage: StageCore | null) => {
|
|
258
|
+
if (config.style?.position !== 'absolute') {
|
|
259
|
+
return config.style;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
...(config.style || {}),
|
|
264
|
+
top: getMiddleTop(config, parent, stage),
|
|
265
|
+
left: fixNodeLeft(config, parent, stage?.renderer.contentWindow?.document),
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// 序列化配置
|
|
270
|
+
export const serializeConfig = (config: any) =>
|
|
271
|
+
serialize(config, {
|
|
272
|
+
space: 2,
|
|
273
|
+
unsafe: true,
|
|
274
|
+
}).replace(/"(\w+)":\s/g, '$1: ');
|
|
275
|
+
|
|
276
|
+
export interface NodeItem {
|
|
277
|
+
items?: NodeItem[];
|
|
278
|
+
[key: string]: any;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export const traverseNode = <T extends NodeItem = NodeItem>(
|
|
282
|
+
node: T,
|
|
283
|
+
cb: (node: T, parents: T[]) => void,
|
|
284
|
+
parents: T[] = [],
|
|
285
|
+
) => {
|
|
286
|
+
cb(node, parents);
|
|
287
|
+
|
|
288
|
+
if (Array.isArray(node.items) && node.items.length) {
|
|
289
|
+
parents.push(node);
|
|
290
|
+
node.items.forEach((item) => {
|
|
291
|
+
traverseNode(item as T, cb, [...parents]);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
export const moveItemsInContainer = (sourceIndices: number[], parent: MContainer, targetIndex: number) => {
|
|
297
|
+
sourceIndices.sort((a, b) => a - b);
|
|
298
|
+
for (let i = sourceIndices.length - 1; i >= 0; i--) {
|
|
299
|
+
const sourceIndex = sourceIndices[i];
|
|
300
|
+
if (sourceIndex === targetIndex) {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const [item] = parent.items.splice(sourceIndex, 1);
|
|
304
|
+
parent.items.splice(sourceIndex < targetIndex ? targetIndex - 1 : targetIndex, 0, item);
|
|
305
|
+
|
|
306
|
+
// 更新后续源索引(因为数组已经改变)
|
|
307
|
+
for (let j = i - 1; j >= 0; j--) {
|
|
308
|
+
if (sourceIndices[j] >= targetIndex) {
|
|
309
|
+
sourceIndices[j] += 1;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
export interface IdleTaskEvents {
|
|
4
|
+
finish: [];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
globalThis.requestIdleCallback =
|
|
8
|
+
globalThis.requestIdleCallback ||
|
|
9
|
+
function (cb) {
|
|
10
|
+
const start = Date.now();
|
|
11
|
+
return setTimeout(() => {
|
|
12
|
+
cb({
|
|
13
|
+
didTimeout: false,
|
|
14
|
+
timeRemaining() {
|
|
15
|
+
return Math.max(0, 50 - (Date.now() - start));
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}, 1);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export class IdleTask<T = any> extends EventEmitter {
|
|
22
|
+
private taskList: {
|
|
23
|
+
handler: (data: T) => void;
|
|
24
|
+
data: T;
|
|
25
|
+
}[] = [];
|
|
26
|
+
|
|
27
|
+
private taskHandle: number | null = null;
|
|
28
|
+
|
|
29
|
+
public enqueueTask(taskHandler: (data: T) => void, taskData: T) {
|
|
30
|
+
this.taskList.push({
|
|
31
|
+
handler: taskHandler,
|
|
32
|
+
data: taskData,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (!this.taskHandle) {
|
|
36
|
+
this.taskHandle = globalThis.requestIdleCallback(this.runTaskQueue.bind(this), { timeout: 10000 });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public on<Name extends keyof IdleTaskEvents, Param extends IdleTaskEvents[Name]>(
|
|
41
|
+
eventName: Name,
|
|
42
|
+
listener: (...args: Param) => void | Promise<void>,
|
|
43
|
+
) {
|
|
44
|
+
return super.on(eventName, listener as any);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public once<Name extends keyof IdleTaskEvents, Param extends IdleTaskEvents[Name]>(
|
|
48
|
+
eventName: Name,
|
|
49
|
+
listener: (...args: Param) => void | Promise<void>,
|
|
50
|
+
) {
|
|
51
|
+
return super.once(eventName, listener as any);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public emit<Name extends keyof IdleTaskEvents, Param extends IdleTaskEvents[Name]>(eventName: Name, ...args: Param) {
|
|
55
|
+
return super.emit(eventName, ...args);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private runTaskQueue(deadline: IdleDeadline) {
|
|
59
|
+
while ((deadline.timeRemaining() > 15 || deadline.didTimeout) && this.taskList.length) {
|
|
60
|
+
const task = this.taskList.shift();
|
|
61
|
+
task!.handler(task!.data);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (this.taskList.length) {
|
|
65
|
+
this.taskHandle = globalThis.requestIdleCallback(this.runTaskQueue.bind(this), { timeout: 10000 });
|
|
66
|
+
} else {
|
|
67
|
+
this.taskHandle = 0;
|
|
68
|
+
|
|
69
|
+
this.emit('finish');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
export * from './config';
|
|
20
|
+
export * from './props';
|
|
21
|
+
export * from './logger';
|
|
22
|
+
export * from './editor';
|
|
23
|
+
export * from './operator';
|
|
24
|
+
export * from './data-source';
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { KeyBindingCommand, KeyBindingItem } from '@editor/type';
|
|
2
|
+
|
|
3
|
+
export enum KeyBindingContainerKey {
|
|
4
|
+
STAGE = 'stage',
|
|
5
|
+
LAYER_PANEL = 'layer-panel',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default [
|
|
9
|
+
{
|
|
10
|
+
command: KeyBindingCommand.DELETE_NODE,
|
|
11
|
+
keybinding: ['delete', 'backspace'],
|
|
12
|
+
when: [
|
|
13
|
+
[KeyBindingContainerKey.STAGE, 'keyup'],
|
|
14
|
+
[KeyBindingContainerKey.LAYER_PANEL, 'keydown'],
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
command: KeyBindingCommand.COPY_NODE,
|
|
19
|
+
keybinding: 'ctrl+c',
|
|
20
|
+
when: [
|
|
21
|
+
[KeyBindingContainerKey.STAGE, 'keydown'],
|
|
22
|
+
[KeyBindingContainerKey.LAYER_PANEL, 'keydown'],
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
command: KeyBindingCommand.PASTE_NODE,
|
|
27
|
+
keybinding: 'ctrl+v',
|
|
28
|
+
when: [
|
|
29
|
+
[KeyBindingContainerKey.STAGE, 'keydown'],
|
|
30
|
+
[KeyBindingContainerKey.LAYER_PANEL, 'keydown'],
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
command: KeyBindingCommand.CUT_NODE,
|
|
35
|
+
keybinding: 'ctrl+x',
|
|
36
|
+
when: [
|
|
37
|
+
[KeyBindingContainerKey.STAGE, 'keydown'],
|
|
38
|
+
[KeyBindingContainerKey.LAYER_PANEL, 'keydown'],
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
command: KeyBindingCommand.UNDO,
|
|
43
|
+
keybinding: 'ctrl+z',
|
|
44
|
+
when: [
|
|
45
|
+
[KeyBindingContainerKey.STAGE, 'keydown'],
|
|
46
|
+
[KeyBindingContainerKey.LAYER_PANEL, 'keydown'],
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
command: KeyBindingCommand.REDO,
|
|
51
|
+
keybinding: 'ctrl+shift+z',
|
|
52
|
+
when: [
|
|
53
|
+
[KeyBindingContainerKey.STAGE, 'keydown'],
|
|
54
|
+
[KeyBindingContainerKey.LAYER_PANEL, 'keydown'],
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
command: KeyBindingCommand.MOVE_UP_1,
|
|
59
|
+
keybinding: 'up',
|
|
60
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
command: KeyBindingCommand.MOVE_DOWN_1,
|
|
64
|
+
keybinding: 'down',
|
|
65
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
command: KeyBindingCommand.MOVE_LEFT_1,
|
|
69
|
+
keybinding: 'left',
|
|
70
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
command: KeyBindingCommand.MOVE_RIGHT_1,
|
|
74
|
+
keybinding: 'right',
|
|
75
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
command: KeyBindingCommand.MOVE_UP_10,
|
|
79
|
+
keybinding: 'ctrl+up',
|
|
80
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
command: KeyBindingCommand.MOVE_DOWN_10,
|
|
84
|
+
keybinding: 'ctrl+down',
|
|
85
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
command: KeyBindingCommand.MOVE_LEFT_10,
|
|
89
|
+
keybinding: 'ctrl+left',
|
|
90
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
command: KeyBindingCommand.MOVE_RIGHT_10,
|
|
94
|
+
keybinding: 'ctrl+right',
|
|
95
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
command: KeyBindingCommand.SWITCH_NODE,
|
|
99
|
+
keybinding: 'tab',
|
|
100
|
+
when: [
|
|
101
|
+
[KeyBindingContainerKey.STAGE, 'keydown'],
|
|
102
|
+
[KeyBindingContainerKey.LAYER_PANEL, 'keydown'],
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
command: KeyBindingCommand.ZOOM_IN,
|
|
107
|
+
keybinding: ['ctrl+=', 'ctrl+numpadplus'],
|
|
108
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
command: KeyBindingCommand.ZOOM_OUT,
|
|
112
|
+
keybinding: ['ctrl+-', 'ctrl+numpad-'],
|
|
113
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
command: KeyBindingCommand.ZOOM_FIT,
|
|
117
|
+
keybinding: 'ctrl+0',
|
|
118
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
command: KeyBindingCommand.ZOOM_RESET,
|
|
122
|
+
keybinding: 'ctrl+1',
|
|
123
|
+
when: [[KeyBindingContainerKey.STAGE, 'keydown']],
|
|
124
|
+
},
|
|
125
|
+
] as KeyBindingItem[];
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
export const log = (...args: any[]) => {
|
|
20
|
+
if (process.env.NODE_ENV === 'development') {
|
|
21
|
+
console.log('magic editor: ', ...args);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const info = (...args: any[]) => {
|
|
26
|
+
if (process.env.NODE_ENV === 'development') {
|
|
27
|
+
console.info('magic editor: ', ...args);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const warn = (...args: any[]) => {
|
|
32
|
+
if (process.env.NODE_ENV === 'development') {
|
|
33
|
+
console.warn('magic editor: ', ...args);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const debug = (...args: any[]) => {
|
|
38
|
+
if (process.env.NODE_ENV === 'development') {
|
|
39
|
+
console.debug('magic editor: ', ...args);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const error = (...args: any[]) => {
|
|
44
|
+
if (process.env.NODE_ENV === 'development') {
|
|
45
|
+
console.error('magic editor: ', ...args);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { toRaw } from 'vue';
|
|
2
|
+
import { isEmpty } from 'lodash-es';
|
|
3
|
+
|
|
4
|
+
import { Id, MContainer, MNode, NodeType } from '@tmagic/schema';
|
|
5
|
+
import { calcValueByFontsize, isPage, isPageFragment } from '@tmagic/utils';
|
|
6
|
+
|
|
7
|
+
import editorService from '@editor/services/editor';
|
|
8
|
+
import propsService from '@editor/services/props';
|
|
9
|
+
import type { AddMNode, PastePosition } from '@editor/type';
|
|
10
|
+
import { generatePageNameByApp, getInitPositionStyle } from '@editor/utils/editor';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 粘贴前置操作:返回分配了新id以及校准了坐标的配置
|
|
14
|
+
* @param position 粘贴的坐标
|
|
15
|
+
* @param config 待粘贴的元素配置(复制时保存的那份配置)
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export const beforePaste = (position: PastePosition, config: MNode[], doc?: Document): MNode[] => {
|
|
19
|
+
if (!config[0]?.style) return config;
|
|
20
|
+
const curNode = editorService.get('node');
|
|
21
|
+
// 将数组中第一个元素的坐标作为参照点
|
|
22
|
+
const { left: referenceLeft, top: referenceTop } = config[0].style;
|
|
23
|
+
// 坐标校准后的粘贴数据
|
|
24
|
+
const pasteConfigs: MNode[] = config.map((configItem: MNode): MNode => {
|
|
25
|
+
// 解构 position 对象,从 position 删除 offsetX、offsetY字段
|
|
26
|
+
const { offsetX = 0, offsetY = 0, ...positionClone } = position;
|
|
27
|
+
let pastePosition = positionClone;
|
|
28
|
+
|
|
29
|
+
if (!isEmpty(pastePosition) && curNode?.items) {
|
|
30
|
+
// 如果没有传入粘贴坐标则可能为键盘操作,不再转换
|
|
31
|
+
// 如果粘贴时选中了容器,则将元素粘贴到容器内,坐标需要转换为相对于容器的坐标
|
|
32
|
+
pastePosition = getPositionInContainer(pastePosition, curNode.id, doc);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 将所有待粘贴元素坐标相对于多选第一个元素坐标重新计算,以保证多选粘贴后元素间距不变
|
|
36
|
+
if (pastePosition.left && configItem.style?.left) {
|
|
37
|
+
pastePosition.left = configItem.style.left - referenceLeft + pastePosition.left;
|
|
38
|
+
}
|
|
39
|
+
if (pastePosition.top && configItem.style?.top) {
|
|
40
|
+
pastePosition.top = configItem.style?.top - referenceTop + pastePosition.top;
|
|
41
|
+
}
|
|
42
|
+
const pasteConfig = propsService.setNewItemId(configItem, false);
|
|
43
|
+
|
|
44
|
+
if (pasteConfig.style) {
|
|
45
|
+
const { left, top } = pasteConfig.style;
|
|
46
|
+
// 判断能转换为数字时,做粘贴偏移量计算
|
|
47
|
+
if (typeof left === 'number' || (!!left && !isNaN(Number(left)))) {
|
|
48
|
+
pasteConfig.style.left = Number(left) + offsetX;
|
|
49
|
+
}
|
|
50
|
+
if (typeof top === 'number' || (!!top && !isNaN(Number(top)))) {
|
|
51
|
+
pasteConfig.style.top = Number(top) + offsetY;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
pasteConfig.style = {
|
|
55
|
+
...pasteConfig.style,
|
|
56
|
+
...pastePosition,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const root = editorService.get('root');
|
|
60
|
+
if ((isPage(pasteConfig) || isPageFragment(pasteConfig)) && root) {
|
|
61
|
+
pasteConfig.name = generatePageNameByApp(root, isPage(pasteConfig) ? NodeType.PAGE : NodeType.PAGE_FRAGMENT);
|
|
62
|
+
}
|
|
63
|
+
return pasteConfig as MNode;
|
|
64
|
+
});
|
|
65
|
+
return pasteConfigs;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 将元素粘贴到容器内时,将相对于画布坐标转换为相对于容器的坐标
|
|
70
|
+
* @param position PastePosition 粘贴时相对于画布的坐标
|
|
71
|
+
* @param id 元素id
|
|
72
|
+
* @returns PastePosition 转换后的坐标
|
|
73
|
+
*/
|
|
74
|
+
export const getPositionInContainer = (position: PastePosition = {}, id: Id, doc?: Document) => {
|
|
75
|
+
let { left = 0, top = 0 } = position;
|
|
76
|
+
const parentEl = editorService.get('stage')?.renderer?.contentWindow?.document.getElementById(`${id}`);
|
|
77
|
+
const parentElRect = parentEl?.getBoundingClientRect();
|
|
78
|
+
left = left - calcValueByFontsize(doc, parentElRect?.left || 0);
|
|
79
|
+
top = top - calcValueByFontsize(doc, parentElRect?.top || 0);
|
|
80
|
+
return {
|
|
81
|
+
left,
|
|
82
|
+
top,
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const getAddParent = (node: MNode) => {
|
|
87
|
+
const curNode = editorService.get('node');
|
|
88
|
+
|
|
89
|
+
let parentNode;
|
|
90
|
+
if (isPage(node)) {
|
|
91
|
+
parentNode = editorService.get('root');
|
|
92
|
+
} else if (curNode?.items) {
|
|
93
|
+
parentNode = curNode as MContainer;
|
|
94
|
+
} else if (curNode?.id) {
|
|
95
|
+
parentNode = editorService.getParentById(curNode.id, false);
|
|
96
|
+
}
|
|
97
|
+
return parentNode;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const getDefaultConfig = async (addNode: AddMNode, parentNode: MContainer) => {
|
|
101
|
+
const { type, inputEvent, ...config } = addNode;
|
|
102
|
+
const layout = await editorService.getLayout(toRaw(parentNode), addNode as MNode);
|
|
103
|
+
const newNode = { ...toRaw(await propsService.getPropsValue(type, config)) };
|
|
104
|
+
newNode.style = getInitPositionStyle(newNode.style, layout);
|
|
105
|
+
return newNode;
|
|
106
|
+
};
|