index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { EditorModule } from "..";
  2. import { CompObject } from "../../controllers/SelectCtrl/compObj";
  3. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  4. import { createCompStyle } from "../../objects/DesignTemp/creates/createCompStyle";
  5. import { Layout } from "../../typings";
  6. export const helpers = EditorModule.helper({
  7. designToNaturalSize(value: number) {
  8. return parseFloat((value / 100).toFixed(2)) + "rem";
  9. },
  10. pxToDesignSize(value: number) {
  11. return value * 2;
  12. },
  13. designSizeToPx(value: number) {
  14. return value / 2.0;
  15. },
  16. findComp(compId: string) {
  17. const { compMap } = this.store.designData;
  18. const comp = compMap[compId];
  19. if (comp) return comp;
  20. },
  21. isStreamCard(compId: string) {
  22. return this.store.streamCardIds.indexOf(compId) > -1;
  23. },
  24. isGroupComp(compId: string) {
  25. return this.store.groupIds.indexOf(compId) > -1;
  26. },
  27. isGroupCompChild(compId: string) {
  28. const comps = this.helper.getCompTrees(compId);
  29. comps.pop();
  30. while (comps.length) {
  31. const comp = comps.pop() as DesignComp;
  32. if (comp.compKey === "Group") {
  33. return true;
  34. }
  35. }
  36. return false;
  37. },
  38. isStreamCardChild(compId:string) {
  39. if (compId == "root" || this.helper.isStreamCard(compId) ) {
  40. return false;
  41. }
  42. const cards = this.store.streamCardIds
  43. let n = cards.length;
  44. const compMap = this.store.designData.compMap;
  45. while(n--) {
  46. const childs = compMap[cards[n]].children.default || [];
  47. if (childs.indexOf(compId) > -1 ) return true;
  48. }
  49. return false;
  50. },
  51. findParentComp(compId: string): DesignComp | undefined {
  52. const comp = this.helper.findComp(compId);
  53. if (comp) return this.helper.findComp(this.store.compPids[compId]);
  54. },
  55. findRootComp(): DesignComp | undefined {
  56. return this.store.designData.compMap["root"];
  57. },
  58. getCompTrees(compId: string) {
  59. const comps: DesignComp[] = [];
  60. const getParentComp = (compId: string) => {
  61. const comp = this.helper.findComp(compId);
  62. if (comp) {
  63. comps.unshift(comp);
  64. getParentComp(this.store.compPids[comp.id]);
  65. }
  66. };
  67. getParentComp(compId);
  68. return comps;
  69. },
  70. createStyle(layout: Partial<Layout> & {size:any[]}) {
  71. return createCompStyle(this, layout);
  72. },
  73. isCurrComp(compId: string) {
  74. return this.store.currCompId === compId;
  75. },
  76. isCustomChildComp(comp: DesignComp): boolean {
  77. const parentComp = this.helper.findParentComp(comp.id);
  78. if (!parentComp) return false;
  79. const i =
  80. parentComp.children.default?.findIndex((d) => d === comp.id) ?? -1;
  81. return i >= 0;
  82. },
  83. isCompCanDelete(compId: string): boolean {
  84. const comp = this.helper.findComp(compId);
  85. if (!comp || !this.helper.isCustomChildComp(comp)) return false;
  86. if (comp.compKey == "Container" && this.store.streamCardIds.length == 1)
  87. return false;
  88. return true;
  89. // if (this.store.isEditComp) return true;
  90. // return this.store.pageCompIds.includes(compId);
  91. },
  92. isShowSaveComp(comp: DesignComp): boolean {
  93. // if (!comp.children?.default || comp.children?.default?.length == 0)
  94. // return false;
  95. if (!this.helper.isCustomChildComp(comp)) return false;
  96. return true;
  97. },
  98. clearUnusedComps(compMap: Record<string, DesignComp>, rootId = "root") {
  99. const used = new Set<string>();
  100. const getUsedIds = (ids: string[]) => {
  101. ids.forEach((id) => {
  102. const comp = compMap[id];
  103. if (!comp) return;
  104. used.add(id);
  105. getUsedIds(comp.getChildIds());
  106. });
  107. return used;
  108. };
  109. getUsedIds([rootId]);
  110. Object.keys(compMap).forEach((compId) => {
  111. if (!used.has(compId)) {
  112. delete compMap[compId];
  113. }
  114. });
  115. },
  116. getPointOffsetWith(e: MouseEvent, dom: HTMLElement) {
  117. const domRect = dom.getBoundingClientRect();
  118. return {
  119. x: e.clientX - domRect.left,
  120. y: e.clientY - domRect.top,
  121. };
  122. },
  123. getCardCompBound(compId :string) {
  124. const compMap = this.store.designData.compMap
  125. const c = compMap[compId];
  126. const obj = new CompObject(c);
  127. const bound = obj.getBox();
  128. return bound;
  129. },
  130. extendStreamCard(streamCardId: string) {
  131. if (!streamCardId) return;
  132. const compMap = this.store.designData.compMap;
  133. const card = compMap[streamCardId];
  134. const childs = card.children.default || [];
  135. let maxH = 0,
  136. n = childs.length;
  137. while (n--) {
  138. const c = childs[n];
  139. const aabb = this.helper.getCardCompBound(c);
  140. maxH = Math.max(maxH, aabb.y + aabb.h);
  141. }
  142. maxH = this.helper.pxToDesignSize(maxH);
  143. card.setH(maxH);
  144. },
  145. });