@tmagic/stage 1.2.7 → 1.2.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.
@@ -3,8 +3,8 @@ import KeyController from 'keycon';
3
3
  import { merge, throttle } from 'lodash-es';
4
4
  import { Env } from '@tmagic/core';
5
5
  import { removeClassName, addClassName, removeClassNameByClassName, getDocument, createDiv, injectStyle, isSameDomain, getHost } from '@tmagic/utils';
6
- import Moveable from 'moveable';
7
6
  import MoveableHelper from 'moveable-helper';
7
+ import Moveable from 'moveable';
8
8
  import Guides from '@scena/guides';
9
9
 
10
10
  const GHOST_EL_ID_PREFIX = "ghost_el_";
@@ -470,6 +470,29 @@ class DragResizeHelper {
470
470
  });
471
471
  this.moveableHelper.onDragGroup(e);
472
472
  }
473
+ getUpdatedElRect(el, parentEl, doc) {
474
+ const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : { left: el.offsetLeft, top: el.offsetTop };
475
+ let left = calcValueByFontsize(doc, offset.left);
476
+ let top = calcValueByFontsize(doc, offset.top);
477
+ const width = calcValueByFontsize(doc, el.clientWidth);
478
+ const height = calcValueByFontsize(doc, el.clientHeight);
479
+ let shadowEl = this.getShadowEl();
480
+ const shadowEls = this.getShadowEls();
481
+ if (shadowEls.length) {
482
+ shadowEl = shadowEls.find((item) => item.id.endsWith(el.id));
483
+ }
484
+ if (parentEl && this.mode === Mode.ABSOLUTE && shadowEl) {
485
+ const targetShadowHtmlEl = shadowEl;
486
+ const targetShadowElOffsetLeft = targetShadowHtmlEl.offsetLeft || 0;
487
+ const targetShadowElOffsetTop = targetShadowHtmlEl.offsetTop || 0;
488
+ const frame = this.getFrame(shadowEl);
489
+ const [translateX, translateY] = frame?.properties.transform.translate.value;
490
+ const { left: parentLeft, top: parentTop } = getOffset(parentEl);
491
+ left = calcValueByFontsize(doc, targetShadowElOffsetLeft) + parseFloat(translateX) - calcValueByFontsize(doc, parentLeft);
492
+ top = calcValueByFontsize(doc, targetShadowElOffsetTop) + parseFloat(translateY) - calcValueByFontsize(doc, parentTop);
493
+ }
494
+ return { width, height, left, top };
495
+ }
473
496
  setFramesSnapShot(events) {
474
497
  if (this.framesSnapShot.length > 0)
475
498
  return;
@@ -791,10 +814,7 @@ class StageDragResize extends MoveableOptionsManager {
791
814
  this.markContainerEnd = config.markContainerEnd;
792
815
  this.delayedMarkContainer = config.delayedMarkContainer;
793
816
  this.disabledDragStart = config.disabledDragStart;
794
- this.dragResizeHelper = new DragResizeHelper({
795
- container: config.container,
796
- updateDragEl: config.updateDragEl
797
- });
817
+ this.dragResizeHelper = config.dragResizeHelper;
798
818
  this.on("update-moveable", () => {
799
819
  if (this.moveable) {
800
820
  this.updateMoveable();
@@ -1001,27 +1021,12 @@ class StageDragResize extends MoveableOptionsManager {
1001
1021
  const doc = this.getRenderDocument();
1002
1022
  if (!doc)
1003
1023
  return;
1004
- const offset = this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : { left: this.target.offsetLeft, top: this.target.offsetTop };
1005
- let left = calcValueByFontsize(doc, offset.left);
1006
- let top = calcValueByFontsize(doc, offset.top);
1007
- const width = calcValueByFontsize(doc, this.target.clientWidth);
1008
- const height = calcValueByFontsize(doc, this.target.clientHeight);
1009
- const shadowEl = this.dragResizeHelper.getShadowEl();
1010
- if (parentEl && this.mode === Mode.ABSOLUTE && shadowEl) {
1011
- const targetShadowHtmlEl = shadowEl;
1012
- const targetShadowElOffsetLeft = targetShadowHtmlEl.offsetLeft || 0;
1013
- const targetShadowElOffsetTop = targetShadowHtmlEl.offsetTop || 0;
1014
- const frame = this.dragResizeHelper.getFrame(shadowEl);
1015
- const [translateX, translateY] = frame?.properties.transform.translate.value;
1016
- const { left: parentLeft, top: parentTop } = getOffset(parentEl);
1017
- left = calcValueByFontsize(doc, targetShadowElOffsetLeft) + parseFloat(translateX) - calcValueByFontsize(doc, parentLeft);
1018
- top = calcValueByFontsize(doc, targetShadowElOffsetTop) + parseFloat(translateY) - calcValueByFontsize(doc, parentTop);
1019
- }
1024
+ const rect = this.dragResizeHelper.getUpdatedElRect(this.target, parentEl, doc);
1020
1025
  this.emit("update", {
1021
1026
  data: [
1022
1027
  {
1023
1028
  el: this.target,
1024
- style: isResize ? { left, top, width, height } : { left, top }
1029
+ style: isResize ? rect : { left: rect.left, top: rect.top }
1025
1030
  }
1026
1031
  ],
1027
1032
  parentEl
@@ -1078,6 +1083,8 @@ class StageMultiDragResize extends MoveableOptionsManager {
1078
1083
  dragStatus = StageDragStatus.END;
1079
1084
  dragResizeHelper;
1080
1085
  getRenderDocument;
1086
+ delayedMarkContainer;
1087
+ markContainerEnd;
1081
1088
  constructor(config) {
1082
1089
  const moveableOptionsManagerConfig = {
1083
1090
  container: config.container,
@@ -1085,12 +1092,11 @@ class StageMultiDragResize extends MoveableOptionsManager {
1085
1092
  getRootContainer: config.getRootContainer
1086
1093
  };
1087
1094
  super(moveableOptionsManagerConfig);
1095
+ this.delayedMarkContainer = config.delayedMarkContainer;
1096
+ this.markContainerEnd = config.markContainerEnd;
1088
1097
  this.container = config.container;
1089
1098
  this.getRenderDocument = config.getRenderDocument;
1090
- this.dragResizeHelper = new DragResizeHelper({
1091
- container: config.container,
1092
- updateDragEl: config.updateDragEl
1093
- });
1099
+ this.dragResizeHelper = config.dragResizeHelper;
1094
1100
  this.on("update-moveable", () => {
1095
1101
  if (this.moveableForMulti) {
1096
1102
  this.updateMoveable();
@@ -1114,6 +1120,7 @@ class StageMultiDragResize extends MoveableOptionsManager {
1114
1120
  target: this.dragResizeHelper.getShadowEls()
1115
1121
  })
1116
1122
  );
1123
+ let timeout;
1117
1124
  this.moveableForMulti.on("resizeGroupStart", (e) => {
1118
1125
  this.dragResizeHelper.onResizeGroupStart(e);
1119
1126
  this.dragStatus = StageDragStatus.START;
@@ -1127,10 +1134,16 @@ class StageMultiDragResize extends MoveableOptionsManager {
1127
1134
  this.dragResizeHelper.onDragGroupStart(e);
1128
1135
  this.dragStatus = StageDragStatus.START;
1129
1136
  }).on("dragGroup", (e) => {
1137
+ if (timeout) {
1138
+ globalThis.clearTimeout(timeout);
1139
+ timeout = void 0;
1140
+ }
1141
+ timeout = this.delayedMarkContainer(e.inputEvent, this.targetList);
1130
1142
  this.dragResizeHelper.onDragGroup(e);
1131
1143
  this.dragStatus = StageDragStatus.ING;
1132
1144
  }).on("dragGroupEnd", () => {
1133
- this.update();
1145
+ const parentEl = this.markContainerEnd();
1146
+ this.update(false, parentEl);
1134
1147
  this.dragStatus = StageDragStatus.END;
1135
1148
  }).on("clickGroup", (e) => {
1136
1149
  const { inputTarget, targets } = e;
@@ -1182,23 +1195,20 @@ class StageMultiDragResize extends MoveableOptionsManager {
1182
1195
  this.moveableForMulti?.destroy();
1183
1196
  this.dragResizeHelper.destroy();
1184
1197
  }
1185
- update(isResize = false) {
1198
+ update(isResize = false, parentEl = null) {
1186
1199
  if (this.targetList.length === 0)
1187
1200
  return;
1188
1201
  const doc = this.getRenderDocument();
1189
1202
  if (!doc)
1190
1203
  return;
1191
1204
  const data = this.targetList.map((targetItem) => {
1192
- const left = calcValueByFontsize(doc, targetItem.offsetLeft);
1193
- const top = calcValueByFontsize(doc, targetItem.offsetTop);
1194
- const width = calcValueByFontsize(doc, targetItem.clientWidth);
1195
- const height = calcValueByFontsize(doc, targetItem.clientHeight);
1205
+ const rect = this.dragResizeHelper.getUpdatedElRect(targetItem, parentEl, doc);
1196
1206
  return {
1197
1207
  el: targetItem,
1198
- style: isResize ? { left, top, width, height } : { left, top }
1208
+ style: isResize ? rect : { left: rect.left, top: rect.top }
1199
1209
  };
1200
1210
  });
1201
- this.emit("update", data, null);
1211
+ this.emit("update", { data, parentEl });
1202
1212
  }
1203
1213
  }
1204
1214
 
@@ -1241,27 +1251,28 @@ class ActionManager extends EventEmitter {
1241
1251
  this.canSelect = config.canSelect || ((el) => !!el.id);
1242
1252
  this.getRenderDocument = config.getRenderDocument;
1243
1253
  this.isContainer = config.isContainer;
1254
+ const createDrHelper = () => new DragResizeHelper({
1255
+ container: config.container,
1256
+ updateDragEl: config.updateDragEl
1257
+ });
1244
1258
  this.dr = new StageDragResize({
1245
1259
  container: config.container,
1246
1260
  disabledDragStart: config.disabledDragStart,
1261
+ moveableOptions: this.changeCallback(config.moveableOptions),
1262
+ dragResizeHelper: createDrHelper(),
1247
1263
  getRootContainer: config.getRootContainer,
1248
1264
  getRenderDocument: config.getRenderDocument,
1249
- updateDragEl: config.updateDragEl,
1250
- markContainerEnd: () => this.markContainerEnd(),
1251
- delayedMarkContainer: (event, exclude) => {
1252
- if (this.canAddToContainer()) {
1253
- return this.delayedMarkContainer(event, exclude);
1254
- }
1255
- return void 0;
1256
- },
1257
- moveableOptions: this.changeCallback(config.moveableOptions)
1265
+ markContainerEnd: this.markContainerEnd.bind(this),
1266
+ delayedMarkContainer: this.delayedMarkContainer.bind(this)
1258
1267
  });
1259
1268
  this.multiDr = new StageMultiDragResize({
1260
1269
  container: config.container,
1261
1270
  multiMoveableOptions: config.multiMoveableOptions,
1271
+ dragResizeHelper: createDrHelper(),
1262
1272
  getRootContainer: config.getRootContainer,
1263
1273
  getRenderDocument: config.getRenderDocument,
1264
- updateDragEl: config.updateDragEl
1274
+ markContainerEnd: this.markContainerEnd.bind(this),
1275
+ delayedMarkContainer: this.delayedMarkContainer.bind(this)
1265
1276
  });
1266
1277
  this.highlightLayer = new StageHighlight({
1267
1278
  container: config.container,
@@ -1387,9 +1398,12 @@ class ActionManager extends EventEmitter {
1387
1398
  }
1388
1399
  }
1389
1400
  delayedMarkContainer(event, excludeElList = []) {
1390
- return globalThis.setTimeout(() => {
1391
- this.addContainerHighlightClassName(event, excludeElList);
1392
- }, this.containerHighlightDuration);
1401
+ if (this.canAddToContainer()) {
1402
+ return globalThis.setTimeout(() => {
1403
+ this.addContainerHighlightClassName(event, excludeElList);
1404
+ }, this.containerHighlightDuration);
1405
+ }
1406
+ return void 0;
1393
1407
  }
1394
1408
  destroy() {
1395
1409
  this.container.removeEventListener("mousedown", this.mouseDownHandler);
@@ -1485,8 +1499,8 @@ class ActionManager extends EventEmitter {
1485
1499
  };
1486
1500
  this.emit("remove", data);
1487
1501
  });
1488
- this.multiDr.on("update", (data, parentEl) => {
1489
- this.emit("multi-update", data, parentEl);
1502
+ this.multiDr.on("update", (data) => {
1503
+ this.emit("multi-update", data);
1490
1504
  }).on("change-to-select", async (id) => {
1491
1505
  if (this.isMultiSelectStatus)
1492
1506
  return false;
@@ -2195,8 +2209,8 @@ class StageCore extends EventEmitter$1 {
2195
2209
  this.actionManager.on("change-to-select", (el) => {
2196
2210
  this.select(el);
2197
2211
  setTimeout(() => this.emit("select", el));
2198
- }).on("multi-update", (data, parentEl) => {
2199
- this.emit("update", { data, parentEl });
2212
+ }).on("multi-update", (data) => {
2213
+ this.emit("update", data);
2200
2214
  });
2201
2215
  }
2202
2216
  initHighlightEvent() {