@tmagic/stage 1.0.0-rc.3 → 1.0.0-rc.6

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.
@@ -16,6 +16,8 @@ var GuidesType = /* @__PURE__ */ ((GuidesType2) => {
16
16
  var ZIndex = /* @__PURE__ */ ((ZIndex2) => {
17
17
  ZIndex2["MASK"] = "99999";
18
18
  ZIndex2["SELECTED_EL"] = "666";
19
+ ZIndex2["GHOST_EL"] = "700";
20
+ ZIndex2["DRAG_EL"] = "9";
19
21
  return ZIndex2;
20
22
  })(ZIndex || {});
21
23
  var MouseButton = /* @__PURE__ */ ((MouseButton2) => {
@@ -31,6 +33,8 @@ var Mode = /* @__PURE__ */ ((Mode2) => {
31
33
  return Mode2;
32
34
  })(Mode || {});
33
35
  const SELECTED_CLASS = "tmagic-stage-selected-area";
36
+ const H_GUIDE_LINE_STORAGE_KEY = "$MagicStageHorizontalGuidelinesData";
37
+ const V_GUIDE_LINE_STORAGE_KEY = "$MagicStageVerticalGuidelinesData";
34
38
 
35
39
  const getParents = (el, relative) => {
36
40
  let cur = el.parentElement;
@@ -177,12 +181,29 @@ const addSelectedClassName = (el, doc) => {
177
181
  item.classList.add(`${SELECTED_CLASS}-parents`);
178
182
  });
179
183
  };
184
+ const getGuideLineFromCache = (type) => {
185
+ const key = {
186
+ [GuidesType.HORIZONTAL]: H_GUIDE_LINE_STORAGE_KEY,
187
+ [GuidesType.VERTICAL]: V_GUIDE_LINE_STORAGE_KEY
188
+ }[type];
189
+ if (!key)
190
+ return [];
191
+ const guideLineCacheData = globalThis.localStorage.getItem(key);
192
+ if (guideLineCacheData) {
193
+ try {
194
+ return JSON.parse(guideLineCacheData) || [];
195
+ } catch (e) {
196
+ console.error(e);
197
+ }
198
+ }
199
+ return [];
200
+ };
180
201
 
181
202
  class StageDragResize extends EventEmitter {
182
203
  constructor(config) {
183
204
  super();
184
- this.horizontalGuidelines = [];
185
- this.verticalGuidelines = [];
205
+ this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
206
+ this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
186
207
  this.elementGuidelines = [];
187
208
  this.mode = Mode.ABSOLUTE;
188
209
  this.moveableOptions = {};
@@ -257,6 +278,26 @@ class StageDragResize extends EventEmitter {
257
278
  target: this.dragEl
258
279
  });
259
280
  }
281
+ setElementGuidelines(nodes) {
282
+ this.elementGuidelines.forEach((node) => {
283
+ node.remove();
284
+ });
285
+ this.elementGuidelines = [];
286
+ if (this.mode === Mode.ABSOLUTE) {
287
+ const frame = document.createDocumentFragment();
288
+ for (const node of nodes) {
289
+ const { width, height } = node.getBoundingClientRect();
290
+ if (node === this.target)
291
+ continue;
292
+ const { left, top } = getOffset(node);
293
+ const elementGuideline = document.createElement("div");
294
+ elementGuideline.style.cssText = `position: absolute;width: ${width}px;height: ${height}px;top: ${top}px;left: ${left}px`;
295
+ this.elementGuidelines.push(elementGuideline);
296
+ frame.append(elementGuideline);
297
+ }
298
+ this.container.append(frame);
299
+ }
300
+ }
260
301
  initMoveable() {
261
302
  this.moveable?.destroy();
262
303
  this.moveable = new Moveable(this.container, {
@@ -341,14 +382,6 @@ class StageDragResize extends EventEmitter {
341
382
  this.destroyGhostEl();
342
383
  });
343
384
  }
344
- getSnapElements(runtime, el) {
345
- const { renderer, mask } = this.core;
346
- const getSnapElements = runtime?.getSnapElements || (() => {
347
- const doc = renderer.contentWindow?.document;
348
- return doc ? Array.from(doc.querySelectorAll("[id]")) : [];
349
- });
350
- return getSnapElements(el).filter((element) => element !== this.target && !this.target?.contains(element) && element !== mask.page);
351
- }
352
385
  sort() {
353
386
  if (!this.target || !this.ghostEl)
354
387
  throw new Error("\u672A\u77E5\u9519\u8BEF");
@@ -387,7 +420,7 @@ class StageDragResize extends EventEmitter {
387
420
  const ghostEl = el.cloneNode(true);
388
421
  const { top, left } = getAbsolutePosition(el, getOffset(el));
389
422
  ghostEl.id = `${GHOST_EL_ID_PREFIX}${el.id}`;
390
- ghostEl.style.zIndex = "5";
423
+ ghostEl.style.zIndex = ZIndex.GHOST_EL;
391
424
  ghostEl.style.opacity = ".5";
392
425
  ghostEl.style.position = "absolute";
393
426
  ghostEl.style.left = `${left}px`;
@@ -408,7 +441,7 @@ class StageDragResize extends EventEmitter {
408
441
  top: ${offset.top}px;
409
442
  width: ${width}px;
410
443
  height: ${height}px;
411
- z-index: 9;
444
+ z-index: ${ZIndex.DRAG_EL};
412
445
  `;
413
446
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
414
447
  }
@@ -424,6 +457,11 @@ class StageDragResize extends EventEmitter {
424
457
  if (typeof moveableOptions === "function") {
425
458
  moveableOptions = moveableOptions(this.core);
426
459
  }
460
+ const elementGuidelines = moveableOptions.elementGuidelines || this.target.parentElement?.children || [];
461
+ this.setElementGuidelines(elementGuidelines);
462
+ if (moveableOptions.elementGuidelines) {
463
+ delete moveableOptions.elementGuidelines;
464
+ }
427
465
  return {
428
466
  origin: false,
429
467
  rootContainer: this.core.container,
@@ -643,8 +681,8 @@ class StageHighlight extends EventEmitter {
643
681
  class Rule extends EventEmitter$1 {
644
682
  constructor(container) {
645
683
  super();
646
- this.horizontalGuidelines = [];
647
- this.verticalGuidelines = [];
684
+ this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
685
+ this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
648
686
  this.getGuidesStyle = (type) => ({
649
687
  position: "fixed",
650
688
  zIndex: 1,
@@ -668,6 +706,7 @@ class Rule extends EventEmitter$1 {
668
706
  type: GuidesType.HORIZONTAL,
669
707
  guides: this.horizontalGuidelines
670
708
  });
709
+ globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
671
710
  };
672
711
  this.vGuidesChangeGuidesHandler = (e) => {
673
712
  this.verticalGuidelines = e.guides;
@@ -675,10 +714,11 @@ class Rule extends EventEmitter$1 {
675
714
  type: GuidesType.VERTICAL,
676
715
  guides: this.verticalGuidelines
677
716
  });
717
+ globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
678
718
  };
679
719
  this.container = container;
680
- this.hGuides = this.createGuides(GuidesType.HORIZONTAL);
681
- this.vGuides = this.createGuides(GuidesType.VERTICAL);
720
+ this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
721
+ this.vGuides = this.createGuides(GuidesType.VERTICAL, this.verticalGuidelines);
682
722
  this.hGuides.on("changeGuides", this.hGuidesChangeGuidesHandler);
683
723
  this.vGuides.on("changeGuides", this.vGuidesChangeGuidesHandler);
684
724
  this.containerResizeObserver = new ResizeObserver(() => {
@@ -712,6 +752,8 @@ class Rule extends EventEmitter$1 {
712
752
  type: GuidesType.HORIZONTAL,
713
753
  guides: []
714
754
  });
755
+ globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, "[]");
756
+ globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, "[]");
715
757
  }
716
758
  showRule(show = true) {
717
759
  if (show) {
@@ -835,7 +877,6 @@ class StageMask extends Rule {
835
877
  if (this.maxScrollLeft > 0) {
836
878
  this.scrollLeft = this.scrollLeft + deltaX;
837
879
  }
838
- this.fixScrollValue();
839
880
  this.scroll();
840
881
  this.emit("scroll", event);
841
882
  };
@@ -890,9 +931,10 @@ class StageMask extends Rule {
890
931
  const { clientHeight, clientWidth } = entry.target;
891
932
  this.setHeight(clientHeight);
892
933
  this.setWidth(clientWidth);
893
- this.fixScrollValue();
894
934
  this.scroll();
895
- this.core.dr.updateMoveable();
935
+ if (this.core.dr.moveable) {
936
+ this.core.dr.updateMoveable();
937
+ }
896
938
  });
897
939
  this.pageResizeObserver.observe(page);
898
940
  this.wrapperResizeObserver = new ResizeObserver((entries) => {
@@ -932,6 +974,7 @@ class StageMask extends Rule {
932
974
  super.destroy();
933
975
  }
934
976
  scroll() {
977
+ this.fixScrollValue();
935
978
  let { scrollLeft, scrollTop } = this;
936
979
  if (this.pageScrollParent) {
937
980
  this.pageScrollParent.scrollTo({
@@ -960,10 +1003,10 @@ class StageMask extends Rule {
960
1003
  this.content.style.width = `${width}px`;
961
1004
  }
962
1005
  setMaxScrollLeft() {
963
- this.maxScrollLeft = this.width - this.wrapperWidth;
1006
+ this.maxScrollLeft = Math.max(this.width - this.wrapperWidth, 0);
964
1007
  }
965
1008
  setMaxScrollTop() {
966
- this.maxScrollTop = this.height - this.wrapperHeight;
1009
+ this.maxScrollTop = Math.max(this.height - this.wrapperHeight, 0);
967
1010
  }
968
1011
  fixScrollValue() {
969
1012
  if (this.scrollTop < 0)
@@ -1009,16 +1052,6 @@ class StageRender extends EventEmitter {
1009
1052
  this.iframe?.contentDocument?.body?.appendChild(el);
1010
1053
  }
1011
1054
  }
1012
- if (this.contentWindow) {
1013
- const style = this.contentWindow.document.createElement("style");
1014
- style.id = "tmagic-stage-render";
1015
- style.innerHTML = `
1016
- .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
1017
- z-index: ${ZIndex.SELECTED_EL};
1018
- }
1019
- `;
1020
- this.contentWindow.document.head.appendChild(style);
1021
- }
1022
1055
  };
1023
1056
  this.core = core;
1024
1057
  this.runtimeUrl = core.config.runtimeUrl || "";