BaseView.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { computed } from "vue";
  2. import { useTheme, type ThemePaddingMargin } from "../theme/ThemeDefine";
  3. import type { FlexProps } from "./FlexView.vue";
  4. import { configMargin, configPadding } from "../theme/ThemeTools";
  5. import { ObjectUtils } from "@imengyu/imengyu-utils";
  6. export function useBaseViewStyleBuilder(props: FlexProps) {
  7. const themeContext = useTheme();
  8. const commonStyle = computed(() => {
  9. const styleObj : Record<string, any> = {};
  10. styleObj.display = 'flex';
  11. styleObj.flexDirection = props.direction;
  12. styleObj.flexBasis = props.flexBasis;
  13. styleObj.flexGrow = props.flexGrow;
  14. styleObj.flexShrink = props.flexShrink;
  15. styleObj.justifyContent = props.center ? (props.justify || 'center') : props.justify;
  16. styleObj.alignItems = props.center ? (props.align || 'center') : props.align;
  17. styleObj.position = props.position;
  18. styleObj.alignSelf = props.alignSelf;
  19. styleObj.flexWrap = props.wrap ? 'wrap' : 'nowrap';
  20. styleObj.backgroundColor = themeContext.resolveThemeColor(props.backgroundColor);
  21. styleObj.width = themeContext.resolveThemeSize(props.width);
  22. styleObj.height = themeContext.resolveThemeSize(props.height);
  23. styleObj.gap = !Array.isArray(props.gap) ? themeContext.resolveThemeSize(props.gap) : props.gap;
  24. styleObj.rowGap = Array.isArray(props.gap) ? themeContext.resolveSize(props.gap[0]) : undefined;
  25. styleObj.columnGap = Array.isArray(props.gap) ? themeContext.resolveSize(props.gap[1]) : undefined;
  26. styleObj.borderRadius = themeContext.resolveThemeSize(props.radius);
  27. styleObj.overflow = props.overflow;
  28. styleObj.border = props.border && !props.border.includes(' ') ? themeContext.getVar('border.' + props.border, undefined) : props.border;
  29. styleObj.borderColor = themeContext.resolveThemeColor(props.borderColor);
  30. styleObj.borderWidth = themeContext.resolveThemeSize(props.borderWidth);
  31. styleObj.borderStyle = props.borderStyle;
  32. styleObj.boxShadow = props.shadow ? themeContext.getVar('shadow.' + props.shadow, undefined) : undefined;
  33. styleObj.zIndex = props.zIndex;
  34. styleObj.pointerEvents = props.pointerEvents;
  35. //内边距样式
  36. configPadding(styleObj, themeContext, props.padding);
  37. //外边距样式
  38. configMargin(styleObj, themeContext, props.margin);
  39. if (props.innerStyle)
  40. ObjectUtils.cloneValuesToObject(props.innerStyle, styleObj);
  41. if (styleObj.paddingVertical) {
  42. if (styleObj.paddingTop === undefined)
  43. styleObj.paddingTop = styleObj.paddingVertical;
  44. if (styleObj.paddingBottom === undefined)
  45. styleObj.paddingBottom = styleObj.paddingVertical;
  46. delete styleObj.paddingVertical;
  47. }
  48. if (styleObj.paddingHorizontal) {
  49. if (styleObj.paddingLeft === undefined)
  50. styleObj.paddingLeft = styleObj.paddingHorizontal;
  51. if (styleObj.paddingRight === undefined)
  52. styleObj.paddingRight = styleObj.paddingHorizontal;
  53. delete styleObj.paddingHorizontal;
  54. }
  55. if (styleObj.marginVertical) {
  56. styleObj.marginTop = styleObj.marginVertical;
  57. styleObj.marginBottom = styleObj.marginVertical;
  58. delete styleObj.marginVertical;
  59. }
  60. if (styleObj.marginHorizontal) {
  61. styleObj.marginLeft = styleObj.marginHorizontal;
  62. styleObj.marginRight = styleObj.marginHorizontal;
  63. delete styleObj.marginHorizontal;
  64. }
  65. if (props.inset != undefined) {
  66. if (Array.isArray(props.inset)) {
  67. styleObj.top = themeContext.resolveThemeSize(props.inset[0]);
  68. styleObj.right = themeContext.resolveThemeSize(props.inset[1]);
  69. styleObj.bottom = themeContext.resolveThemeSize(props.inset[2]);
  70. styleObj.left = themeContext.resolveThemeSize(props.inset[3]);
  71. } else if (typeof props.inset === 'object') {
  72. styleObj.left = themeContext.resolveThemeSize(props.inset.l);
  73. styleObj.right = themeContext.resolveThemeSize(props.inset.r);
  74. styleObj.top = themeContext.resolveThemeSize(props.inset.t);
  75. styleObj.bottom = themeContext.resolveThemeSize(props.inset.b);
  76. } else {
  77. const v = themeContext.resolveThemeSize(props.inset);
  78. styleObj.left = v;
  79. styleObj.right = v;
  80. styleObj.top = v;
  81. styleObj.bottom = v;
  82. }
  83. }
  84. //绝对距样式
  85. if (typeof props.left !== 'undefined')
  86. styleObj.left = themeContext.resolveThemeSize(props.left);
  87. if (typeof props.right !== 'undefined')
  88. styleObj.right = themeContext.resolveThemeSize(props.right);
  89. if (typeof props.top !== 'undefined')
  90. styleObj.top = themeContext.resolveThemeSize(props.top);
  91. if (typeof props.bottom !== 'undefined')
  92. styleObj.bottom = themeContext.resolveThemeSize(props.bottom);
  93. if (typeof props.flex !== 'undefined')
  94. styleObj.flex = props.flex;
  95. return styleObj
  96. });
  97. return {
  98. commonStyle
  99. }
  100. }