@tmagic/editor 1.0.0-beta.2 → 1.0.0-beta.3

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 (52) hide show
  1. package/package.json +8 -6
  2. package/src/Editor.vue +5 -5
  3. package/src/components/ScrollViewer.vue +65 -0
  4. package/src/layouts/CodeEditor.vue +35 -59
  5. package/src/layouts/workspace/Stage.vue +62 -50
  6. package/src/layouts/workspace/Workspace.vue +2 -21
  7. package/src/services/ui.ts +36 -2
  8. package/src/theme/stage.scss +7 -7
  9. package/src/type.ts +17 -6
  10. package/src/utils/scroll-viewer.ts +213 -0
  11. package/dist/style.css +0 -505
  12. package/dist/tmagic-editor.es.js +0 -4233
  13. package/dist/tmagic-editor.es.js.map +0 -1
  14. package/dist/tmagic-editor.umd.js +0 -4276
  15. package/dist/tmagic-editor.umd.js.map +0 -1
  16. package/dist/types/src/Editor.vue.d.ts +0 -136
  17. package/dist/types/src/components/Icon.vue.d.ts +0 -13
  18. package/dist/types/src/components/ToolButton.vue.d.ts +0 -35
  19. package/dist/types/src/fields/Code.vue.d.ts +0 -24
  20. package/dist/types/src/fields/CodeLink.vue.d.ts +0 -54
  21. package/dist/types/src/fields/UISelect.vue.d.ts +0 -32
  22. package/dist/types/src/index.d.ts +0 -19
  23. package/dist/types/src/layouts/AddPageBox.vue.d.ts +0 -5
  24. package/dist/types/src/layouts/CodeEditor.vue.d.ts +0 -46
  25. package/dist/types/src/layouts/Framework.vue.d.ts +0 -11
  26. package/dist/types/src/layouts/NavMenu.vue.d.ts +0 -24
  27. package/dist/types/src/layouts/PropsPanel.vue.d.ts +0 -13
  28. package/dist/types/src/layouts/Resizer.vue.d.ts +0 -13
  29. package/dist/types/src/layouts/sidebar/ComponentListPanel.vue.d.ts +0 -12
  30. package/dist/types/src/layouts/sidebar/LayerMenu.vue.d.ts +0 -31
  31. package/dist/types/src/layouts/sidebar/LayerPanel.vue.d.ts +0 -973
  32. package/dist/types/src/layouts/sidebar/Sidebar.vue.d.ts +0 -27
  33. package/dist/types/src/layouts/workspace/PageBar.vue.d.ts +0 -11
  34. package/dist/types/src/layouts/workspace/Stage.vue.d.ts +0 -153
  35. package/dist/types/src/layouts/workspace/ViewerMenu.vue.d.ts +0 -18
  36. package/dist/types/src/layouts/workspace/Workspace.vue.d.ts +0 -37
  37. package/dist/types/src/services/BaseService.d.ts +0 -56
  38. package/dist/types/src/services/editor.d.ts +0 -122
  39. package/dist/types/src/services/events.d.ts +0 -16
  40. package/dist/types/src/services/history.d.ts +0 -51
  41. package/dist/types/src/services/props.d.ts +0 -36
  42. package/dist/types/src/services/ui.d.ts +0 -26
  43. package/dist/types/src/shims-vue.d.ts +0 -6
  44. package/dist/types/src/type.d.ts +0 -183
  45. package/dist/types/src/utils/compose.d.ts +0 -5
  46. package/dist/types/src/utils/config.d.ts +0 -4
  47. package/dist/types/src/utils/editor.d.ts +0 -45
  48. package/dist/types/src/utils/index.d.ts +0 -4
  49. package/dist/types/src/utils/logger.d.ts +0 -5
  50. package/dist/types/src/utils/props.d.ts +0 -49
  51. package/dist/types/src/utils/undo-redo.d.ts +0 -12
  52. package/dist/types/src/vite-env.d.ts +0 -1
package/src/type.ts CHANGED
@@ -16,11 +16,11 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { Component } from 'vue';
19
+ import type { Component } from 'vue';
20
20
 
21
- import { FormConfig } from '@tmagic/form';
22
- import { Id, MApp, MContainer, MNode, MPage } from '@tmagic/schema';
23
- import StageCore from '@tmagic/stage';
21
+ import type { FormConfig } from '@tmagic/form';
22
+ import type { Id, MApp, MContainer, MNode, MPage } from '@tmagic/schema';
23
+ import type StageCore from '@tmagic/stage';
24
24
 
25
25
  import type { ComponentListService } from '@editor/services/componentList';
26
26
  import type { EditorService } from '@editor/services/editor';
@@ -75,6 +75,11 @@ export interface GetColumnWidth {
75
75
  right: number;
76
76
  }
77
77
 
78
+ export interface StageRect {
79
+ width: number;
80
+ height: number;
81
+ }
82
+
78
83
  export interface UiState {
79
84
  /** 当前点击画布是否触发选中,true: 不触发,false: 触发,默认为false */
80
85
  uiSelectMode: boolean;
@@ -82,8 +87,10 @@ export interface UiState {
82
87
  showSrc: boolean;
83
88
  /** 画布显示放大倍数,默认为 1 */
84
89
  zoom: number;
85
- /** 画布顶层div的样式,可用于改变画布的大小 */
86
- stageStyle: Record<string, string | number>;
90
+ /** 画布容器的宽高 */
91
+ stageContainerRect: StageRect;
92
+ /** 画布顶层div的宽高,可用于改变画布的大小 */
93
+ stageRect: StageRect;
87
94
  /** 编辑器列布局每一列的宽度,分为左中右三列 */
88
95
  columnWidth: GetColumnWidth;
89
96
  /** 是否显示画布参考线,true: 显示,false: 不显示,默认为true */
@@ -234,3 +241,7 @@ export enum Layout {
234
241
  RELATIVE = 'relative',
235
242
  ABSOLUTE = 'absolute',
236
243
  }
244
+
245
+ export enum Keys {
246
+ ESCAPE = 'Space',
247
+ }
@@ -0,0 +1,213 @@
1
+ import { Keys } from '@editor/type';
2
+
3
+ interface ScrollViewerOptions {
4
+ container: HTMLDivElement;
5
+ target: HTMLDivElement;
6
+ zoom: number;
7
+ }
8
+
9
+ export class ScrollViewer {
10
+ private enter = false;
11
+ private targetEnter = false;
12
+ private keydown = false;
13
+ private container: HTMLDivElement;
14
+ private target: HTMLDivElement;
15
+ private zoom = 1;
16
+
17
+ private scrollLeft = 0;
18
+ private scrollTop = 0;
19
+
20
+ private x = 0;
21
+ private y = 0;
22
+
23
+ private resizeObserver = new ResizeObserver((entries) => {
24
+ for (const { contentRect } of entries) {
25
+ const { width, height } = contentRect;
26
+ const targetRect = this.target.getBoundingClientRect();
27
+ const targetWidth = targetRect.width * this.zoom;
28
+ const targetHeight = targetRect.height * this.zoom;
29
+
30
+ if (targetWidth < width) {
31
+ (this.target as any)._left = 0;
32
+ }
33
+ if (targetHeight < height) {
34
+ (this.target as any)._top = 0;
35
+ }
36
+
37
+ this.scroll();
38
+ }
39
+ });
40
+
41
+ constructor(options: ScrollViewerOptions) {
42
+ this.container = options.container;
43
+ this.target = options.target;
44
+ this.zoom = options.zoom;
45
+
46
+ globalThis.addEventListener('keydown', this.keydownHandler);
47
+ globalThis.addEventListener('keyup', this.keyupHandler);
48
+
49
+ this.container.addEventListener('mouseenter', this.mouseEnterHandler);
50
+ this.container.addEventListener('mouseleave', this.mouseLeaveHandler);
51
+ this.target.addEventListener('mouseenter', this.targetMouseEnterHandler);
52
+ this.target.addEventListener('mouseleave', this.targetMouseLeaveHandler);
53
+
54
+ this.container.addEventListener('wheel', this.wheelHandler);
55
+
56
+ this.resizeObserver.observe(this.container);
57
+ }
58
+
59
+ public destroy() {
60
+ this.resizeObserver.disconnect();
61
+
62
+ this.container.removeEventListener('mouseenter', this.mouseEnterHandler);
63
+ this.container.removeEventListener('mouseleave', this.mouseLeaveHandler);
64
+ this.target.removeEventListener('mouseenter', this.targetMouseEnterHandler);
65
+ this.target.removeEventListener('mouseleave', this.targetMouseLeaveHandler);
66
+ globalThis.removeEventListener('keydown', this.keydownHandler);
67
+ globalThis.removeEventListener('keyup', this.keyupHandler);
68
+ }
69
+
70
+ public setZoom(zoom: number) {
71
+ this.zoom = zoom;
72
+ }
73
+
74
+ private scroll() {
75
+ const scrollLeft = (this.target as any)._left;
76
+ const scrollTop = (this.target as any)._top;
77
+
78
+ this.target.style.transform = `translate(${scrollLeft}px, ${scrollTop}px)`;
79
+ }
80
+
81
+ private removeHandler() {
82
+ this.target.style.cursor = '';
83
+ this.target.removeEventListener('mousedown', this.mousedownHandler);
84
+ document.removeEventListener('mousemove', this.mousemoveHandler);
85
+ document.removeEventListener('mouseup', this.mouseupHandler);
86
+ }
87
+
88
+ private wheelHandler = (event: WheelEvent) => {
89
+ if (this.targetEnter) return;
90
+
91
+ const { deltaX, deltaY, currentTarget } = event;
92
+
93
+ if (currentTarget !== this.container) return;
94
+
95
+ this.setScrollOffset(deltaX, deltaY);
96
+ this.scroll();
97
+ this.scrollLeft = (this.target as any)._left;
98
+ this.scrollTop = (this.target as any)._top;
99
+ };
100
+
101
+ private mouseEnterHandler = () => {
102
+ this.enter = true;
103
+ };
104
+
105
+ private mouseLeaveHandler = () => {
106
+ this.enter = false;
107
+ };
108
+
109
+ private targetMouseEnterHandler = () => {
110
+ this.targetEnter = true;
111
+ };
112
+
113
+ private targetMouseLeaveHandler = () => {
114
+ this.targetEnter = false;
115
+ };
116
+
117
+ private mousedownHandler = (event: MouseEvent) => {
118
+ if (!this.keydown) return;
119
+
120
+ event.stopImmediatePropagation();
121
+ event.stopPropagation();
122
+
123
+ this.target.style.cursor = 'grabbing';
124
+
125
+ this.x = event.clientX;
126
+ this.y = event.clientY;
127
+
128
+ document.addEventListener('mousemove', this.mousemoveHandler);
129
+ document.addEventListener('mouseup', this.mouseupHandler);
130
+ };
131
+
132
+ private mouseupHandler = () => {
133
+ this.x = 0;
134
+ this.y = 0;
135
+
136
+ this.scrollLeft = (this.target as any)._left;
137
+ this.scrollTop = (this.target as any)._top;
138
+ this.removeHandler();
139
+ };
140
+
141
+ private mousemoveHandler = (event: MouseEvent) => {
142
+ event.stopImmediatePropagation();
143
+ event.stopPropagation();
144
+
145
+ const deltaX = event.clientX - this.x;
146
+ const deltaY = event.clientY - this.y;
147
+
148
+ this.setScrollOffset(deltaX, deltaY);
149
+ this.scroll();
150
+ };
151
+
152
+ private keydownHandler = (event: KeyboardEvent) => {
153
+ if (event.code === Keys.ESCAPE && this.enter) {
154
+ event.preventDefault();
155
+ event.stopImmediatePropagation();
156
+ event.stopPropagation();
157
+ } else if (this.keydown) return;
158
+
159
+ this.keydown = true;
160
+
161
+ event.preventDefault();
162
+
163
+ this.target.style.cursor = 'grab';
164
+ this.container.addEventListener('mousedown', this.mousedownHandler);
165
+ };
166
+
167
+ private keyupHandler = (event: KeyboardEvent) => {
168
+ if (event.code !== Keys.ESCAPE || !this.keydown) {
169
+ return;
170
+ }
171
+
172
+ event.preventDefault();
173
+ event.stopImmediatePropagation();
174
+ event.stopPropagation();
175
+
176
+ this.keydown = false;
177
+
178
+ event.preventDefault();
179
+
180
+ this.removeHandler();
181
+ };
182
+
183
+ private setScrollOffset(deltaX: number, deltaY: number) {
184
+ const { width, height } = this.container.getBoundingClientRect();
185
+ const targetRect = this.target.getBoundingClientRect();
186
+
187
+ const targetWidth = targetRect.width * this.zoom;
188
+ const targetHeight = targetRect.height * this.zoom;
189
+
190
+ let y = 0;
191
+
192
+ if (targetHeight > height) {
193
+ if (deltaY > 0) {
194
+ y = this.scrollTop + Math.min(targetHeight - height - this.scrollTop, deltaY);
195
+ } else {
196
+ y = this.scrollTop + Math.max(-(targetHeight - height + this.scrollTop), deltaY);
197
+ }
198
+ }
199
+
200
+ let x = 0;
201
+
202
+ if (targetWidth > width) {
203
+ if (deltaX > 0) {
204
+ x = this.scrollLeft + Math.min(targetWidth - width - this.scrollLeft, deltaX);
205
+ } else {
206
+ x = this.scrollLeft + Math.max(-(targetWidth - width + this.scrollLeft), deltaX);
207
+ }
208
+ }
209
+
210
+ (this.target as any)._left = x;
211
+ (this.target as any)._top = y;
212
+ }
213
+ }