createCompStyle.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { EditorModule } from "@/modules/editor/module";
  2. import { Layout } from "@/modules/editor/typings";
  3. export function createCompStyle(module: EditorModule, layout: Layout) {
  4. const { designToNaturalSize } = module.helper;
  5. const style: any = {};
  6. const transform: any = {};
  7. if (layout.alignSelf) {
  8. style.alignSelf = layout.alignSelf;
  9. }
  10. if (layout.visible === false) {
  11. style.display = "none";
  12. }
  13. if (layout.zIndex) {
  14. style["zIndex"] = layout.zIndex;
  15. }
  16. if (layout.margin) {
  17. style.margin = layout.margin;
  18. } else if (layout.offset) {
  19. if (layout.offset.t) {
  20. style.marginTop = designToNaturalSize(layout.offset.t);
  21. }
  22. if (layout.offset.b) {
  23. style.marginBottom = designToNaturalSize(layout.offset.b);
  24. }
  25. if (layout.offset.x) {
  26. transform.translateX = designToNaturalSize(layout.offset.x);
  27. }
  28. }
  29. if (layout.padding) {
  30. style.padding = layout.padding;
  31. }
  32. if (layout.size) {
  33. const [w, h] = layout.size;
  34. if (w) style.width = designToNaturalSize(w);
  35. if (h) style.height = designToNaturalSize(h);
  36. }
  37. if (layout.position) {
  38. style.position = layout.position;
  39. }
  40. const styleTransform = parseTransform(transform);
  41. if (styleTransform) {
  42. style.transform = styleTransform;
  43. }
  44. return style;
  45. }
  46. function parseTransform(trans: any) {
  47. const transforms: string[] = [];
  48. Object.entries(trans).forEach(([key, value]) => {
  49. transforms.push(`${key}(${value})`);
  50. });
  51. return transforms.join(" ");
  52. }