@tmagic/stage 1.0.0-beta.10 → 1.0.0-beta.13

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.
@@ -170,21 +170,14 @@
170
170
  };
171
171
 
172
172
  class StageDragResize extends EventEmitter.EventEmitter {
173
- core;
174
- container;
175
- target;
176
- dragEl;
177
- moveable;
178
- horizontalGuidelines = [];
179
- verticalGuidelines = [];
180
- elementGuidelines = [];
181
- moveableOptions = {};
182
- dragStatus = "end" /* END */;
183
- ghostEl;
184
- mode = Mode.ABSOLUTE;
185
- moveableHelper;
186
173
  constructor(config) {
187
174
  super();
175
+ this.horizontalGuidelines = [];
176
+ this.verticalGuidelines = [];
177
+ this.elementGuidelines = [];
178
+ this.moveableOptions = {};
179
+ this.dragStatus = "end" /* END */;
180
+ this.mode = Mode.ABSOLUTE;
188
181
  this.core = config.core;
189
182
  this.container = config.container;
190
183
  this.dragEl = globalThis.document.createElement("div");
@@ -416,6 +409,7 @@
416
409
  if (!this.target)
417
410
  return {};
418
411
  const isAbsolute = this.mode === Mode.ABSOLUTE;
412
+ const isFixed = this.mode === Mode.FIXED;
419
413
  let { moveableOptions = {} } = this.core.config;
420
414
  if (typeof moveableOptions === "function") {
421
415
  moveableOptions = moveableOptions(this.core);
@@ -427,8 +421,8 @@
427
421
  dragArea: false,
428
422
  draggable: true,
429
423
  resizable: true,
430
- snappable: isAbsolute,
431
- snapGap: isAbsolute,
424
+ snappable: isAbsolute || isFixed,
425
+ snapGap: isAbsolute || isFixed,
432
426
  snapThreshold: 5,
433
427
  snapDigit: 0,
434
428
  throttleDrag: 0,
@@ -520,10 +514,6 @@
520
514
  };
521
515
 
522
516
  class StageHighlight extends EventEmitter.EventEmitter {
523
- core;
524
- container;
525
- target;
526
- moveable;
527
517
  constructor(config) {
528
518
  super();
529
519
  this.core = config.core;
@@ -543,6 +533,7 @@
543
533
  clearHighlight() {
544
534
  if (!this.moveable)
545
535
  return;
536
+ this.target = void 0;
546
537
  this.moveable.target = null;
547
538
  this.moveable.updateTarget();
548
539
  }
@@ -552,14 +543,41 @@
552
543
  }
553
544
 
554
545
  class Rule extends EventEmitter__default["default"] {
555
- hGuides;
556
- vGuides;
557
- horizontalGuidelines = [];
558
- verticalGuidelines = [];
559
- container;
560
- containerResizeObserver;
561
546
  constructor(container) {
562
547
  super();
548
+ this.horizontalGuidelines = [];
549
+ this.verticalGuidelines = [];
550
+ this.getGuidesStyle = (type) => ({
551
+ position: "fixed",
552
+ zIndex: 1,
553
+ left: type === GuidesType.HORIZONTAL ? 0 : "-30px",
554
+ top: type === GuidesType.HORIZONTAL ? "-30px" : 0,
555
+ width: type === GuidesType.HORIZONTAL ? "100%" : "30px",
556
+ height: type === GuidesType.HORIZONTAL ? "30px" : "100%"
557
+ });
558
+ this.createGuides = (type, defaultGuides = []) => new Guides__default["default"](this.container, {
559
+ type,
560
+ defaultGuides,
561
+ displayDragPos: true,
562
+ backgroundColor: "#fff",
563
+ lineColor: "#000",
564
+ textColor: "#000",
565
+ style: this.getGuidesStyle(type)
566
+ });
567
+ this.hGuidesChangeGuidesHandler = (e) => {
568
+ this.horizontalGuidelines = e.guides;
569
+ this.emit("changeGuides", {
570
+ type: GuidesType.HORIZONTAL,
571
+ guides: this.horizontalGuidelines
572
+ });
573
+ };
574
+ this.vGuidesChangeGuidesHandler = (e) => {
575
+ this.verticalGuidelines = e.guides;
576
+ this.emit("changeGuides", {
577
+ type: GuidesType.VERTICAL,
578
+ guides: this.verticalGuidelines
579
+ });
580
+ };
563
581
  this.container = container;
564
582
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL);
565
583
  this.vGuides = this.createGuides(GuidesType.VERTICAL);
@@ -628,41 +646,10 @@
628
646
  this.containerResizeObserver.disconnect();
629
647
  this.removeAllListeners();
630
648
  }
631
- getGuidesStyle = (type) => ({
632
- position: "fixed",
633
- zIndex: 1,
634
- left: type === GuidesType.HORIZONTAL ? 0 : "-30px",
635
- top: type === GuidesType.HORIZONTAL ? "-30px" : 0,
636
- width: type === GuidesType.HORIZONTAL ? "100%" : "30px",
637
- height: type === GuidesType.HORIZONTAL ? "30px" : "100%"
638
- });
639
- createGuides = (type, defaultGuides = []) => new Guides__default["default"](this.container, {
640
- type,
641
- defaultGuides,
642
- displayDragPos: true,
643
- backgroundColor: "#fff",
644
- lineColor: "#000",
645
- textColor: "#000",
646
- style: this.getGuidesStyle(type)
647
- });
648
- hGuidesChangeGuidesHandler = (e) => {
649
- this.horizontalGuidelines = e.guides;
650
- this.emit("changeGuides", {
651
- type: GuidesType.HORIZONTAL,
652
- guides: this.horizontalGuidelines
653
- });
654
- };
655
- vGuidesChangeGuidesHandler = (e) => {
656
- this.verticalGuidelines = e.guides;
657
- this.emit("changeGuides", {
658
- type: GuidesType.VERTICAL,
659
- guides: this.verticalGuidelines
660
- });
661
- };
662
649
  }
663
650
 
664
651
  const wrapperClassName = "editor-mask-wrapper";
665
- const throttleTime = 300;
652
+ const throttleTime = 100;
666
653
  const hideScrollbar = () => {
667
654
  const style = globalThis.document.createElement("style");
668
655
  style.innerHTML = `
@@ -696,32 +683,73 @@
696
683
  return el;
697
684
  };
698
685
  class StageMask extends Rule {
699
- content = createContent();
700
- wrapper;
701
- core;
702
- page = null;
703
- pageScrollParent = null;
704
- scrollTop = 0;
705
- scrollLeft = 0;
706
- width = 0;
707
- height = 0;
708
- wrapperHeight = 0;
709
- wrapperWidth = 0;
710
- mode = Mode.ABSOLUTE;
711
- pageResizeObserver = null;
712
- wrapperResizeObserver = null;
713
- highlightHandler = lodashEs.throttle((event) => {
714
- this.emit("highlight", event);
715
- }, throttleTime);
716
686
  constructor(config) {
717
687
  const wrapper = createWrapper();
718
688
  super(wrapper);
689
+ this.content = createContent();
690
+ this.page = null;
691
+ this.pageScrollParent = null;
692
+ this.scrollTop = 0;
693
+ this.scrollLeft = 0;
694
+ this.width = 0;
695
+ this.height = 0;
696
+ this.wrapperHeight = 0;
697
+ this.wrapperWidth = 0;
698
+ this.maxScrollTop = 0;
699
+ this.maxScrollLeft = 0;
700
+ this.mode = Mode.ABSOLUTE;
701
+ this.pageResizeObserver = null;
702
+ this.wrapperResizeObserver = null;
703
+ this.highlightHandler = lodashEs.throttle((event) => {
704
+ this.emit("highlight", event);
705
+ }, throttleTime);
706
+ this.mouseDownHandler = (event) => {
707
+ this.emit("clearHighlight");
708
+ event.stopImmediatePropagation();
709
+ event.stopPropagation();
710
+ if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT)
711
+ return;
712
+ if (event.target.className.indexOf("moveable-control") !== -1) {
713
+ return;
714
+ }
715
+ this.content.removeEventListener("mousemove", this.highlightHandler);
716
+ this.emit("beforeSelect", event);
717
+ globalThis.document.addEventListener("mouseup", this.mouseUpHandler);
718
+ };
719
+ this.mouseUpHandler = () => {
720
+ globalThis.document.removeEventListener("mouseup", this.mouseUpHandler);
721
+ this.content.addEventListener("mousemove", this.highlightHandler);
722
+ this.emit("select");
723
+ };
724
+ this.mouseWheelHandler = (event) => {
725
+ this.emit("clearHighlight");
726
+ if (!this.page)
727
+ throw new Error("page \u672A\u521D\u59CB\u5316");
728
+ const { deltaY, deltaX } = event;
729
+ if (this.page.clientHeight < this.wrapperHeight && deltaY)
730
+ return;
731
+ if (this.page.clientWidth < this.wrapperWidth && deltaX)
732
+ return;
733
+ if (this.maxScrollTop > 0) {
734
+ this.scrollTop = this.scrollTop + deltaY;
735
+ }
736
+ if (this.maxScrollLeft > 0) {
737
+ this.scrollLeft = this.scrollLeft + deltaX;
738
+ }
739
+ this.fixScrollValue();
740
+ this.scroll();
741
+ this.emit("scroll", event);
742
+ };
743
+ this.mouseLeaveHandler = () => {
744
+ setTimeout(() => this.emit("clearHighlight"), throttleTime);
745
+ };
719
746
  this.wrapper = wrapper;
720
747
  this.core = config.core;
721
748
  this.content.addEventListener("mousedown", this.mouseDownHandler);
722
749
  this.wrapper.appendChild(this.content);
723
750
  this.content.addEventListener("wheel", this.mouseWheelHandler);
724
751
  this.content.addEventListener("mousemove", this.highlightHandler);
752
+ this.content.addEventListener("mouseleave", this.mouseLeaveHandler);
725
753
  }
726
754
  setMode(mode) {
727
755
  this.mode = mode;
@@ -746,6 +774,9 @@
746
774
  const { clientHeight, clientWidth } = entry.target;
747
775
  this.setHeight(clientHeight);
748
776
  this.setWidth(clientWidth);
777
+ this.fixScrollValue();
778
+ this.scroll();
779
+ this.core.dr.updateMoveable();
749
780
  });
750
781
  this.pageResizeObserver.observe(page);
751
782
  this.wrapperResizeObserver = new ResizeObserver((entries) => {
@@ -753,6 +784,8 @@
753
784
  const { clientHeight, clientWidth } = entry.target;
754
785
  this.wrapperHeight = clientHeight;
755
786
  this.wrapperWidth = clientWidth;
787
+ this.setMaxScrollLeft();
788
+ this.setMaxScrollTop();
756
789
  });
757
790
  this.wrapperResizeObserver.observe(this.wrapper);
758
791
  }
@@ -771,10 +804,17 @@
771
804
  this.pageScrollParent = null;
772
805
  this.pageResizeObserver?.disconnect();
773
806
  this.wrapperResizeObserver?.disconnect();
807
+ this.content.removeEventListener("mouseleave", this.mouseLeaveHandler);
774
808
  super.destroy();
775
809
  }
776
810
  scroll() {
777
811
  let { scrollLeft, scrollTop } = this;
812
+ if (this.pageScrollParent) {
813
+ this.pageScrollParent.scrollTo({
814
+ top: scrollTop,
815
+ left: scrollLeft
816
+ });
817
+ }
778
818
  if (this.mode === Mode.FIXED) {
779
819
  scrollLeft = 0;
780
820
  scrollTop = 0;
@@ -787,75 +827,75 @@
787
827
  }
788
828
  setHeight(height) {
789
829
  this.height = height;
830
+ this.setMaxScrollTop();
790
831
  this.content.style.height = `${height}px`;
791
832
  }
792
833
  setWidth(width) {
793
834
  this.width = width;
835
+ this.setMaxScrollLeft();
794
836
  this.content.style.width = `${width}px`;
795
837
  }
796
- mouseDownHandler = (event) => {
797
- this.emit("clearHighlight");
798
- event.stopImmediatePropagation();
799
- event.stopPropagation();
800
- if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT)
801
- return;
802
- if (event.target.className.indexOf("moveable-control") !== -1) {
803
- return;
804
- }
805
- this.content.removeEventListener("mousemove", this.highlightHandler);
806
- this.emit("beforeSelect", event);
807
- globalThis.document.addEventListener("mouseup", this.mouseUpHandler);
808
- };
809
- mouseUpHandler = () => {
810
- globalThis.document.removeEventListener("mouseup", this.mouseUpHandler);
811
- this.content.addEventListener("mousemove", this.highlightHandler);
812
- this.emit("select");
813
- };
814
- mouseWheelHandler = (event) => {
815
- this.emit("clearHighlight");
816
- if (!this.page)
817
- throw new Error("page \u672A\u521D\u59CB\u5316");
818
- const { deltaY, deltaX } = event;
819
- const { height, wrapperHeight, width, wrapperWidth } = this;
820
- const maxScrollTop = height - wrapperHeight;
821
- const maxScrollLeft = width - wrapperWidth;
822
- if (maxScrollTop > 0) {
823
- if (deltaY > 0) {
824
- this.scrollTop = this.scrollTop + Math.min(maxScrollTop - this.scrollTop, deltaY);
825
- } else {
826
- this.scrollTop = Math.max(this.scrollTop + deltaY, 0);
827
- }
828
- }
829
- if (width > wrapperWidth) {
830
- if (deltaX > 0) {
831
- this.scrollLeft = this.scrollLeft + Math.min(maxScrollLeft - this.scrollLeft, deltaX);
832
- } else {
833
- this.scrollLeft = Math.max(this.scrollLeft + deltaX, 0);
834
- }
835
- }
836
- if (this.mode !== Mode.FIXED) {
837
- this.scrollTo(this.scrollLeft, this.scrollTop);
838
- }
839
- if (this.pageScrollParent) {
840
- this.pageScrollParent.scrollTo({
841
- top: this.scrollTop,
842
- left: this.scrollLeft
843
- });
844
- }
845
- this.scroll();
846
- this.emit("scroll", event);
847
- };
838
+ setMaxScrollLeft() {
839
+ this.maxScrollLeft = this.width - this.wrapperWidth;
840
+ }
841
+ setMaxScrollTop() {
842
+ this.maxScrollTop = this.height - this.wrapperHeight;
843
+ }
844
+ fixScrollValue() {
845
+ if (this.scrollTop < 0)
846
+ this.scrollTop = 0;
847
+ if (this.scrollLeft < 0)
848
+ this.scrollLeft = 0;
849
+ if (this.maxScrollTop < this.scrollTop)
850
+ this.scrollTop = this.maxScrollTop;
851
+ if (this.maxScrollLeft < this.scrollLeft)
852
+ this.scrollLeft = this.maxScrollLeft;
853
+ }
848
854
  }
849
855
 
850
856
  class StageRender extends EventEmitter.EventEmitter {
851
- contentWindow = null;
852
- runtime = null;
853
- iframe;
854
- runtimeUrl;
855
- core;
856
- render;
857
857
  constructor({ core }) {
858
858
  super();
859
+ this.contentWindow = null;
860
+ this.runtime = null;
861
+ this.getMagicApi = () => ({
862
+ onPageElUpdate: (el) => this.emit("page-el-update", el),
863
+ onRuntimeReady: (runtime) => {
864
+ this.runtime = runtime;
865
+ globalThis.runtime = runtime;
866
+ this.emit("runtime-ready", runtime);
867
+ }
868
+ });
869
+ this.getRuntime = () => {
870
+ if (this.runtime)
871
+ return Promise.resolve(this.runtime);
872
+ return new Promise((resolve) => {
873
+ const listener = (runtime) => {
874
+ this.off("runtime-ready", listener);
875
+ resolve(runtime);
876
+ };
877
+ this.on("runtime-ready", listener);
878
+ });
879
+ };
880
+ this.loadHandler = async () => {
881
+ this.emit("onload");
882
+ if (this.render) {
883
+ const el = await this.render(this.core);
884
+ if (el) {
885
+ this.iframe?.contentDocument?.body?.appendChild(el);
886
+ }
887
+ }
888
+ if (this.contentWindow) {
889
+ const style = this.contentWindow.document.createElement("style");
890
+ style.id = "tmagic-stage-render";
891
+ style.innerHTML = `
892
+ .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
893
+ z-index: ${ZIndex.SELECTED_EL};
894
+ }
895
+ `;
896
+ this.contentWindow.document.head.appendChild(style);
897
+ }
898
+ };
859
899
  this.core = core;
860
900
  this.runtimeUrl = core.config.runtimeUrl || "";
861
901
  this.render = core.config.render;
@@ -868,14 +908,6 @@
868
908
  `;
869
909
  this.iframe.addEventListener("load", this.loadHandler);
870
910
  }
871
- getMagicApi = () => ({
872
- onPageElUpdate: (el) => this.emit("page-el-update", el),
873
- onRuntimeReady: (runtime) => {
874
- this.runtime = runtime;
875
- globalThis.runtime = runtime;
876
- this.emit("runtime-ready", runtime);
877
- }
878
- });
879
911
  async mount(el) {
880
912
  if (this.iframe) {
881
913
  if (!isSameDomain(this.runtimeUrl) && this.runtimeUrl) {
@@ -892,17 +924,6 @@
892
924
  throw Error("mount \u5931\u8D25");
893
925
  }
894
926
  }
895
- getRuntime = () => {
896
- if (this.runtime)
897
- return Promise.resolve(this.runtime);
898
- return new Promise((resolve) => {
899
- const listener = (runtime) => {
900
- this.off("runtime-ready", listener);
901
- resolve(runtime);
902
- };
903
- this.on("runtime-ready", listener);
904
- });
905
- };
906
927
  destroy() {
907
928
  this.iframe?.removeEventListener("load", this.loadHandler);
908
929
  this.contentWindow = null;
@@ -910,40 +931,12 @@
910
931
  this.iframe = void 0;
911
932
  this.removeAllListeners();
912
933
  }
913
- loadHandler = async () => {
914
- this.emit("onload");
915
- if (this.render) {
916
- const el = await this.render(this.core);
917
- if (el) {
918
- this.iframe?.contentDocument?.body?.appendChild(el);
919
- }
920
- }
921
- if (this.contentWindow) {
922
- const style = this.contentWindow.document.createElement("style");
923
- style.id = "tmagic-stage-render";
924
- style.innerHTML = `
925
- .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
926
- z-index: ${ZIndex.SELECTED_EL};
927
- }
928
- `;
929
- this.contentWindow.document.head.appendChild(style);
930
- }
931
- };
932
934
  }
933
935
 
934
936
  class StageCore extends EventEmitter.EventEmitter {
935
- selectedDom;
936
- highlightedDom;
937
- renderer;
938
- mask;
939
- dr;
940
- highlightLayer;
941
- config;
942
- zoom = DEFAULT_ZOOM;
943
- container;
944
- canSelect;
945
937
  constructor(config) {
946
938
  super();
939
+ this.zoom = DEFAULT_ZOOM;
947
940
  this.config = config;
948
941
  this.setZoom(config.zoom);
949
942
  this.canSelect = config.canSelect || ((el) => !!el.id);
@@ -1030,7 +1023,7 @@
1030
1023
  }
1031
1024
  update(data) {
1032
1025
  const { config } = data;
1033
- this.renderer?.getRuntime().then((runtime) => {
1026
+ return this.renderer?.getRuntime().then((runtime) => {
1034
1027
  runtime?.update?.(data);
1035
1028
  setTimeout(() => {
1036
1029
  const el = this.renderer.contentWindow?.document.getElementById(`${config.id}`);
@@ -1056,13 +1049,13 @@
1056
1049
  this.highlightedDom = el;
1057
1050
  }
1058
1051
  sortNode(data) {
1059
- this.renderer?.getRuntime().then((runtime) => runtime?.sortNode?.(data));
1052
+ return this.renderer?.getRuntime().then((runtime) => runtime?.sortNode?.(data));
1060
1053
  }
1061
1054
  add(data) {
1062
- this.renderer?.getRuntime().then((runtime) => runtime?.add?.(data));
1055
+ return this.renderer?.getRuntime().then((runtime) => runtime?.add?.(data));
1063
1056
  }
1064
1057
  remove(data) {
1065
- this.renderer?.getRuntime().then((runtime) => runtime?.remove?.(data));
1058
+ return this.renderer?.getRuntime().then((runtime) => runtime?.remove?.(data));
1066
1059
  }
1067
1060
  setZoom(zoom = DEFAULT_ZOOM) {
1068
1061
  this.zoom = zoom;
@@ -1103,8 +1096,7 @@
1103
1096
  exports.StageRender = StageRender;
1104
1097
  exports["default"] = StageCore;
1105
1098
 
1106
- Object.defineProperty(exports, '__esModule', { value: true });
1107
- exports[Symbol.toStringTag] = 'Module';
1099
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
1108
1100
 
1109
1101
  }));
1110
1102
  //# sourceMappingURL=tmagic-stage.umd.js.map