@tmagic/stage 1.0.0-rc.5 → 1.0.0-rc.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.
@@ -46,29 +46,9 @@ const getParents = (el, relative) => {
46
46
  return parents;
47
47
  };
48
48
  const getOffset = (el) => {
49
- const { transform } = getComputedStyle(el);
50
49
  const { offsetParent } = el;
51
- let left = el.offsetLeft;
52
- let top = el.offsetTop;
53
- if (transform.indexOf("matrix") > -1) {
54
- let a = 1;
55
- let b = 1;
56
- let c = 1;
57
- let d = 1;
58
- let e = 0;
59
- let f = 0;
60
- transform.replace(/matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/, ($0, $1, $2, $3, $4, $5, $6) => {
61
- a = +$1;
62
- b = +$2;
63
- c = +$3;
64
- d = +$4;
65
- e = +$5;
66
- f = +$6;
67
- return transform;
68
- });
69
- left = a * left + c * top + e;
70
- top = b * left + d * top + f;
71
- }
50
+ const left = el.offsetLeft;
51
+ const top = el.offsetTop;
72
52
  if (offsetParent) {
73
53
  const parentOffset = getOffset(offsetParent);
74
54
  return {
@@ -216,8 +196,8 @@ class StageDragResize extends EventEmitter {
216
196
  select(el, event) {
217
197
  const oldTarget = this.target;
218
198
  this.target = el;
219
- this.init(el);
220
199
  if (!this.moveable || this.target !== oldTarget) {
200
+ this.init(el);
221
201
  this.moveableHelper = MoveableHelper.create({
222
202
  useBeforeRender: true,
223
203
  useRender: false,
@@ -305,35 +285,34 @@ class StageDragResize extends EventEmitter {
305
285
  });
306
286
  this.bindResizeEvent();
307
287
  this.bindDragEvent();
288
+ this.bindRotateEvent();
289
+ this.bindScaleEvent();
308
290
  }
309
291
  bindResizeEvent() {
310
292
  if (!this.moveable)
311
293
  throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
294
+ const frame = {
295
+ left: 0,
296
+ top: 0
297
+ };
312
298
  this.moveable.on("resizeStart", (e) => {
313
- if (e.dragStart) {
314
- const rect = this.moveable.getRect();
315
- const offset = getAbsolutePosition(e.target, rect);
316
- e.dragStart.set([offset.left, offset.top]);
317
- }
318
- }).on("resize", ({ width, height, drag }) => {
299
+ if (!this.target)
300
+ return;
301
+ this.dragStatus = "start" /* START */;
302
+ this.moveableHelper?.onResizeStart(e);
303
+ frame.top = this.target.offsetTop;
304
+ frame.left = this.target.offsetLeft;
305
+ }).on("resize", (e) => {
306
+ const { width, height, drag } = e;
319
307
  if (!this.moveable || !this.target || !this.dragEl)
320
308
  return;
321
309
  const { beforeTranslate } = drag;
322
310
  this.dragStatus = "ing" /* ING */;
311
+ this.moveableHelper?.onResize(e);
323
312
  this.target.style.width = `${width}px`;
324
313
  this.target.style.height = `${height}px`;
325
- this.dragEl.style.width = `${width}px`;
326
- this.dragEl.style.height = `${height}px`;
327
- if (this.mode === Mode.SORTABLE) {
328
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
329
- this.target.style.top = `0px`;
330
- return;
331
- }
332
- this.dragEl.style.left = `${beforeTranslate[0]}px`;
333
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
334
- const offset = getAbsolutePosition(this.target, { left: beforeTranslate[0], top: beforeTranslate[1] });
335
- this.target.style.left = `${offset.left}px`;
336
- this.target.style.top = `${offset.top}px`;
314
+ this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
315
+ this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
337
316
  }).on("resizeEnd", () => {
338
317
  this.dragStatus = "end" /* END */;
339
318
  this.update(true);
@@ -382,6 +361,54 @@ class StageDragResize extends EventEmitter {
382
361
  this.destroyGhostEl();
383
362
  });
384
363
  }
364
+ bindRotateEvent() {
365
+ if (!this.moveable)
366
+ throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
367
+ this.moveable.on("rotateStart", (e) => {
368
+ this.dragStatus = "start" /* START */;
369
+ this.moveableHelper?.onRotateStart(e);
370
+ }).on("rotate", (e) => {
371
+ if (!this.target || !this.dragEl)
372
+ return;
373
+ this.dragStatus = "ing" /* ING */;
374
+ this.moveableHelper?.onRotate(e);
375
+ const frame = this.moveableHelper?.getFrame(e.target);
376
+ this.target.style.transform = frame?.toCSSObject().transform || "";
377
+ }).on("rotateEnd", (e) => {
378
+ this.dragStatus = "end" /* END */;
379
+ const frame = this.moveableHelper?.getFrame(e.target);
380
+ this.emit("update", {
381
+ el: this.target,
382
+ style: {
383
+ transform: frame?.get("transform")
384
+ }
385
+ });
386
+ });
387
+ }
388
+ bindScaleEvent() {
389
+ if (!this.moveable)
390
+ throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
391
+ this.moveable.on("scaleStart", (e) => {
392
+ this.dragStatus = "start" /* START */;
393
+ this.moveableHelper?.onScaleStart(e);
394
+ }).on("scale", (e) => {
395
+ if (!this.target || !this.dragEl)
396
+ return;
397
+ this.dragStatus = "ing" /* ING */;
398
+ this.moveableHelper?.onScale(e);
399
+ const frame = this.moveableHelper?.getFrame(e.target);
400
+ this.target.style.transform = frame?.toCSSObject().transform || "";
401
+ }).on("scaleEnd", (e) => {
402
+ this.dragStatus = "end" /* END */;
403
+ const frame = this.moveableHelper?.getFrame(e.target);
404
+ this.emit("update", {
405
+ el: this.target,
406
+ style: {
407
+ transform: frame?.get("transform")
408
+ }
409
+ });
410
+ });
411
+ }
385
412
  sort() {
386
413
  if (!this.target || !this.ghostEl)
387
414
  throw new Error("\u672A\u77E5\u9519\u8BEF");
@@ -402,12 +429,13 @@ class StageDragResize extends EventEmitter {
402
429
  }
403
430
  }
404
431
  update(isResize = false) {
405
- const rect = this.moveable.getRect();
406
- const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : getAbsolutePosition(this.target, rect);
432
+ if (!this.target)
433
+ return;
434
+ const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : { left: this.target.offsetLeft, top: this.target.offsetTop };
407
435
  const left = this.calcValueByFontsize(offset.left);
408
436
  const top = this.calcValueByFontsize(offset.top);
409
- const width = this.calcValueByFontsize(rect.width);
410
- const height = this.calcValueByFontsize(rect.height);
437
+ const width = this.calcValueByFontsize(this.target.clientWidth);
438
+ const height = this.calcValueByFontsize(this.target.clientHeight);
411
439
  this.emit("update", {
412
440
  el: this.target,
413
441
  style: isResize ? { left, top, width, height } : { left, top }
@@ -433,17 +461,21 @@ class StageDragResize extends EventEmitter {
433
461
  this.ghostEl = void 0;
434
462
  }
435
463
  updateDragEl(el) {
436
- const { width, height } = el.getBoundingClientRect();
437
464
  const offset = getOffset(el);
465
+ const { transform } = getComputedStyle(el);
438
466
  this.dragEl.style.cssText = `
439
467
  position: absolute;
468
+ transform: ${transform};
440
469
  left: ${offset.left}px;
441
470
  top: ${offset.top}px;
442
- width: ${width}px;
443
- height: ${height}px;
471
+ width: ${el.clientWidth}px;
472
+ height: ${el.clientHeight}px;
444
473
  z-index: ${ZIndex.DRAG_EL};
445
474
  `;
446
475
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
476
+ if (typeof this.core.config.updateDragEl === "function") {
477
+ this.core.config.updateDragEl(this.dragEl, el);
478
+ }
447
479
  }
448
480
  destroyDragEl() {
449
481
  this.dragEl?.remove();
@@ -469,6 +501,8 @@ class StageDragResize extends EventEmitter {
469
501
  dragArea: false,
470
502
  draggable: true,
471
503
  resizable: true,
504
+ scalable: false,
505
+ rotatable: false,
472
506
  snappable: isAbsolute || isFixed,
473
507
  snapGap: isAbsolute || isFixed,
474
508
  snapThreshold: 5,
@@ -567,49 +601,34 @@ class TargetCalibrate extends EventEmitter {
567
601
  this.parent = config.parent;
568
602
  this.mask = config.mask;
569
603
  this.dr = config.dr;
604
+ this.core = config.core;
570
605
  this.operationEl = globalThis.document.createElement("div");
571
606
  this.parent.append(this.operationEl);
572
607
  }
573
608
  update(el, prefix) {
574
- const { width, height } = el.getBoundingClientRect();
575
609
  const { left, top } = this.getOffset(el);
610
+ const { transform } = getComputedStyle(el);
576
611
  this.operationEl.style.cssText = `
577
612
  position: absolute;
613
+ transform: ${transform};
578
614
  left: ${left}px;
579
615
  top: ${top}px;
580
- width: ${width}px;
581
- height: ${height}px;
616
+ width: ${el.clientWidth}px;
617
+ height: ${el.clientHeight}px;
582
618
  `;
583
619
  this.operationEl.id = `${prefix}${el.id}`;
620
+ if (typeof this.core.config.updateDragEl === "function") {
621
+ this.core.config.updateDragEl(this.operationEl, el);
622
+ }
584
623
  return this.operationEl;
585
624
  }
586
625
  destroy() {
587
626
  this.operationEl?.remove();
588
627
  }
589
628
  getOffset(el) {
590
- const { transform } = getComputedStyle(el);
591
629
  const { offsetParent } = el;
592
- let left = el.offsetLeft;
593
- let top = el.offsetTop;
594
- if (transform.indexOf("matrix") > -1) {
595
- let a = 1;
596
- let b = 1;
597
- let c = 1;
598
- let d = 1;
599
- let e = 0;
600
- let f = 0;
601
- transform.replace(/matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/, ($0, $1, $2, $3, $4, $5, $6) => {
602
- a = +$1;
603
- b = +$2;
604
- c = +$3;
605
- d = +$4;
606
- e = +$5;
607
- f = +$6;
608
- return transform;
609
- });
610
- left = a * left + c * top + e;
611
- top = b * left + d * top + f;
612
- }
630
+ const left = el.offsetLeft;
631
+ const top = el.offsetTop;
613
632
  if (offsetParent) {
614
633
  const parentOffset = this.getOffset(offsetParent);
615
634
  return {
@@ -650,7 +669,8 @@ class StageHighlight extends EventEmitter {
650
669
  this.calibrationTarget = new TargetCalibrate({
651
670
  parent: this.core.mask.content,
652
671
  mask: this.core.mask,
653
- dr: this.core.dr
672
+ dr: this.core.dr,
673
+ core: this.core
654
674
  });
655
675
  }
656
676
  highlight(el) {
@@ -1052,16 +1072,6 @@ class StageRender extends EventEmitter {
1052
1072
  this.iframe?.contentDocument?.body?.appendChild(el);
1053
1073
  }
1054
1074
  }
1055
- if (this.contentWindow) {
1056
- const style = this.contentWindow.document.createElement("style");
1057
- style.id = "tmagic-stage-render";
1058
- style.innerHTML = `
1059
- .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
1060
- z-index: ${ZIndex.SELECTED_EL};
1061
- }
1062
- `;
1063
- this.contentWindow.document.head.appendChild(style);
1064
- }
1065
1075
  };
1066
1076
  this.core = core;
1067
1077
  this.runtimeUrl = core.config.runtimeUrl || "";