index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { EditorModule } from "..";
  2. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  3. import { createCompStyle } from "../../objects/DesignTemp/creates/createCompStyle";
  4. import { Layout } from "../../typings";
  5. export const helpers = EditorModule.helper({
  6. designToNaturalSize(value: number) {
  7. return parseFloat((value / 100).toFixed(2)) + "rem";
  8. },
  9. pxToDesignSize(value: number) {
  10. return value * 2;
  11. },
  12. findComp(compId: string) {
  13. const { compMap } = this.store.designData;
  14. const comp = compMap[compId];
  15. if (comp) return comp;
  16. },
  17. isStreamCard(compId: string) {
  18. return this.store.streamCardIds.indexOf(compId) > -1;
  19. },
  20. isGroupComp(compId: string) {
  21. return this.store.groupIds.indexOf(compId) > -1;
  22. },
  23. isGroupCompChild(compId: string) {
  24. const comps = this.helper.getCompTrees(compId);
  25. comps.pop();
  26. while (comps.length) {
  27. const comp = comps.pop() as DesignComp;
  28. if (comp.compKey === "Group") {
  29. return true;
  30. }
  31. }
  32. return false;
  33. },
  34. findParentComp(compId: string): DesignComp | undefined {
  35. const comp = this.helper.findComp(compId);
  36. if (comp) return this.helper.findComp(this.store.compPids[compId]);
  37. },
  38. findRootComp(): DesignComp | undefined {
  39. return this.store.designData.compMap["root"];
  40. },
  41. getCompTrees(compId: string) {
  42. const comps: DesignComp[] = [];
  43. const getParentComp = (compId: string) => {
  44. const comp = this.helper.findComp(compId);
  45. if (comp) {
  46. comps.unshift(comp);
  47. getParentComp(this.store.compPids[comp.id]);
  48. }
  49. };
  50. getParentComp(compId);
  51. return comps;
  52. },
  53. createStyle(layout: Partial<Layout>) {
  54. return createCompStyle(this, layout);
  55. },
  56. isCurrComp(compId: string) {
  57. return this.store.currCompId === compId;
  58. },
  59. isCustomChildComp(comp: DesignComp): boolean {
  60. const parentComp = this.helper.findParentComp(comp.id);
  61. if (!parentComp) return false;
  62. const i =
  63. parentComp.children.default?.findIndex((d) => d === comp.id) ?? -1;
  64. return i >= 0;
  65. },
  66. isCompCanDelete(compId: string): boolean {
  67. const comp = this.helper.findComp(compId);
  68. if (!comp || !this.helper.isCustomChildComp(comp)) return false;
  69. if (comp.compKey == "Container" && this.store.streamCardIds.length == 1)
  70. return false;
  71. return true;
  72. // if (this.store.isEditComp) return true;
  73. // return this.store.pageCompIds.includes(compId);
  74. },
  75. isShowSaveComp(comp: DesignComp): boolean {
  76. // if (!comp.children?.default || comp.children?.default?.length == 0)
  77. // return false;
  78. if (!this.helper.isCustomChildComp(comp)) return false;
  79. return true;
  80. },
  81. clearUnusedComps(compMap: Record<string, DesignComp>, rootId = "root") {
  82. const used = new Set<string>();
  83. const getUsedIds = (ids: string[]) => {
  84. ids.forEach((id) => {
  85. const comp = compMap[id];
  86. if (!comp) return;
  87. used.add(id);
  88. getUsedIds(comp.getChildIds());
  89. });
  90. return used;
  91. };
  92. getUsedIds([rootId]);
  93. Object.keys(compMap).forEach((compId) => {
  94. if (!used.has(compId)) {
  95. delete compMap[compId];
  96. }
  97. });
  98. },
  99. getPointOffsetWith(e: MouseEvent, dom: HTMLElement) {
  100. const domRect = dom.getBoundingClientRect();
  101. return {
  102. x: e.clientX - domRect.left,
  103. y: e.clientY - domRect.top,
  104. };
  105. },
  106. });