@tmagic/stage 1.1.0-beta.5 → 1.1.0-beta.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.
@@ -204,14 +204,22 @@ const up = (deltaTop, target) => {
204
204
  };
205
205
 
206
206
  class StageDragResize extends EventEmitter {
207
+ core;
208
+ mask;
209
+ container;
210
+ target;
211
+ dragEl;
212
+ moveable;
213
+ horizontalGuidelines = [];
214
+ verticalGuidelines = [];
215
+ elementGuidelines = [];
216
+ mode = Mode.ABSOLUTE;
217
+ moveableOptions = {};
218
+ dragStatus = "end" /* END */;
219
+ ghostEl;
220
+ moveableHelper;
207
221
  constructor(config) {
208
222
  super();
209
- this.horizontalGuidelines = [];
210
- this.verticalGuidelines = [];
211
- this.elementGuidelines = [];
212
- this.mode = Mode.ABSOLUTE;
213
- this.moveableOptions = {};
214
- this.dragStatus = "end" /* END */;
215
223
  this.core = config.core;
216
224
  this.container = config.container;
217
225
  this.mask = config.mask;
@@ -537,7 +545,7 @@ class StageDragResize extends EventEmitter {
537
545
  return ghostEl;
538
546
  }
539
547
  setGhostElChildrenId(el) {
540
- for (const child of el.children) {
548
+ for (const child of Array.from(el.children)) {
541
549
  if (child.id) {
542
550
  child.id = `${GHOST_EL_ID_PREFIX}${child.id}`;
543
551
  }
@@ -611,6 +619,11 @@ class StageDragResize extends EventEmitter {
611
619
  }
612
620
 
613
621
  class TargetCalibrate extends EventEmitter {
622
+ parent;
623
+ mask;
624
+ dr;
625
+ core;
626
+ operationEl;
614
627
  constructor(config) {
615
628
  super();
616
629
  this.parent = config.parent;
@@ -678,6 +691,11 @@ class TargetCalibrate extends EventEmitter {
678
691
  }
679
692
 
680
693
  class StageHighlight extends EventEmitter {
694
+ core;
695
+ container;
696
+ target;
697
+ moveable;
698
+ calibrationTarget;
681
699
  constructor(config) {
682
700
  super();
683
701
  this.core = config.core;
@@ -715,41 +733,14 @@ class StageHighlight extends EventEmitter {
715
733
  }
716
734
 
717
735
  class Rule extends EventEmitter$1 {
736
+ hGuides;
737
+ vGuides;
738
+ horizontalGuidelines = [];
739
+ verticalGuidelines = [];
740
+ container;
741
+ containerResizeObserver;
718
742
  constructor(container) {
719
743
  super();
720
- this.horizontalGuidelines = [];
721
- this.verticalGuidelines = [];
722
- this.getGuidesStyle = (type) => ({
723
- position: "fixed",
724
- zIndex: 1,
725
- left: type === GuidesType.HORIZONTAL ? 0 : "-30px",
726
- top: type === GuidesType.HORIZONTAL ? "-30px" : 0,
727
- width: type === GuidesType.HORIZONTAL ? "100%" : "30px",
728
- height: type === GuidesType.HORIZONTAL ? "30px" : "100%"
729
- });
730
- this.createGuides = (type, defaultGuides = []) => new Guides(this.container, {
731
- type,
732
- defaultGuides,
733
- displayDragPos: true,
734
- backgroundColor: "#fff",
735
- lineColor: "#000",
736
- textColor: "#000",
737
- style: this.getGuidesStyle(type)
738
- });
739
- this.hGuidesChangeGuidesHandler = (e) => {
740
- this.horizontalGuidelines = e.guides;
741
- this.emit("changeGuides", {
742
- type: GuidesType.HORIZONTAL,
743
- guides: this.horizontalGuidelines
744
- });
745
- };
746
- this.vGuidesChangeGuidesHandler = (e) => {
747
- this.verticalGuidelines = e.guides;
748
- this.emit("changeGuides", {
749
- type: GuidesType.VERTICAL,
750
- guides: this.verticalGuidelines
751
- });
752
- };
753
744
  this.container = container;
754
745
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
755
746
  this.vGuides = this.createGuides(GuidesType.VERTICAL, this.verticalGuidelines);
@@ -821,6 +812,37 @@ class Rule extends EventEmitter$1 {
821
812
  this.containerResizeObserver.disconnect();
822
813
  this.removeAllListeners();
823
814
  }
815
+ getGuidesStyle = (type) => ({
816
+ position: "fixed",
817
+ zIndex: 1,
818
+ left: type === GuidesType.HORIZONTAL ? 0 : "-30px",
819
+ top: type === GuidesType.HORIZONTAL ? "-30px" : 0,
820
+ width: type === GuidesType.HORIZONTAL ? "100%" : "30px",
821
+ height: type === GuidesType.HORIZONTAL ? "30px" : "100%"
822
+ });
823
+ createGuides = (type, defaultGuides = []) => new Guides(this.container, {
824
+ type,
825
+ defaultGuides,
826
+ displayDragPos: true,
827
+ backgroundColor: "#fff",
828
+ lineColor: "#000",
829
+ textColor: "#000",
830
+ style: this.getGuidesStyle(type)
831
+ });
832
+ hGuidesChangeGuidesHandler = (e) => {
833
+ this.horizontalGuidelines = e.guides;
834
+ this.emit("changeGuides", {
835
+ type: GuidesType.HORIZONTAL,
836
+ guides: this.horizontalGuidelines
837
+ });
838
+ };
839
+ vGuidesChangeGuidesHandler = (e) => {
840
+ this.verticalGuidelines = e.guides;
841
+ this.emit("changeGuides", {
842
+ type: GuidesType.VERTICAL,
843
+ guides: this.verticalGuidelines
844
+ });
845
+ };
824
846
  }
825
847
 
826
848
  const wrapperClassName = "editor-mask-wrapper";
@@ -854,74 +876,30 @@ const createWrapper = () => {
854
876
  return el;
855
877
  };
856
878
  class StageMask extends Rule {
879
+ content = createContent();
880
+ wrapper;
881
+ core;
882
+ page = null;
883
+ pageScrollParent = null;
884
+ scrollTop = 0;
885
+ scrollLeft = 0;
886
+ width = 0;
887
+ height = 0;
888
+ wrapperHeight = 0;
889
+ wrapperWidth = 0;
890
+ maxScrollTop = 0;
891
+ maxScrollLeft = 0;
892
+ intersectionObserver = null;
893
+ isMultiSelectStatus = false;
894
+ mode = Mode.ABSOLUTE;
895
+ pageResizeObserver = null;
896
+ wrapperResizeObserver = null;
897
+ highlightHandler = throttle((event) => {
898
+ this.emit("highlight", event);
899
+ }, throttleTime);
857
900
  constructor(config) {
858
901
  const wrapper = createWrapper();
859
902
  super(wrapper);
860
- this.content = createContent();
861
- this.page = null;
862
- this.pageScrollParent = null;
863
- this.scrollTop = 0;
864
- this.scrollLeft = 0;
865
- this.width = 0;
866
- this.height = 0;
867
- this.wrapperHeight = 0;
868
- this.wrapperWidth = 0;
869
- this.maxScrollTop = 0;
870
- this.maxScrollLeft = 0;
871
- this.intersectionObserver = null;
872
- this.isMultiSelectStatus = false;
873
- this.mode = Mode.ABSOLUTE;
874
- this.pageResizeObserver = null;
875
- this.wrapperResizeObserver = null;
876
- this.highlightHandler = throttle((event) => {
877
- this.emit("highlight", event);
878
- }, throttleTime);
879
- this.mouseDownHandler = (event) => {
880
- this.emit("clearHighlight");
881
- event.stopImmediatePropagation();
882
- event.stopPropagation();
883
- if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT)
884
- return;
885
- if (!this.isMultiSelectStatus && event.target.className.indexOf("moveable-area") !== -1) {
886
- return;
887
- }
888
- if (event.target.className.indexOf("moveable-control") !== -1) {
889
- return;
890
- }
891
- this.content.removeEventListener("mousemove", this.highlightHandler);
892
- if (this.isMultiSelectStatus) {
893
- this.emit("beforeMultiSelect", event);
894
- } else {
895
- this.emit("beforeSelect", event);
896
- }
897
- globalThis.document.addEventListener("mouseup", this.mouseUpHandler);
898
- };
899
- this.mouseUpHandler = () => {
900
- globalThis.document.removeEventListener("mouseup", this.mouseUpHandler);
901
- this.content.addEventListener("mousemove", this.highlightHandler);
902
- this.emit("select");
903
- };
904
- this.mouseWheelHandler = (event) => {
905
- this.emit("clearHighlight");
906
- if (!this.page)
907
- throw new Error("page \u672A\u521D\u59CB\u5316");
908
- const { deltaY, deltaX } = event;
909
- if (this.page.clientHeight < this.wrapperHeight && deltaY)
910
- return;
911
- if (this.page.clientWidth < this.wrapperWidth && deltaX)
912
- return;
913
- if (this.maxScrollTop > 0) {
914
- this.scrollTop = this.scrollTop + deltaY;
915
- }
916
- if (this.maxScrollLeft > 0) {
917
- this.scrollLeft = this.scrollLeft + deltaX;
918
- }
919
- this.scroll();
920
- this.emit("scroll", event);
921
- };
922
- this.mouseLeaveHandler = () => {
923
- setTimeout(() => this.emit("clearHighlight"), throttleTime);
924
- };
925
903
  this.wrapper = wrapper;
926
904
  this.core = config.core;
927
905
  this.content.addEventListener("mousedown", this.mouseDownHandler);
@@ -929,11 +907,13 @@ class StageMask extends Rule {
929
907
  this.content.addEventListener("wheel", this.mouseWheelHandler);
930
908
  this.content.addEventListener("mousemove", this.highlightHandler);
931
909
  this.content.addEventListener("mouseleave", this.mouseLeaveHandler);
932
- KeyController.global.keydown("shift", (e) => {
910
+ const isMac = /mac os x/.test(navigator.userAgent.toLowerCase());
911
+ const ctrl = isMac ? "meta" : "ctrl";
912
+ KeyController.global.keydown(ctrl, (e) => {
933
913
  e.inputEvent.preventDefault();
934
914
  this.isMultiSelectStatus = true;
935
915
  });
936
- KeyController.global.keyup("shift", (e) => {
916
+ KeyController.global.keyup(ctrl, (e) => {
937
917
  e.inputEvent.preventDefault();
938
918
  this.isMultiSelectStatus = false;
939
919
  });
@@ -1065,13 +1045,66 @@ class StageMask extends Rule {
1065
1045
  if (this.maxScrollLeft < this.scrollLeft)
1066
1046
  this.scrollLeft = this.maxScrollLeft;
1067
1047
  }
1048
+ mouseDownHandler = (event) => {
1049
+ this.emit("clearHighlight");
1050
+ event.stopImmediatePropagation();
1051
+ event.stopPropagation();
1052
+ if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT)
1053
+ return;
1054
+ if (!this.isMultiSelectStatus && event.target.className.indexOf("moveable-area") !== -1) {
1055
+ return;
1056
+ }
1057
+ if (event.target.className.indexOf("moveable-control") !== -1) {
1058
+ return;
1059
+ }
1060
+ this.content.removeEventListener("mousemove", this.highlightHandler);
1061
+ if (this.isMultiSelectStatus) {
1062
+ this.emit("beforeMultiSelect", event);
1063
+ } else {
1064
+ this.emit("beforeSelect", event);
1065
+ }
1066
+ globalThis.document.addEventListener("mouseup", this.mouseUpHandler);
1067
+ };
1068
+ mouseUpHandler = () => {
1069
+ globalThis.document.removeEventListener("mouseup", this.mouseUpHandler);
1070
+ this.content.addEventListener("mousemove", this.highlightHandler);
1071
+ if (!this.isMultiSelectStatus) {
1072
+ this.emit("select");
1073
+ }
1074
+ };
1075
+ mouseWheelHandler = (event) => {
1076
+ this.emit("clearHighlight");
1077
+ if (!this.page)
1078
+ throw new Error("page \u672A\u521D\u59CB\u5316");
1079
+ const { deltaY, deltaX } = event;
1080
+ if (this.page.clientHeight < this.wrapperHeight && deltaY)
1081
+ return;
1082
+ if (this.page.clientWidth < this.wrapperWidth && deltaX)
1083
+ return;
1084
+ if (this.maxScrollTop > 0) {
1085
+ this.scrollTop = this.scrollTop + deltaY;
1086
+ }
1087
+ if (this.maxScrollLeft > 0) {
1088
+ this.scrollLeft = this.scrollLeft + deltaX;
1089
+ }
1090
+ this.scroll();
1091
+ this.emit("scroll", event);
1092
+ };
1093
+ mouseLeaveHandler = () => {
1094
+ setTimeout(() => this.emit("clearHighlight"), throttleTime);
1095
+ };
1068
1096
  }
1069
1097
 
1070
1098
  class StageMultiDragResize extends EventEmitter {
1099
+ core;
1100
+ mask;
1101
+ container;
1102
+ targetList = [];
1103
+ dragElList = [];
1104
+ moveableForMulti;
1105
+ multiMoveableHelper;
1071
1106
  constructor(config) {
1072
1107
  super();
1073
- this.targetList = [];
1074
- this.dragElList = [];
1075
1108
  this.core = config.core;
1076
1109
  this.container = config.container;
1077
1110
  this.mask = config.mask;
@@ -1134,14 +1167,18 @@ class StageMultiDragResize extends EventEmitter {
1134
1167
  this.update();
1135
1168
  });
1136
1169
  }
1137
- canSelect(el) {
1170
+ canSelect(el, stop) {
1138
1171
  if (el.className.includes(PAGE_CLASS)) {
1139
1172
  this.core.highlightedDom = void 0;
1140
1173
  this.core.highlightLayer.clearHighlight();
1174
+ stop();
1141
1175
  return false;
1142
1176
  }
1143
1177
  const currentTargetMode = getMode(el);
1144
1178
  let selectedDomMode = "";
1179
+ if (this.core.selectedDom?.className.includes(PAGE_CLASS)) {
1180
+ return true;
1181
+ }
1145
1182
  if (this.targetList.length === 0 && this.core.selectedDom) {
1146
1183
  selectedDomMode = getMode(this.core.selectedDom);
1147
1184
  } else if (this.targetList.length > 0) {
@@ -1187,9 +1224,9 @@ class StageMultiDragResize extends EventEmitter {
1187
1224
  });
1188
1225
  }
1189
1226
  getOptions(options = {}) {
1190
- let { moveableOptions = {} } = this.core.config;
1191
- if (typeof moveableOptions === "function") {
1192
- moveableOptions = moveableOptions(this.core);
1227
+ let { multiMoveableOptions = {} } = this.core.config;
1228
+ if (typeof multiMoveableOptions === "function") {
1229
+ multiMoveableOptions = multiMoveableOptions(this.core);
1193
1230
  }
1194
1231
  return {
1195
1232
  defaultGroupRotate: 0,
@@ -1203,52 +1240,22 @@ class StageMultiDragResize extends EventEmitter {
1203
1240
  origin: true,
1204
1241
  padding: { left: 0, top: 0, right: 0, bottom: 0 },
1205
1242
  ...options,
1206
- ...moveableOptions
1243
+ ...multiMoveableOptions
1207
1244
  };
1208
1245
  }
1209
1246
  }
1210
1247
 
1211
- var style = ".tmagic-stage-container-highlight::after {\n content: '';\n position: absolute;\n width: 100%;\n height: 100%;\n top: 0;\n left: 0;\n background-color: #000;\n opacity: .1;\n pointer-events: none;\n}\n";
1248
+ const style = ".tmagic-stage-container-highlight::after {\n content: '';\n position: absolute;\n width: 100%;\n height: 100%;\n top: 0;\n left: 0;\n background-color: #000;\n opacity: .1;\n pointer-events: none;\n}\n";
1212
1249
 
1213
1250
  class StageRender extends EventEmitter {
1251
+ contentWindow = null;
1252
+ runtime = null;
1253
+ iframe;
1254
+ runtimeUrl;
1255
+ core;
1256
+ render;
1214
1257
  constructor({ core }) {
1215
1258
  super();
1216
- this.contentWindow = null;
1217
- this.runtime = null;
1218
- this.getMagicApi = () => ({
1219
- onPageElUpdate: (el) => this.emit("page-el-update", el),
1220
- onRuntimeReady: (runtime) => {
1221
- this.runtime = runtime;
1222
- globalThis.runtime = runtime;
1223
- this.emit("runtime-ready", runtime);
1224
- }
1225
- });
1226
- this.getRuntime = () => {
1227
- if (this.runtime)
1228
- return Promise.resolve(this.runtime);
1229
- return new Promise((resolve) => {
1230
- const listener = (runtime) => {
1231
- this.off("runtime-ready", listener);
1232
- resolve(runtime);
1233
- };
1234
- this.on("runtime-ready", listener);
1235
- });
1236
- };
1237
- this.loadHandler = async () => {
1238
- if (!this.contentWindow?.magic) {
1239
- this.postTmagicRuntimeReady();
1240
- }
1241
- if (!this.contentWindow)
1242
- return;
1243
- if (this.render) {
1244
- const el = await this.render(this.core);
1245
- if (el) {
1246
- this.contentWindow.document?.body?.appendChild(el);
1247
- }
1248
- }
1249
- this.emit("onload");
1250
- injectStyle(this.contentWindow.document, style);
1251
- };
1252
1259
  this.core = core;
1253
1260
  this.runtimeUrl = core.config.runtimeUrl || "";
1254
1261
  this.render = core.config.render;
@@ -1261,6 +1268,14 @@ class StageRender extends EventEmitter {
1261
1268
  `;
1262
1269
  this.iframe.addEventListener("load", this.loadHandler);
1263
1270
  }
1271
+ getMagicApi = () => ({
1272
+ onPageElUpdate: (el) => this.emit("page-el-update", el),
1273
+ onRuntimeReady: (runtime) => {
1274
+ this.runtime = runtime;
1275
+ globalThis.runtime = runtime;
1276
+ this.emit("runtime-ready", runtime);
1277
+ }
1278
+ });
1264
1279
  async mount(el) {
1265
1280
  if (!this.iframe) {
1266
1281
  throw Error("mount \u5931\u8D25");
@@ -1275,6 +1290,17 @@ class StageRender extends EventEmitter {
1275
1290
  el.appendChild(this.iframe);
1276
1291
  this.postTmagicRuntimeReady();
1277
1292
  }
1293
+ getRuntime = () => {
1294
+ if (this.runtime)
1295
+ return Promise.resolve(this.runtime);
1296
+ return new Promise((resolve) => {
1297
+ const listener = (runtime) => {
1298
+ this.off("runtime-ready", listener);
1299
+ resolve(runtime);
1300
+ };
1301
+ this.on("runtime-ready", listener);
1302
+ });
1303
+ };
1278
1304
  destroy() {
1279
1305
  this.iframe?.removeEventListener("load", this.loadHandler);
1280
1306
  this.contentWindow = null;
@@ -1282,6 +1308,21 @@ class StageRender extends EventEmitter {
1282
1308
  this.iframe = void 0;
1283
1309
  this.removeAllListeners();
1284
1310
  }
1311
+ loadHandler = async () => {
1312
+ if (!this.contentWindow?.magic) {
1313
+ this.postTmagicRuntimeReady();
1314
+ }
1315
+ if (!this.contentWindow)
1316
+ return;
1317
+ if (this.render) {
1318
+ const el = await this.render(this.core);
1319
+ if (el) {
1320
+ this.contentWindow.document?.body?.appendChild(el);
1321
+ }
1322
+ }
1323
+ this.emit("onload");
1324
+ injectStyle(this.contentWindow.document, style);
1325
+ };
1285
1326
  postTmagicRuntimeReady() {
1286
1327
  this.contentWindow = this.iframe?.contentWindow;
1287
1328
  this.contentWindow.magic = this.getMagicApi();
@@ -1292,10 +1333,23 @@ class StageRender extends EventEmitter {
1292
1333
  }
1293
1334
 
1294
1335
  class StageCore extends EventEmitter {
1336
+ container;
1337
+ selectedDom;
1338
+ selectedDomList = [];
1339
+ highlightedDom;
1340
+ renderer;
1341
+ mask;
1342
+ dr;
1343
+ multiDr;
1344
+ highlightLayer;
1345
+ config;
1346
+ zoom = DEFAULT_ZOOM;
1347
+ containerHighlightClassName;
1348
+ containerHighlightDuration;
1349
+ isContainer;
1350
+ canSelect;
1295
1351
  constructor(config) {
1296
1352
  super();
1297
- this.selectedDomList = [];
1298
- this.zoom = DEFAULT_ZOOM;
1299
1353
  this.config = config;
1300
1354
  this.setZoom(config.zoom);
1301
1355
  this.canSelect = config.canSelect || ((el) => !!el.id);
@@ -1340,7 +1394,6 @@ class StageCore extends EventEmitter {
1340
1394
  const el = await this.getElementFromPoint(event);
1341
1395
  if (!el)
1342
1396
  return;
1343
- this.clearSelectStatus("select");
1344
1397
  if (this.selectedDom && !this.selectedDom.className.includes(PAGE_CLASS)) {
1345
1398
  this.selectedDomList.push(this.selectedDom);
1346
1399
  this.selectedDom = void 0;
@@ -1351,8 +1404,7 @@ class StageCore extends EventEmitter {
1351
1404
  } else {
1352
1405
  this.selectedDomList.push(el);
1353
1406
  }
1354
- this.multiDr.multiSelect(this.selectedDomList);
1355
- this.emit("multiSelect", this.selectedDomList);
1407
+ this.multiSelect(this.selectedDomList);
1356
1408
  });
1357
1409
  this.dr.on("update", (data) => {
1358
1410
  setTimeout(() => this.emit("update", data));
@@ -1394,7 +1446,7 @@ class StageCore extends EventEmitter {
1394
1446
  if (!canSelectByProp)
1395
1447
  return false;
1396
1448
  if (this.mask.isMultiSelectStatus) {
1397
- return this.multiDr.canSelect(el);
1449
+ return this.multiDr.canSelect(el, stop);
1398
1450
  }
1399
1451
  return true;
1400
1452
  }
@@ -1421,6 +1473,12 @@ class StageCore extends EventEmitter {
1421
1473
  }
1422
1474
  }
1423
1475
  }
1476
+ async multiSelect(idOrElList) {
1477
+ this.clearSelectStatus("select");
1478
+ const elList = await Promise.all(idOrElList.map(async (idOrEl) => await this.getTargetElement(idOrEl)));
1479
+ this.multiDr.multiSelect(elList);
1480
+ this.emit("multiSelect", elList);
1481
+ }
1424
1482
  update(data) {
1425
1483
  const { config } = data;
1426
1484
  return this.renderer?.getRuntime().then((runtime) => {
@@ -1518,4 +1576,4 @@ class StageCore extends EventEmitter {
1518
1576
  }
1519
1577
 
1520
1578
  export { CONTAINER_HIGHLIGHT_CLASS, DEFAULT_ZOOM, DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, GuidesType, HIGHLIGHT_EL_ID_PREFIX, Mode, MouseButton, PAGE_CLASS, SELECTED_CLASS, StageDragResize, StageMask, StageRender, ZIndex, addSelectedClassName, calcValueByFontsize, StageCore as default, down, getAbsolutePosition, getMode, getOffset, getScrollParent, getTargetElStyle, isAbsolute, isFixed, isFixedParent, isRelative, isStatic, removeSelectedClassName, up };
1521
- //# sourceMappingURL=tmagic-stage.es.js.map
1579
+ //# sourceMappingURL=tmagic-stage.mjs.map