index.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { isPc } from "@queenjs/utils";
  2. import { nanoid } from "nanoid";
  3. import { EditorModule } from "..";
  4. import { CompObject } from "../../controllers/SelectCtrl/compObj";
  5. import { Matrix } from "../../controllers/SelectCtrl/matrix";
  6. import { Design_Page_Size, Design_Pixel_Ratio } from "../../dicts/CompOptions";
  7. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  8. import { createCompStyle } from "../../objects/DesignTemp/creates/createCompStyle";
  9. import { Layout } from "../../typings";
  10. export const helpers = EditorModule.helper({
  11. pxToDesignSize(value: number) {
  12. return value * Design_Pixel_Ratio;
  13. },
  14. designSizeToPx(value: number) {
  15. return value / Design_Pixel_Ratio;
  16. },
  17. designToNaturalSize(
  18. value: number,
  19. options?: { adaptiveH?: boolean }
  20. ): string {
  21. if (options?.adaptiveH && !isPc()) {
  22. value =
  23. value *
  24. ((window.innerHeight * Design_Pixel_Ratio) / Design_Page_Size[1]);
  25. }
  26. return this.helper.designSizeToPx(value) + "px";
  27. },
  28. findComp(compId: string) {
  29. const { compMap } = this.store.designData;
  30. const comp = compMap[compId];
  31. if (comp) return comp;
  32. },
  33. isStreamCard(compId: string) {
  34. return this.store.streamCardIds.indexOf(compId) > -1;
  35. },
  36. isGroupComp(compId: string) {
  37. return this.store.groupIds.indexOf(compId) > -1;
  38. },
  39. isGroupCompChild(compId: string) {
  40. const comps = this.helper.getCompTrees(compId);
  41. comps.pop();
  42. while (comps.length) {
  43. const comp = comps.pop() as DesignComp;
  44. if (comp.compKey === "Group") {
  45. return true;
  46. }
  47. }
  48. return false;
  49. },
  50. isStreamCardChild(compId: string) {
  51. if (compId == "root" || this.helper.isStreamCard(compId)) {
  52. return false;
  53. }
  54. const cards = this.store.streamCardIds;
  55. let n = cards.length;
  56. const compMap = this.store.designData.compMap;
  57. while (n--) {
  58. const childs = compMap[cards[n]].children.default || [];
  59. if (childs.indexOf(compId) > -1) return true;
  60. }
  61. return false;
  62. },
  63. findParentComp(compId: string): DesignComp | undefined {
  64. const comp = this.helper.findComp(compId);
  65. if (comp) return this.helper.findComp(this.store.compPids[compId]);
  66. },
  67. findRootComp(): DesignComp | undefined {
  68. return this.store.designData.compMap["root"];
  69. },
  70. findCardAllChildren(index:number) {
  71. const cardId = this.store.streamCardIds[index];
  72. return this.store.designData.compMap[cardId].children.default || [] as string[];
  73. },
  74. getCompCard(compId: string) {
  75. const paths: DesignComp[] = this.helper.getCompTrees(compId);
  76. return paths[1];
  77. },
  78. getCompWorlParentPos(compId: string, vx: number, vy: number) {
  79. const paths: DesignComp[] = this.helper.getCompTrees(compId);
  80. const m = new Matrix();
  81. let n = paths.length;
  82. let box = paths[1].$el.getBoundingClientRect();
  83. const s = this.controls.editorCtrl.state.scale;
  84. m.translate((box.left - vx) / s, (box.top - vy)/s);
  85. for (let i = 2; i < n - 1; i++) {
  86. //card开始遍历
  87. const m1 = new Matrix();
  88. m1.setMatrixStr(paths[i].layout.transformMatrix || "matrix(1,0,0,1,0,0)");
  89. m.append(m1);
  90. }
  91. return m;
  92. },
  93. getCompTrees(compId: string) {
  94. const comps: DesignComp[] = [];
  95. const getParentComp = (compId: string) => {
  96. const comp = this.helper.findComp(compId);
  97. if (comp) {
  98. comps.unshift(comp);
  99. getParentComp(this.store.compPids[comp.id]);
  100. }
  101. };
  102. getParentComp(compId);
  103. return comps;
  104. },
  105. createStyle(layout: Partial<Layout> & { size: any[] }, comp: DesignComp) {
  106. return createCompStyle(this, layout, comp);
  107. },
  108. isCurrComp(compId: string) {
  109. return this.store.currCompId === compId;
  110. },
  111. isCustomChildComp(comp: DesignComp): boolean {
  112. const parentComp = this.helper.findParentComp(comp.id);
  113. if (!parentComp) return false;
  114. const i =
  115. parentComp.children.default?.findIndex((d) => d === comp.id) ?? -1;
  116. return i >= 0;
  117. },
  118. isCompCanDelete(compId: string): boolean {
  119. return (
  120. this.helper.isStreamCardChild(compId) ||
  121. (this.helper.isStreamCard(compId) && this.store.streamCardIds.length > 1)
  122. );
  123. },
  124. isShowSaveComp(comp: DesignComp): boolean {
  125. if (!this.helper.isStreamCardChild(comp.id)) return false;
  126. return true;
  127. },
  128. clearUnusedComps(compMap: Record<string, DesignComp>, rootId = "root") {
  129. const used = new Set<string>();
  130. const getUsedIds = (ids: string[]) => {
  131. ids.forEach((id) => {
  132. const comp = compMap[id];
  133. if (!comp) return;
  134. used.add(id);
  135. getUsedIds(comp.getChildIds());
  136. });
  137. return used;
  138. };
  139. getUsedIds([rootId]);
  140. Object.keys(compMap).forEach((compId) => {
  141. if (!used.has(compId)) {
  142. delete compMap[compId];
  143. }
  144. });
  145. },
  146. getPointOffsetWith(e: MouseEvent, dom: HTMLElement) {
  147. const domRect = dom.getBoundingClientRect();
  148. return {
  149. x: e.clientX - domRect.left,
  150. y: e.clientY - domRect.top,
  151. };
  152. },
  153. getCardCompBound(compId: string) {
  154. const compMap = this.store.designData.compMap;
  155. const c = compMap[compId];
  156. const obj = new CompObject(c);
  157. const bound = obj.getBox();
  158. return bound;
  159. },
  160. extendStreamCard(streamCardId: string) {
  161. if (!streamCardId) return;
  162. const compMap = this.store.designData.compMap;
  163. const card = compMap[streamCardId];
  164. if (this.store.isShortPage) {
  165. card.setH(this.controls.screenCtrl.getCurrScreenHeight());
  166. return;
  167. }
  168. const childs = card.children.default || [];
  169. let maxH = 0,
  170. n = childs.length;
  171. while (n--) {
  172. const c = childs[n];
  173. const aabb = this.helper.getCardCompBound(c);
  174. maxH = Math.max(maxH, aabb.y + aabb.h);
  175. }
  176. maxH = this.helper.pxToDesignSize(maxH);
  177. if (childs.length < 1) {
  178. maxH = 200;
  179. }
  180. card.setH(maxH);
  181. },
  182. getClientId() {
  183. let clientId = undefined;
  184. try {
  185. const userInfo = JSON.parse(localStorage.getItem("userInfo") || "{}");
  186. clientId = userInfo._id || localStorage.getItem("clientId");
  187. } catch (error) {
  188. console.error(error);
  189. }
  190. if (!clientId) {
  191. clientId = nanoid();
  192. localStorage.setItem("clientId", clientId);
  193. }
  194. return clientId;
  195. },
  196. });