useSlideBackgroundStyle.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { type Ref, computed } from 'vue'
  2. import type { SlideBackground } from '@/types/slides'
  3. // 将页面背景数据转换为css样式
  4. export default (background: Ref<SlideBackground | undefined>) => {
  5. const backgroundStyle = computed(() => {
  6. if (!background.value) return { backgroundColor: '#fff' }
  7. const {
  8. type,
  9. color,
  10. image,
  11. gradient,
  12. } = background.value
  13. // 纯色背景
  14. if (type === 'solid') return { backgroundColor: color }
  15. // 背景图模式
  16. // 包括:背景图、背景大小,是否重复
  17. else if (type === 'image' && image) {
  18. const { src, size } = image
  19. if (!src) return { backgroundColor: '#fff' }
  20. if (size === 'repeat') {
  21. return {
  22. backgroundImage: `url(${src}`,
  23. backgroundRepeat: 'repeat',
  24. backgroundSize: 'contain',
  25. }
  26. }
  27. return {
  28. backgroundImage: `url(${src}`,
  29. backgroundRepeat: 'no-repeat',
  30. backgroundSize: size || 'cover',
  31. }
  32. }
  33. // 渐变色背景
  34. else if (type === 'gradient' && gradient) {
  35. const { type, colors, rotate } = gradient
  36. const list = colors.map(item => `${item.color} ${item.pos}%`)
  37. if (type === 'radial') return { backgroundImage: `radial-gradient(${list.join(',')}` }
  38. return { backgroundImage: `linear-gradient(${rotate}deg, ${list.join(',')}` }
  39. }
  40. return { backgroundColor: '#fff' }
  41. })
  42. return {
  43. backgroundStyle,
  44. }
  45. }