@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.
@@ -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
@@ -316,10 +283,14 @@
316
283
  const { beforeTranslate } = drag;
317
284
  this.dragStatus = "ing" /* ING */;
318
285
  this.moveableHelper?.onResize(e);
286
+ if (this.mode === Mode.SORTABLE) {
287
+ this.target.style.top = "0px";
288
+ } else {
289
+ this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
290
+ this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
291
+ }
319
292
  this.target.style.width = `${width}px`;
320
293
  this.target.style.height = `${height}px`;
321
- this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
322
- this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
323
294
  }).on("resizeEnd", () => {
324
295
  this.dragStatus = "end" /* END */;
325
296
  this.update(true);
@@ -327,8 +298,11 @@
327
298
  }
328
299
  bindDragEvent() {
329
300
  if (!this.moveable)
330
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
331
- let offset = null;
301
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
302
+ const frame = {
303
+ left: 0,
304
+ top: 0
305
+ };
332
306
  this.moveable.on("dragStart", (e) => {
333
307
  if (!this.target)
334
308
  throw new Error("\u672A\u9009\u4E2D\u7EC4\u4EF6");
@@ -337,22 +311,19 @@
337
311
  if (this.mode === Mode.SORTABLE) {
338
312
  this.ghostEl = this.generateGhostEl(this.target);
339
313
  }
314
+ frame.top = this.target.offsetTop;
315
+ frame.left = this.target.offsetLeft;
340
316
  }).on("drag", (e) => {
341
317
  if (!this.target || !this.dragEl)
342
318
  return;
343
- if (!offset) {
344
- offset = getAbsolutePosition(this.target, { left: 0, top: 0 });
345
- }
346
319
  this.dragStatus = "ing" /* ING */;
347
- const { left, top } = e;
348
320
  if (this.ghostEl) {
349
- this.dragEl.style.top = `${top}px`;
350
- this.ghostEl.style.top = `${top + offset.top}px`;
321
+ this.ghostEl.style.top = `${frame.top + e.beforeTranslate[1]}px`;
351
322
  return;
352
323
  }
353
324
  this.moveableHelper?.onDrag(e);
354
- this.target.style.left = `${left + offset.left}px`;
355
- this.target.style.top = `${top + offset.top}px`;
325
+ this.target.style.left = `${frame.left + e.beforeTranslate[0]}px`;
326
+ this.target.style.top = `${frame.top + e.beforeTranslate[1]}px`;
356
327
  }).on("dragEnd", () => {
357
328
  if (this.dragStatus === "ing" /* ING */) {
358
329
  switch (this.mode) {
@@ -363,14 +334,13 @@
363
334
  this.update();
364
335
  }
365
336
  }
366
- offset = null;
367
337
  this.dragStatus = "end" /* END */;
368
338
  this.destroyGhostEl();
369
339
  });
370
340
  }
371
341
  bindRotateEvent() {
372
342
  if (!this.moveable)
373
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
343
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
374
344
  this.moveable.on("rotateStart", (e) => {
375
345
  this.dragStatus = "start" /* START */;
376
346
  this.moveableHelper?.onRotateStart(e);
@@ -394,7 +364,7 @@
394
364
  }
395
365
  bindScaleEvent() {
396
366
  if (!this.moveable)
397
- throw new Error("moveable \u4E3A\u521D\u59CB\u5316");
367
+ throw new Error("moveable \u672A\u521D\u59CB\u5316");
398
368
  this.moveable.on("scaleStart", (e) => {
399
369
  this.dragStatus = "start" /* START */;
400
370
  this.moveableHelper?.onScaleStart(e);
@@ -536,7 +506,7 @@
536
506
  elementGuidelines: this.elementGuidelines,
537
507
  bounds: {
538
508
  top: 0,
539
- left: 0,
509
+ left: -1,
540
510
  right: this.container.clientWidth,
541
511
  bottom: this.container.clientHeight,
542
512
  ...moveableOptions.bounds || {}
@@ -708,8 +678,8 @@
708
678
  class Rule extends EventEmitter__default["default"] {
709
679
  constructor(container) {
710
680
  super();
711
- this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
712
- this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
681
+ this.horizontalGuidelines = [];
682
+ this.verticalGuidelines = [];
713
683
  this.getGuidesStyle = (type) => ({
714
684
  position: "fixed",
715
685
  zIndex: 1,
@@ -733,7 +703,6 @@
733
703
  type: GuidesType.HORIZONTAL,
734
704
  guides: this.horizontalGuidelines
735
705
  });
736
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
737
706
  };
738
707
  this.vGuidesChangeGuidesHandler = (e) => {
739
708
  this.verticalGuidelines = e.guides;
@@ -741,7 +710,6 @@
741
710
  type: GuidesType.VERTICAL,
742
711
  guides: this.verticalGuidelines
743
712
  });
744
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
745
713
  };
746
714
  this.container = container;
747
715
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
@@ -762,25 +730,26 @@
762
730
  showGuides: show
763
731
  });
764
732
  }
765
- clearGuides() {
766
- this.horizontalGuidelines = [];
767
- this.verticalGuidelines = [];
768
- this.vGuides.setState({
769
- defaultGuides: []
770
- });
733
+ setGuides([hLines, vLines]) {
734
+ this.horizontalGuidelines = hLines;
735
+ this.verticalGuidelines = vLines;
771
736
  this.hGuides.setState({
772
- defaultGuides: []
737
+ defaultGuides: hLines
773
738
  });
774
- this.emit("changeGuides", {
775
- type: GuidesType.VERTICAL,
776
- guides: []
739
+ this.vGuides.setState({
740
+ defaultGuides: vLines
777
741
  });
778
742
  this.emit("changeGuides", {
779
743
  type: GuidesType.HORIZONTAL,
780
- guides: []
744
+ guides: hLines
781
745
  });
782
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, "[]");
783
- 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([[], []]);
784
753
  }
785
754
  showRule(show = true) {
786
755
  if (show) {
@@ -1072,13 +1041,18 @@
1072
1041
  });
1073
1042
  };
1074
1043
  this.loadHandler = async () => {
1075
- this.emit("onload");
1044
+ this.contentWindow = this.iframe?.contentWindow;
1045
+ this.contentWindow.magic = this.getMagicApi();
1076
1046
  if (this.render) {
1077
1047
  const el = await this.render(this.core);
1078
1048
  if (el) {
1079
1049
  this.iframe?.contentDocument?.body?.appendChild(el);
1080
1050
  }
1081
1051
  }
1052
+ this.emit("onload");
1053
+ this.contentWindow.postMessage({
1054
+ tmagicRuntimeReady: true
1055
+ }, "*");
1082
1056
  };
1083
1057
  this.core = core;
1084
1058
  this.runtimeUrl = core.config.runtimeUrl || "";
@@ -1102,8 +1076,6 @@
1102
1076
  this.iframe.srcdoc = html;
1103
1077
  }
1104
1078
  el.appendChild(this.iframe);
1105
- this.contentWindow = this.iframe?.contentWindow;
1106
- this.contentWindow.magic = this.getMagicApi();
1107
1079
  } else {
1108
1080
  throw Error("mount \u5931\u8D25");
1109
1081
  }
@@ -1247,10 +1219,10 @@
1247
1219
  setZoom(zoom = DEFAULT_ZOOM) {
1248
1220
  this.zoom = zoom;
1249
1221
  }
1250
- mount(el) {
1222
+ async mount(el) {
1251
1223
  this.container = el;
1252
1224
  const { mask, renderer } = this;
1253
- renderer.mount(el);
1225
+ await renderer.mount(el);
1254
1226
  mask.mount(el);
1255
1227
  this.emit("mounted");
1256
1228
  }
@@ -1278,9 +1250,18 @@
1278
1250
  }
1279
1251
  }
1280
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;
1281
1261
  exports.StageDragResize = StageDragResize;
1282
1262
  exports.StageMask = StageMask;
1283
1263
  exports.StageRender = StageRender;
1264
+ exports.ZIndex = ZIndex;
1284
1265
  exports["default"] = StageCore;
1285
1266
 
1286
1267
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });