@tmagic/stage 1.0.0-rc.8 → 1.0.1

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 = {};
@@ -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);
@@ -529,7 +499,7 @@ class StageDragResize extends EventEmitter {
529
499
  elementGuidelines: this.elementGuidelines,
530
500
  bounds: {
531
501
  top: 0,
532
- left: 0,
502
+ left: -1,
533
503
  right: this.container.clientWidth,
534
504
  bottom: this.container.clientHeight,
535
505
  ...moveableOptions.bounds || {}
@@ -701,8 +671,8 @@ class StageHighlight extends EventEmitter {
701
671
  class Rule extends EventEmitter$1 {
702
672
  constructor(container) {
703
673
  super();
704
- this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
705
- this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
674
+ this.horizontalGuidelines = [];
675
+ this.verticalGuidelines = [];
706
676
  this.getGuidesStyle = (type) => ({
707
677
  position: "fixed",
708
678
  zIndex: 1,
@@ -726,7 +696,6 @@ class Rule extends EventEmitter$1 {
726
696
  type: GuidesType.HORIZONTAL,
727
697
  guides: this.horizontalGuidelines
728
698
  });
729
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
730
699
  };
731
700
  this.vGuidesChangeGuidesHandler = (e) => {
732
701
  this.verticalGuidelines = e.guides;
@@ -734,7 +703,6 @@ class Rule extends EventEmitter$1 {
734
703
  type: GuidesType.VERTICAL,
735
704
  guides: this.verticalGuidelines
736
705
  });
737
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
738
706
  };
739
707
  this.container = container;
740
708
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
@@ -755,25 +723,26 @@ class Rule extends EventEmitter$1 {
755
723
  showGuides: show
756
724
  });
757
725
  }
758
- clearGuides() {
759
- this.horizontalGuidelines = [];
760
- this.verticalGuidelines = [];
761
- this.vGuides.setState({
762
- defaultGuides: []
763
- });
726
+ setGuides([hLines, vLines]) {
727
+ this.horizontalGuidelines = hLines;
728
+ this.verticalGuidelines = vLines;
764
729
  this.hGuides.setState({
765
- defaultGuides: []
730
+ defaultGuides: hLines
766
731
  });
767
- this.emit("changeGuides", {
768
- type: GuidesType.VERTICAL,
769
- guides: []
732
+ this.vGuides.setState({
733
+ defaultGuides: vLines
770
734
  });
771
735
  this.emit("changeGuides", {
772
736
  type: GuidesType.HORIZONTAL,
773
- guides: []
737
+ guides: hLines
774
738
  });
775
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, "[]");
776
- 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([[], []]);
777
746
  }
778
747
  showRule(show = true) {
779
748
  if (show) {
@@ -1065,13 +1034,18 @@ class StageRender extends EventEmitter {
1065
1034
  });
1066
1035
  };
1067
1036
  this.loadHandler = async () => {
1068
- this.emit("onload");
1037
+ this.contentWindow = this.iframe?.contentWindow;
1038
+ this.contentWindow.magic = this.getMagicApi();
1069
1039
  if (this.render) {
1070
1040
  const el = await this.render(this.core);
1071
1041
  if (el) {
1072
1042
  this.iframe?.contentDocument?.body?.appendChild(el);
1073
1043
  }
1074
1044
  }
1045
+ this.emit("onload");
1046
+ this.contentWindow.postMessage({
1047
+ tmagicRuntimeReady: true
1048
+ }, "*");
1075
1049
  };
1076
1050
  this.core = core;
1077
1051
  this.runtimeUrl = core.config.runtimeUrl || "";
@@ -1095,8 +1069,6 @@ class StageRender extends EventEmitter {
1095
1069
  this.iframe.srcdoc = html;
1096
1070
  }
1097
1071
  el.appendChild(this.iframe);
1098
- this.contentWindow = this.iframe?.contentWindow;
1099
- this.contentWindow.magic = this.getMagicApi();
1100
1072
  } else {
1101
1073
  throw Error("mount \u5931\u8D25");
1102
1074
  }
@@ -1240,10 +1212,10 @@ class StageCore extends EventEmitter {
1240
1212
  setZoom(zoom = DEFAULT_ZOOM) {
1241
1213
  this.zoom = zoom;
1242
1214
  }
1243
- mount(el) {
1215
+ async mount(el) {
1244
1216
  this.container = el;
1245
1217
  const { mask, renderer } = this;
1246
- renderer.mount(el);
1218
+ await renderer.mount(el);
1247
1219
  mask.mount(el);
1248
1220
  this.emit("mounted");
1249
1221
  }
@@ -1271,5 +1243,5 @@ class StageCore extends EventEmitter {
1271
1243
  }
1272
1244
  }
1273
1245
 
1274
- 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 };
1275
1247
  //# sourceMappingURL=tmagic-stage.es.js.map