@tmagic/stage 1.0.0-rc.9 → 1.0.2

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.
@@ -40,8 +40,6 @@
40
40
  return Mode2;
41
41
  })(Mode || {});
42
42
  const SELECTED_CLASS = "tmagic-stage-selected-area";
43
- const H_GUIDE_LINE_STORAGE_KEY = "$MagicStageHorizontalGuidelinesData";
44
- const V_GUIDE_LINE_STORAGE_KEY = "$MagicStageVerticalGuidelinesData";
45
43
 
46
44
  const getParents = (el, relative) => {
47
45
  let cur = el.parentElement;
@@ -86,31 +84,15 @@
86
84
  return true;
87
85
  return getHost(targetUrl) === source;
88
86
  };
89
- const isAbsolute = (el) => {
90
- if (!el)
91
- return false;
92
- return getComputedStyle(el).position === "absolute";
93
- };
94
- const isRelative = (el) => {
95
- if (!el)
96
- return false;
97
- return getComputedStyle(el).position === "relative";
98
- };
99
- const isStatic = (el) => {
100
- if (!el)
101
- return false;
102
- return getComputedStyle(el).position === "static";
103
- };
104
- const isFixed = (el) => {
105
- if (!el)
106
- return false;
107
- return getComputedStyle(el).position === "fixed";
108
- };
87
+ const isAbsolute = (style) => style.position === "absolute";
88
+ const isRelative = (style) => style.position === "relative";
89
+ const isStatic = (style) => style.position === "static";
90
+ const isFixed = (style) => style.position === "fixed";
109
91
  const isFixedParent = (el) => {
110
92
  let fixed = false;
111
93
  let dom = el;
112
94
  while (dom) {
113
- fixed = isFixed(dom);
95
+ fixed = isFixed(getComputedStyle(dom));
114
96
  if (fixed) {
115
97
  break;
116
98
  }
@@ -125,21 +107,21 @@
125
107
  const getMode = (el) => {
126
108
  if (isFixedParent(el))
127
109
  return Mode.FIXED;
128
- if (isStatic(el) || isRelative(el))
110
+ const style = getComputedStyle(el);
111
+ if (isStatic(style) || isRelative(style))
129
112
  return Mode.SORTABLE;
130
113
  return Mode.ABSOLUTE;
131
114
  };
132
115
  const getScrollParent = (element, includeHidden = false) => {
133
116
  let style = getComputedStyle(element);
134
117
  const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
135
- if (isFixed(element))
118
+ if (isFixed(style))
136
119
  return null;
137
120
  for (let parent = element; parent.parentElement; ) {
138
121
  parent = parent.parentElement;
139
122
  style = getComputedStyle(parent);
140
- if (isAbsolute(element) && isStatic(element)) {
123
+ if (isAbsolute(style) && isStatic(style))
141
124
  continue;
142
- }
143
125
  if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX))
144
126
  return parent;
145
127
  }
@@ -168,29 +150,12 @@
168
150
  item.classList.add(`${SELECTED_CLASS}-parents`);
169
151
  });
170
152
  };
171
- const getGuideLineFromCache = (type) => {
172
- const key = {
173
- [GuidesType.HORIZONTAL]: H_GUIDE_LINE_STORAGE_KEY,
174
- [GuidesType.VERTICAL]: V_GUIDE_LINE_STORAGE_KEY
175
- }[type];
176
- if (!key)
177
- return [];
178
- const guideLineCacheData = globalThis.localStorage.getItem(key);
179
- if (guideLineCacheData) {
180
- try {
181
- return JSON.parse(guideLineCacheData) || [];
182
- } catch (e) {
183
- console.error(e);
184
- }
185
- }
186
- return [];
187
- };
188
153
 
189
154
  class StageDragResize extends EventEmitter.EventEmitter {
190
155
  constructor(config) {
191
156
  super();
192
- this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
193
- this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
157
+ this.horizontalGuidelines = [];
158
+ this.verticalGuidelines = [];
194
159
  this.elementGuidelines = [];
195
160
  this.mode = Mode.ABSOLUTE;
196
161
  this.moveableOptions = {};
@@ -222,7 +187,7 @@
222
187
  if (!this.moveable)
223
188
  throw new Error("\u672A\u521D\u59CB\u5316moveable");
224
189
  if (!el)
225
- throw new Error("\u4E3A\u9009\u4E2D\u4EFB\u4F55\u8282\u70B9");
190
+ throw new Error("\u672A\u9009\u4E2D\u4EFB\u4F55\u8282\u70B9");
226
191
  this.target = el;
227
192
  this.init(el);
228
193
  Object.entries(this.moveableOptions).forEach(([key, value]) => {
@@ -238,7 +203,9 @@
238
203
  this.verticalGuidelines = guidelines;
239
204
  this.moveableOptions.verticalGuidelines = guidelines;
240
205
  }
241
- this.updateMoveable();
206
+ if (this.moveable) {
207
+ this.updateMoveable();
208
+ }
242
209
  }
243
210
  clearGuides() {
244
211
  this.horizontalGuidelines = [];
@@ -297,7 +264,7 @@
297
264
  }
298
265
  bindResizeEvent() {
299
266
  if (!this.moveable)
300
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
267
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
301
268
  const frame = {
302
269
  left: 0,
303
270
  top: 0
@@ -331,7 +298,7 @@
331
298
  }
332
299
  bindDragEvent() {
333
300
  if (!this.moveable)
334
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
301
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
335
302
  const frame = {
336
303
  left: 0,
337
304
  top: 0
@@ -373,7 +340,7 @@
373
340
  }
374
341
  bindRotateEvent() {
375
342
  if (!this.moveable)
376
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
343
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
377
344
  this.moveable.on("rotateStart", (e) => {
378
345
  this.dragStatus = "start" /* START */;
379
346
  this.moveableHelper?.onRotateStart(e);
@@ -397,7 +364,7 @@
397
364
  }
398
365
  bindScaleEvent() {
399
366
  if (!this.moveable)
400
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
367
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
401
368
  this.moveable.on("scaleStart", (e) => {
402
369
  this.dragStatus = "start" /* START */;
403
370
  this.moveableHelper?.onScaleStart(e);
@@ -539,7 +506,7 @@
539
506
  elementGuidelines: this.elementGuidelines,
540
507
  bounds: {
541
508
  top: 0,
542
- left: 0,
509
+ left: -1,
543
510
  right: this.container.clientWidth,
544
511
  bottom: this.container.clientHeight,
545
512
  ...moveableOptions.bounds || {}
@@ -711,8 +678,8 @@
711
678
  class Rule extends EventEmitter__default["default"] {
712
679
  constructor(container) {
713
680
  super();
714
- this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
715
- this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
681
+ this.horizontalGuidelines = [];
682
+ this.verticalGuidelines = [];
716
683
  this.getGuidesStyle = (type) => ({
717
684
  position: "fixed",
718
685
  zIndex: 1,
@@ -736,7 +703,6 @@
736
703
  type: GuidesType.HORIZONTAL,
737
704
  guides: this.horizontalGuidelines
738
705
  });
739
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
740
706
  };
741
707
  this.vGuidesChangeGuidesHandler = (e) => {
742
708
  this.verticalGuidelines = e.guides;
@@ -744,7 +710,6 @@
744
710
  type: GuidesType.VERTICAL,
745
711
  guides: this.verticalGuidelines
746
712
  });
747
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
748
713
  };
749
714
  this.container = container;
750
715
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
@@ -765,25 +730,26 @@
765
730
  showGuides: show
766
731
  });
767
732
  }
768
- clearGuides() {
769
- this.horizontalGuidelines = [];
770
- this.verticalGuidelines = [];
771
- this.vGuides.setState({
772
- defaultGuides: []
773
- });
733
+ setGuides([hLines, vLines]) {
734
+ this.horizontalGuidelines = hLines;
735
+ this.verticalGuidelines = vLines;
774
736
  this.hGuides.setState({
775
- defaultGuides: []
737
+ defaultGuides: hLines
776
738
  });
777
- this.emit("changeGuides", {
778
- type: GuidesType.VERTICAL,
779
- guides: []
739
+ this.vGuides.setState({
740
+ defaultGuides: vLines
780
741
  });
781
742
  this.emit("changeGuides", {
782
743
  type: GuidesType.HORIZONTAL,
783
- guides: []
744
+ guides: hLines
784
745
  });
785
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, "[]");
786
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, "[]");
746
+ this.emit("changeGuides", {
747
+ type: GuidesType.VERTICAL,
748
+ guides: vLines
749
+ });
750
+ }
751
+ clearGuides() {
752
+ this.setGuides([[], []]);
787
753
  }
788
754
  showRule(show = true) {
789
755
  if (show) {
@@ -1075,13 +1041,18 @@
1075
1041
  });
1076
1042
  };
1077
1043
  this.loadHandler = async () => {
1078
- this.emit("onload");
1044
+ this.contentWindow = this.iframe?.contentWindow;
1045
+ this.contentWindow.magic = this.getMagicApi();
1079
1046
  if (this.render) {
1080
1047
  const el = await this.render(this.core);
1081
1048
  if (el) {
1082
1049
  this.iframe?.contentDocument?.body?.appendChild(el);
1083
1050
  }
1084
1051
  }
1052
+ this.emit("onload");
1053
+ this.contentWindow.postMessage({
1054
+ tmagicRuntimeReady: true
1055
+ }, "*");
1085
1056
  };
1086
1057
  this.core = core;
1087
1058
  this.runtimeUrl = core.config.runtimeUrl || "";
@@ -1105,8 +1076,6 @@
1105
1076
  this.iframe.srcdoc = html;
1106
1077
  }
1107
1078
  el.appendChild(this.iframe);
1108
- this.contentWindow = this.iframe?.contentWindow;
1109
- this.contentWindow.magic = this.getMagicApi();
1110
1079
  } else {
1111
1080
  throw Error("mount \u5931\u8D25");
1112
1081
  }
@@ -1250,10 +1219,10 @@
1250
1219
  setZoom(zoom = DEFAULT_ZOOM) {
1251
1220
  this.zoom = zoom;
1252
1221
  }
1253
- mount(el) {
1222
+ async mount(el) {
1254
1223
  this.container = el;
1255
1224
  const { mask, renderer } = this;
1256
- renderer.mount(el);
1225
+ await renderer.mount(el);
1257
1226
  mask.mount(el);
1258
1227
  this.emit("mounted");
1259
1228
  }
@@ -1281,9 +1250,18 @@
1281
1250
  }
1282
1251
  }
1283
1252
 
1253
+ exports.DEFAULT_ZOOM = DEFAULT_ZOOM;
1254
+ exports.DRAG_EL_ID_PREFIX = DRAG_EL_ID_PREFIX;
1255
+ exports.GHOST_EL_ID_PREFIX = GHOST_EL_ID_PREFIX;
1256
+ exports.GuidesType = GuidesType;
1257
+ exports.HIGHLIGHT_EL_ID_PREFIX = HIGHLIGHT_EL_ID_PREFIX;
1258
+ exports.Mode = Mode;
1259
+ exports.MouseButton = MouseButton;
1260
+ exports.SELECTED_CLASS = SELECTED_CLASS;
1284
1261
  exports.StageDragResize = StageDragResize;
1285
1262
  exports.StageMask = StageMask;
1286
1263
  exports.StageRender = StageRender;
1264
+ exports.ZIndex = ZIndex;
1287
1265
  exports["default"] = StageCore;
1288
1266
 
1289
1267
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });