index.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. m.translate(box.left - vx, box.top - vy);
  84. for (let i = 2; i < n - 1; i++) {
  85. //card开始遍历
  86. const m1 = new Matrix();
  87. m1.setMatrixStr(paths[i].layout.transformMatrix || "matrix(1,0,0,1,0,0)");
  88. m.append(m1);
  89. }
  90. return m;
  91. },
  92. getCompTrees(compId: string) {
  93. const comps: DesignComp[] = [];
  94. const getParentComp = (compId: string) => {
  95. const comp = this.helper.findComp(compId);
  96. if (comp) {
  97. comps.unshift(comp);
  98. getParentComp(this.store.compPids[comp.id]);
  99. }
  100. };
  101. getParentComp(compId);
  102. return comps;
  103. },
  104. createStyle(layout: Partial<Layout> & { size: any[] }, comp: DesignComp) {
  105. return createCompStyle(this, layout, comp);
  106. },
  107. isCurrComp(compId: string) {
  108. return this.store.currCompId === compId;
  109. },
  110. isCustomChildComp(comp: DesignComp): boolean {
  111. const parentComp = this.helper.findParentComp(comp.id);
  112. if (!parentComp) return false;
  113. const i =
  114. parentComp.children.default?.findIndex((d) => d === comp.id) ?? -1;
  115. return i >= 0;
  116. },
  117. isCompCanDelete(compId: string): boolean {
  118. return (
  119. this.helper.isStreamCardChild(compId) ||
  120. (this.helper.isStreamCard(compId) && this.store.streamCardIds.length > 1)
  121. );
  122. },
  123. isShowSaveComp(comp: DesignComp): boolean {
  124. if (!this.helper.isStreamCardChild(comp.id)) return false;
  125. return true;
  126. },
  127. clearUnusedComps(compMap: Record<string, DesignComp>, rootId = "root") {
  128. const used = new Set<string>();
  129. const getUsedIds = (ids: string[]) => {
  130. ids.forEach((id) => {
  131. const comp = compMap[id];
  132. if (!comp) return;
  133. used.add(id);
  134. getUsedIds(comp.getChildIds());
  135. });
  136. return used;
  137. };
  138. getUsedIds([rootId]);
  139. Object.keys(compMap).forEach((compId) => {
  140. if (!used.has(compId)) {
  141. delete compMap[compId];
  142. }
  143. });
  144. },
  145. getPointOffsetWith(e: MouseEvent, dom: HTMLElement) {
  146. const domRect = dom.getBoundingClientRect();
  147. return {
  148. x: e.clientX - domRect.left,
  149. y: e.clientY - domRect.top,
  150. };
  151. },
  152. getCardCompBound(compId: string) {
  153. const compMap = this.store.designData.compMap;
  154. const c = compMap[compId];
  155. const obj = new CompObject(c);
  156. const bound = obj.getBox();
  157. return bound;
  158. },
  159. extendStreamCard(streamCardId: string) {
  160. if (!streamCardId) return;
  161. const compMap = this.store.designData.compMap;
  162. const card = compMap[streamCardId];
  163. const childs = card.children.default || [];
  164. let maxH = 0,
  165. n = childs.length;
  166. while (n--) {
  167. const c = childs[n];
  168. const aabb = this.helper.getCardCompBound(c);
  169. maxH = Math.max(maxH, aabb.y + aabb.h);
  170. }
  171. maxH = this.helper.pxToDesignSize(maxH);
  172. if (childs.length < 1) {
  173. maxH = 200;
  174. }
  175. card.setH(maxH);
  176. },
  177. getClientId() {
  178. let clientId = undefined;
  179. try {
  180. const userInfo = JSON.parse(localStorage.getItem("userInfo") || "{}");
  181. clientId = userInfo._id || localStorage.getItem("clientId");
  182. } catch (error) {
  183. console.error(error);
  184. }
  185. if (!clientId) {
  186. clientId = nanoid();
  187. localStorage.setItem("clientId", clientId);
  188. }
  189. return clientId;
  190. },
  191. });