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

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,38 @@ 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 */;
323
- this.target.style.width = `${width}px`;
324
- this.target.style.height = `${height}px`;
325
- this.dragEl.style.width = `${width}px`;
326
- this.dragEl.style.height = `${height}px`;
311
+ this.moveableHelper?.onResize(e);
327
312
  if (this.mode === Mode.SORTABLE) {
328
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
329
- this.target.style.top = `0px`;
330
- return;
313
+ this.target.style.top = "0px";
314
+ } else {
315
+ this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
316
+ this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
331
317
  }
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`;
318
+ this.target.style.width = `${width}px`;
319
+ this.target.style.height = `${height}px`;
337
320
  }).on("resizeEnd", () => {
338
321
  this.dragStatus = "end" /* END */;
339
322
  this.update(true);
@@ -342,7 +325,10 @@ class StageDragResize extends EventEmitter {
342
325
  bindDragEvent() {
343
326
  if (!this.moveable)
344
327
  throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
345
- let offset = null;
328
+ const frame = {
329
+ left: 0,
330
+ top: 0
331
+ };
346
332
  this.moveable.on("dragStart", (e) => {
347
333
  if (!this.target)
348
334
  throw new Error("\u672A\u9009\u4E2D\u7EC4\u4EF6");
@@ -351,22 +337,19 @@ class StageDragResize extends EventEmitter {
351
337
  if (this.mode === Mode.SORTABLE) {
352
338
  this.ghostEl = this.generateGhostEl(this.target);
353
339
  }
340
+ frame.top = this.target.offsetTop;
341
+ frame.left = this.target.offsetLeft;
354
342
  }).on("drag", (e) => {
355
343
  if (!this.target || !this.dragEl)
356
344
  return;
357
- if (!offset) {
358
- offset = getAbsolutePosition(this.target, { left: 0, top: 0 });
359
- }
360
345
  this.dragStatus = "ing" /* ING */;
361
- const { left, top } = e;
362
346
  if (this.ghostEl) {
363
- this.dragEl.style.top = `${top}px`;
364
- this.ghostEl.style.top = `${top + offset.top}px`;
347
+ this.ghostEl.style.top = `${frame.top + e.beforeTranslate[1]}px`;
365
348
  return;
366
349
  }
367
350
  this.moveableHelper?.onDrag(e);
368
- this.target.style.left = `${left + offset.left}px`;
369
- this.target.style.top = `${top + offset.top}px`;
351
+ this.target.style.left = `${frame.left + e.beforeTranslate[0]}px`;
352
+ this.target.style.top = `${frame.top + e.beforeTranslate[1]}px`;
370
353
  }).on("dragEnd", () => {
371
354
  if (this.dragStatus === "ing" /* ING */) {
372
355
  switch (this.mode) {
@@ -377,11 +360,58 @@ class StageDragResize extends EventEmitter {
377
360
  this.update();
378
361
  }
379
362
  }
380
- offset = null;
381
363
  this.dragStatus = "end" /* END */;
382
364
  this.destroyGhostEl();
383
365
  });
384
366
  }
367
+ bindRotateEvent() {
368
+ if (!this.moveable)
369
+ throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
370
+ this.moveable.on("rotateStart", (e) => {
371
+ this.dragStatus = "start" /* START */;
372
+ this.moveableHelper?.onRotateStart(e);
373
+ }).on("rotate", (e) => {
374
+ if (!this.target || !this.dragEl)
375
+ return;
376
+ this.dragStatus = "ing" /* ING */;
377
+ this.moveableHelper?.onRotate(e);
378
+ const frame = this.moveableHelper?.getFrame(e.target);
379
+ this.target.style.transform = frame?.toCSSObject().transform || "";
380
+ }).on("rotateEnd", (e) => {
381
+ this.dragStatus = "end" /* END */;
382
+ const frame = this.moveableHelper?.getFrame(e.target);
383
+ this.emit("update", {
384
+ el: this.target,
385
+ style: {
386
+ transform: frame?.get("transform")
387
+ }
388
+ });
389
+ });
390
+ }
391
+ bindScaleEvent() {
392
+ if (!this.moveable)
393
+ throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
394
+ this.moveable.on("scaleStart", (e) => {
395
+ this.dragStatus = "start" /* START */;
396
+ this.moveableHelper?.onScaleStart(e);
397
+ }).on("scale", (e) => {
398
+ if (!this.target || !this.dragEl)
399
+ return;
400
+ this.dragStatus = "ing" /* ING */;
401
+ this.moveableHelper?.onScale(e);
402
+ const frame = this.moveableHelper?.getFrame(e.target);
403
+ this.target.style.transform = frame?.toCSSObject().transform || "";
404
+ }).on("scaleEnd", (e) => {
405
+ this.dragStatus = "end" /* END */;
406
+ const frame = this.moveableHelper?.getFrame(e.target);
407
+ this.emit("update", {
408
+ el: this.target,
409
+ style: {
410
+ transform: frame?.get("transform")
411
+ }
412
+ });
413
+ });
414
+ }
385
415
  sort() {
386
416
  if (!this.target || !this.ghostEl)
387
417
  throw new Error("\u672A\u77E5\u9519\u8BEF");
@@ -402,12 +432,13 @@ class StageDragResize extends EventEmitter {
402
432
  }
403
433
  }
404
434
  update(isResize = false) {
405
- const rect = this.moveable.getRect();
406
- const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : getAbsolutePosition(this.target, rect);
435
+ if (!this.target)
436
+ return;
437
+ const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : { left: this.target.offsetLeft, top: this.target.offsetTop };
407
438
  const left = this.calcValueByFontsize(offset.left);
408
439
  const top = this.calcValueByFontsize(offset.top);
409
- const width = this.calcValueByFontsize(rect.width);
410
- const height = this.calcValueByFontsize(rect.height);
440
+ const width = this.calcValueByFontsize(this.target.clientWidth);
441
+ const height = this.calcValueByFontsize(this.target.clientHeight);
411
442
  this.emit("update", {
412
443
  el: this.target,
413
444
  style: isResize ? { left, top, width, height } : { left, top }
@@ -433,17 +464,21 @@ class StageDragResize extends EventEmitter {
433
464
  this.ghostEl = void 0;
434
465
  }
435
466
  updateDragEl(el) {
436
- const { width, height } = el.getBoundingClientRect();
437
467
  const offset = getOffset(el);
468
+ const { transform } = getComputedStyle(el);
438
469
  this.dragEl.style.cssText = `
439
470
  position: absolute;
471
+ transform: ${transform};
440
472
  left: ${offset.left}px;
441
473
  top: ${offset.top}px;
442
- width: ${width}px;
443
- height: ${height}px;
474
+ width: ${el.clientWidth}px;
475
+ height: ${el.clientHeight}px;
444
476
  z-index: ${ZIndex.DRAG_EL};
445
477
  `;
446
478
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
479
+ if (typeof this.core.config.updateDragEl === "function") {
480
+ this.core.config.updateDragEl(this.dragEl, el);
481
+ }
447
482
  }
448
483
  destroyDragEl() {
449
484
  this.dragEl?.remove();
@@ -469,6 +504,8 @@ class StageDragResize extends EventEmitter {
469
504
  dragArea: false,
470
505
  draggable: true,
471
506
  resizable: true,
507
+ scalable: false,
508
+ rotatable: false,
472
509
  snappable: isAbsolute || isFixed,
473
510
  snapGap: isAbsolute || isFixed,
474
511
  snapThreshold: 5,
@@ -567,49 +604,34 @@ class TargetCalibrate extends EventEmitter {
567
604
  this.parent = config.parent;
568
605
  this.mask = config.mask;
569
606
  this.dr = config.dr;
607
+ this.core = config.core;
570
608
  this.operationEl = globalThis.document.createElement("div");
571
609
  this.parent.append(this.operationEl);
572
610
  }
573
611
  update(el, prefix) {
574
- const { width, height } = el.getBoundingClientRect();
575
612
  const { left, top } = this.getOffset(el);
613
+ const { transform } = getComputedStyle(el);
576
614
  this.operationEl.style.cssText = `
577
615
  position: absolute;
616
+ transform: ${transform};
578
617
  left: ${left}px;
579
618
  top: ${top}px;
580
- width: ${width}px;
581
- height: ${height}px;
619
+ width: ${el.clientWidth}px;
620
+ height: ${el.clientHeight}px;
582
621
  `;
583
622
  this.operationEl.id = `${prefix}${el.id}`;
623
+ if (typeof this.core.config.updateDragEl === "function") {
624
+ this.core.config.updateDragEl(this.operationEl, el);
625
+ }
584
626
  return this.operationEl;
585
627
  }
586
628
  destroy() {
587
629
  this.operationEl?.remove();
588
630
  }
589
631
  getOffset(el) {
590
- const { transform } = getComputedStyle(el);
591
632
  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
- }
633
+ const left = el.offsetLeft;
634
+ const top = el.offsetTop;
613
635
  if (offsetParent) {
614
636
  const parentOffset = this.getOffset(offsetParent);
615
637
  return {
@@ -650,7 +672,8 @@ class StageHighlight extends EventEmitter {
650
672
  this.calibrationTarget = new TargetCalibrate({
651
673
  parent: this.core.mask.content,
652
674
  mask: this.core.mask,
653
- dr: this.core.dr
675
+ dr: this.core.dr,
676
+ core: this.core
654
677
  });
655
678
  }
656
679
  highlight(el) {