createCompStyle.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { formatColor } from "@/modules/editor/components/CompUI/formItems/NewColorPicker/ColorList";
  2. import { Matrix } from "@/modules/editor/controllers/SelectCtrl/matrix";
  3. import { EditorModule } from "@/modules/editor/module";
  4. import { Layout } from "@/modules/editor/typings";
  5. import { DesignComp } from "../DesignComp";
  6. import { compMasks } from "./CompMasks";
  7. export function createCompStyle(
  8. module: EditorModule,
  9. layout: Layout,
  10. comp: DesignComp
  11. ) {
  12. const { designToNaturalSize, pxToDesignSize , designSizeToPx} = module.helper;
  13. const style: any = {};
  14. const transform: any = {};
  15. // if (layout.alignSelf) {
  16. // style.alignSelf = layout.alignSelf;
  17. // }
  18. if (layout.visible === false) {
  19. style.display = "none";
  20. }
  21. if (layout.opacity !== undefined) {
  22. style["opacity"] = layout.opacity;
  23. }
  24. if (layout.border) {
  25. if (layout.border.style) {
  26. style["border-style"] = layout.border.style;
  27. }
  28. style["border-width"] = (layout.border.width || 0) + "px";
  29. style["border-color"] = layout.border.color || "#000";
  30. if (layout.border.radius) {
  31. style["border-radius"] = layout.border.radius / 2 + "%";
  32. style["overflow"] = "hidden";
  33. }
  34. if (layout.border.radius2 !== undefined) {
  35. style["border-radius"] = designSizeToPx(Math.min(layout.size[0], layout.size[1])) * layout.border.radius2 / 100.0 + "px";
  36. style["overflow"] = "hidden";
  37. }
  38. }
  39. if (layout.radius !== undefined) {
  40. style["border-radius"] = layout.radius / 2 + "%";
  41. style["overflow"] = "hidden";
  42. }
  43. if (layout.locked === true) {
  44. style["pointer-events"] = "none";
  45. }
  46. if (layout.zIndex) {
  47. style["zIndex"] = layout.zIndex;
  48. }
  49. // if (layout.margin) {
  50. // style.margin = layout.margin;
  51. // }
  52. if (layout.padding) {
  53. style.padding = layout.padding;
  54. }
  55. if (layout.mask) {
  56. style.clipPath = compMasks[layout.mask]?.value || layout.mask;
  57. }
  58. if (layout.transform) {
  59. if (layout.transform.x) {
  60. transform.translateX = designToNaturalSize(layout.transform.x);
  61. }
  62. if (layout.transform.y) {
  63. transform.translateY = designToNaturalSize(layout.transform.y);
  64. }
  65. if (layout.transform.r) {
  66. transform.rotate = layout.transform.r + "deg";
  67. }
  68. if (layout.transform.s) {
  69. transform.scale = layout.transform.s;
  70. }
  71. }
  72. if (layout.size) {
  73. const [w, h, sizeOpts] = layout.size;
  74. if (w) {
  75. style.width = designToNaturalSize(w);
  76. }
  77. if (h) {
  78. style.height = designToNaturalSize(h, {
  79. adaptiveH: sizeOpts?.unit === "%",
  80. });
  81. }
  82. }
  83. if (layout.position) {
  84. style.position = layout.position;
  85. }
  86. if (layout.transformMatrix) {
  87. const m = Matrix.createFromMatrixStr(layout.transformMatrix);
  88. // m.ty = parseFloat(
  89. // designToNaturalSize(pxToDesignSize(m.ty), { adaptiveH: true })
  90. // );
  91. style.transform = m.getMatrixStr(); //layout.transformMatrix;
  92. style.transformOrigin = "0 0";
  93. } else {
  94. const v = parseTransform(transform);
  95. if (v) style.transform = v;
  96. }
  97. if (layout.background) {
  98. if (layout.background.color) {
  99. style.background = formatColor(layout.background.color);
  100. }
  101. }
  102. return style;
  103. }
  104. export function parseTransform(trans: any) {
  105. let transform = "";
  106. if (trans.translateX || trans.translateY) {
  107. transform = `translate(${trans.translateX || 0}, ${trans.translateY || 0})`;
  108. }
  109. if (trans.scale) {
  110. transform += ` scale(${trans.scale})`;
  111. }
  112. if (trans.rotate) {
  113. transform += ` rotate(${trans.rotate})`;
  114. }
  115. return transform;
  116. }