@tmagic/stage 1.1.0-beta.5 → 1.1.0-beta.6

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);
@@ -1065,13 +1043,66 @@ class StageMask extends Rule {
1065
1043
  if (this.maxScrollLeft < this.scrollLeft)
1066
1044
  this.scrollLeft = this.maxScrollLeft;
1067
1045
  }
1046
+ mouseDownHandler = (event) => {
1047
+ this.emit("clearHighlight");
1048
+ event.stopImmediatePropagation();
1049
+ event.stopPropagation();
1050
+ if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT)
1051
+ return;
1052
+ if (!this.isMultiSelectStatus && event.target.className.indexOf("moveable-area") !== -1) {
1053
+ return;
1054
+ }
1055
+ if (event.target.className.indexOf("moveable-control") !== -1) {
1056
+ return;
1057
+ }
1058
+ this.content.removeEventListener("mousemove", this.highlightHandler);
1059
+ if (this.isMultiSelectStatus) {
1060
+ this.emit("beforeMultiSelect", event);
1061
+ } else {
1062
+ this.emit("beforeSelect", event);
1063
+ }
1064
+ globalThis.document.addEventListener("mouseup", this.mouseUpHandler);
1065
+ };
1066
+ mouseUpHandler = () => {
1067
+ globalThis.document.removeEventListener("mouseup", this.mouseUpHandler);
1068
+ this.content.addEventListener("mousemove", this.highlightHandler);
1069
+ if (!this.isMultiSelectStatus) {
1070
+ this.emit("select");
1071
+ }
1072
+ };
1073
+ mouseWheelHandler = (event) => {
1074
+ this.emit("clearHighlight");
1075
+ if (!this.page)
1076
+ throw new Error("page \u672A\u521D\u59CB\u5316");
1077
+ const { deltaY, deltaX } = event;
1078
+ if (this.page.clientHeight < this.wrapperHeight && deltaY)
1079
+ return;
1080
+ if (this.page.clientWidth < this.wrapperWidth && deltaX)
1081
+ return;
1082
+ if (this.maxScrollTop > 0) {
1083
+ this.scrollTop = this.scrollTop + deltaY;
1084
+ }
1085
+ if (this.maxScrollLeft > 0) {
1086
+ this.scrollLeft = this.scrollLeft + deltaX;
1087
+ }
1088
+ this.scroll();
1089
+ this.emit("scroll", event);
1090
+ };
1091
+ mouseLeaveHandler = () => {
1092
+ setTimeout(() => this.emit("clearHighlight"), throttleTime);
1093
+ };
1068
1094
  }
1069
1095
 
1070
1096
  class StageMultiDragResize extends EventEmitter {
1097
+ core;
1098
+ mask;
1099
+ container;
1100
+ targetList = [];
1101
+ dragElList = [];
1102
+ moveableForMulti;
1103
+ multiMoveableHelper;
1071
1104
  constructor(config) {
1072
1105
  super();
1073
- this.targetList = [];
1074
- this.dragElList = [];
1075
1106
  this.core = config.core;
1076
1107
  this.container = config.container;
1077
1108
  this.mask = config.mask;
@@ -1187,9 +1218,9 @@ class StageMultiDragResize extends EventEmitter {
1187
1218
  });
1188
1219
  }
1189
1220
  getOptions(options = {}) {
1190
- let { moveableOptions = {} } = this.core.config;
1191
- if (typeof moveableOptions === "function") {
1192
- moveableOptions = moveableOptions(this.core);
1221
+ let { multiMoveableOptions = {} } = this.core.config;
1222
+ if (typeof multiMoveableOptions === "function") {
1223
+ multiMoveableOptions = multiMoveableOptions(this.core);
1193
1224
  }
1194
1225
  return {
1195
1226
  defaultGroupRotate: 0,
@@ -1203,52 +1234,22 @@ class StageMultiDragResize extends EventEmitter {
1203
1234
  origin: true,
1204
1235
  padding: { left: 0, top: 0, right: 0, bottom: 0 },
1205
1236
  ...options,
1206
- ...moveableOptions
1237
+ ...multiMoveableOptions
1207
1238
  };
1208
1239
  }
1209
1240
  }
1210
1241
 
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";
1242
+ 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
1243
 
1213
1244
  class StageRender extends EventEmitter {
1245
+ contentWindow = null;
1246
+ runtime = null;
1247
+ iframe;
1248
+ runtimeUrl;
1249
+ core;
1250
+ render;
1214
1251
  constructor({ core }) {
1215
1252
  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
1253
  this.core = core;
1253
1254
  this.runtimeUrl = core.config.runtimeUrl || "";
1254
1255
  this.render = core.config.render;
@@ -1261,6 +1262,14 @@ class StageRender extends EventEmitter {
1261
1262
  `;
1262
1263
  this.iframe.addEventListener("load", this.loadHandler);
1263
1264
  }
1265
+ getMagicApi = () => ({
1266
+ onPageElUpdate: (el) => this.emit("page-el-update", el),
1267
+ onRuntimeReady: (runtime) => {
1268
+ this.runtime = runtime;
1269
+ globalThis.runtime = runtime;
1270
+ this.emit("runtime-ready", runtime);
1271
+ }
1272
+ });
1264
1273
  async mount(el) {
1265
1274
  if (!this.iframe) {
1266
1275
  throw Error("mount \u5931\u8D25");
@@ -1275,6 +1284,17 @@ class StageRender extends EventEmitter {
1275
1284
  el.appendChild(this.iframe);
1276
1285
  this.postTmagicRuntimeReady();
1277
1286
  }
1287
+ getRuntime = () => {
1288
+ if (this.runtime)
1289
+ return Promise.resolve(this.runtime);
1290
+ return new Promise((resolve) => {
1291
+ const listener = (runtime) => {
1292
+ this.off("runtime-ready", listener);
1293
+ resolve(runtime);
1294
+ };
1295
+ this.on("runtime-ready", listener);
1296
+ });
1297
+ };
1278
1298
  destroy() {
1279
1299
  this.iframe?.removeEventListener("load", this.loadHandler);
1280
1300
  this.contentWindow = null;
@@ -1282,6 +1302,21 @@ class StageRender extends EventEmitter {
1282
1302
  this.iframe = void 0;
1283
1303
  this.removeAllListeners();
1284
1304
  }
1305
+ loadHandler = async () => {
1306
+ if (!this.contentWindow?.magic) {
1307
+ this.postTmagicRuntimeReady();
1308
+ }
1309
+ if (!this.contentWindow)
1310
+ return;
1311
+ if (this.render) {
1312
+ const el = await this.render(this.core);
1313
+ if (el) {
1314
+ this.contentWindow.document?.body?.appendChild(el);
1315
+ }
1316
+ }
1317
+ this.emit("onload");
1318
+ injectStyle(this.contentWindow.document, style);
1319
+ };
1285
1320
  postTmagicRuntimeReady() {
1286
1321
  this.contentWindow = this.iframe?.contentWindow;
1287
1322
  this.contentWindow.magic = this.getMagicApi();
@@ -1292,10 +1327,23 @@ class StageRender extends EventEmitter {
1292
1327
  }
1293
1328
 
1294
1329
  class StageCore extends EventEmitter {
1330
+ container;
1331
+ selectedDom;
1332
+ selectedDomList = [];
1333
+ highlightedDom;
1334
+ renderer;
1335
+ mask;
1336
+ dr;
1337
+ multiDr;
1338
+ highlightLayer;
1339
+ config;
1340
+ zoom = DEFAULT_ZOOM;
1341
+ containerHighlightClassName;
1342
+ containerHighlightDuration;
1343
+ isContainer;
1344
+ canSelect;
1295
1345
  constructor(config) {
1296
1346
  super();
1297
- this.selectedDomList = [];
1298
- this.zoom = DEFAULT_ZOOM;
1299
1347
  this.config = config;
1300
1348
  this.setZoom(config.zoom);
1301
1349
  this.canSelect = config.canSelect || ((el) => !!el.id);
@@ -1340,7 +1388,6 @@ class StageCore extends EventEmitter {
1340
1388
  const el = await this.getElementFromPoint(event);
1341
1389
  if (!el)
1342
1390
  return;
1343
- this.clearSelectStatus("select");
1344
1391
  if (this.selectedDom && !this.selectedDom.className.includes(PAGE_CLASS)) {
1345
1392
  this.selectedDomList.push(this.selectedDom);
1346
1393
  this.selectedDom = void 0;
@@ -1351,8 +1398,7 @@ class StageCore extends EventEmitter {
1351
1398
  } else {
1352
1399
  this.selectedDomList.push(el);
1353
1400
  }
1354
- this.multiDr.multiSelect(this.selectedDomList);
1355
- this.emit("multiSelect", this.selectedDomList);
1401
+ this.multiSelect(this.selectedDomList);
1356
1402
  });
1357
1403
  this.dr.on("update", (data) => {
1358
1404
  setTimeout(() => this.emit("update", data));
@@ -1421,6 +1467,12 @@ class StageCore extends EventEmitter {
1421
1467
  }
1422
1468
  }
1423
1469
  }
1470
+ async multiSelect(idOrElList) {
1471
+ this.clearSelectStatus("select");
1472
+ const elList = await Promise.all(idOrElList.map(async (idOrEl) => await this.getTargetElement(idOrEl)));
1473
+ this.multiDr.multiSelect(elList);
1474
+ this.emit("multiSelect", elList);
1475
+ }
1424
1476
  update(data) {
1425
1477
  const { config } = data;
1426
1478
  return this.renderer?.getRuntime().then((runtime) => {
@@ -1518,4 +1570,4 @@ class StageCore extends EventEmitter {
1518
1570
  }
1519
1571
 
1520
1572
  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
1573
+ //# sourceMappingURL=tmagic-stage.mjs.map