@tmagic/stage 1.0.0-rc.7 → 1.0.0

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.
@@ -33,8 +33,6 @@ var Mode = /* @__PURE__ */ ((Mode2) => {
33
33
  return Mode2;
34
34
  })(Mode || {});
35
35
  const SELECTED_CLASS = "tmagic-stage-selected-area";
36
- const H_GUIDE_LINE_STORAGE_KEY = "$MagicStageHorizontalGuidelinesData";
37
- const V_GUIDE_LINE_STORAGE_KEY = "$MagicStageVerticalGuidelinesData";
38
36
 
39
37
  const getParents = (el, relative) => {
40
38
  let cur = el.parentElement;
@@ -79,31 +77,15 @@ const isSameDomain = (targetUrl = "", source = globalThis.location.host) => {
79
77
  return true;
80
78
  return getHost(targetUrl) === source;
81
79
  };
82
- const isAbsolute = (el) => {
83
- if (!el)
84
- return false;
85
- return getComputedStyle(el).position === "absolute";
86
- };
87
- const isRelative = (el) => {
88
- if (!el)
89
- return false;
90
- return getComputedStyle(el).position === "relative";
91
- };
92
- const isStatic = (el) => {
93
- if (!el)
94
- return false;
95
- return getComputedStyle(el).position === "static";
96
- };
97
- const isFixed = (el) => {
98
- if (!el)
99
- return false;
100
- return getComputedStyle(el).position === "fixed";
101
- };
80
+ const isAbsolute = (style) => style.position === "absolute";
81
+ const isRelative = (style) => style.position === "relative";
82
+ const isStatic = (style) => style.position === "static";
83
+ const isFixed = (style) => style.position === "fixed";
102
84
  const isFixedParent = (el) => {
103
85
  let fixed = false;
104
86
  let dom = el;
105
87
  while (dom) {
106
- fixed = isFixed(dom);
88
+ fixed = isFixed(getComputedStyle(dom));
107
89
  if (fixed) {
108
90
  break;
109
91
  }
@@ -118,21 +100,21 @@ const isFixedParent = (el) => {
118
100
  const getMode = (el) => {
119
101
  if (isFixedParent(el))
120
102
  return Mode.FIXED;
121
- if (isStatic(el) || isRelative(el))
103
+ const style = getComputedStyle(el);
104
+ if (isStatic(style) || isRelative(style))
122
105
  return Mode.SORTABLE;
123
106
  return Mode.ABSOLUTE;
124
107
  };
125
108
  const getScrollParent = (element, includeHidden = false) => {
126
109
  let style = getComputedStyle(element);
127
110
  const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
128
- if (isFixed(element))
111
+ if (isFixed(style))
129
112
  return null;
130
113
  for (let parent = element; parent.parentElement; ) {
131
114
  parent = parent.parentElement;
132
115
  style = getComputedStyle(parent);
133
- if (isAbsolute(element) && isStatic(element)) {
116
+ if (isAbsolute(style) && isStatic(style))
134
117
  continue;
135
- }
136
118
  if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX))
137
119
  return parent;
138
120
  }
@@ -161,29 +143,12 @@ const addSelectedClassName = (el, doc) => {
161
143
  item.classList.add(`${SELECTED_CLASS}-parents`);
162
144
  });
163
145
  };
164
- const getGuideLineFromCache = (type) => {
165
- const key = {
166
- [GuidesType.HORIZONTAL]: H_GUIDE_LINE_STORAGE_KEY,
167
- [GuidesType.VERTICAL]: V_GUIDE_LINE_STORAGE_KEY
168
- }[type];
169
- if (!key)
170
- return [];
171
- const guideLineCacheData = globalThis.localStorage.getItem(key);
172
- if (guideLineCacheData) {
173
- try {
174
- return JSON.parse(guideLineCacheData) || [];
175
- } catch (e) {
176
- console.error(e);
177
- }
178
- }
179
- return [];
180
- };
181
146
 
182
147
  class StageDragResize extends EventEmitter {
183
148
  constructor(config) {
184
149
  super();
185
- this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
186
- this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
150
+ this.horizontalGuidelines = [];
151
+ this.verticalGuidelines = [];
187
152
  this.elementGuidelines = [];
188
153
  this.mode = Mode.ABSOLUTE;
189
154
  this.moveableOptions = {};
@@ -196,8 +161,8 @@ class StageDragResize extends EventEmitter {
196
161
  select(el, event) {
197
162
  const oldTarget = this.target;
198
163
  this.target = el;
199
- this.init(el);
200
164
  if (!this.moveable || this.target !== oldTarget) {
165
+ this.init(el);
201
166
  this.moveableHelper = MoveableHelper.create({
202
167
  useBeforeRender: true,
203
168
  useRender: false,
@@ -215,7 +180,7 @@ class StageDragResize extends EventEmitter {
215
180
  if (!this.moveable)
216
181
  throw new Error("\u672A\u521D\u59CB\u5316moveable");
217
182
  if (!el)
218
- throw new Error("\u4E3A\u9009\u4E2D\u4EFB\u4F55\u8282\u70B9");
183
+ throw new Error("\u672A\u9009\u4E2D\u4EFB\u4F55\u8282\u70B9");
219
184
  this.target = el;
220
185
  this.init(el);
221
186
  Object.entries(this.moveableOptions).forEach(([key, value]) => {
@@ -231,7 +196,9 @@ class StageDragResize extends EventEmitter {
231
196
  this.verticalGuidelines = guidelines;
232
197
  this.moveableOptions.verticalGuidelines = guidelines;
233
198
  }
234
- this.updateMoveable();
199
+ if (this.moveable) {
200
+ this.updateMoveable();
201
+ }
235
202
  }
236
203
  clearGuides() {
237
204
  this.horizontalGuidelines = [];
@@ -290,7 +257,7 @@ class StageDragResize extends EventEmitter {
290
257
  }
291
258
  bindResizeEvent() {
292
259
  if (!this.moveable)
293
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
260
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
294
261
  const frame = {
295
262
  left: 0,
296
263
  top: 0
@@ -309,10 +276,14 @@ class StageDragResize extends EventEmitter {
309
276
  const { beforeTranslate } = drag;
310
277
  this.dragStatus = "ing" /* ING */;
311
278
  this.moveableHelper?.onResize(e);
279
+ if (this.mode === Mode.SORTABLE) {
280
+ this.target.style.top = "0px";
281
+ } else {
282
+ this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
283
+ this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
284
+ }
312
285
  this.target.style.width = `${width}px`;
313
286
  this.target.style.height = `${height}px`;
314
- this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
315
- this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
316
287
  }).on("resizeEnd", () => {
317
288
  this.dragStatus = "end" /* END */;
318
289
  this.update(true);
@@ -320,8 +291,11 @@ class StageDragResize extends EventEmitter {
320
291
  }
321
292
  bindDragEvent() {
322
293
  if (!this.moveable)
323
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
324
- let offset = null;
294
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
295
+ const frame = {
296
+ left: 0,
297
+ top: 0
298
+ };
325
299
  this.moveable.on("dragStart", (e) => {
326
300
  if (!this.target)
327
301
  throw new Error("\u672A\u9009\u4E2D\u7EC4\u4EF6");
@@ -330,22 +304,19 @@ class StageDragResize extends EventEmitter {
330
304
  if (this.mode === Mode.SORTABLE) {
331
305
  this.ghostEl = this.generateGhostEl(this.target);
332
306
  }
307
+ frame.top = this.target.offsetTop;
308
+ frame.left = this.target.offsetLeft;
333
309
  }).on("drag", (e) => {
334
310
  if (!this.target || !this.dragEl)
335
311
  return;
336
- if (!offset) {
337
- offset = getAbsolutePosition(this.target, { left: 0, top: 0 });
338
- }
339
312
  this.dragStatus = "ing" /* ING */;
340
- const { left, top } = e;
341
313
  if (this.ghostEl) {
342
- this.dragEl.style.top = `${top}px`;
343
- this.ghostEl.style.top = `${top + offset.top}px`;
314
+ this.ghostEl.style.top = `${frame.top + e.beforeTranslate[1]}px`;
344
315
  return;
345
316
  }
346
317
  this.moveableHelper?.onDrag(e);
347
- this.target.style.left = `${left + offset.left}px`;
348
- this.target.style.top = `${top + offset.top}px`;
318
+ this.target.style.left = `${frame.left + e.beforeTranslate[0]}px`;
319
+ this.target.style.top = `${frame.top + e.beforeTranslate[1]}px`;
349
320
  }).on("dragEnd", () => {
350
321
  if (this.dragStatus === "ing" /* ING */) {
351
322
  switch (this.mode) {
@@ -356,14 +327,13 @@ class StageDragResize extends EventEmitter {
356
327
  this.update();
357
328
  }
358
329
  }
359
- offset = null;
360
330
  this.dragStatus = "end" /* END */;
361
331
  this.destroyGhostEl();
362
332
  });
363
333
  }
364
334
  bindRotateEvent() {
365
335
  if (!this.moveable)
366
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
336
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
367
337
  this.moveable.on("rotateStart", (e) => {
368
338
  this.dragStatus = "start" /* START */;
369
339
  this.moveableHelper?.onRotateStart(e);
@@ -387,7 +357,7 @@ class StageDragResize extends EventEmitter {
387
357
  }
388
358
  bindScaleEvent() {
389
359
  if (!this.moveable)
390
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
360
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
391
361
  this.moveable.on("scaleStart", (e) => {
392
362
  this.dragStatus = "start" /* START */;
393
363
  this.moveableHelper?.onScaleStart(e);
@@ -473,6 +443,9 @@ class StageDragResize extends EventEmitter {
473
443
  z-index: ${ZIndex.DRAG_EL};
474
444
  `;
475
445
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
446
+ if (typeof this.core.config.updateDragEl === "function") {
447
+ this.core.config.updateDragEl(this.dragEl, el);
448
+ }
476
449
  }
477
450
  destroyDragEl() {
478
451
  this.dragEl?.remove();
@@ -526,7 +499,7 @@ class StageDragResize extends EventEmitter {
526
499
  elementGuidelines: this.elementGuidelines,
527
500
  bounds: {
528
501
  top: 0,
529
- left: 0,
502
+ left: -1,
530
503
  right: this.container.clientWidth,
531
504
  bottom: this.container.clientHeight,
532
505
  ...moveableOptions.bounds || {}
@@ -598,6 +571,7 @@ class TargetCalibrate extends EventEmitter {
598
571
  this.parent = config.parent;
599
572
  this.mask = config.mask;
600
573
  this.dr = config.dr;
574
+ this.core = config.core;
601
575
  this.operationEl = globalThis.document.createElement("div");
602
576
  this.parent.append(this.operationEl);
603
577
  }
@@ -613,6 +587,9 @@ class TargetCalibrate extends EventEmitter {
613
587
  height: ${el.clientHeight}px;
614
588
  `;
615
589
  this.operationEl.id = `${prefix}${el.id}`;
590
+ if (typeof this.core.config.updateDragEl === "function") {
591
+ this.core.config.updateDragEl(this.operationEl, el);
592
+ }
616
593
  return this.operationEl;
617
594
  }
618
595
  destroy() {
@@ -662,7 +639,8 @@ class StageHighlight extends EventEmitter {
662
639
  this.calibrationTarget = new TargetCalibrate({
663
640
  parent: this.core.mask.content,
664
641
  mask: this.core.mask,
665
- dr: this.core.dr
642
+ dr: this.core.dr,
643
+ core: this.core
666
644
  });
667
645
  }
668
646
  highlight(el) {
@@ -693,8 +671,8 @@ class StageHighlight extends EventEmitter {
693
671
  class Rule extends EventEmitter$1 {
694
672
  constructor(container) {
695
673
  super();
696
- this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
697
- this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
674
+ this.horizontalGuidelines = [];
675
+ this.verticalGuidelines = [];
698
676
  this.getGuidesStyle = (type) => ({
699
677
  position: "fixed",
700
678
  zIndex: 1,
@@ -718,7 +696,6 @@ class Rule extends EventEmitter$1 {
718
696
  type: GuidesType.HORIZONTAL,
719
697
  guides: this.horizontalGuidelines
720
698
  });
721
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
722
699
  };
723
700
  this.vGuidesChangeGuidesHandler = (e) => {
724
701
  this.verticalGuidelines = e.guides;
@@ -726,7 +703,6 @@ class Rule extends EventEmitter$1 {
726
703
  type: GuidesType.VERTICAL,
727
704
  guides: this.verticalGuidelines
728
705
  });
729
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
730
706
  };
731
707
  this.container = container;
732
708
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
@@ -747,25 +723,26 @@ class Rule extends EventEmitter$1 {
747
723
  showGuides: show
748
724
  });
749
725
  }
750
- clearGuides() {
751
- this.horizontalGuidelines = [];
752
- this.verticalGuidelines = [];
753
- this.vGuides.setState({
754
- defaultGuides: []
755
- });
726
+ setGuides([hLines, vLines]) {
727
+ this.horizontalGuidelines = hLines;
728
+ this.verticalGuidelines = vLines;
756
729
  this.hGuides.setState({
757
- defaultGuides: []
730
+ defaultGuides: hLines
758
731
  });
759
- this.emit("changeGuides", {
760
- type: GuidesType.VERTICAL,
761
- guides: []
732
+ this.vGuides.setState({
733
+ defaultGuides: vLines
762
734
  });
763
735
  this.emit("changeGuides", {
764
736
  type: GuidesType.HORIZONTAL,
765
- guides: []
737
+ guides: hLines
766
738
  });
767
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, "[]");
768
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, "[]");
739
+ this.emit("changeGuides", {
740
+ type: GuidesType.VERTICAL,
741
+ guides: vLines
742
+ });
743
+ }
744
+ clearGuides() {
745
+ this.setGuides([[], []]);
769
746
  }
770
747
  showRule(show = true) {
771
748
  if (show) {
@@ -1057,13 +1034,18 @@ class StageRender extends EventEmitter {
1057
1034
  });
1058
1035
  };
1059
1036
  this.loadHandler = async () => {
1060
- this.emit("onload");
1037
+ this.contentWindow = this.iframe?.contentWindow;
1038
+ this.contentWindow.magic = this.getMagicApi();
1061
1039
  if (this.render) {
1062
1040
  const el = await this.render(this.core);
1063
1041
  if (el) {
1064
1042
  this.iframe?.contentDocument?.body?.appendChild(el);
1065
1043
  }
1066
1044
  }
1045
+ this.emit("onload");
1046
+ this.contentWindow.postMessage({
1047
+ tmagicRuntimeReady: true
1048
+ }, "*");
1067
1049
  };
1068
1050
  this.core = core;
1069
1051
  this.runtimeUrl = core.config.runtimeUrl || "";
@@ -1087,8 +1069,6 @@ class StageRender extends EventEmitter {
1087
1069
  this.iframe.srcdoc = html;
1088
1070
  }
1089
1071
  el.appendChild(this.iframe);
1090
- this.contentWindow = this.iframe?.contentWindow;
1091
- this.contentWindow.magic = this.getMagicApi();
1092
1072
  } else {
1093
1073
  throw Error("mount \u5931\u8D25");
1094
1074
  }
@@ -1232,10 +1212,10 @@ class StageCore extends EventEmitter {
1232
1212
  setZoom(zoom = DEFAULT_ZOOM) {
1233
1213
  this.zoom = zoom;
1234
1214
  }
1235
- mount(el) {
1215
+ async mount(el) {
1236
1216
  this.container = el;
1237
1217
  const { mask, renderer } = this;
1238
- renderer.mount(el);
1218
+ await renderer.mount(el);
1239
1219
  mask.mount(el);
1240
1220
  this.emit("mounted");
1241
1221
  }
@@ -1263,5 +1243,5 @@ class StageCore extends EventEmitter {
1263
1243
  }
1264
1244
  }
1265
1245
 
1266
- export { StageDragResize, StageMask, StageRender, StageCore as default };
1246
+ export { DEFAULT_ZOOM, DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, GuidesType, HIGHLIGHT_EL_ID_PREFIX, Mode, MouseButton, SELECTED_CLASS, StageDragResize, StageMask, StageRender, ZIndex, StageCore as default };
1267
1247
  //# sourceMappingURL=tmagic-stage.es.js.map