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

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.
@@ -31,6 +31,8 @@ var Mode = /* @__PURE__ */ ((Mode2) => {
31
31
  return Mode2;
32
32
  })(Mode || {});
33
33
  const SELECTED_CLASS = "tmagic-stage-selected-area";
34
+ const H_GUIDE_LINE_STORAGE_KEY = "$MagicStageHorizontalGuidelinesData";
35
+ const V_GUIDE_LINE_STORAGE_KEY = "$MagicStageVerticalGuidelinesData";
34
36
 
35
37
  const getParents = (el, relative) => {
36
38
  let cur = el.parentElement;
@@ -177,12 +179,29 @@ const addSelectedClassName = (el, doc) => {
177
179
  item.classList.add(`${SELECTED_CLASS}-parents`);
178
180
  });
179
181
  };
182
+ const getGuideLineFromCache = (type) => {
183
+ const key = {
184
+ [GuidesType.HORIZONTAL]: H_GUIDE_LINE_STORAGE_KEY,
185
+ [GuidesType.VERTICAL]: V_GUIDE_LINE_STORAGE_KEY
186
+ }[type];
187
+ if (!key)
188
+ return [];
189
+ const guideLineCacheData = globalThis.localStorage.getItem(key);
190
+ if (guideLineCacheData) {
191
+ try {
192
+ return JSON.parse(guideLineCacheData) || [];
193
+ } catch (e) {
194
+ console.error(e);
195
+ }
196
+ }
197
+ return [];
198
+ };
180
199
 
181
200
  class StageDragResize extends EventEmitter {
182
201
  constructor(config) {
183
202
  super();
184
- this.horizontalGuidelines = [];
185
- this.verticalGuidelines = [];
203
+ this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
204
+ this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
186
205
  this.elementGuidelines = [];
187
206
  this.mode = Mode.ABSOLUTE;
188
207
  this.moveableOptions = {};
@@ -253,10 +272,32 @@ class StageDragResize extends EventEmitter {
253
272
  this.mode = getMode(el);
254
273
  this.destroyGhostEl();
255
274
  this.updateDragEl(el);
275
+ this.setElementGuidelines(el);
256
276
  this.moveableOptions = this.getOptions({
257
277
  target: this.dragEl
258
278
  });
259
279
  }
280
+ setElementGuidelines(el) {
281
+ const nodes = el.parentElement?.children || [];
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 === el)
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");
@@ -643,8 +676,8 @@ class StageHighlight extends EventEmitter {
643
676
  class Rule extends EventEmitter$1 {
644
677
  constructor(container) {
645
678
  super();
646
- this.horizontalGuidelines = [];
647
- this.verticalGuidelines = [];
679
+ this.horizontalGuidelines = getGuideLineFromCache(GuidesType.HORIZONTAL);
680
+ this.verticalGuidelines = getGuideLineFromCache(GuidesType.VERTICAL);
648
681
  this.getGuidesStyle = (type) => ({
649
682
  position: "fixed",
650
683
  zIndex: 1,
@@ -668,6 +701,7 @@ class Rule extends EventEmitter$1 {
668
701
  type: GuidesType.HORIZONTAL,
669
702
  guides: this.horizontalGuidelines
670
703
  });
704
+ globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
671
705
  };
672
706
  this.vGuidesChangeGuidesHandler = (e) => {
673
707
  this.verticalGuidelines = e.guides;
@@ -675,10 +709,11 @@ class Rule extends EventEmitter$1 {
675
709
  type: GuidesType.VERTICAL,
676
710
  guides: this.verticalGuidelines
677
711
  });
712
+ globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
678
713
  };
679
714
  this.container = container;
680
- this.hGuides = this.createGuides(GuidesType.HORIZONTAL);
681
- this.vGuides = this.createGuides(GuidesType.VERTICAL);
715
+ this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
716
+ this.vGuides = this.createGuides(GuidesType.VERTICAL, this.verticalGuidelines);
682
717
  this.hGuides.on("changeGuides", this.hGuidesChangeGuidesHandler);
683
718
  this.vGuides.on("changeGuides", this.vGuidesChangeGuidesHandler);
684
719
  this.containerResizeObserver = new ResizeObserver(() => {
@@ -712,6 +747,8 @@ class Rule extends EventEmitter$1 {
712
747
  type: GuidesType.HORIZONTAL,
713
748
  guides: []
714
749
  });
750
+ globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, "[]");
751
+ globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, "[]");
715
752
  }
716
753
  showRule(show = true) {
717
754
  if (show) {
@@ -835,7 +872,6 @@ class StageMask extends Rule {
835
872
  if (this.maxScrollLeft > 0) {
836
873
  this.scrollLeft = this.scrollLeft + deltaX;
837
874
  }
838
- this.fixScrollValue();
839
875
  this.scroll();
840
876
  this.emit("scroll", event);
841
877
  };
@@ -890,9 +926,10 @@ class StageMask extends Rule {
890
926
  const { clientHeight, clientWidth } = entry.target;
891
927
  this.setHeight(clientHeight);
892
928
  this.setWidth(clientWidth);
893
- this.fixScrollValue();
894
929
  this.scroll();
895
- this.core.dr.updateMoveable();
930
+ if (this.core.dr.moveable) {
931
+ this.core.dr.updateMoveable();
932
+ }
896
933
  });
897
934
  this.pageResizeObserver.observe(page);
898
935
  this.wrapperResizeObserver = new ResizeObserver((entries) => {
@@ -932,6 +969,7 @@ class StageMask extends Rule {
932
969
  super.destroy();
933
970
  }
934
971
  scroll() {
972
+ this.fixScrollValue();
935
973
  let { scrollLeft, scrollTop } = this;
936
974
  if (this.pageScrollParent) {
937
975
  this.pageScrollParent.scrollTo({
@@ -960,10 +998,10 @@ class StageMask extends Rule {
960
998
  this.content.style.width = `${width}px`;
961
999
  }
962
1000
  setMaxScrollLeft() {
963
- this.maxScrollLeft = this.width - this.wrapperWidth;
1001
+ this.maxScrollLeft = Math.max(this.width - this.wrapperWidth, 0);
964
1002
  }
965
1003
  setMaxScrollTop() {
966
- this.maxScrollTop = this.height - this.wrapperHeight;
1004
+ this.maxScrollTop = Math.max(this.height - this.wrapperHeight, 0);
967
1005
  }
968
1006
  fixScrollValue() {
969
1007
  if (this.scrollTop < 0)