@tmagic/editor 1.1.0-beta.7 → 1.1.0

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 (63) hide show
  1. package/dist/style.css +50 -0
  2. package/dist/tmagic-editor.mjs +1198 -1027
  3. package/dist/tmagic-editor.mjs.map +1 -1
  4. package/dist/tmagic-editor.umd.js +1201 -1026
  5. package/dist/tmagic-editor.umd.js.map +1 -1
  6. package/package.json +10 -10
  7. package/src/Editor.vue +22 -6
  8. package/src/components/ContentMenu.vue +4 -4
  9. package/src/components/Icon.vue +7 -21
  10. package/src/components/ScrollBar.vue +147 -0
  11. package/src/components/ScrollViewer.vue +89 -44
  12. package/src/components/ToolButton.vue +40 -138
  13. package/src/fields/UISelect.vue +2 -2
  14. package/src/index.ts +2 -0
  15. package/src/layouts/NavMenu.vue +156 -23
  16. package/src/layouts/PropsPanel.vue +49 -51
  17. package/src/layouts/sidebar/LayerMenu.vue +3 -3
  18. package/src/layouts/sidebar/LayerPanel.vue +39 -8
  19. package/src/layouts/workspace/PageBar.vue +1 -1
  20. package/src/layouts/workspace/Stage.vue +8 -86
  21. package/src/layouts/workspace/ViewerMenu.vue +10 -13
  22. package/src/layouts/workspace/Workspace.vue +133 -138
  23. package/src/services/componentList.ts +5 -0
  24. package/src/services/editor.ts +201 -112
  25. package/src/services/events.ts +8 -2
  26. package/src/services/props.ts +31 -52
  27. package/src/services/storage.ts +4 -0
  28. package/src/services/ui.ts +32 -30
  29. package/src/theme/component-list-panel.scss +5 -0
  30. package/src/theme/nav-menu.scss +6 -0
  31. package/src/type.ts +28 -11
  32. package/src/utils/editor.ts +17 -10
  33. package/src/utils/index.ts +1 -0
  34. package/src/utils/operator.ts +22 -123
  35. package/src/utils/props.ts +0 -3
  36. package/src/utils/scroll-viewer.ts +90 -158
  37. package/src/utils/stage.ts +91 -0
  38. package/types/Editor.vue.d.ts +17 -8
  39. package/types/components/ContentMenu.vue.d.ts +8 -8
  40. package/types/components/Icon.vue.d.ts +62 -12
  41. package/types/components/ScrollBar.vue.d.ts +83 -0
  42. package/types/components/ScrollViewer.vue.d.ts +131 -19
  43. package/types/components/ToolButton.vue.d.ts +56 -59
  44. package/types/index.d.ts +2 -0
  45. package/types/layouts/NavMenu.vue.d.ts +92 -23
  46. package/types/layouts/PropsPanel.vue.d.ts +25 -208
  47. package/types/layouts/sidebar/LayerMenu.vue.d.ts +7 -7
  48. package/types/layouts/sidebar/LayerPanel.vue.d.ts +17 -15
  49. package/types/layouts/workspace/ViewerMenu.vue.d.ts +3 -3
  50. package/types/layouts/workspace/Workspace.vue.d.ts +54 -4
  51. package/types/services/componentList.d.ts +1 -0
  52. package/types/services/editor.d.ts +10 -17
  53. package/types/services/events.d.ts +1 -0
  54. package/types/services/props.d.ts +41 -9
  55. package/types/services/storage.d.ts +1 -0
  56. package/types/services/ui.d.ts +3 -1
  57. package/types/type.d.ts +25 -11
  58. package/types/utils/editor.d.ts +4 -1
  59. package/types/utils/index.d.ts +1 -0
  60. package/types/utils/operator.d.ts +3 -26
  61. package/types/utils/props.d.ts +0 -1
  62. package/types/utils/scroll-viewer.d.ts +16 -18
  63. package/types/utils/stage.d.ts +3 -0
@@ -64,16 +64,6 @@ class Ui extends BaseService {
64
64
  center: 'auto',
65
65
  });
66
66
  });
67
-
68
- const columnWidthCacheData = globalThis.localStorage.getItem(COLUMN_WIDTH_STORAGE_KEY);
69
- if (columnWidthCacheData) {
70
- try {
71
- const columnWidthCache = JSON.parse(columnWidthCacheData);
72
- this.setColumnWidth(columnWidthCache);
73
- } catch (e) {
74
- console.error(e);
75
- }
76
- }
77
67
  }
78
68
 
79
69
  public set<T = any>(name: keyof UiState, value: T) {
@@ -98,21 +88,49 @@ class Ui extends BaseService {
98
88
  }
99
89
 
100
90
  (state as any)[name] = value;
101
-
102
- if (name === 'stageContainerRect') {
103
- state.zoom = this.calcZoom();
104
- }
105
91
  }
106
92
 
107
93
  public get<T>(name: keyof typeof state): T {
108
94
  return (state as any)[name];
109
95
  }
110
96
 
97
+ public initColumnWidth() {
98
+ const columnWidthCacheData = globalThis.localStorage.getItem(COLUMN_WIDTH_STORAGE_KEY);
99
+ if (columnWidthCacheData) {
100
+ try {
101
+ const columnWidthCache = JSON.parse(columnWidthCacheData);
102
+ this.setColumnWidth(columnWidthCache);
103
+ } catch (e) {
104
+ console.error(e);
105
+ }
106
+ }
107
+ }
108
+
111
109
  public zoom(zoom: number) {
112
110
  this.set('zoom', (this.get<number>('zoom') * 100 + zoom * 100) / 100);
113
111
  if (this.get<number>('zoom') < 0.1) this.set('zoom', 0.1);
114
112
  }
115
113
 
114
+ public calcZoom() {
115
+ const { stageRect, stageContainerRect } = state;
116
+ const { height, width } = stageContainerRect;
117
+ if (!width || !height) return 1;
118
+
119
+ // 30为标尺的大小
120
+ const stageWidth = stageRect.width + 30;
121
+ const stageHeight = stageRect.height + 30;
122
+
123
+ if (width > stageWidth && height > stageHeight) {
124
+ return 1;
125
+ }
126
+ // 60/80是为了不要让画布太过去贴住四周(这样好看些)
127
+ return Math.min((width - 60) / stageWidth || 1, (height - 80) / stageHeight || 1);
128
+ }
129
+
130
+ public destroy() {
131
+ this.removeAllListeners();
132
+ }
133
+
116
134
  private setColumnWidth({ left, center, right }: SetColumnWidth) {
117
135
  const columnWidth = {
118
136
  ...toRaw(this.get<GetColumnWidth>('columnWidth')),
@@ -150,22 +168,6 @@ class Ui extends BaseService {
150
168
  };
151
169
  state.zoom = this.calcZoom();
152
170
  }
153
-
154
- private calcZoom() {
155
- const { stageRect, stageContainerRect } = state;
156
- const { height, width } = stageContainerRect;
157
- if (!width || !height) return 1;
158
-
159
- // 30为标尺的大小
160
- const stageWidth = stageRect.width + 30;
161
- const stageHeight = stageRect.height + 30;
162
-
163
- if (width > stageWidth && height > stageHeight) {
164
- return 1;
165
- }
166
- // 60/80是为了不要让画布太过去贴住四周(这样好看些)
167
- return Math.min((width - 60) / stageWidth || 1, (height - 80) / stageHeight || 1);
168
- }
169
171
  }
170
172
 
171
173
  export type UiService = Ui;
@@ -86,6 +86,11 @@
86
86
  }
87
87
  }
88
88
 
89
+ img {
90
+ max-width: 100%;
91
+ max-height: 100%;
92
+ }
93
+
89
94
  span {
90
95
  font-size: 12px;
91
96
  text-align: center;
@@ -69,5 +69,11 @@
69
69
  .menu-item-text {
70
70
  color: $--nav-color;
71
71
  }
72
+
73
+ &.rule {
74
+ .el-icon {
75
+ transform: rotate(-90deg);
76
+ }
77
+ }
72
78
  }
73
79
  }
package/src/type.ts CHANGED
@@ -21,7 +21,7 @@ import type { Component } from 'vue';
21
21
  import type { FormConfig } from '@tmagic/form';
22
22
  import type { Id, MApp, MContainer, MNode, MPage } from '@tmagic/schema';
23
23
  import type StageCore from '@tmagic/stage';
24
- import type { MoveableOptions } from '@tmagic/stage';
24
+ import type { ContainerHighlightType, MoveableOptions } from '@tmagic/stage';
25
25
 
26
26
  import type { ComponentListService } from './services/componentList';
27
27
  import type { EditorService } from './services/editor';
@@ -53,6 +53,7 @@ export interface StageOptions {
53
53
  autoScrollIntoView: boolean;
54
54
  containerHighlightClassName: string;
55
55
  containerHighlightDuration: number;
56
+ containerHighlightType: ContainerHighlightType;
56
57
  render: () => HTMLDivElement;
57
58
  moveableOptions: MoveableOptions | ((core?: StageCore) => MoveableOptions);
58
59
  canSelect: (el: HTMLElement) => boolean | Promise<boolean>;
@@ -81,16 +82,22 @@ export interface ComponentGroupState {
81
82
  list: ComponentGroup[];
82
83
  }
83
84
 
85
+ export enum ColumnLayout {
86
+ LEFT = 'left',
87
+ CENTER = 'center',
88
+ RIGHT = 'right',
89
+ }
90
+
84
91
  export interface SetColumnWidth {
85
- left?: number;
86
- center?: number | 'auto';
87
- right?: number;
92
+ [ColumnLayout.LEFT]?: number;
93
+ [ColumnLayout.CENTER]?: number | 'auto';
94
+ [ColumnLayout.RIGHT]?: number;
88
95
  }
89
96
 
90
97
  export interface GetColumnWidth {
91
- left: number;
92
- center: number;
93
- right: number;
98
+ [ColumnLayout.LEFT]: number;
99
+ [ColumnLayout.CENTER]: number;
100
+ [ColumnLayout.RIGHT]: number;
94
101
  }
95
102
 
96
103
  export interface StageRect {
@@ -173,6 +180,7 @@ export interface MenuButton {
173
180
  /** type为button/dropdown时点击运行的方法 */
174
181
  handler?: (data: Services, event: MouseEvent) => Promise<any> | any;
175
182
  /** type为dropdown时,下拉的菜单列表, 或者有子菜单时 */
183
+ className?: string;
176
184
  items?: MenuButton[];
177
185
  }
178
186
 
@@ -186,6 +194,7 @@ export interface MenuComponent {
186
194
  listeners?: Record<string, Function>;
187
195
  slots?: Record<string, any>;
188
196
  /** 是否显示,默认为true */
197
+ className?: string;
189
198
  display?: boolean | ((data?: Services) => Promise<boolean> | boolean);
190
199
  }
191
200
 
@@ -208,16 +217,17 @@ export type MenuItem =
208
217
  | 'guides'
209
218
  | 'rule'
210
219
  | MenuButton
211
- | MenuComponent;
220
+ | MenuComponent
221
+ | string;
212
222
 
213
223
  /** 工具栏 */
214
224
  export interface MenuBarData {
215
225
  /** 顶部工具栏左边项 */
216
- left?: MenuItem[];
226
+ [ColumnLayout.LEFT]?: MenuItem[];
217
227
  /** 顶部工具栏中间项 */
218
- center?: MenuItem[];
228
+ [ColumnLayout.CENTER]?: MenuItem[];
219
229
  /** 顶部工具栏右边项 */
220
- right?: MenuItem[];
230
+ [ColumnLayout.RIGHT]?: MenuItem[];
221
231
  }
222
232
 
223
233
  export interface SideComponent extends MenuComponent {
@@ -286,3 +296,10 @@ export enum Keys {
286
296
 
287
297
  export const H_GUIDE_LINE_STORAGE_KEY = '$MagicStageHorizontalGuidelinesData';
288
298
  export const V_GUIDE_LINE_STORAGE_KEY = '$MagicStageVerticalGuidelinesData';
299
+
300
+ export interface ScrollViewerEvent {
301
+ scrollLeft: number;
302
+ scrollTop: number;
303
+ scrollHeight: number;
304
+ scrollWidth: number;
305
+ }
@@ -88,10 +88,10 @@ export const getRelativeStyle = (style: Record<string, any> = {}): Record<string
88
88
  left: 0,
89
89
  });
90
90
 
91
- const getMiddleTop = (style: Record<string, any> = {}, parentNode: MNode, stage: StageCore) => {
92
- let height = style.height || 0;
91
+ const getMiddleTop = (node: MNode, parentNode: MNode, stage: StageCore | null) => {
92
+ let height = node.style?.height || 0;
93
93
 
94
- if (!stage || typeof style.top !== 'undefined' || !parentNode.style) return style.top;
94
+ if (!stage || typeof node.style?.top !== 'undefined' || !parentNode.style) return node.style?.top;
95
95
 
96
96
  if (!isNumber(height)) {
97
97
  height = 0;
@@ -103,20 +103,15 @@ const getMiddleTop = (style: Record<string, any> = {}, parentNode: MNode, stage:
103
103
  const { scrollTop = 0, wrapperHeight } = stage.mask;
104
104
  return (wrapperHeight - height) / 2 + scrollTop;
105
105
  }
106
+
106
107
  return (parentHeight - height) / 2;
107
108
  };
108
109
 
109
- export const getInitPositionStyle = (
110
- style: Record<string, any> = {},
111
- layout: Layout,
112
- parentNode: MNode,
113
- stage: StageCore,
114
- ) => {
110
+ export const getInitPositionStyle = (style: Record<string, any> = {}, layout: Layout) => {
115
111
  if (layout === Layout.ABSOLUTE) {
116
112
  return {
117
113
  ...style,
118
114
  position: 'absolute',
119
- top: getMiddleTop(style, parentNode, stage),
120
115
  };
121
116
  }
122
117
 
@@ -230,3 +225,15 @@ export const fixNodeLeft = (config: MNode, parent: MContainer, doc?: Document) =
230
225
 
231
226
  return config.style.left;
232
227
  };
228
+
229
+ export const fixNodePosition = (config: MNode, parent: MContainer, stage: StageCore | null) => {
230
+ if (config.style?.position !== 'absolute') {
231
+ return config.style;
232
+ }
233
+
234
+ return {
235
+ ...(config.style || {}),
236
+ top: getMiddleTop(config, parent, stage),
237
+ left: fixNodeLeft(config, parent, stage?.renderer.contentWindow?.document),
238
+ };
239
+ };
@@ -20,3 +20,4 @@ export * from './config';
20
20
  export * from './props';
21
21
  export * from './logger';
22
22
  export * from './editor';
23
+ export * from './stage';
@@ -1,15 +1,14 @@
1
1
  import { toRaw } from 'vue';
2
- import { cloneDeep, isEmpty } from 'lodash-es';
2
+ import { isEmpty } from 'lodash-es';
3
3
 
4
- import { Id, MApp, MContainer, MNode, NodeType } from '@tmagic/schema';
4
+ import { Id, MApp, MContainer, MNode } from '@tmagic/schema';
5
5
  import StageCore from '@tmagic/stage';
6
6
  import { isPage } from '@tmagic/utils';
7
7
 
8
8
  import editorService from '../services/editor';
9
- import historyService from '../services/history';
10
9
  import propsService from '../services/props';
11
- import { AddMNode, Layout, PastePosition } from '../type';
12
- import { fixNodeLeft, generatePageNameByApp, getInitPositionStyle, getNodeIndex } from '../utils/editor';
10
+ import { AddMNode, PastePosition } from '../type';
11
+ import { generatePageNameByApp, getInitPositionStyle } from '../utils/editor';
13
12
 
14
13
  /**
15
14
  * 粘贴前置操作:返回分配了新id以及校准了坐标的配置
@@ -17,7 +16,7 @@ import { fixNodeLeft, generatePageNameByApp, getInitPositionStyle, getNodeIndex
17
16
  * @param config 待粘贴的元素配置(复制时保存的那份配置)
18
17
  * @returns
19
18
  */
20
- export const beforePaste = async (position: PastePosition, config: MNode[]) => {
19
+ export const beforePaste = async (position: PastePosition, config: MNode[]): Promise<MNode[]> => {
21
20
  if (!config[0]?.style) return config;
22
21
  const curNode = editorService.get<MContainer>('node');
23
22
  // 将数组中第一个元素的坐标作为参照点
@@ -43,7 +42,7 @@ export const beforePaste = async (position: PastePosition, config: MNode[]) => {
43
42
  pastePosition.top = configItem.style?.top - referenceTop + pastePosition.top;
44
43
  }
45
44
 
46
- const pasteConfig = await propsService.setNewItemId(configItem, editorService.get('root'));
45
+ const pasteConfig = await propsService.setNewItemId(configItem);
47
46
 
48
47
  if (pasteConfig.style) {
49
48
  const { left, top } = pasteConfig.style;
@@ -69,54 +68,6 @@ export const beforePaste = async (position: PastePosition, config: MNode[]) => {
69
68
  return pasteConfigs;
70
69
  };
71
70
 
72
- /**
73
- * 新增元素前置操作,实现了在编辑器中新增元素节点,并返回新增元素信息以供stage更新
74
- * @param addNode 待添加元素的配置
75
- * @param parent 父级容器(可选)
76
- * @returns 新增的元素,父级元素,布局方式,是否为根页面
77
- */
78
- export const beforeAdd = async (
79
- addNode: AddMNode,
80
- parent?: MContainer | null,
81
- ): Promise<{ parentNode: MContainer; newNode: MNode; layout: Layout; isPage: boolean }> => {
82
- // 加入inputEvent是为给业务扩展时可以获取到更多的信息,只有在使用拖拽添加组件时才有改对象
83
- const { type, inputEvent, ...config } = addNode;
84
- const curNode = editorService.get<MContainer>('node');
85
-
86
- let parentNode: MContainer | undefined;
87
- const isPage = type === NodeType.PAGE;
88
-
89
- if (isPage) {
90
- parentNode = editorService.get<MApp>('root');
91
- // 由于支持中间件扩展,在parent参数为undefined时,parent会变成next函数
92
- } else if (parent && typeof parent !== 'function') {
93
- parentNode = parent;
94
- } else if (curNode.items) {
95
- parentNode = curNode;
96
- } else {
97
- parentNode = editorService.getParentById(curNode.id, false);
98
- }
99
-
100
- if (!parentNode) throw new Error('未找到父元素');
101
-
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, parentNode, editorService.get<StageCore>('stage'));
105
-
106
- if ((parentNode?.type === NodeType.ROOT || curNode.type === NodeType.ROOT) && newNode.type !== NodeType.PAGE) {
107
- throw new Error('app下不能添加组件');
108
- }
109
- // 新增节点添加到配置中
110
- parentNode?.items?.push(newNode);
111
- // 返回新增信息以供stage更新
112
- return {
113
- parentNode,
114
- newNode,
115
- layout,
116
- isPage,
117
- };
118
- };
119
-
120
71
  /**
121
72
  * 将元素粘贴到容器内时,将相对于画布坐标转换为相对于容器的坐标
122
73
  * @param position PastePosition 粘贴时相对于画布的坐标
@@ -135,76 +86,24 @@ export const getPositionInContainer = (position: PastePosition = {}, id: Id) =>
135
86
  };
136
87
  };
137
88
 
138
- /**
139
- * 将新增元素事件通知到stage以更新渲染
140
- * @param parentNode 父元素
141
- * @param newNode 当前新增元素
142
- * @param layout 布局方式
143
- */
144
- export const notifyAddToStage = async (parentNode: MContainer, newNode: MNode, layout: Layout) => {
145
- const stage = editorService.get<StageCore | null>('stage');
146
- const root = editorService.get<MApp>('root');
147
-
148
- await stage?.add({ config: cloneDeep(newNode), parent: cloneDeep(parentNode), root: cloneDeep(root) });
89
+ export const getAddParent = (node: MNode) => {
90
+ const curNode = editorService.get<MContainer>('node');
149
91
 
150
- if (layout === Layout.ABSOLUTE) {
151
- const fixedLeft = fixNodeLeft(newNode, parentNode, stage?.renderer.contentWindow?.document);
152
- if (typeof fixedLeft !== 'undefined' && newNode.style) {
153
- newNode.style.left = fixedLeft;
154
- await stage?.update({ config: cloneDeep(newNode), root: cloneDeep(root) });
155
- }
92
+ let parentNode;
93
+ if (isPage(node)) {
94
+ parentNode = editorService.get<MApp>('root');
95
+ } else if (curNode.items) {
96
+ parentNode = curNode;
97
+ } else {
98
+ parentNode = editorService.getParentById(curNode.id, false);
156
99
  }
100
+ return parentNode;
157
101
  };
158
102
 
159
- /**
160
- * 删除前置操作:实现了在编辑器中删除元素节点,并返回父级元素信息以供stage更新
161
- * @param node 待删除的节点
162
- * @returns 父级元素,root根元素
163
- */
164
- export const beforeRemove = async (node: MNode): Promise<MContainer | void> => {
165
- if (!node?.id) return;
166
-
167
- const stage = editorService.get<StageCore | null>('stage');
168
- const root = editorService.get<MApp | null>('root');
169
-
170
- if (!root) throw new Error('没有root');
171
-
172
- const { parent, node: curNode } = editorService.getNodeInfo(node.id, false);
173
-
174
- if (!parent || !curNode) throw new Error('找不要删除的节点');
175
-
176
- const index = getNodeIndex(curNode, parent);
177
-
178
- if (typeof index !== 'number' || index === -1) throw new Error('找不要删除的节点');
179
- // 从配置中删除元素
180
- parent.items?.splice(index, 1);
181
-
182
- // 通知stage更新
183
- stage?.remove({ id: node.id, root: cloneDeep(root) });
184
-
185
- if (node.type === NodeType.PAGE) {
186
- editorService.state.pageLength -= 1;
187
-
188
- if (root.items[0]) {
189
- await editorService.select(root.items[0]);
190
- stage?.select(root.items[0].id);
191
- } else {
192
- editorService.set('node', null);
193
- editorService.set('nodes', []);
194
- editorService.set('parent', null);
195
- editorService.set('page', null);
196
- editorService.set('stage', null);
197
- editorService.set('highlightNode', null);
198
- editorService.resetModifiedNodeId();
199
- historyService.reset();
200
-
201
- editorService.emit('remove', node);
202
-
203
- return;
204
- }
205
- } else {
206
- await editorService.select(parent);
207
- stage?.select(parent.id);
208
- }
209
- return parent;
103
+ export const getDefaultConfig = async (addNode: AddMNode, parentNode: MContainer) => {
104
+ const { type, inputEvent, ...config } = addNode;
105
+ const layout = await editorService.getLayout(toRaw(parentNode), addNode as MNode);
106
+ const newNode = { ...toRaw(await propsService.getPropsValue(type, config)) };
107
+ newNode.style = getInitPositionStyle(newNode.style, layout);
108
+ return newNode;
210
109
  };
@@ -234,6 +234,3 @@ export const fillConfig = (config: FormConfig = []) => [
234
234
  ],
235
235
  },
236
236
  ];
237
-
238
- // 默认组件属性表单配置
239
- export const DEFAULT_CONFIG: FormConfig = fillConfig([]);