style.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { PickCreateComponentType } from '@/packages/index.d'
  2. import { EditCanvasConfigType } from '@/store/modules/chartEditStore/chartEditStore.d'
  3. type AttrType = PickCreateComponentType<'attr'>
  4. type StylesType = PickCreateComponentType<'styles'>
  5. export const getComponentAttrStyle = (attr: AttrType, index: number) => {
  6. const componentStyle = {
  7. zIndex: index + 1,
  8. left: `${attr.x}px`,
  9. top: `${attr.y}px`
  10. }
  11. return componentStyle
  12. }
  13. export const getSizeStyle = (attr: AttrType, scale?: number) => {
  14. const sizeStyle = {
  15. width: `${scale ? scale * attr.w : attr.w}px`,
  16. height: `${scale ? scale * attr.h : attr.h}px`
  17. }
  18. return sizeStyle
  19. }
  20. export const getEditCanvasConfigStyle = (canvas: EditCanvasConfigType) => {
  21. // 背景
  22. const computedBackground = canvas.selectColor
  23. ? { background: canvas.background }
  24. : {
  25. background: `url(${canvas.backgroundImage}) no-repeat center/100% !important`
  26. }
  27. return {
  28. position: 'relative',
  29. width: canvas.width ? `${canvas.width || 100}px` : '100%',
  30. height: canvas.height ? `${canvas.height}px` : '100%',
  31. ...computedBackground
  32. }
  33. }
  34. // 动画
  35. export const animationsClass = (animations: string[]) => {
  36. if (animations.length) {
  37. return `animate__animated animate__${animations[0]}`
  38. }
  39. return ''
  40. }
  41. // 样式
  42. export const getStyle = (styles: StylesType | EditCanvasConfigType) => {
  43. const { opacity, saturate, contrast, hueRotate, brightness } = styles
  44. return {
  45. // 透明度
  46. opacity: opacity,
  47. filter: `saturate(${saturate}) contrast(${contrast}) hue-rotate(${hueRotate}deg) brightness(${brightness})`
  48. }
  49. }