index.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (this.store.isEditComp) return true;
  51. return this.store.pageCompIds.includes(compId);
  52. },
  53. async screenshot(options?: { ratio: number }): Promise<Blob> {
  54. const dom = document.querySelector(".page-editing-content")
  55. ?.parentElement as HTMLElement;
  56. if (options?.ratio) {
  57. const result = await domtoimage.toJpeg(dom);
  58. const img = await new Promise<HTMLImageElement>((resolve) => {
  59. const image = new Image();
  60. image.src = result;
  61. image.onload = function () {
  62. resolve(image);
  63. };
  64. });
  65. const canvas = document.createElement("canvas");
  66. canvas.width = img.naturalWidth;
  67. canvas.height = Math.min(
  68. img.naturalWidth / options.ratio,
  69. img.naturalHeight
  70. );
  71. const ctx = canvas.getContext("2d");
  72. ctx?.drawImage(
  73. img,
  74. 0,
  75. 0,
  76. canvas.width,
  77. canvas.height,
  78. 0,
  79. 0,
  80. canvas.width,
  81. canvas.height
  82. );
  83. return new Promise((resolve) => {
  84. canvas.toBlob((blob) => {
  85. if (blob) {
  86. resolve(blob);
  87. }
  88. });
  89. });
  90. } else {
  91. return domtoimage.toBlob(dom);
  92. }
  93. },
  94. });