@tmagic/stage 1.0.0-rc.4 → 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.
@@ -23,6 +23,8 @@
23
23
  var ZIndex = /* @__PURE__ */ ((ZIndex2) => {
24
24
  ZIndex2["MASK"] = "99999";
25
25
  ZIndex2["SELECTED_EL"] = "666";
26
+ ZIndex2["GHOST_EL"] = "700";
27
+ ZIndex2["DRAG_EL"] = "9";
26
28
  return ZIndex2;
27
29
  })(ZIndex || {});
28
30
  var MouseButton = /* @__PURE__ */ ((MouseButton2) => {
@@ -51,29 +53,9 @@
51
53
  return parents;
52
54
  };
53
55
  const getOffset = (el) => {
54
- const { transform } = getComputedStyle(el);
55
56
  const { offsetParent } = el;
56
- let left = el.offsetLeft;
57
- let top = el.offsetTop;
58
- if (transform.indexOf("matrix") > -1) {
59
- let a = 1;
60
- let b = 1;
61
- let c = 1;
62
- let d = 1;
63
- let e = 0;
64
- let f = 0;
65
- transform.replace(/matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/, ($0, $1, $2, $3, $4, $5, $6) => {
66
- a = +$1;
67
- b = +$2;
68
- c = +$3;
69
- d = +$4;
70
- e = +$5;
71
- f = +$6;
72
- return transform;
73
- });
74
- left = a * left + c * top + e;
75
- top = b * left + d * top + f;
76
- }
57
+ const left = el.offsetLeft;
58
+ const top = el.offsetTop;
77
59
  if (offsetParent) {
78
60
  const parentOffset = getOffset(offsetParent);
79
61
  return {
@@ -279,13 +261,11 @@
279
261
  this.mode = getMode(el);
280
262
  this.destroyGhostEl();
281
263
  this.updateDragEl(el);
282
- this.setElementGuidelines(el);
283
264
  this.moveableOptions = this.getOptions({
284
265
  target: this.dragEl
285
266
  });
286
267
  }
287
- setElementGuidelines(el) {
288
- const nodes = el.parentElement?.children || [];
268
+ setElementGuidelines(nodes) {
289
269
  this.elementGuidelines.forEach((node) => {
290
270
  node.remove();
291
271
  });
@@ -294,7 +274,7 @@
294
274
  const frame = document.createDocumentFragment();
295
275
  for (const node of nodes) {
296
276
  const { width, height } = node.getBoundingClientRect();
297
- if (node === el)
277
+ if (node === this.target)
298
278
  continue;
299
279
  const { left, top } = getOffset(node);
300
280
  const elementGuideline = document.createElement("div");
@@ -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 }
@@ -427,7 +455,7 @@
427
455
  const ghostEl = el.cloneNode(true);
428
456
  const { top, left } = getAbsolutePosition(el, getOffset(el));
429
457
  ghostEl.id = `${GHOST_EL_ID_PREFIX}${el.id}`;
430
- ghostEl.style.zIndex = "5";
458
+ ghostEl.style.zIndex = ZIndex.GHOST_EL;
431
459
  ghostEl.style.opacity = ".5";
432
460
  ghostEl.style.position = "absolute";
433
461
  ghostEl.style.left = `${left}px`;
@@ -440,15 +468,16 @@
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;
451
- z-index: 9;
478
+ width: ${el.clientWidth}px;
479
+ height: ${el.clientHeight}px;
480
+ z-index: ${ZIndex.DRAG_EL};
452
481
  `;
453
482
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
454
483
  }
@@ -464,6 +493,11 @@
464
493
  if (typeof moveableOptions === "function") {
465
494
  moveableOptions = moveableOptions(this.core);
466
495
  }
496
+ const elementGuidelines = moveableOptions.elementGuidelines || this.target.parentElement?.children || [];
497
+ this.setElementGuidelines(elementGuidelines);
498
+ if (moveableOptions.elementGuidelines) {
499
+ delete moveableOptions.elementGuidelines;
500
+ }
467
501
  return {
468
502
  origin: false,
469
503
  rootContainer: this.core.container,
@@ -471,6 +505,8 @@
471
505
  dragArea: false,
472
506
  draggable: true,
473
507
  resizable: true,
508
+ scalable: false,
509
+ rotatable: false,
474
510
  snappable: isAbsolute || isFixed,
475
511
  snapGap: isAbsolute || isFixed,
476
512
  snapThreshold: 5,
@@ -573,14 +609,15 @@
573
609
  this.parent.append(this.operationEl);
574
610
  }
575
611
  update(el, prefix) {
576
- const { width, height } = el.getBoundingClientRect();
577
612
  const { left, top } = this.getOffset(el);
613
+ const { transform } = getComputedStyle(el);
578
614
  this.operationEl.style.cssText = `
579
615
  position: absolute;
616
+ transform: ${transform};
580
617
  left: ${left}px;
581
618
  top: ${top}px;
582
- width: ${width}px;
583
- height: ${height}px;
619
+ width: ${el.clientWidth}px;
620
+ height: ${el.clientHeight}px;
584
621
  `;
585
622
  this.operationEl.id = `${prefix}${el.id}`;
586
623
  return this.operationEl;
@@ -589,29 +626,9 @@
589
626
  this.operationEl?.remove();
590
627
  }
591
628
  getOffset(el) {
592
- const { transform } = getComputedStyle(el);
593
629
  const { offsetParent } = el;
594
- let left = el.offsetLeft;
595
- let top = el.offsetTop;
596
- if (transform.indexOf("matrix") > -1) {
597
- let a = 1;
598
- let b = 1;
599
- let c = 1;
600
- let d = 1;
601
- let e = 0;
602
- let f = 0;
603
- transform.replace(/matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/, ($0, $1, $2, $3, $4, $5, $6) => {
604
- a = +$1;
605
- b = +$2;
606
- c = +$3;
607
- d = +$4;
608
- e = +$5;
609
- f = +$6;
610
- return transform;
611
- });
612
- left = a * left + c * top + e;
613
- top = b * left + d * top + f;
614
- }
630
+ const left = el.offsetLeft;
631
+ const top = el.offsetTop;
615
632
  if (offsetParent) {
616
633
  const parentOffset = this.getOffset(offsetParent);
617
634
  return {
@@ -1054,16 +1071,6 @@
1054
1071
  this.iframe?.contentDocument?.body?.appendChild(el);
1055
1072
  }
1056
1073
  }
1057
- if (this.contentWindow) {
1058
- const style = this.contentWindow.document.createElement("style");
1059
- style.id = "tmagic-stage-render";
1060
- style.innerHTML = `
1061
- .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
1062
- z-index: ${ZIndex.SELECTED_EL};
1063
- }
1064
- `;
1065
- this.contentWindow.document.head.appendChild(style);
1066
- }
1067
1074
  };
1068
1075
  this.core = core;
1069
1076
  this.runtimeUrl = core.config.runtimeUrl || "";