index.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { ModuleControl } from "queenjs";
  2. import { reactive } from "vue";
  3. import { EditorModule } from "../../module";
  4. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  5. import { Matrix } from "./Matrix";
  6. import * as transforms from "./transforms";
  7. import { GroupActionCtrl } from "./GroupCtrl";
  8. type TransHandle = (this: TransferCtrl, e: MouseEvent) => void;
  9. export type TransCreateFn = (...args: any[]) => {
  10. mousedown?: TransHandle;
  11. mousemove: TransHandle;
  12. mouseup: TransHandle;
  13. };
  14. export class TransferCtrl extends ModuleControl<EditorModule> {
  15. compEl!: HTMLElement;
  16. pageEl!: HTMLElement;
  17. currComp!: DesignComp;
  18. currTransfer!: ReturnType<TransCreateFn>;
  19. currObserver?: MutationObserver;
  20. transforms = transforms;
  21. groupCtrl = new GroupActionCtrl(this.module);
  22. transEvent = {
  23. startX: 0,
  24. startY: 0,
  25. offsetX: 0,
  26. offsetY: 0,
  27. width: 0,
  28. height: 0,
  29. };
  30. transferStyle = reactive({
  31. top: "",
  32. left: "",
  33. width: "",
  34. height: "",
  35. transform: {
  36. scale: 1,
  37. rotate: "0deg",
  38. translateX: "-50%",
  39. translateY: "-50%",
  40. },
  41. });
  42. originPiont = {
  43. x: 0,
  44. y: 0,
  45. };
  46. init(pageEl: HTMLElement) {
  47. this.groupCtrl.init();
  48. this.currComp = this.module.store.currComp;
  49. this.compEl = this.currComp.$el;
  50. this.pageEl = pageEl;
  51. this.observe();
  52. }
  53. mousedown(e: MouseEvent, type: keyof typeof transforms, currComp?: any) {
  54. if (!currComp) {
  55. currComp = this.store.currComp;
  56. }
  57. this.currComp = currComp;
  58. this.compEl = currComp.$el;
  59. this.transEvent = {
  60. startX: e.clientX,
  61. startY: e.clientY,
  62. offsetX: 0,
  63. offsetY: 0,
  64. width: currComp.layout.size?.[0] || currComp.$el.clientWidth * 2,
  65. height: currComp.layout.size?.[1] || currComp.$el.clientHeight * 2,
  66. };
  67. this.currTransfer = this.transforms[type]();
  68. document.addEventListener("mousemove", this.mousemove);
  69. document.addEventListener("mouseup", this.mouseup);
  70. this.currTransfer.mousedown?.call(this, e);
  71. }
  72. private mousemove = (e: MouseEvent) => {
  73. const { transEvent } = this;
  74. transEvent.offsetX = e.clientX - transEvent.startX;
  75. transEvent.offsetY = e.clientY - transEvent.startY;
  76. this.currTransfer.mousemove.call(this, e);
  77. };
  78. private mouseup = (e: MouseEvent) => {
  79. document.removeEventListener("mousemove", this.mousemove);
  80. document.removeEventListener("mouseup", this.mouseup);
  81. this.currTransfer.mouseup.call(this, e);
  82. };
  83. observe() {
  84. if (this.currObserver) {
  85. this.currObserver.disconnect();
  86. }
  87. this.initStyle();
  88. this.currObserver = new MutationObserver((mutations) => {
  89. mutations.forEach((mutation) => {
  90. if (
  91. mutation.type === "childList" ||
  92. (mutation.type === "attributes" && mutation.attributeName === "style")
  93. ) {
  94. this.initStyle();
  95. }
  96. });
  97. });
  98. this.currObserver.observe(this.compEl, {
  99. attributes: true,
  100. childList: true,
  101. subtree: true,
  102. characterData: true,
  103. });
  104. }
  105. initStyle() {
  106. if (!this.compEl) return;
  107. const rect = this.compEl.getBoundingClientRect();
  108. const pageRect = this.pageEl.getBoundingClientRect();
  109. const matrix = new Matrix();
  110. matrix.setFormDiv(this.compEl);
  111. const width = this.compEl.clientWidth * matrix.getScale();
  112. const height = this.compEl.clientHeight * matrix.getScale();
  113. this.transferStyle.width = width + "px";
  114. this.transferStyle.height = height + "px";
  115. this.transferStyle.top = rect.top + rect.height / 2 - pageRect.top + "px";
  116. this.transferStyle.left = rect.left + rect.width / 2 - pageRect.left + "px";
  117. if (!this.transferStyle.transform) {
  118. this.transferStyle.transform = {
  119. scale: 1,
  120. rotate: "0deg",
  121. translateX: "-50%",
  122. translateY: "-50%",
  123. };
  124. }
  125. this.transferStyle.transform.scale = matrix.getScale();
  126. this.transferStyle.transform.rotate = matrix.getRotate() + "deg";
  127. this.transferStyle.transform.translateY = "-" + height / 2 + "px";
  128. this.originPiont.x = rect.left + rect.width / 2;
  129. this.originPiont.y = rect.top + rect.height / 2;
  130. }
  131. resetStyle() {
  132. Object.keys(this.transferStyle).forEach((key) => {
  133. (this.transferStyle as any)[key] = "";
  134. });
  135. }
  136. destroy() {
  137. this.groupCtrl.destroy();
  138. this.currObserver?.disconnect();
  139. this.resetStyle();
  140. }
  141. }