@tmagic/editor 1.0.0-beta.2 → 1.0.0-beta.5
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 +11 -10
- package/dist/tmagic-editor.es.js +418 -192
- package/dist/tmagic-editor.es.js.map +1 -1
- package/dist/tmagic-editor.umd.js +438 -194
- package/dist/tmagic-editor.umd.js.map +1 -1
- package/dist/types/src/Editor.vue.d.ts +5 -5
- package/dist/types/src/components/ScrollViewer.vue.d.ts +23 -0
- package/dist/types/src/layouts/CodeEditor.vue.d.ts +5 -4
- package/dist/types/src/layouts/NavMenu.vue.d.ts +2 -1
- package/dist/types/src/layouts/workspace/Stage.vue.d.ts +76 -37
- package/dist/types/src/layouts/workspace/Workspace.vue.d.ts +1 -5
- package/dist/types/src/services/ui.d.ts +10 -3
- package/dist/types/src/type.d.ts +15 -6
- package/dist/types/src/utils/editor.d.ts +0 -1
- package/dist/types/src/utils/scroll-viewer.d.ts +35 -0
- package/package.json +8 -6
- package/src/Editor.vue +5 -5
- package/src/components/ScrollViewer.vue +66 -0
- package/src/layouts/CodeEditor.vue +35 -59
- package/src/layouts/NavMenu.vue +7 -3
- package/src/layouts/PropsPanel.vue +5 -10
- package/src/layouts/workspace/Stage.vue +62 -50
- package/src/layouts/workspace/Workspace.vue +2 -21
- package/src/services/editor.ts +6 -3
- package/src/services/ui.ts +36 -2
- package/src/theme/nav-menu.scss +3 -3
- package/src/theme/stage.scss +8 -7
- package/src/type.ts +17 -6
- package/src/utils/editor.ts +1 -20
- package/src/utils/scroll-viewer.ts +214 -0
package/src/utils/editor.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import {
|
|
19
|
+
import { random } from 'lodash-es';
|
|
20
20
|
|
|
21
21
|
import { Id, MApp, MContainer, MNode, MPage } from '@tmagic/schema';
|
|
22
22
|
import { getNodePath, isPop } from '@tmagic/utils';
|
|
@@ -223,22 +223,3 @@ export const Fixed2Other = async (node: MNode, root: MApp, getLayout: (node: MNo
|
|
|
223
223
|
|
|
224
224
|
return toRelative(node);
|
|
225
225
|
};
|
|
226
|
-
|
|
227
|
-
export const defaults = (object: any, source: any) => {
|
|
228
|
-
let o = source;
|
|
229
|
-
if (Array.isArray(object)) {
|
|
230
|
-
o = object;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
Object.entries(o).forEach(([key, value]: [string, any]) => {
|
|
234
|
-
if (typeof object[key] === 'undefined') {
|
|
235
|
-
object[key] = value;
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (value && typeof value !== 'string' && Object.keys(value).length) {
|
|
240
|
-
object[key] = defaults(cloneDeep(object[key]), value);
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
return object;
|
|
244
|
-
};
|
|
@@ -0,0 +1,214 @@
|
|
|
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 targetMarginTop = Number(this.target.style.marginTop) || 0;
|
|
29
|
+
const targetHeight = (targetRect.height + targetMarginTop) * this.zoom;
|
|
30
|
+
|
|
31
|
+
if (targetWidth < width) {
|
|
32
|
+
(this.target as any)._left = 0;
|
|
33
|
+
}
|
|
34
|
+
if (targetHeight < height) {
|
|
35
|
+
(this.target as any)._top = 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.scroll();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
constructor(options: ScrollViewerOptions) {
|
|
43
|
+
this.container = options.container;
|
|
44
|
+
this.target = options.target;
|
|
45
|
+
this.zoom = options.zoom;
|
|
46
|
+
|
|
47
|
+
globalThis.addEventListener('keydown', this.keydownHandler);
|
|
48
|
+
globalThis.addEventListener('keyup', this.keyupHandler);
|
|
49
|
+
|
|
50
|
+
this.container.addEventListener('mouseenter', this.mouseEnterHandler);
|
|
51
|
+
this.container.addEventListener('mouseleave', this.mouseLeaveHandler);
|
|
52
|
+
this.target.addEventListener('mouseenter', this.targetMouseEnterHandler);
|
|
53
|
+
this.target.addEventListener('mouseleave', this.targetMouseLeaveHandler);
|
|
54
|
+
|
|
55
|
+
this.container.addEventListener('wheel', this.wheelHandler);
|
|
56
|
+
|
|
57
|
+
this.resizeObserver.observe(this.container);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public destroy() {
|
|
61
|
+
this.resizeObserver.disconnect();
|
|
62
|
+
|
|
63
|
+
this.container.removeEventListener('mouseenter', this.mouseEnterHandler);
|
|
64
|
+
this.container.removeEventListener('mouseleave', this.mouseLeaveHandler);
|
|
65
|
+
this.target.removeEventListener('mouseenter', this.targetMouseEnterHandler);
|
|
66
|
+
this.target.removeEventListener('mouseleave', this.targetMouseLeaveHandler);
|
|
67
|
+
globalThis.removeEventListener('keydown', this.keydownHandler);
|
|
68
|
+
globalThis.removeEventListener('keyup', this.keyupHandler);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public setZoom(zoom: number) {
|
|
72
|
+
this.zoom = zoom;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private scroll() {
|
|
76
|
+
const scrollLeft = (this.target as any)._left;
|
|
77
|
+
const scrollTop = (this.target as any)._top;
|
|
78
|
+
|
|
79
|
+
this.target.style.transform = `translate(${scrollLeft}px, ${scrollTop}px)`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private removeHandler() {
|
|
83
|
+
this.target.style.cursor = '';
|
|
84
|
+
this.target.removeEventListener('mousedown', this.mousedownHandler);
|
|
85
|
+
document.removeEventListener('mousemove', this.mousemoveHandler);
|
|
86
|
+
document.removeEventListener('mouseup', this.mouseupHandler);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private wheelHandler = (event: WheelEvent) => {
|
|
90
|
+
if (this.targetEnter) return;
|
|
91
|
+
|
|
92
|
+
const { deltaX, deltaY, currentTarget } = event;
|
|
93
|
+
|
|
94
|
+
if (currentTarget !== this.container) return;
|
|
95
|
+
|
|
96
|
+
this.setScrollOffset(deltaX, deltaY);
|
|
97
|
+
this.scroll();
|
|
98
|
+
this.scrollLeft = (this.target as any)._left;
|
|
99
|
+
this.scrollTop = (this.target as any)._top;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
private mouseEnterHandler = () => {
|
|
103
|
+
this.enter = true;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
private mouseLeaveHandler = () => {
|
|
107
|
+
this.enter = false;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
private targetMouseEnterHandler = () => {
|
|
111
|
+
this.targetEnter = true;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
private targetMouseLeaveHandler = () => {
|
|
115
|
+
this.targetEnter = false;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
private mousedownHandler = (event: MouseEvent) => {
|
|
119
|
+
if (!this.keydown) return;
|
|
120
|
+
|
|
121
|
+
event.stopImmediatePropagation();
|
|
122
|
+
event.stopPropagation();
|
|
123
|
+
|
|
124
|
+
this.target.style.cursor = 'grabbing';
|
|
125
|
+
|
|
126
|
+
this.x = event.clientX;
|
|
127
|
+
this.y = event.clientY;
|
|
128
|
+
|
|
129
|
+
document.addEventListener('mousemove', this.mousemoveHandler);
|
|
130
|
+
document.addEventListener('mouseup', this.mouseupHandler);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
private mouseupHandler = () => {
|
|
134
|
+
this.x = 0;
|
|
135
|
+
this.y = 0;
|
|
136
|
+
|
|
137
|
+
this.scrollLeft = (this.target as any)._left;
|
|
138
|
+
this.scrollTop = (this.target as any)._top;
|
|
139
|
+
this.removeHandler();
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
private mousemoveHandler = (event: MouseEvent) => {
|
|
143
|
+
event.stopImmediatePropagation();
|
|
144
|
+
event.stopPropagation();
|
|
145
|
+
|
|
146
|
+
const deltaX = event.clientX - this.x;
|
|
147
|
+
const deltaY = event.clientY - this.y;
|
|
148
|
+
|
|
149
|
+
this.setScrollOffset(deltaX, deltaY);
|
|
150
|
+
this.scroll();
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
private keydownHandler = (event: KeyboardEvent) => {
|
|
154
|
+
if (event.code === Keys.ESCAPE && this.enter) {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
event.stopImmediatePropagation();
|
|
157
|
+
event.stopPropagation();
|
|
158
|
+
} else if (this.keydown) return;
|
|
159
|
+
|
|
160
|
+
this.keydown = true;
|
|
161
|
+
|
|
162
|
+
event.preventDefault();
|
|
163
|
+
|
|
164
|
+
this.target.style.cursor = 'grab';
|
|
165
|
+
this.container.addEventListener('mousedown', this.mousedownHandler);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
private keyupHandler = (event: KeyboardEvent) => {
|
|
169
|
+
if (event.code !== Keys.ESCAPE || !this.keydown) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
event.stopImmediatePropagation();
|
|
175
|
+
event.stopPropagation();
|
|
176
|
+
|
|
177
|
+
this.keydown = false;
|
|
178
|
+
|
|
179
|
+
event.preventDefault();
|
|
180
|
+
|
|
181
|
+
this.removeHandler();
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
private setScrollOffset(deltaX: number, deltaY: number) {
|
|
185
|
+
const { width, height } = this.container.getBoundingClientRect();
|
|
186
|
+
const targetRect = this.target.getBoundingClientRect();
|
|
187
|
+
|
|
188
|
+
const targetWidth = targetRect.width * this.zoom;
|
|
189
|
+
const targetHeight = targetRect.height * this.zoom;
|
|
190
|
+
|
|
191
|
+
let y = 0;
|
|
192
|
+
|
|
193
|
+
if (targetHeight > height) {
|
|
194
|
+
if (deltaY > 0) {
|
|
195
|
+
y = this.scrollTop + Math.min(targetHeight - height - this.scrollTop, deltaY);
|
|
196
|
+
} else {
|
|
197
|
+
y = this.scrollTop + Math.max(-(targetHeight - height + this.scrollTop), deltaY);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let x = 0;
|
|
202
|
+
|
|
203
|
+
if (targetWidth > width) {
|
|
204
|
+
if (deltaX > 0) {
|
|
205
|
+
x = this.scrollLeft + Math.min(targetWidth - width - this.scrollLeft, deltaX);
|
|
206
|
+
} else {
|
|
207
|
+
x = this.scrollLeft + Math.max(-(targetWidth - width + this.scrollLeft), deltaX);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
(this.target as any)._left = x;
|
|
212
|
+
(this.target as any)._top = y;
|
|
213
|
+
}
|
|
214
|
+
}
|