@tmagic/stage 1.0.0-beta.11 → 1.0.0-beta.12

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/StageCore.ts CHANGED
@@ -169,10 +169,10 @@ export default class StageCore extends EventEmitter {
169
169
  * 更新选中的节点
170
170
  * @param data 更新的数据
171
171
  */
172
- public update(data: UpdateData): void {
172
+ public update(data: UpdateData): Promise<void> {
173
173
  const { config } = data;
174
174
 
175
- this.renderer?.getRuntime().then((runtime) => {
175
+ return this.renderer?.getRuntime().then((runtime) => {
176
176
  runtime?.update?.(data);
177
177
  // 更新配置后,需要等组件渲染更新
178
178
  setTimeout(() => {
@@ -205,16 +205,16 @@ export default class StageCore extends EventEmitter {
205
205
  this.highlightedDom = el;
206
206
  }
207
207
 
208
- public sortNode(data: SortEventData): void {
209
- this.renderer?.getRuntime().then((runtime) => runtime?.sortNode?.(data));
208
+ public sortNode(data: SortEventData): Promise<void> {
209
+ return this.renderer?.getRuntime().then((runtime) => runtime?.sortNode?.(data));
210
210
  }
211
211
 
212
- public add(data: UpdateData): void {
213
- this.renderer?.getRuntime().then((runtime) => runtime?.add?.(data));
212
+ public add(data: UpdateData): Promise<void> {
213
+ return this.renderer?.getRuntime().then((runtime) => runtime?.add?.(data));
214
214
  }
215
215
 
216
- public remove(data: RemoveData): void {
217
- this.renderer?.getRuntime().then((runtime) => runtime?.remove?.(data));
216
+ public remove(data: RemoveData): Promise<void> {
217
+ return this.renderer?.getRuntime().then((runtime) => runtime?.remove?.(data));
218
218
  }
219
219
 
220
220
  public setZoom(zoom: number = DEFAULT_ZOOM): void {
@@ -367,6 +367,7 @@ export default class StageDragResize extends EventEmitter {
367
367
  if (!this.target) return {};
368
368
 
369
369
  const isAbsolute = this.mode === Mode.ABSOLUTE;
370
+ const isFixed = this.mode === Mode.FIXED;
370
371
 
371
372
  let { moveableOptions = {} } = this.core.config;
372
373
 
@@ -381,8 +382,8 @@ export default class StageDragResize extends EventEmitter {
381
382
  dragArea: false,
382
383
  draggable: true,
383
384
  resizable: true,
384
- snappable: isAbsolute,
385
- snapGap: isAbsolute,
385
+ snappable: isAbsolute || isFixed,
386
+ snapGap: isAbsolute || isFixed,
386
387
  snapThreshold: 5,
387
388
  snapDigit: 0,
388
389
  throttleDrag: 0,
@@ -56,6 +56,7 @@ export default class StageHighlight extends EventEmitter {
56
56
  */
57
57
  public clearHighlight(): void {
58
58
  if (!this.moveable) return;
59
+ this.target = undefined;
59
60
  this.moveable.target = null;
60
61
  this.moveable.updateTarget();
61
62
  }
package/src/StageMask.ts CHANGED
@@ -25,7 +25,7 @@ import type { StageMaskConfig } from './types';
25
25
  import { createDiv, getScrollParent, isFixedParent } from './util';
26
26
 
27
27
  const wrapperClassName = 'editor-mask-wrapper';
28
- const throttleTime = 300;
28
+ const throttleTime = 100;
29
29
 
30
30
  const hideScrollbar = () => {
31
31
  const style = globalThis.document.createElement('style');
@@ -81,6 +81,8 @@ export default class StageMask extends Rule {
81
81
  public height = 0;
82
82
  public wrapperHeight = 0;
83
83
  public wrapperWidth = 0;
84
+ public maxScrollTop = 0;
85
+ public maxScrollLeft = 0;
84
86
 
85
87
  private mode: Mode = Mode.ABSOLUTE;
86
88
  private pageResizeObserver: ResizeObserver | null = null;
@@ -104,6 +106,7 @@ export default class StageMask extends Rule {
104
106
  this.wrapper.appendChild(this.content);
105
107
  this.content.addEventListener('wheel', this.mouseWheelHandler);
106
108
  this.content.addEventListener('mousemove', this.highlightHandler);
109
+ this.content.addEventListener('mouseleave', this.mouseLeaveHandler);
107
110
  }
108
111
 
109
112
  public setMode(mode: Mode) {
@@ -136,6 +139,10 @@ export default class StageMask extends Rule {
136
139
  const { clientHeight, clientWidth } = entry.target;
137
140
  this.setHeight(clientHeight);
138
141
  this.setWidth(clientWidth);
142
+
143
+ this.fixScrollValue();
144
+ this.scroll();
145
+ this.core.dr.updateMoveable();
139
146
  });
140
147
 
141
148
  this.pageResizeObserver.observe(page);
@@ -145,6 +152,8 @@ export default class StageMask extends Rule {
145
152
  const { clientHeight, clientWidth } = entry.target;
146
153
  this.wrapperHeight = clientHeight;
147
154
  this.wrapperWidth = clientWidth;
155
+ this.setMaxScrollLeft();
156
+ this.setMaxScrollTop();
148
157
  });
149
158
  this.wrapperResizeObserver.observe(this.wrapper);
150
159
  }
@@ -173,12 +182,21 @@ export default class StageMask extends Rule {
173
182
  this.pageScrollParent = null;
174
183
  this.pageResizeObserver?.disconnect();
175
184
  this.wrapperResizeObserver?.disconnect();
185
+
186
+ this.content.removeEventListener('mouseleave', this.mouseLeaveHandler);
176
187
  super.destroy();
177
188
  }
178
189
 
179
190
  private scroll() {
180
191
  let { scrollLeft, scrollTop } = this;
181
192
 
193
+ if (this.pageScrollParent) {
194
+ this.pageScrollParent.scrollTo({
195
+ top: scrollTop,
196
+ left: scrollLeft,
197
+ });
198
+ }
199
+
182
200
  if (this.mode === Mode.FIXED) {
183
201
  scrollLeft = 0;
184
202
  scrollTop = 0;
@@ -198,6 +216,7 @@ export default class StageMask extends Rule {
198
216
  */
199
217
  private setHeight(height: number): void {
200
218
  this.height = height;
219
+ this.setMaxScrollTop();
201
220
  this.content.style.height = `${height}px`;
202
221
  }
203
222
 
@@ -207,9 +226,35 @@ export default class StageMask extends Rule {
207
226
  */
208
227
  private setWidth(width: number): void {
209
228
  this.width = width;
229
+ this.setMaxScrollLeft();
210
230
  this.content.style.width = `${width}px`;
211
231
  }
212
232
 
233
+ /**
234
+ * 计算并设置最大滚动宽度
235
+ */
236
+ private setMaxScrollLeft(): void {
237
+ this.maxScrollLeft = this.width - this.wrapperWidth;
238
+ }
239
+
240
+ /**
241
+ * 计算并设置最大滚动高度
242
+ */
243
+ private setMaxScrollTop(): void {
244
+ this.maxScrollTop = this.height - this.wrapperHeight;
245
+ }
246
+
247
+ /**
248
+ * 修复滚动距离
249
+ * 由于滚动容器变化等因素,会导致当前滚动的距离不正确
250
+ */
251
+ private fixScrollValue(): void {
252
+ if (this.scrollTop < 0) this.scrollTop = 0;
253
+ if (this.scrollLeft < 0) this.scrollLeft = 0;
254
+ if (this.maxScrollTop < this.scrollTop) this.scrollTop = this.maxScrollTop;
255
+ if (this.maxScrollLeft < this.scrollLeft) this.scrollLeft = this.maxScrollLeft;
256
+ }
257
+
213
258
  /**
214
259
  * 点击事件处理函数
215
260
  * @param event 事件对象
@@ -246,39 +291,22 @@ export default class StageMask extends Rule {
246
291
  if (!this.page) throw new Error('page 未初始化');
247
292
 
248
293
  const { deltaY, deltaX } = event;
249
- const { height, wrapperHeight, width, wrapperWidth } = this;
250
-
251
- const maxScrollTop = height - wrapperHeight;
252
- const maxScrollLeft = width - wrapperWidth;
253
-
254
- if (maxScrollTop > 0) {
255
- if (deltaY > 0) {
256
- this.scrollTop = this.scrollTop + Math.min(maxScrollTop - this.scrollTop, deltaY);
257
- } else {
258
- this.scrollTop = Math.max(this.scrollTop + deltaY, 0);
259
- }
294
+ if (this.maxScrollTop > 0) {
295
+ this.scrollTop = this.scrollTop + deltaY;
260
296
  }
261
297
 
262
- if (width > wrapperWidth) {
263
- if (deltaX > 0) {
264
- this.scrollLeft = this.scrollLeft + Math.min(maxScrollLeft - this.scrollLeft, deltaX);
265
- } else {
266
- this.scrollLeft = Math.max(this.scrollLeft + deltaX, 0);
267
- }
298
+ if (this.maxScrollLeft > 0) {
299
+ this.scrollLeft = this.scrollLeft + deltaX;
268
300
  }
269
301
 
270
- if (this.mode !== Mode.FIXED) {
271
- this.scrollTo(this.scrollLeft, this.scrollTop);
272
- }
302
+ this.fixScrollValue();
273
303
 
274
- if (this.pageScrollParent) {
275
- this.pageScrollParent.scrollTo({
276
- top: this.scrollTop,
277
- left: this.scrollLeft,
278
- });
279
- }
280
304
  this.scroll();
281
305
 
282
306
  this.emit('scroll', event);
283
307
  };
308
+
309
+ private mouseLeaveHandler = () => {
310
+ setTimeout(() => this.emit('clearHighlight'), throttleTime);
311
+ };
284
312
  }