@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.
@@ -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 = {};
@@ -203,8 +168,8 @@
203
168
  select(el, event) {
204
169
  const oldTarget = this.target;
205
170
  this.target = el;
206
- this.init(el);
207
171
  if (!this.moveable || this.target !== oldTarget) {
172
+ this.init(el);
208
173
  this.moveableHelper = MoveableHelper__default["default"].create({
209
174
  useBeforeRender: true,
210
175
  useRender: false,
@@ -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);
@@ -480,6 +450,9 @@
480
450
  z-index: ${ZIndex.DRAG_EL};
481
451
  `;
482
452
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
453
+ if (typeof this.core.config.updateDragEl === "function") {
454
+ this.core.config.updateDragEl(this.dragEl, el);
455
+ }
483
456
  }
484
457
  destroyDragEl() {
485
458
  this.dragEl?.remove();
@@ -533,7 +506,7 @@
533
506
  elementGuidelines: this.elementGuidelines,
534
507
  bounds: {
535
508
  top: 0,
536
- left: 0,
509
+ left: -1,
537
510
  right: this.container.clientWidth,
538
511
  bottom: this.container.clientHeight,
539
512
  ...moveableOptions.bounds || {}
@@ -605,6 +578,7 @@
605
578
  this.parent = config.parent;
606
579
  this.mask = config.mask;
607
580
  this.dr = config.dr;
581
+ this.core = config.core;
608
582
  this.operationEl = globalThis.document.createElement("div");
609
583
  this.parent.append(this.operationEl);
610
584
  }
@@ -620,6 +594,9 @@
620
594
  height: ${el.clientHeight}px;
621
595
  `;
622
596
  this.operationEl.id = `${prefix}${el.id}`;
597
+ if (typeof this.core.config.updateDragEl === "function") {
598
+ this.core.config.updateDragEl(this.operationEl, el);
599
+ }
623
600
  return this.operationEl;
624
601
  }
625
602
  destroy() {
@@ -669,7 +646,8 @@
669
646
  this.calibrationTarget = new TargetCalibrate({
670
647
  parent: this.core.mask.content,
671
648
  mask: this.core.mask,
672
- dr: this.core.dr
649
+ dr: this.core.dr,
650
+ core: this.core
673
651
  });
674
652
  }
675
653
  highlight(el) {
@@ -700,8 +678,8 @@
700
678
  class Rule extends EventEmitter__default["default"] {
701
679
  constructor(container) {
702
680
  super();
703
- this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
704
- this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
681
+ this.horizontalGuidelines = [];
682
+ this.verticalGuidelines = [];
705
683
  this.getGuidesStyle = (type) => ({
706
684
  position: "fixed",
707
685
  zIndex: 1,
@@ -725,7 +703,6 @@
725
703
  type: GuidesType.HORIZONTAL,
726
704
  guides: this.horizontalGuidelines
727
705
  });
728
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
729
706
  };
730
707
  this.vGuidesChangeGuidesHandler = (e) => {
731
708
  this.verticalGuidelines = e.guides;
@@ -733,7 +710,6 @@
733
710
  type: GuidesType.VERTICAL,
734
711
  guides: this.verticalGuidelines
735
712
  });
736
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
737
713
  };
738
714
  this.container = container;
739
715
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
@@ -754,25 +730,26 @@
754
730
  showGuides: show
755
731
  });
756
732
  }
757
- clearGuides() {
758
- this.horizontalGuidelines = [];
759
- this.verticalGuidelines = [];
760
- this.vGuides.setState({
761
- defaultGuides: []
762
- });
733
+ setGuides([hLines, vLines]) {
734
+ this.horizontalGuidelines = hLines;
735
+ this.verticalGuidelines = vLines;
763
736
  this.hGuides.setState({
764
- defaultGuides: []
737
+ defaultGuides: hLines
765
738
  });
766
- this.emit("changeGuides", {
767
- type: GuidesType.VERTICAL,
768
- guides: []
739
+ this.vGuides.setState({
740
+ defaultGuides: vLines
769
741
  });
770
742
  this.emit("changeGuides", {
771
743
  type: GuidesType.HORIZONTAL,
772
- guides: []
744
+ guides: hLines
773
745
  });
774
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, "[]");
775
- 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([[], []]);
776
753
  }
777
754
  showRule(show = true) {
778
755
  if (show) {
@@ -1064,13 +1041,18 @@
1064
1041
  });
1065
1042
  };
1066
1043
  this.loadHandler = async () => {
1067
- this.emit("onload");
1044
+ this.contentWindow = this.iframe?.contentWindow;
1045
+ this.contentWindow.magic = this.getMagicApi();
1068
1046
  if (this.render) {
1069
1047
  const el = await this.render(this.core);
1070
1048
  if (el) {
1071
1049
  this.iframe?.contentDocument?.body?.appendChild(el);
1072
1050
  }
1073
1051
  }
1052
+ this.emit("onload");
1053
+ this.contentWindow.postMessage({
1054
+ tmagicRuntimeReady: true
1055
+ }, "*");
1074
1056
  };
1075
1057
  this.core = core;
1076
1058
  this.runtimeUrl = core.config.runtimeUrl || "";
@@ -1094,8 +1076,6 @@
1094
1076
  this.iframe.srcdoc = html;
1095
1077
  }
1096
1078
  el.appendChild(this.iframe);
1097
- this.contentWindow = this.iframe?.contentWindow;
1098
- this.contentWindow.magic = this.getMagicApi();
1099
1079
  } else {
1100
1080
  throw Error("mount \u5931\u8D25");
1101
1081
  }
@@ -1239,10 +1219,10 @@
1239
1219
  setZoom(zoom = DEFAULT_ZOOM) {
1240
1220
  this.zoom = zoom;
1241
1221
  }
1242
- mount(el) {
1222
+ async mount(el) {
1243
1223
  this.container = el;
1244
1224
  const { mask, renderer } = this;
1245
- renderer.mount(el);
1225
+ await renderer.mount(el);
1246
1226
  mask.mount(el);
1247
1227
  this.emit("mounted");
1248
1228
  }
@@ -1270,9 +1250,18 @@
1270
1250
  }
1271
1251
  }
1272
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;
1273
1261
  exports.StageDragResize = StageDragResize;
1274
1262
  exports.StageMask = StageMask;
1275
1263
  exports.StageRender = StageRender;
1264
+ exports.ZIndex = ZIndex;
1276
1265
  exports["default"] = StageCore;
1277
1266
 
1278
1267
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });