@tmagic/stage 1.0.0-rc.2 → 1.0.0-rc.5

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) {
@@ -795,6 +837,7 @@ class StageMask extends Rule {
795
837
  this.wrapperWidth = 0;
796
838
  this.maxScrollTop = 0;
797
839
  this.maxScrollLeft = 0;
840
+ this.intersectionObserver = null;
798
841
  this.mode = Mode.ABSOLUTE;
799
842
  this.pageResizeObserver = null;
800
843
  this.wrapperResizeObserver = null;
@@ -834,7 +877,6 @@ class StageMask extends Rule {
834
877
  if (this.maxScrollLeft > 0) {
835
878
  this.scrollLeft = this.scrollLeft + deltaX;
836
879
  }
837
- this.fixScrollValue();
838
880
  this.scroll();
839
881
  this.emit("scroll", event);
840
882
  };
@@ -866,15 +908,33 @@ class StageMask extends Rule {
866
908
  this.page = page;
867
909
  this.pageScrollParent = getScrollParent(page) || this.core.renderer.contentWindow?.document.documentElement || null;
868
910
  this.pageResizeObserver?.disconnect();
911
+ this.wrapperResizeObserver?.disconnect();
912
+ this.intersectionObserver?.disconnect();
913
+ if (typeof IntersectionObserver !== "undefined") {
914
+ this.intersectionObserver = new IntersectionObserver((entries) => {
915
+ entries.forEach((entry) => {
916
+ const { target, intersectionRatio } = entry;
917
+ if (intersectionRatio <= 0) {
918
+ this.scrollIntoView(target);
919
+ }
920
+ this.intersectionObserver?.unobserve(target);
921
+ });
922
+ }, {
923
+ root: this.pageScrollParent,
924
+ rootMargin: "0px",
925
+ threshold: 1
926
+ });
927
+ }
869
928
  if (typeof ResizeObserver !== "undefined") {
870
929
  this.pageResizeObserver = new ResizeObserver((entries) => {
871
930
  const [entry] = entries;
872
931
  const { clientHeight, clientWidth } = entry.target;
873
932
  this.setHeight(clientHeight);
874
933
  this.setWidth(clientWidth);
875
- this.fixScrollValue();
876
934
  this.scroll();
877
- this.core.dr.updateMoveable();
935
+ if (this.core.dr.moveable) {
936
+ this.core.dr.updateMoveable();
937
+ }
878
938
  });
879
939
  this.pageResizeObserver.observe(page);
880
940
  this.wrapperResizeObserver = new ResizeObserver((entries) => {
@@ -897,8 +957,6 @@ class StageMask extends Rule {
897
957
  this.setMode(isFixedParent(el) ? Mode.FIXED : Mode.ABSOLUTE);
898
958
  }
899
959
  scrollIntoView(el) {
900
- if (this.mode === Mode.FIXED)
901
- return;
902
960
  el.scrollIntoView();
903
961
  if (!this.pageScrollParent)
904
962
  return;
@@ -916,6 +974,7 @@ class StageMask extends Rule {
916
974
  super.destroy();
917
975
  }
918
976
  scroll() {
977
+ this.fixScrollValue();
919
978
  let { scrollLeft, scrollTop } = this;
920
979
  if (this.pageScrollParent) {
921
980
  this.pageScrollParent.scrollTo({
@@ -944,10 +1003,10 @@ class StageMask extends Rule {
944
1003
  this.content.style.width = `${width}px`;
945
1004
  }
946
1005
  setMaxScrollLeft() {
947
- this.maxScrollLeft = this.width - this.wrapperWidth;
1006
+ this.maxScrollLeft = Math.max(this.width - this.wrapperWidth, 0);
948
1007
  }
949
1008
  setMaxScrollTop() {
950
- this.maxScrollTop = this.height - this.wrapperHeight;
1009
+ this.maxScrollTop = Math.max(this.height - this.wrapperHeight, 0);
951
1010
  }
952
1011
  fixScrollValue() {
953
1012
  if (this.scrollTop < 0)
@@ -1121,7 +1180,9 @@ class StageCore extends EventEmitter {
1121
1180
  }
1122
1181
  this.mask.setLayout(el);
1123
1182
  this.dr.select(el, event);
1124
- this.mask.scrollIntoView(el);
1183
+ if (this.config.autoScrollIntoView || el.dataset.autoScrollIntoView) {
1184
+ this.mask.intersectionObserver?.observe(el);
1185
+ }
1125
1186
  this.selectedDom = el;
1126
1187
  if (this.renderer.contentWindow) {
1127
1188
  removeSelectedClassName(this.renderer.contentWindow.document);