index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. findComp(compId: string) {
  11. const { compMap } = this.store.designData;
  12. const comp = compMap[compId];
  13. if (comp) return comp;
  14. },
  15. findParentComp(compId: string): DesignComp | undefined {
  16. const comp = this.helper.findComp(compId);
  17. if (comp) return this.helper.findComp(comp.pid);
  18. },
  19. findRootComp(): DesignComp | undefined {
  20. return this.store.designData.compMap["root"];
  21. },
  22. getCompTrees(compId: string) {
  23. const comps: DesignComp[] = [];
  24. const getParentComp = (compId: string) => {
  25. const comp = this.helper.findComp(compId);
  26. if (comp) {
  27. comps.unshift(comp);
  28. getParentComp(comp.pid);
  29. }
  30. };
  31. getParentComp(compId);
  32. return comps;
  33. },
  34. createStyle(layout: Partial<Layout>) {
  35. return createCompStyle(this, layout);
  36. },
  37. isCurrComp(compId: string) {
  38. return this.store.currCompId === compId;
  39. },
  40. isCustomChildComp(comp: DesignComp): boolean {
  41. const parentComp = this.helper.findParentComp(comp.id);
  42. if (!parentComp) return false;
  43. const i =
  44. parentComp.children.default?.findIndex((d) => d === comp.id) ?? -1;
  45. return i >= 0;
  46. },
  47. isCompCanDelete(compId: string): boolean {
  48. const comp = this.helper.findComp(compId);
  49. if (!comp || !this.helper.isCustomChildComp(comp)) return false;
  50. return true;
  51. // if (this.store.isEditComp) return true;
  52. // return this.store.pageCompIds.includes(compId);
  53. },
  54. isShowSaveComp(comp: DesignComp): boolean {
  55. // if (!comp.children?.default || comp.children?.default?.length == 0)
  56. // return false;
  57. if (!this.helper.isCustomChildComp(comp)) return false;
  58. return true;
  59. },
  60. async screenshot(options?: { ratio: number }): Promise<Blob> {
  61. const dom = document.querySelector(".page-editing-content")
  62. ?.parentElement as HTMLElement;
  63. const transferEl = document.querySelector(".transfer") as
  64. | HTMLElement
  65. | undefined;
  66. if (transferEl) {
  67. transferEl.style.display = "none";
  68. }
  69. if (options?.ratio) {
  70. const result = await domtoimage.toJpeg(dom);
  71. const img = await new Promise<HTMLImageElement>((resolve) => {
  72. const image = new Image();
  73. image.src = result;
  74. image.onload = function () {
  75. if (transferEl) {
  76. transferEl.style.display = "block";
  77. }
  78. resolve(image);
  79. };
  80. });
  81. const canvas = document.createElement("canvas");
  82. canvas.width = img.naturalWidth;
  83. canvas.height = Math.min(
  84. img.naturalWidth / options.ratio,
  85. img.naturalHeight
  86. );
  87. const ctx = canvas.getContext("2d");
  88. ctx?.drawImage(
  89. img,
  90. 0,
  91. 0,
  92. canvas.width,
  93. canvas.height,
  94. 0,
  95. 0,
  96. canvas.width,
  97. canvas.height
  98. );
  99. return new Promise((resolve) => {
  100. canvas.toBlob((blob) => {
  101. if (transferEl) {
  102. transferEl.style.display = "block";
  103. }
  104. if (blob) {
  105. resolve(blob);
  106. }
  107. });
  108. });
  109. } else {
  110. return domtoimage.toBlob(dom);
  111. }
  112. },
  113. });