@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.
@@ -53,29 +53,9 @@
53
53
  return parents;
54
54
  };
55
55
  const getOffset = (el) => {
56
- const { transform } = getComputedStyle(el);
57
56
  const { offsetParent } = el;
58
- let left = el.offsetLeft;
59
- let top = el.offsetTop;
60
- if (transform.indexOf("matrix") > -1) {
61
- let a = 1;
62
- let b = 1;
63
- let c = 1;
64
- let d = 1;
65
- let e = 0;
66
- let f = 0;
67
- transform.replace(/matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/, ($0, $1, $2, $3, $4, $5, $6) => {
68
- a = +$1;
69
- b = +$2;
70
- c = +$3;
71
- d = +$4;
72
- e = +$5;
73
- f = +$6;
74
- return transform;
75
- });
76
- left = a * left + c * top + e;
77
- top = b * left + d * top + f;
78
- }
57
+ const left = el.offsetLeft;
58
+ const top = el.offsetTop;
79
59
  if (offsetParent) {
80
60
  const parentOffset = getOffset(offsetParent);
81
61
  return {
@@ -312,35 +292,34 @@
312
292
  });
313
293
  this.bindResizeEvent();
314
294
  this.bindDragEvent();
295
+ this.bindRotateEvent();
296
+ this.bindScaleEvent();
315
297
  }
316
298
  bindResizeEvent() {
317
299
  if (!this.moveable)
318
300
  throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
301
+ const frame = {
302
+ left: 0,
303
+ top: 0
304
+ };
319
305
  this.moveable.on("resizeStart", (e) => {
320
- if (e.dragStart) {
321
- const rect = this.moveable.getRect();
322
- const offset = getAbsolutePosition(e.target, rect);
323
- e.dragStart.set([offset.left, offset.top]);
324
- }
325
- }).on("resize", ({ width, height, drag }) => {
306
+ if (!this.target)
307
+ return;
308
+ this.dragStatus = "start" /* START */;
309
+ this.moveableHelper?.onResizeStart(e);
310
+ frame.top = this.target.offsetTop;
311
+ frame.left = this.target.offsetLeft;
312
+ }).on("resize", (e) => {
313
+ const { width, height, drag } = e;
326
314
  if (!this.moveable || !this.target || !this.dragEl)
327
315
  return;
328
316
  const { beforeTranslate } = drag;
329
317
  this.dragStatus = "ing" /* ING */;
318
+ this.moveableHelper?.onResize(e);
330
319
  this.target.style.width = `${width}px`;
331
320
  this.target.style.height = `${height}px`;
332
- this.dragEl.style.width = `${width}px`;
333
- this.dragEl.style.height = `${height}px`;
334
- if (this.mode === Mode.SORTABLE) {
335
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
336
- this.target.style.top = `0px`;
337
- return;
338
- }
339
- this.dragEl.style.left = `${beforeTranslate[0]}px`;
340
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
341
- const offset = getAbsolutePosition(this.target, { left: beforeTranslate[0], top: beforeTranslate[1] });
342
- this.target.style.left = `${offset.left}px`;
343
- this.target.style.top = `${offset.top}px`;
321
+ this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
322
+ this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
344
323
  }).on("resizeEnd", () => {
345
324
  this.dragStatus = "end" /* END */;
346
325
  this.update(true);
@@ -389,6 +368,54 @@
389
368
  this.destroyGhostEl();
390
369
  });
391
370
  }
371
+ bindRotateEvent() {
372
+ if (!this.moveable)
373
+ throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
374
+ this.moveable.on("rotateStart", (e) => {
375
+ this.dragStatus = "start" /* START */;
376
+ this.moveableHelper?.onRotateStart(e);
377
+ }).on("rotate", (e) => {
378
+ if (!this.target || !this.dragEl)
379
+ return;
380
+ this.dragStatus = "ing" /* ING */;
381
+ this.moveableHelper?.onRotate(e);
382
+ const frame = this.moveableHelper?.getFrame(e.target);
383
+ this.target.style.transform = frame?.toCSSObject().transform || "";
384
+ }).on("rotateEnd", (e) => {
385
+ this.dragStatus = "end" /* END */;
386
+ const frame = this.moveableHelper?.getFrame(e.target);
387
+ this.emit("update", {
388
+ el: this.target,
389
+ style: {
390
+ transform: frame?.get("transform")
391
+ }
392
+ });
393
+ });
394
+ }
395
+ bindScaleEvent() {
396
+ if (!this.moveable)
397
+ throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
398
+ this.moveable.on("scaleStart", (e) => {
399
+ this.dragStatus = "start" /* START */;
400
+ this.moveableHelper?.onScaleStart(e);
401
+ }).on("scale", (e) => {
402
+ if (!this.target || !this.dragEl)
403
+ return;
404
+ this.dragStatus = "ing" /* ING */;
405
+ this.moveableHelper?.onScale(e);
406
+ const frame = this.moveableHelper?.getFrame(e.target);
407
+ this.target.style.transform = frame?.toCSSObject().transform || "";
408
+ }).on("scaleEnd", (e) => {
409
+ this.dragStatus = "end" /* END */;
410
+ const frame = this.moveableHelper?.getFrame(e.target);
411
+ this.emit("update", {
412
+ el: this.target,
413
+ style: {
414
+ transform: frame?.get("transform")
415
+ }
416
+ });
417
+ });
418
+ }
392
419
  sort() {
393
420
  if (!this.target || !this.ghostEl)
394
421
  throw new Error("\u672A\u77E5\u9519\u8BEF");
@@ -409,12 +436,13 @@
409
436
  }
410
437
  }
411
438
  update(isResize = false) {
412
- const rect = this.moveable.getRect();
413
- const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : getAbsolutePosition(this.target, rect);
439
+ if (!this.target)
440
+ return;
441
+ const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : { left: this.target.offsetLeft, top: this.target.offsetTop };
414
442
  const left = this.calcValueByFontsize(offset.left);
415
443
  const top = this.calcValueByFontsize(offset.top);
416
- const width = this.calcValueByFontsize(rect.width);
417
- const height = this.calcValueByFontsize(rect.height);
444
+ const width = this.calcValueByFontsize(this.target.clientWidth);
445
+ const height = this.calcValueByFontsize(this.target.clientHeight);
418
446
  this.emit("update", {
419
447
  el: this.target,
420
448
  style: isResize ? { left, top, width, height } : { left, top }
@@ -440,14 +468,15 @@
440
468
  this.ghostEl = void 0;
441
469
  }
442
470
  updateDragEl(el) {
443
- const { width, height } = el.getBoundingClientRect();
444
471
  const offset = getOffset(el);
472
+ const { transform } = getComputedStyle(el);
445
473
  this.dragEl.style.cssText = `
446
474
  position: absolute;
475
+ transform: ${transform};
447
476
  left: ${offset.left}px;
448
477
  top: ${offset.top}px;
449
- width: ${width}px;
450
- height: ${height}px;
478
+ width: ${el.clientWidth}px;
479
+ height: ${el.clientHeight}px;
451
480
  z-index: ${ZIndex.DRAG_EL};
452
481
  `;
453
482
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
@@ -476,6 +505,8 @@
476
505
  dragArea: false,
477
506
  draggable: true,
478
507
  resizable: true,
508
+ scalable: false,
509
+ rotatable: false,
479
510
  snappable: isAbsolute || isFixed,
480
511
  snapGap: isAbsolute || isFixed,
481
512
  snapThreshold: 5,
@@ -578,14 +609,15 @@
578
609
  this.parent.append(this.operationEl);
579
610
  }
580
611
  update(el, prefix) {
581
- const { width, height } = el.getBoundingClientRect();
582
612
  const { left, top } = this.getOffset(el);
613
+ const { transform } = getComputedStyle(el);
583
614
  this.operationEl.style.cssText = `
584
615
  position: absolute;
616
+ transform: ${transform};
585
617
  left: ${left}px;
586
618
  top: ${top}px;
587
- width: ${width}px;
588
- height: ${height}px;
619
+ width: ${el.clientWidth}px;
620
+ height: ${el.clientHeight}px;
589
621
  `;
590
622
  this.operationEl.id = `${prefix}${el.id}`;
591
623
  return this.operationEl;
@@ -594,29 +626,9 @@
594
626
  this.operationEl?.remove();
595
627
  }
596
628
  getOffset(el) {
597
- const { transform } = getComputedStyle(el);
598
629
  const { offsetParent } = el;
599
- let left = el.offsetLeft;
600
- let top = el.offsetTop;
601
- if (transform.indexOf("matrix") > -1) {
602
- let a = 1;
603
- let b = 1;
604
- let c = 1;
605
- let d = 1;
606
- let e = 0;
607
- let f = 0;
608
- transform.replace(/matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/, ($0, $1, $2, $3, $4, $5, $6) => {
609
- a = +$1;
610
- b = +$2;
611
- c = +$3;
612
- d = +$4;
613
- e = +$5;
614
- f = +$6;
615
- return transform;
616
- });
617
- left = a * left + c * top + e;
618
- top = b * left + d * top + f;
619
- }
630
+ const left = el.offsetLeft;
631
+ const top = el.offsetTop;
620
632
  if (offsetParent) {
621
633
  const parentOffset = this.getOffset(offsetParent);
622
634
  return {