@tmagic/stage 1.0.0-beta.7 → 1.0.0-beta.8

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/src/StageMask.ts CHANGED
@@ -16,15 +16,11 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { EventEmitter } from 'events';
20
-
21
- import Guides from '@scena/guides';
22
- import { isNumber } from 'lodash';
23
-
24
- import { GHOST_EL_ID_PREFIX } from './const';
19
+ import { Mode, MouseButton, ZIndex } from './const';
20
+ import Rule from './Rule';
25
21
  import type StageCore from './StageCore';
26
- import type { CanSelect, StageMaskConfig } from './types';
27
- import { MouseButton, ZIndex } from './types';
22
+ import type { StageMaskConfig } from './types';
23
+ import { createDiv, getScrollParent, isFixedParent } from './util';
28
24
 
29
25
  const wrapperClassName = 'editor-mask-wrapper';
30
26
 
@@ -36,30 +32,30 @@ const hideScrollbar = () => {
36
32
  globalThis.document.head.appendChild(style);
37
33
  };
38
34
 
39
- const createContent = (): HTMLDivElement => {
40
- const el = globalThis.document.createElement('div');
41
- el.className = 'editor-mask';
42
- el.style.cssText = `
35
+ const createContent = (): HTMLDivElement =>
36
+ createDiv({
37
+ className: 'editor-mask',
38
+ cssText: `
43
39
  position: absolute;
44
40
  top: 0;
45
41
  left: 0;
46
42
  transform: translate3d(0, 0, 0);
47
- `;
48
- return el;
49
- };
43
+ `,
44
+ });
50
45
 
51
46
  const createWrapper = (): HTMLDivElement => {
52
- const el = globalThis.document.createElement('div');
53
- el.className = wrapperClassName;
54
- el.style.cssText = `
55
- position: absolute;
56
- top: 0;
57
- left: 0;
58
- height: 100%;
59
- width: 100%;
60
- overflow: auto;
61
- z-index: ${ZIndex.MASK};
62
- `;
47
+ const el = createDiv({
48
+ className: wrapperClassName,
49
+ cssText: `
50
+ position: absolute;
51
+ top: 0;
52
+ left: 0;
53
+ height: 100%;
54
+ width: 100%;
55
+ overflow: hidden;
56
+ z-index: ${ZIndex.MASK};
57
+ `,
58
+ });
63
59
 
64
60
  hideScrollbar();
65
61
 
@@ -70,54 +66,45 @@ const createWrapper = (): HTMLDivElement => {
70
66
  * 蒙层
71
67
  * @description 用于拦截页面的点击动作,避免点击时触发组件自身动作;在编辑器中点击组件应当是选中组件;
72
68
  */
73
- export default class StageMask extends EventEmitter {
69
+ export default class StageMask extends Rule {
74
70
  public content: HTMLDivElement = createContent();
75
- public wrapper: HTMLDivElement = createWrapper();
71
+ public wrapper: HTMLDivElement;
76
72
  public core: StageCore;
77
73
  public page: HTMLElement | null = null;
78
-
79
- public hGuides: Guides;
80
- public vGuides: Guides;
81
-
82
- private target: Element | null = null;
83
- private resizeObserver: ResizeObserver | null = null;
84
- private parentResizeObserver: ResizeObserver | null = null;
85
- private canSelect: CanSelect;
74
+ public pageScrollParent: HTMLElement | null = null;
75
+ public scrollTop = 0;
76
+ public scrollLeft = 0;
77
+ public width = 0;
78
+ public height = 0;
79
+ public wrapperHeight = 0;
80
+ public wrapperWidth = 0;
81
+
82
+ private mode: Mode = Mode.ABSOLUTE;
83
+ private pageResizeObserver: ResizeObserver | null = null;
84
+ private wrapperResizeObserver: ResizeObserver | null = null;
86
85
 
87
86
  constructor(config: StageMaskConfig) {
88
- super();
87
+ const wrapper = createWrapper();
88
+ super(wrapper);
89
89
 
90
+ this.wrapper = wrapper;
90
91
  this.core = config.core;
91
- const { config: coreConfig } = config.core;
92
92
 
93
- this.canSelect = coreConfig.canSelect || ((el: HTMLElement) => !!el.id);
94
93
  this.content.addEventListener('mousedown', this.mouseDownHandler);
95
- this.content.addEventListener('contextmenu', this.contextmenuHandler);
96
94
  this.wrapper.appendChild(this.content);
97
-
98
- this.hGuides = this.createGuides('horizontal');
99
- this.vGuides = this.createGuides('vertical');
95
+ this.content.addEventListener('wheel', this.mouseWheelHandler);
100
96
  }
101
97
 
102
- /**
103
- * 设置成固定定位模式
104
- */
105
- public setFixed(): void {
106
- this.wrapper.scrollTo({
107
- top: 0,
108
- });
109
- this.wrapper.style.overflow = 'hidden';
110
- // 要等滚动条滚上去,才刷新选中框
111
- setTimeout(() => {
112
- this.core.dr.refresh();
113
- });
114
- }
115
-
116
- /**
117
- * 设置成绝对定位模式
118
- */
119
- public setAbsolute(): void {
120
- this.wrapper.style.overflow = 'auto';
98
+ public setMode(mode: Mode) {
99
+ this.mode = mode;
100
+ this.scroll();
101
+ if (mode === Mode.FIXED) {
102
+ this.content.style.width = `${this.wrapperWidth}px`;
103
+ this.content.style.height = `${this.wrapperHeight}px`;
104
+ } else {
105
+ this.content.style.width = `${this.width}px`;
106
+ this.content.style.height = `${this.height}px`;
107
+ }
121
108
  }
122
109
 
123
110
  /**
@@ -129,17 +116,26 @@ export default class StageMask extends EventEmitter {
129
116
  if (!page) return;
130
117
 
131
118
  this.page = page;
132
- this.resizeObserver?.disconnect();
119
+ this.pageScrollParent = getScrollParent(page) || this.core.renderer.contentWindow?.document.documentElement || null;
120
+ this.pageResizeObserver?.disconnect();
133
121
 
134
122
  if (typeof ResizeObserver !== 'undefined') {
135
- this.resizeObserver = new ResizeObserver((entries) => {
123
+ this.pageResizeObserver = new ResizeObserver((entries) => {
136
124
  const [entry] = entries;
137
125
  const { clientHeight, clientWidth } = entry.target;
138
126
  this.setHeight(clientHeight);
139
127
  this.setWidth(clientWidth);
140
128
  });
141
129
 
142
- this.resizeObserver.observe(page);
130
+ this.pageResizeObserver.observe(page);
131
+
132
+ this.wrapperResizeObserver = new ResizeObserver((entries) => {
133
+ const [entry] = entries;
134
+ const { clientHeight, clientWidth } = entry.target;
135
+ this.wrapperHeight = clientHeight;
136
+ this.wrapperWidth = clientWidth;
137
+ });
138
+ this.wrapperResizeObserver.observe(this.wrapper);
143
139
  }
144
140
  }
145
141
 
@@ -151,12 +147,10 @@ export default class StageMask extends EventEmitter {
151
147
  if (!this.content) throw new Error('content 不存在');
152
148
 
153
149
  el.appendChild(this.wrapper);
150
+ }
154
151
 
155
- this.parentResizeObserver = new ResizeObserver(() => {
156
- this.vGuides.resize();
157
- this.hGuides.resize();
158
- });
159
- this.parentResizeObserver.observe(el);
152
+ public setLayout(el: HTMLElement): void {
153
+ this.setMode(isFixedParent(el) ? Mode.FIXED : Mode.ABSOLUTE);
160
154
  }
161
155
 
162
156
  /**
@@ -165,78 +159,44 @@ export default class StageMask extends EventEmitter {
165
159
  public destroy(): void {
166
160
  this.content?.remove();
167
161
  this.page = null;
168
- this.resizeObserver?.disconnect();
169
- this.parentResizeObserver?.disconnect();
170
- this.removeAllListeners();
162
+ this.pageScrollParent = null;
163
+ this.pageResizeObserver?.disconnect();
164
+ this.wrapperResizeObserver?.disconnect();
165
+ super.destroy();
171
166
  }
172
167
 
173
- /**
174
- * 是否显示标尺
175
- * @param show 是否显示
176
- */
177
- public showGuides(show = true) {
178
- this.hGuides.setState({
179
- showGuides: show,
180
- });
181
-
182
- this.vGuides.setState({
183
- showGuides: show,
184
- });
185
- }
168
+ private scroll() {
169
+ let { scrollLeft, scrollTop } = this;
186
170
 
187
- /**
188
- * 是否显示标尺
189
- * @param show 是否显示
190
- */
191
- public showRule(show = true) {
192
- // 当尺子隐藏时发现大小变化,显示后会变形,所以这里做重新初始化处理
193
- if (show) {
194
- this.hGuides.destroy();
195
- this.hGuides = this.createGuides('horizontal', this.core.dr.horizontalGuidelines);
196
-
197
- this.vGuides.destroy();
198
- this.vGuides = this.createGuides('vertical', this.core.dr.verticalGuidelines);
199
- } else {
200
- this.hGuides.setState({
201
- rulerStyle: {
202
- visibility: 'hidden',
203
- },
204
- });
205
-
206
- this.vGuides.setState({
207
- rulerStyle: {
208
- visibility: 'hidden',
209
- },
210
- });
171
+ if (this.mode === Mode.FIXED) {
172
+ scrollLeft = 0;
173
+ scrollTop = 0;
211
174
  }
175
+
176
+ this.scrollRule(scrollTop);
177
+ this.scrollTo(scrollLeft, scrollTop);
212
178
  }
213
179
 
214
- /**
215
- * 清空所有参考线
216
- */
217
- public clearGuides() {
218
- this.vGuides.setState({
219
- defaultGuides: [],
220
- });
221
- this.hGuides.setState({
222
- defaultGuides: [],
223
- });
180
+ private scrollTo(scrollLeft: number, scrollTop: number): void {
181
+ this.content.style.transform = `translate3d(${-scrollLeft}px, ${-scrollTop}px, 0)`;
224
182
  }
225
183
 
226
184
  /**
227
185
  * 设置蒙层高度
228
186
  * @param height 高度
229
187
  */
230
- private setHeight(height: number | string): void {
231
- this.content.style.height = isNumber(height) ? `${height}px` : height;
188
+ private setHeight(height: number): void {
189
+ this.height = height;
190
+ this.content.style.height = `${height}px`;
232
191
  }
233
192
 
234
193
  /**
235
194
  * 设置蒙层宽度
236
195
  * @param width 宽度
237
196
  */
238
- private setWidth(width: number | string): void {
239
- this.content.style.width = isNumber(width) ? `${width}px` : width;
197
+ private setWidth(width: number): void {
198
+ this.width = width;
199
+ this.content.style.width = `${width}px`;
240
200
  }
241
201
 
242
202
  /**
@@ -244,86 +204,64 @@ export default class StageMask extends EventEmitter {
244
204
  * @param event 事件对象
245
205
  */
246
206
  private mouseDownHandler = async (event: MouseEvent): Promise<void> => {
247
- if (event.button !== MouseButton.LEFT) return;
207
+ event.stopImmediatePropagation();
208
+ event.stopPropagation();
209
+
210
+ if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT) return;
248
211
 
249
212
  // 点击的对象如果是选中框,则不需要再触发选中了,而可能是拖动行为
250
213
  if ((event.target as HTMLDivElement).className.indexOf('moveable-control') !== -1) {
251
214
  return;
252
215
  }
253
216
 
254
- this.content.addEventListener('mousemove', this.mouseMoveHandler);
217
+ this.emit('beforeSelect', event);
255
218
 
256
- this.select(event);
219
+ // 如果是右键点击,这里的mouseup事件监听没有效果
220
+ globalThis.document.addEventListener('mouseup', this.mouseUpHandler);
257
221
  };
258
222
 
259
223
  private mouseUpHandler = (): void => {
260
- this.content.removeEventListener('mousemove', this.mouseMoveHandler);
261
- this.content.removeEventListener('mouseup', this.mouseUpHandler);
262
- this.emit('selected', this.target);
263
- this.target = null;
224
+ globalThis.document.removeEventListener('mouseup', this.mouseUpHandler);
225
+ this.emit('select');
264
226
  };
265
227
 
266
- private mouseMoveHandler = (event: MouseEvent): void => {
267
- // 避免触摸板轻触移动拖动组件
268
- if (event.buttons) {
269
- this.core.dr.moveable?.dragStart(event);
270
- }
271
- this.content.removeEventListener('mousemove', this.mouseMoveHandler);
272
- };
273
-
274
- private contextmenuHandler = async (event: MouseEvent): Promise<void> => {
275
- await this.select(event);
276
- this.mouseUpHandler();
277
- };
228
+ private mouseWheelHandler = (event: WheelEvent) => {
229
+ if (!this.page) throw new Error('page 未初始化');
278
230
 
279
- private async select(event: MouseEvent) {
280
- const { renderer, zoom } = this.core;
231
+ const { deltaY, deltaX } = event;
232
+ const { height, wrapperHeight, width, wrapperWidth } = this;
281
233
 
282
- const doc = renderer.contentWindow?.document;
283
- let x = event.clientX;
284
- let y = event.clientY;
234
+ const maxScrollTop = height - wrapperHeight;
235
+ const maxScrollLeft = width - wrapperWidth;
285
236
 
286
- if (renderer.iframe) {
287
- const rect = renderer.iframe.getClientRects()[0];
288
- if (rect) {
289
- x = x - rect.left;
290
- y = y - rect.top;
237
+ if (maxScrollTop > 0) {
238
+ if (deltaY > 0) {
239
+ this.scrollTop = this.scrollTop + Math.min(maxScrollTop - this.scrollTop, deltaY);
240
+ } else {
241
+ this.scrollTop = Math.max(this.scrollTop + deltaY, 0);
291
242
  }
292
243
  }
293
244
 
294
- const els = doc?.elementsFromPoint(x / zoom, y / zoom) as HTMLElement[];
295
-
296
- let stopped = false;
297
- const stop = () => (stopped = true);
298
- for (const el of els) {
299
- if (!el.id.startsWith(GHOST_EL_ID_PREFIX) && (await this.canSelect(el, stop))) {
300
- if (stopped) break;
301
-
302
- this.emit('select', el, event);
303
- this.target = el;
304
- // 如果是右键点击,这里的mouseup事件监听没有效果
305
- this.content.addEventListener('mouseup', this.mouseUpHandler);
306
- break;
245
+ if (width > wrapperWidth) {
246
+ if (deltaX > 0) {
247
+ this.scrollLeft = this.scrollLeft + Math.min(maxScrollLeft - this.scrollLeft, deltaX);
248
+ } else {
249
+ this.scrollLeft = Math.max(this.scrollLeft + deltaX, 0);
307
250
  }
308
251
  }
309
- }
310
252
 
311
- private getGuidesStyle = (type: 'horizontal' | 'vertical') => ({
312
- position: 'fixed',
313
- left: type === 'horizontal' ? 0 : '-30px',
314
- top: type === 'horizontal' ? '-30px' : 0,
315
- width: type === 'horizontal' ? '100%' : '30px',
316
- height: type === 'horizontal' ? '30px' : '100%',
317
- });
253
+ if (this.mode !== Mode.FIXED) {
254
+ this.scrollTo(this.scrollLeft, this.scrollTop);
255
+ }
256
+
257
+ if (this.pageScrollParent) {
258
+ this.pageScrollParent.scrollTo({
259
+ top: this.scrollTop,
260
+ left: this.scrollLeft,
261
+ });
262
+ }
263
+ this.scroll();
318
264
 
319
- private createGuides = (type: 'horizontal' | 'vertical', defaultGuides: number[] = []): Guides =>
320
- new Guides(this.wrapper, {
321
- type,
322
- defaultGuides,
323
- displayDragPos: true,
324
- backgroundColor: '#fff',
325
- lineColor: '#000',
326
- textColor: '#000',
327
- style: this.getGuidesStyle(type),
328
- });
265
+ this.emit('scroll', event);
266
+ };
329
267
  }
@@ -52,15 +52,7 @@ export default class StageRender extends EventEmitter {
52
52
  height: 100%;
53
53
  `;
54
54
 
55
- this.iframe.onload = async () => {
56
- this.emit('onload');
57
- if (this.render) {
58
- const el = await this.render(this.core);
59
- if (el) {
60
- this.iframe?.contentDocument?.body?.appendChild(el);
61
- }
62
- }
63
- };
55
+ this.iframe.addEventListener('load', this.loadHandler);
64
56
  }
65
57
 
66
58
  public getMagicApi = () => ({
@@ -113,9 +105,20 @@ export default class StageRender extends EventEmitter {
113
105
  * 销毁实例
114
106
  */
115
107
  public destroy(): void {
108
+ this.iframe?.removeEventListener('load', this.loadHandler);
116
109
  this.contentWindow = null;
117
110
  this.iframe?.remove();
118
111
  this.iframe = undefined;
119
112
  this.removeAllListeners();
120
113
  }
114
+
115
+ private loadHandler = async () => {
116
+ this.emit('onload');
117
+ if (this.render) {
118
+ const el = await this.render(this.core);
119
+ if (el) {
120
+ this.iframe?.contentDocument?.body?.appendChild(el);
121
+ }
122
+ }
123
+ };
121
124
  }
package/src/const.ts CHANGED
@@ -21,3 +21,24 @@ export const GHOST_EL_ID_PREFIX = 'ghost_el_';
21
21
 
22
22
  // 默认放到缩小倍数
23
23
  export const DEFAULT_ZOOM = 1;
24
+
25
+ export enum GuidesType {
26
+ HORIZONTAL = 'horizontal',
27
+ VERTICAL = 'vertical',
28
+ }
29
+
30
+ export enum ZIndex {
31
+ MASK = '99999',
32
+ }
33
+
34
+ export enum MouseButton {
35
+ LEFT = 0,
36
+ MIDDLE = 1,
37
+ RIGHT = 2,
38
+ }
39
+
40
+ export enum Mode {
41
+ ABSOLUTE = 'absolute',
42
+ FIXED = 'fixed',
43
+ SORTABLE = 'sortable',
44
+ }
package/src/types.ts CHANGED
@@ -1,7 +1,26 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making TMagicEditor available.
3
+ *
4
+ * Copyright (C) 2021 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
+
1
19
  import { MoveableOptions } from 'react-moveable/declaration/types';
2
20
 
3
21
  import { Id, MApp, MNode } from '@tmagic/schema';
4
22
 
23
+ import { GuidesType } from './const';
5
24
  import StageCore from './StageCore';
6
25
 
7
26
  export type CanSelect = (el: HTMLElement, stop: () => boolean) => boolean | Promise<boolean>;
@@ -41,23 +60,10 @@ export interface Offset {
41
60
  top: number;
42
61
  }
43
62
 
44
- /*
45
- * Tencent is pleased to support the open source community by making TMagicEditor available.
46
- *
47
- * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
48
- *
49
- * Licensed under the Apache License, Version 2.0 (the "License");
50
- * you may not use this file except in compliance with the License.
51
- * You may obtain a copy of the License at
52
- *
53
- * http://www.apache.org/licenses/LICENSE-2.0
54
- *
55
- * Unless required by applicable law or agreed to in writing, software
56
- * distributed under the License is distributed on an "AS IS" BASIS,
57
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
58
- * See the License for the specific language governing permissions and
59
- * limitations under the License.
60
- */
63
+ export interface GuidesEventData {
64
+ type: GuidesType;
65
+ guides: number[];
66
+ }
61
67
 
62
68
  export interface UpdateEventData {
63
69
  el: HTMLElement;
@@ -108,14 +114,3 @@ export interface Magic {
108
114
  export interface RuntimeWindow extends Window {
109
115
  magic: Magic;
110
116
  }
111
-
112
- export enum ZIndex {
113
- MASK = '99999',
114
- GHOST_EL = '99998',
115
- }
116
-
117
- export enum MouseButton {
118
- LEFT = 0,
119
- MIDDLE = 1,
120
- RIGHT = 2,
121
- }
package/src/util.ts CHANGED
@@ -16,14 +16,9 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
+ import { Mode } from './const';
19
20
  import type { Offset } from './types';
20
21
 
21
- export enum Mode {
22
- ABSOLUTE = 'absolute',
23
- FIXED = 'fixed',
24
- SORTABLE = 'sortable',
25
- }
26
-
27
22
  export const getOffset = (el: HTMLElement): Offset => {
28
23
  const { transform } = getComputedStyle(el);
29
24
  const { offsetParent } = el;
@@ -113,8 +108,49 @@ export const isFixed = (el?: HTMLElement): boolean => {
113
108
  return getComputedStyle(el).position === 'fixed';
114
109
  };
115
110
 
111
+ export const isFixedParent = (el: HTMLElement) => {
112
+ let fixed = false;
113
+ let dom = el;
114
+ while (dom) {
115
+ fixed = isFixed(dom);
116
+ if (fixed) {
117
+ break;
118
+ }
119
+ const { parentElement } = dom;
120
+ if (!parentElement || parentElement.tagName === 'BODY') {
121
+ break;
122
+ }
123
+ dom = parentElement;
124
+ }
125
+ return fixed;
126
+ };
127
+
116
128
  export const getMode = (el: HTMLElement): Mode => {
117
- if (isFixed(el)) return Mode.FIXED;
129
+ if (isFixedParent(el)) return Mode.FIXED;
118
130
  if (isStatic(el) || isRelative(el)) return Mode.SORTABLE;
119
131
  return Mode.ABSOLUTE;
120
132
  };
133
+
134
+ export const getScrollParent = (element: HTMLElement, includeHidden = false): HTMLElement | null => {
135
+ let style = getComputedStyle(element);
136
+ const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
137
+
138
+ if (isFixed(element)) return null;
139
+ for (let parent = element; parent.parentElement; ) {
140
+ parent = parent.parentElement;
141
+ style = getComputedStyle(parent);
142
+ if (isAbsolute(element) && isStatic(element)) {
143
+ continue;
144
+ }
145
+ if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) return parent;
146
+ }
147
+
148
+ return null;
149
+ };
150
+
151
+ export const createDiv = ({ className, cssText }: { className: string; cssText: string }) => {
152
+ const el = globalThis.document.createElement('div');
153
+ el.className = className;
154
+ el.style.cssText = cssText;
155
+ return el;
156
+ };