@tmagic/stage 1.0.0-rc.6 → 1.0.0-rc.7

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.
@@ -203,49 +203,41 @@ export default class StageDragResize extends EventEmitter {
203
203
 
204
204
  this.bindResizeEvent();
205
205
  this.bindDragEvent();
206
+ this.bindRotateEvent();
207
+ this.bindScaleEvent();
206
208
  }
207
209
 
208
210
  private bindResizeEvent(): void {
209
211
  if (!this.moveable) throw new Error('moveable 为初始化');
210
212
 
211
213
  const frame = {
212
- translate: [0, 0],
214
+ left: 0,
215
+ top: 0,
213
216
  };
214
217
 
215
218
  this.moveable
216
219
  .on('resizeStart', (e) => {
217
- if (e.dragStart) {
218
- const rect = this.moveable!.getRect();
219
- const offset = getAbsolutePosition(e.target as HTMLElement, rect);
220
- e.dragStart.set([offset.left, offset.top]);
221
- }
220
+ if (!this.target) return;
221
+
222
+ this.dragStatus = ActionStatus.START;
223
+ this.moveableHelper?.onResizeStart(e);
224
+
225
+ frame.top = this.target.offsetTop;
226
+ frame.left = this.target.offsetLeft;
222
227
  })
223
- .on('resize', ({ width, height, drag }) => {
228
+ .on('resize', (e) => {
229
+ const { width, height, drag } = e;
224
230
  if (!this.moveable || !this.target || !this.dragEl) return;
225
231
 
226
232
  const { beforeTranslate } = drag;
227
- frame.translate = beforeTranslate;
228
233
  this.dragStatus = ActionStatus.ING;
229
234
 
235
+ this.moveableHelper?.onResize(e);
236
+
230
237
  this.target.style.width = `${width}px`;
231
238
  this.target.style.height = `${height}px`;
232
- this.dragEl.style.width = `${width}px`;
233
- this.dragEl.style.height = `${height}px`;
234
-
235
- // 流式布局
236
- if (this.mode === Mode.SORTABLE) {
237
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
238
- this.target.style.top = `0px`;
239
- return;
240
- }
241
-
242
- this.dragEl.style.left = `${beforeTranslate[0]}px`;
243
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
244
-
245
- const offset = getAbsolutePosition(this.target, { left: beforeTranslate[0], top: beforeTranslate[1] });
246
-
247
- this.target.style.left = `${offset.left}px`;
248
- this.target.style.top = `${offset.top}px`;
239
+ this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
240
+ this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
249
241
  })
250
242
  .on('resizeEnd', () => {
251
243
  this.dragStatus = ActionStatus.END;
@@ -311,6 +303,60 @@ export default class StageDragResize extends EventEmitter {
311
303
  });
312
304
  }
313
305
 
306
+ private bindRotateEvent(): void {
307
+ if (!this.moveable) throw new Error('moveable 为初始化');
308
+
309
+ this.moveable
310
+ .on('rotateStart', (e) => {
311
+ this.dragStatus = ActionStatus.START;
312
+ this.moveableHelper?.onRotateStart(e);
313
+ })
314
+ .on('rotate', (e) => {
315
+ if (!this.target || !this.dragEl) return;
316
+ this.dragStatus = ActionStatus.ING;
317
+ this.moveableHelper?.onRotate(e);
318
+ const frame = this.moveableHelper?.getFrame(e.target);
319
+ this.target.style.transform = frame?.toCSSObject().transform || '';
320
+ })
321
+ .on('rotateEnd', (e) => {
322
+ this.dragStatus = ActionStatus.END;
323
+ const frame = this.moveableHelper?.getFrame(e.target);
324
+ this.emit('update', {
325
+ el: this.target,
326
+ style: {
327
+ transform: frame?.get('transform'),
328
+ },
329
+ });
330
+ });
331
+ }
332
+
333
+ private bindScaleEvent(): void {
334
+ if (!this.moveable) throw new Error('moveable 为初始化');
335
+
336
+ this.moveable
337
+ .on('scaleStart', (e) => {
338
+ this.dragStatus = ActionStatus.START;
339
+ this.moveableHelper?.onScaleStart(e);
340
+ })
341
+ .on('scale', (e) => {
342
+ if (!this.target || !this.dragEl) return;
343
+ this.dragStatus = ActionStatus.ING;
344
+ this.moveableHelper?.onScale(e);
345
+ const frame = this.moveableHelper?.getFrame(e.target);
346
+ this.target.style.transform = frame?.toCSSObject().transform || '';
347
+ })
348
+ .on('scaleEnd', (e) => {
349
+ this.dragStatus = ActionStatus.END;
350
+ const frame = this.moveableHelper?.getFrame(e.target);
351
+ this.emit('update', {
352
+ el: this.target,
353
+ style: {
354
+ transform: frame?.get('transform'),
355
+ },
356
+ });
357
+ });
358
+ }
359
+
314
360
  private sort(): void {
315
361
  if (!this.target || !this.ghostEl) throw new Error('未知错误');
316
362
  const { top } = this.ghostEl.getBoundingClientRect();
@@ -331,14 +377,15 @@ export default class StageDragResize extends EventEmitter {
331
377
  }
332
378
 
333
379
  private update(isResize = false): void {
334
- const rect = this.moveable!.getRect();
380
+ if (!this.target) return;
381
+
335
382
  const offset =
336
- this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : getAbsolutePosition(this.target as HTMLElement, rect);
383
+ this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : { left: this.target.offsetLeft, top: this.target.offsetTop };
337
384
 
338
385
  const left = this.calcValueByFontsize(offset.left);
339
386
  const top = this.calcValueByFontsize(offset.top);
340
- const width = this.calcValueByFontsize(rect.width);
341
- const height = this.calcValueByFontsize(rect.height);
387
+ const width = this.calcValueByFontsize(this.target.clientWidth);
388
+ const height = this.calcValueByFontsize(this.target.clientHeight);
342
389
 
343
390
  this.emit('update', {
344
391
  el: this.target,
@@ -369,15 +416,16 @@ export default class StageDragResize extends EventEmitter {
369
416
  }
370
417
 
371
418
  private updateDragEl(el: HTMLElement) {
372
- const { width, height } = el.getBoundingClientRect();
373
419
  const offset = getOffset(el);
420
+ const { transform } = getComputedStyle(el);
374
421
 
375
422
  this.dragEl.style.cssText = `
376
423
  position: absolute;
424
+ transform: ${transform};
377
425
  left: ${offset.left}px;
378
426
  top: ${offset.top}px;
379
- width: ${width}px;
380
- height: ${height}px;
427
+ width: ${el.clientWidth}px;
428
+ height: ${el.clientHeight}px;
381
429
  z-index: ${ZIndex.DRAG_EL};
382
430
  `;
383
431
 
@@ -415,6 +463,8 @@ export default class StageDragResize extends EventEmitter {
415
463
  dragArea: false,
416
464
  draggable: true,
417
465
  resizable: true,
466
+ scalable: false,
467
+ rotatable: false,
418
468
  snappable: isAbsolute || isFixed,
419
469
  snapGap: isAbsolute || isFixed,
420
470
  snapThreshold: 5,
@@ -46,14 +46,15 @@ export default class TargetCalibrate extends EventEmitter {
46
46
  }
47
47
 
48
48
  public update(el: HTMLElement, prefix: String): HTMLElement {
49
- const { width, height } = el.getBoundingClientRect();
50
49
  const { left, top } = this.getOffset(el);
50
+ const { transform } = getComputedStyle(el);
51
51
  this.operationEl.style.cssText = `
52
52
  position: absolute;
53
+ transform: ${transform};
53
54
  left: ${left}px;
54
55
  top: ${top}px;
55
- width: ${width}px;
56
- height: ${height}px;
56
+ width: ${el.clientWidth}px;
57
+ height: ${el.clientHeight}px;
57
58
  `;
58
59
 
59
60
  this.operationEl.id = `${prefix}${el.id}`;
@@ -65,35 +66,10 @@ export default class TargetCalibrate extends EventEmitter {
65
66
  }
66
67
 
67
68
  private getOffset(el: HTMLElement): Offset {
68
- const { transform } = getComputedStyle(el);
69
69
  const { offsetParent } = el;
70
70
 
71
- let left = el.offsetLeft;
72
- let top = el.offsetTop;
73
-
74
- if (transform.indexOf('matrix') > -1) {
75
- let a = 1;
76
- let b = 1;
77
- let c = 1;
78
- let d = 1;
79
- let e = 0;
80
- let f = 0;
81
- transform.replace(
82
- /matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/,
83
- ($0: string, $1: string, $2: string, $3: string, $4: string, $5: string, $6: string): string => {
84
- a = +$1;
85
- b = +$2;
86
- c = +$3;
87
- d = +$4;
88
- e = +$5;
89
- f = +$6;
90
- return transform;
91
- },
92
- );
93
-
94
- left = a * left + c * top + e;
95
- top = b * left + d * top + f;
96
- }
71
+ const left = el.offsetLeft;
72
+ const top = el.offsetTop;
97
73
 
98
74
  if (offsetParent) {
99
75
  const parentOffset = this.getOffset(offsetParent as HTMLElement);
package/src/types.ts CHANGED
@@ -16,7 +16,7 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { MoveableOptions } from 'react-moveable/declaration/types';
19
+ import { MoveableOptions } from 'moveable';
20
20
 
21
21
  import Core from '@tmagic/core';
22
22
  import { Id, MApp, MNode } from '@tmagic/schema';
@@ -58,6 +58,7 @@ export type Rect = {
58
58
  width: number;
59
59
  height: number;
60
60
  } & Offset;
61
+
61
62
  export interface Offset {
62
63
  left: number;
63
64
  top: number;
@@ -72,10 +73,14 @@ export interface UpdateEventData {
72
73
  el: HTMLElement;
73
74
  ghostEl: HTMLElement;
74
75
  style: {
75
- width: number;
76
- height: number;
76
+ width?: number;
77
+ height?: number;
77
78
  left?: number;
78
79
  top?: number;
80
+ transform?: {
81
+ rotate?: string;
82
+ scale?: string;
83
+ };
79
84
  };
80
85
  }
81
86
 
package/src/util.ts CHANGED
@@ -30,35 +30,10 @@ const getParents = (el: Element, relative: Element) => {
30
30
  };
31
31
 
32
32
  export const getOffset = (el: HTMLElement): Offset => {
33
- const { transform } = getComputedStyle(el);
34
33
  const { offsetParent } = el;
35
34
 
36
- let left = el.offsetLeft;
37
- let top = el.offsetTop;
38
-
39
- if (transform.indexOf('matrix') > -1) {
40
- let a = 1;
41
- let b = 1;
42
- let c = 1;
43
- let d = 1;
44
- let e = 0;
45
- let f = 0;
46
- transform.replace(
47
- /matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/,
48
- ($0: string, $1: string, $2: string, $3: string, $4: string, $5: string, $6: string): string => {
49
- a = +$1;
50
- b = +$2;
51
- c = +$3;
52
- d = +$4;
53
- e = +$5;
54
- f = +$6;
55
- return transform;
56
- },
57
- );
58
-
59
- left = a * left + c * top + e;
60
- top = b * left + d * top + f;
61
- }
35
+ const left = el.offsetLeft;
36
+ const top = el.offsetTop;
62
37
 
63
38
  if (offsetParent) {
64
39
  const parentOffset = getOffset(offsetParent as HTMLElement);