index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import domtoimage from "dom-to-image";
  2. import { EditorModule } from "..";
  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. findComp(compId: string) {
  14. const { compMap } = this.store.designData;
  15. const comp = compMap[compId];
  16. if (comp) return comp;
  17. },
  18. isStreamCard(compId: string) {
  19. return this.store.streamCardIds.indexOf(compId) > -1;
  20. },
  21. isGroupComp(compId: string) {
  22. return this.store.groupIds.indexOf(compId) > -1;
  23. },
  24. isGroupCompChild(compId: string) {
  25. const comps = this.helper.getCompTrees(compId);
  26. comps.pop();
  27. while (comps.length) {
  28. const comp = comps.pop() as DesignComp;
  29. if (comp.compKey === "Group") {
  30. return true;
  31. }
  32. }
  33. return false;
  34. },
  35. findParentComp(compId: string): DesignComp | undefined {
  36. const comp = this.helper.findComp(compId);
  37. if (comp) return this.helper.findComp(this.store.compPids[compId]);
  38. },
  39. findRootComp(): DesignComp | undefined {
  40. return this.store.designData.compMap["root"];
  41. },
  42. getCompTrees(compId: string) {
  43. const comps: DesignComp[] = [];
  44. const getParentComp = (compId: string) => {
  45. const comp = this.helper.findComp(compId);
  46. if (comp) {
  47. comps.unshift(comp);
  48. getParentComp(this.store.compPids[comp.id]);
  49. }
  50. };
  51. getParentComp(compId);
  52. return comps;
  53. },
  54. createStyle(layout: Partial<Layout>) {
  55. return createCompStyle(this, layout);
  56. },
  57. isCurrComp(compId: string) {
  58. return this.store.currCompId === compId;
  59. },
  60. isCustomChildComp(comp: DesignComp): boolean {
  61. const parentComp = this.helper.findParentComp(comp.id);
  62. if (!parentComp) return false;
  63. const i =
  64. parentComp.children.default?.findIndex((d) => d === comp.id) ?? -1;
  65. return i >= 0;
  66. },
  67. isCompCanDelete(compId: string): boolean {
  68. const comp = this.helper.findComp(compId);
  69. if (!comp || !this.helper.isCustomChildComp(comp)) return false;
  70. if (comp.compKey == "Container" && this.store.streamCardIds.length == 1)
  71. return false;
  72. return true;
  73. // if (this.store.isEditComp) return true;
  74. // return this.store.pageCompIds.includes(compId);
  75. },
  76. isShowSaveComp(comp: DesignComp): boolean {
  77. // if (!comp.children?.default || comp.children?.default?.length == 0)
  78. // return false;
  79. if (!this.helper.isCustomChildComp(comp)) return false;
  80. return true;
  81. },
  82. clearUnusedComps(compMap: Record<string, DesignComp>, rootId = "root") {
  83. const used = new Set<string>();
  84. const getUsedIds = (ids: string[]) => {
  85. ids.forEach((id) => {
  86. const comp = compMap[id];
  87. if (!comp) return;
  88. used.add(id);
  89. getUsedIds(comp.getChildIds());
  90. });
  91. return used;
  92. };
  93. getUsedIds([rootId]);
  94. Object.keys(compMap).forEach((compId) => {
  95. if (!used.has(compId)) {
  96. delete compMap[compId];
  97. }
  98. });
  99. },
  100. async screenshot(options: {
  101. element: HTMLElement;
  102. ratio?: number;
  103. }): Promise<Blob> {
  104. const dom = options.element;
  105. const transferEl = document.querySelector(".transfer") as
  106. | HTMLElement
  107. | undefined;
  108. if (transferEl) {
  109. transferEl.style.display = "none";
  110. }
  111. if (options.ratio) {
  112. const result = await domtoimage.toJpeg(dom);
  113. const img = await new Promise<HTMLImageElement>((resolve) => {
  114. const image = new Image();
  115. image.src = result;
  116. image.onload = function () {
  117. if (transferEl) {
  118. transferEl.style.display = "block";
  119. }
  120. resolve(image);
  121. };
  122. });
  123. const canvas = document.createElement("canvas");
  124. canvas.width = img.naturalWidth;
  125. canvas.height = Math.min(
  126. img.naturalWidth / options.ratio,
  127. img.naturalHeight
  128. );
  129. const ctx = canvas.getContext("2d");
  130. ctx?.drawImage(
  131. img,
  132. 0,
  133. 0,
  134. canvas.width,
  135. canvas.height,
  136. 0,
  137. 0,
  138. canvas.width,
  139. canvas.height
  140. );
  141. return new Promise((resolve) => {
  142. canvas.toBlob((blob) => {
  143. if (transferEl) {
  144. transferEl.style.display = "block";
  145. }
  146. if (blob) {
  147. resolve(blob);
  148. }
  149. });
  150. });
  151. } else {
  152. return domtoimage.toBlob(dom);
  153. }
  154. },
  155. });