BaseView.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { computed } from "vue";
  2. import { useTheme } 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 obj : Record<string, any> = {
  10. display: 'flex',
  11. flexDirection: props.direction,
  12. flexBasis: props.flexBasis,
  13. flexGrow: props.flexGrow,
  14. flexShrink: props.flexShrink,
  15. justifyContent: props.center ? (props.justify || 'center') : props.justify,
  16. alignItems: props.center ? (props.align || 'center') : props.align,
  17. position: props.position,
  18. alignSelf: props.alignSelf,
  19. flexWrap: props.wrap ? 'wrap' : 'nowrap',
  20. backgroundColor: themeContext.resolveThemeColor(props.backgroundColor),
  21. width: themeContext.resolveThemeSize(props.width),
  22. height: themeContext.resolveThemeSize(props.height),
  23. gap: !Array.isArray(props.gap) ? themeContext.resolveThemeSize(props.gap) : props.gap,
  24. rowGap: Array.isArray(props.gap) ? themeContext.resolveSize(props.gap[0]) : undefined,
  25. columnGap: Array.isArray(props.gap) ? themeContext.resolveSize(props.gap[1]) : undefined,
  26. borderRadius: themeContext.resolveThemeSize(props.radius),
  27. overflow: props.overflow,
  28. border: props.border && !props.border.includes(' ') ? themeContext.getVar('border.' + props.border, undefined) : props.border,
  29. borderColor: themeContext.resolveThemeColor(props.borderColor),
  30. borderWidth: themeContext.resolveThemeSize(props.borderWidth),
  31. borderStyle: props.borderStyle,
  32. boxShadow: props.shadow ? themeContext.getVar('shadow.' + props.shadow, undefined) : undefined,
  33. zIndex: props.zIndex,
  34. }
  35. //内边距样式
  36. configPadding(obj, themeContext.theme.value, props.padding as any);
  37. //外边距样式
  38. configMargin(obj, themeContext.theme.value, props.margin as any);
  39. if (props.innerStyle)
  40. ObjectUtils.cloneValuesToObject(props.innerStyle, obj);
  41. if (obj.paddingVertical) {
  42. if (obj.paddingTop === undefined)
  43. obj.paddingTop = obj.paddingVertical;
  44. if (obj.paddingBottom === undefined)
  45. obj.paddingBottom = obj.paddingVertical;
  46. obj.paddingVertical = undefined;
  47. }
  48. if (obj.paddingHorizontal) {
  49. if (obj.paddingLeft === undefined)
  50. obj.paddingLeft = obj.paddingHorizontal;
  51. if (obj.paddingRight === undefined)
  52. obj.paddingRight = obj.paddingHorizontal;
  53. obj.paddingHorizontal = undefined;
  54. }
  55. if (obj.marginVertical) {
  56. obj.marginTop = obj.marginVertical;
  57. obj.marginBottom = obj.marginVertical;
  58. obj.marginVertical = undefined;
  59. }
  60. if (obj.marginHorizontal) {
  61. obj.marginLeft = obj.marginHorizontal;
  62. obj.marginRight = obj.marginHorizontal;
  63. obj.marginHorizontal = undefined;
  64. }
  65. //绝对距样式
  66. if (typeof props.left !== 'undefined')
  67. obj.left = themeContext.resolveThemeSize(props.left);
  68. if (typeof props.right !== 'undefined')
  69. obj.right = themeContext.resolveThemeSize(props.right);
  70. if (typeof props.top !== 'undefined')
  71. obj.top = themeContext.resolveThemeSize(props.top);
  72. if (typeof props.bottom !== 'undefined')
  73. obj.bottom = themeContext.resolveThemeSize(props.bottom);
  74. if (typeof props.flex !== 'undefined')
  75. obj.flex = props.flex;
  76. return obj
  77. });
  78. return {
  79. commonStyle
  80. }
  81. }