@tmagic/stage 1.0.0-beta.12 → 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.
@@ -163,21 +163,14 @@ const addSelectedClassName = (el) => {
163
163
  };
164
164
 
165
165
  class StageDragResize extends EventEmitter {
166
- core;
167
- container;
168
- target;
169
- dragEl;
170
- moveable;
171
- horizontalGuidelines = [];
172
- verticalGuidelines = [];
173
- elementGuidelines = [];
174
- moveableOptions = {};
175
- dragStatus = "end" /* END */;
176
- ghostEl;
177
- mode = Mode.ABSOLUTE;
178
- moveableHelper;
179
166
  constructor(config) {
180
167
  super();
168
+ this.horizontalGuidelines = [];
169
+ this.verticalGuidelines = [];
170
+ this.elementGuidelines = [];
171
+ this.moveableOptions = {};
172
+ this.dragStatus = "end" /* END */;
173
+ this.mode = Mode.ABSOLUTE;
181
174
  this.core = config.core;
182
175
  this.container = config.container;
183
176
  this.dragEl = globalThis.document.createElement("div");
@@ -514,10 +507,6 @@ const up = (deltaTop, target) => {
514
507
  };
515
508
 
516
509
  class StageHighlight extends EventEmitter {
517
- core;
518
- container;
519
- target;
520
- moveable;
521
510
  constructor(config) {
522
511
  super();
523
512
  this.core = config.core;
@@ -547,14 +536,41 @@ class StageHighlight extends EventEmitter {
547
536
  }
548
537
 
549
538
  class Rule extends EventEmitter$1 {
550
- hGuides;
551
- vGuides;
552
- horizontalGuidelines = [];
553
- verticalGuidelines = [];
554
- container;
555
- containerResizeObserver;
556
539
  constructor(container) {
557
540
  super();
541
+ this.horizontalGuidelines = [];
542
+ this.verticalGuidelines = [];
543
+ this.getGuidesStyle = (type) => ({
544
+ position: "fixed",
545
+ zIndex: 1,
546
+ left: type === GuidesType.HORIZONTAL ? 0 : "-30px",
547
+ top: type === GuidesType.HORIZONTAL ? "-30px" : 0,
548
+ width: type === GuidesType.HORIZONTAL ? "100%" : "30px",
549
+ height: type === GuidesType.HORIZONTAL ? "30px" : "100%"
550
+ });
551
+ this.createGuides = (type, defaultGuides = []) => new Guides(this.container, {
552
+ type,
553
+ defaultGuides,
554
+ displayDragPos: true,
555
+ backgroundColor: "#fff",
556
+ lineColor: "#000",
557
+ textColor: "#000",
558
+ style: this.getGuidesStyle(type)
559
+ });
560
+ this.hGuidesChangeGuidesHandler = (e) => {
561
+ this.horizontalGuidelines = e.guides;
562
+ this.emit("changeGuides", {
563
+ type: GuidesType.HORIZONTAL,
564
+ guides: this.horizontalGuidelines
565
+ });
566
+ };
567
+ this.vGuidesChangeGuidesHandler = (e) => {
568
+ this.verticalGuidelines = e.guides;
569
+ this.emit("changeGuides", {
570
+ type: GuidesType.VERTICAL,
571
+ guides: this.verticalGuidelines
572
+ });
573
+ };
558
574
  this.container = container;
559
575
  this.hGuides = this.createGuides(GuidesType.HORIZONTAL);
560
576
  this.vGuides = this.createGuides(GuidesType.VERTICAL);
@@ -623,37 +639,6 @@ class Rule extends EventEmitter$1 {
623
639
  this.containerResizeObserver.disconnect();
624
640
  this.removeAllListeners();
625
641
  }
626
- getGuidesStyle = (type) => ({
627
- position: "fixed",
628
- zIndex: 1,
629
- left: type === GuidesType.HORIZONTAL ? 0 : "-30px",
630
- top: type === GuidesType.HORIZONTAL ? "-30px" : 0,
631
- width: type === GuidesType.HORIZONTAL ? "100%" : "30px",
632
- height: type === GuidesType.HORIZONTAL ? "30px" : "100%"
633
- });
634
- createGuides = (type, defaultGuides = []) => new Guides(this.container, {
635
- type,
636
- defaultGuides,
637
- displayDragPos: true,
638
- backgroundColor: "#fff",
639
- lineColor: "#000",
640
- textColor: "#000",
641
- style: this.getGuidesStyle(type)
642
- });
643
- hGuidesChangeGuidesHandler = (e) => {
644
- this.horizontalGuidelines = e.guides;
645
- this.emit("changeGuides", {
646
- type: GuidesType.HORIZONTAL,
647
- guides: this.horizontalGuidelines
648
- });
649
- };
650
- vGuidesChangeGuidesHandler = (e) => {
651
- this.verticalGuidelines = e.guides;
652
- this.emit("changeGuides", {
653
- type: GuidesType.VERTICAL,
654
- guides: this.verticalGuidelines
655
- });
656
- };
657
642
  }
658
643
 
659
644
  const wrapperClassName = "editor-mask-wrapper";
@@ -691,28 +676,66 @@ const createWrapper = () => {
691
676
  return el;
692
677
  };
693
678
  class StageMask extends Rule {
694
- content = createContent();
695
- wrapper;
696
- core;
697
- page = null;
698
- pageScrollParent = null;
699
- scrollTop = 0;
700
- scrollLeft = 0;
701
- width = 0;
702
- height = 0;
703
- wrapperHeight = 0;
704
- wrapperWidth = 0;
705
- maxScrollTop = 0;
706
- maxScrollLeft = 0;
707
- mode = Mode.ABSOLUTE;
708
- pageResizeObserver = null;
709
- wrapperResizeObserver = null;
710
- highlightHandler = throttle((event) => {
711
- this.emit("highlight", event);
712
- }, throttleTime);
713
679
  constructor(config) {
714
680
  const wrapper = createWrapper();
715
681
  super(wrapper);
682
+ this.content = createContent();
683
+ this.page = null;
684
+ this.pageScrollParent = null;
685
+ this.scrollTop = 0;
686
+ this.scrollLeft = 0;
687
+ this.width = 0;
688
+ this.height = 0;
689
+ this.wrapperHeight = 0;
690
+ this.wrapperWidth = 0;
691
+ this.maxScrollTop = 0;
692
+ this.maxScrollLeft = 0;
693
+ this.mode = Mode.ABSOLUTE;
694
+ this.pageResizeObserver = null;
695
+ this.wrapperResizeObserver = null;
696
+ this.highlightHandler = throttle((event) => {
697
+ this.emit("highlight", event);
698
+ }, throttleTime);
699
+ this.mouseDownHandler = (event) => {
700
+ this.emit("clearHighlight");
701
+ event.stopImmediatePropagation();
702
+ event.stopPropagation();
703
+ if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT)
704
+ return;
705
+ if (event.target.className.indexOf("moveable-control") !== -1) {
706
+ return;
707
+ }
708
+ this.content.removeEventListener("mousemove", this.highlightHandler);
709
+ this.emit("beforeSelect", event);
710
+ globalThis.document.addEventListener("mouseup", this.mouseUpHandler);
711
+ };
712
+ this.mouseUpHandler = () => {
713
+ globalThis.document.removeEventListener("mouseup", this.mouseUpHandler);
714
+ this.content.addEventListener("mousemove", this.highlightHandler);
715
+ this.emit("select");
716
+ };
717
+ this.mouseWheelHandler = (event) => {
718
+ this.emit("clearHighlight");
719
+ if (!this.page)
720
+ throw new Error("page \u672A\u521D\u59CB\u5316");
721
+ const { deltaY, deltaX } = event;
722
+ if (this.page.clientHeight < this.wrapperHeight && deltaY)
723
+ return;
724
+ if (this.page.clientWidth < this.wrapperWidth && deltaX)
725
+ return;
726
+ if (this.maxScrollTop > 0) {
727
+ this.scrollTop = this.scrollTop + deltaY;
728
+ }
729
+ if (this.maxScrollLeft > 0) {
730
+ this.scrollLeft = this.scrollLeft + deltaX;
731
+ }
732
+ this.fixScrollValue();
733
+ this.scroll();
734
+ this.emit("scroll", event);
735
+ };
736
+ this.mouseLeaveHandler = () => {
737
+ setTimeout(() => this.emit("clearHighlight"), throttleTime);
738
+ };
716
739
  this.wrapper = wrapper;
717
740
  this.core = config.core;
718
741
  this.content.addEventListener("mousedown", this.mouseDownHandler);
@@ -821,53 +844,51 @@ class StageMask extends Rule {
821
844
  if (this.maxScrollLeft < this.scrollLeft)
822
845
  this.scrollLeft = this.maxScrollLeft;
823
846
  }
824
- mouseDownHandler = (event) => {
825
- this.emit("clearHighlight");
826
- event.stopImmediatePropagation();
827
- event.stopPropagation();
828
- if (event.button !== MouseButton.LEFT && event.button !== MouseButton.RIGHT)
829
- return;
830
- if (event.target.className.indexOf("moveable-control") !== -1) {
831
- return;
832
- }
833
- this.content.removeEventListener("mousemove", this.highlightHandler);
834
- this.emit("beforeSelect", event);
835
- globalThis.document.addEventListener("mouseup", this.mouseUpHandler);
836
- };
837
- mouseUpHandler = () => {
838
- globalThis.document.removeEventListener("mouseup", this.mouseUpHandler);
839
- this.content.addEventListener("mousemove", this.highlightHandler);
840
- this.emit("select");
841
- };
842
- mouseWheelHandler = (event) => {
843
- this.emit("clearHighlight");
844
- if (!this.page)
845
- throw new Error("page \u672A\u521D\u59CB\u5316");
846
- const { deltaY, deltaX } = event;
847
- if (this.maxScrollTop > 0) {
848
- this.scrollTop = this.scrollTop + deltaY;
849
- }
850
- if (this.maxScrollLeft > 0) {
851
- this.scrollLeft = this.scrollLeft + deltaX;
852
- }
853
- this.fixScrollValue();
854
- this.scroll();
855
- this.emit("scroll", event);
856
- };
857
- mouseLeaveHandler = () => {
858
- setTimeout(() => this.emit("clearHighlight"), throttleTime);
859
- };
860
847
  }
861
848
 
862
849
  class StageRender extends EventEmitter {
863
- contentWindow = null;
864
- runtime = null;
865
- iframe;
866
- runtimeUrl;
867
- core;
868
- render;
869
850
  constructor({ core }) {
870
851
  super();
852
+ this.contentWindow = null;
853
+ this.runtime = null;
854
+ this.getMagicApi = () => ({
855
+ onPageElUpdate: (el) => this.emit("page-el-update", el),
856
+ onRuntimeReady: (runtime) => {
857
+ this.runtime = runtime;
858
+ globalThis.runtime = runtime;
859
+ this.emit("runtime-ready", runtime);
860
+ }
861
+ });
862
+ this.getRuntime = () => {
863
+ if (this.runtime)
864
+ return Promise.resolve(this.runtime);
865
+ return new Promise((resolve) => {
866
+ const listener = (runtime) => {
867
+ this.off("runtime-ready", listener);
868
+ resolve(runtime);
869
+ };
870
+ this.on("runtime-ready", listener);
871
+ });
872
+ };
873
+ this.loadHandler = async () => {
874
+ this.emit("onload");
875
+ if (this.render) {
876
+ const el = await this.render(this.core);
877
+ if (el) {
878
+ this.iframe?.contentDocument?.body?.appendChild(el);
879
+ }
880
+ }
881
+ if (this.contentWindow) {
882
+ const style = this.contentWindow.document.createElement("style");
883
+ style.id = "tmagic-stage-render";
884
+ style.innerHTML = `
885
+ .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
886
+ z-index: ${ZIndex.SELECTED_EL};
887
+ }
888
+ `;
889
+ this.contentWindow.document.head.appendChild(style);
890
+ }
891
+ };
871
892
  this.core = core;
872
893
  this.runtimeUrl = core.config.runtimeUrl || "";
873
894
  this.render = core.config.render;
@@ -880,14 +901,6 @@ class StageRender extends EventEmitter {
880
901
  `;
881
902
  this.iframe.addEventListener("load", this.loadHandler);
882
903
  }
883
- getMagicApi = () => ({
884
- onPageElUpdate: (el) => this.emit("page-el-update", el),
885
- onRuntimeReady: (runtime) => {
886
- this.runtime = runtime;
887
- globalThis.runtime = runtime;
888
- this.emit("runtime-ready", runtime);
889
- }
890
- });
891
904
  async mount(el) {
892
905
  if (this.iframe) {
893
906
  if (!isSameDomain(this.runtimeUrl) && this.runtimeUrl) {
@@ -904,17 +917,6 @@ class StageRender extends EventEmitter {
904
917
  throw Error("mount \u5931\u8D25");
905
918
  }
906
919
  }
907
- getRuntime = () => {
908
- if (this.runtime)
909
- return Promise.resolve(this.runtime);
910
- return new Promise((resolve) => {
911
- const listener = (runtime) => {
912
- this.off("runtime-ready", listener);
913
- resolve(runtime);
914
- };
915
- this.on("runtime-ready", listener);
916
- });
917
- };
918
920
  destroy() {
919
921
  this.iframe?.removeEventListener("load", this.loadHandler);
920
922
  this.contentWindow = null;
@@ -922,40 +924,12 @@ class StageRender extends EventEmitter {
922
924
  this.iframe = void 0;
923
925
  this.removeAllListeners();
924
926
  }
925
- loadHandler = async () => {
926
- this.emit("onload");
927
- if (this.render) {
928
- const el = await this.render(this.core);
929
- if (el) {
930
- this.iframe?.contentDocument?.body?.appendChild(el);
931
- }
932
- }
933
- if (this.contentWindow) {
934
- const style = this.contentWindow.document.createElement("style");
935
- style.id = "tmagic-stage-render";
936
- style.innerHTML = `
937
- .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
938
- z-index: ${ZIndex.SELECTED_EL};
939
- }
940
- `;
941
- this.contentWindow.document.head.appendChild(style);
942
- }
943
- };
944
927
  }
945
928
 
946
929
  class StageCore extends EventEmitter {
947
- selectedDom;
948
- highlightedDom;
949
- renderer;
950
- mask;
951
- dr;
952
- highlightLayer;
953
- config;
954
- zoom = DEFAULT_ZOOM;
955
- container;
956
- canSelect;
957
930
  constructor(config) {
958
931
  super();
932
+ this.zoom = DEFAULT_ZOOM;
959
933
  this.config = config;
960
934
  this.setZoom(config.zoom);
961
935
  this.canSelect = config.canSelect || ((el) => !!el.id);