createCompStyle.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { EditorModule } from "@/modules/editor/module";
  2. import { Layout } from "@/modules/editor/typings";
  3. import { compMasks } from "./CompMasks";
  4. import { Matrix } from "@/modules/editor/controllers/TransferCtrl/Matrix";
  5. export function createCompStyle(module: EditorModule, layout: Layout) {
  6. const { designToNaturalSize } = module.helper;
  7. const style: any = {};
  8. const transform: any = {};
  9. // if (layout.alignSelf) {
  10. // style.alignSelf = layout.alignSelf;
  11. // }
  12. if (layout.visible === false) {
  13. style.display = "none";
  14. }
  15. if (layout.zIndex) {
  16. style["zIndex"] = layout.zIndex;
  17. }
  18. // if (layout.margin) {
  19. // style.margin = layout.margin;
  20. // }
  21. if (layout.padding) {
  22. style.padding = layout.padding;
  23. }
  24. if (layout.mask) {
  25. style.clipPath = compMasks[layout.mask]?.value || layout.mask;
  26. }
  27. if (layout.transform) {
  28. if (layout.transform.x) {
  29. transform.translateX = designToNaturalSize(layout.transform.x);
  30. }
  31. if (layout.transform.y) {
  32. transform.translateY = designToNaturalSize(layout.transform.y);
  33. }
  34. if (layout.transform.r) {
  35. transform.rotate = layout.transform.r + "deg";
  36. }
  37. if (layout.transform.s) {
  38. transform.scale = layout.transform.s;
  39. }
  40. }
  41. if (layout.size) {
  42. const [w, h] = layout.size;
  43. if (w) style.width = designToNaturalSize(w);
  44. if (h) style.height = designToNaturalSize(h);
  45. }
  46. if (layout.position) {
  47. style.position = layout.position;
  48. }
  49. if (layout.transformMatrix) {
  50. style.transform = layout.transformMatrix;
  51. } else {
  52. //转换成matrix形式
  53. const m = new Matrix();
  54. const transform = layout.transform;
  55. if (transform) {
  56. m.translate(transform.x || 0, transform.y || 0);
  57. if (transform.s != undefined) {
  58. m.scale(transform.s, transform.s);
  59. }
  60. if (transform.r != undefined) {
  61. m.rotateDeg(transform.r);
  62. }
  63. }
  64. style.transform = m.getMatrixStr();
  65. // const s =
  66. // style.transform = parseTransform(transform);
  67. }
  68. style.transformOrigin = "0 0";
  69. if (layout.background) {
  70. if (layout.background.color) {
  71. style.backgroundColor = layout.background.color;
  72. }
  73. }
  74. return style;
  75. }
  76. export function parseTransform(trans: any) {
  77. let transform = "";
  78. if (trans.translateX || trans.translateY) {
  79. transform = `translate(${trans.translateX || 0}, ${trans.translateY || 0})`;
  80. }
  81. if (trans.scale) {
  82. transform += ` scale(${trans.scale})`;
  83. }
  84. if (trans.rotate) {
  85. transform += ` rotate(${trans.rotate})`;
  86. }
  87. return transform;
  88. }