useStyle.hook.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { PickCreateComponentType } from '@/packages/index.d'
  2. type AttrType = PickCreateComponentType<'attr'>
  3. export const useComponentStyle = (attr: AttrType, index: number) => {
  4. if(!attr) return {}
  5. const componentStyle = {
  6. zIndex: index + 1,
  7. left: `${attr.x}px`,
  8. top: `${attr.y}px`
  9. }
  10. return componentStyle
  11. }
  12. export const useSizeStyle = (attr: AttrType, scale?: number) => {
  13. if(!attr) return {}
  14. return {
  15. width: `${scale ? scale * attr.w : attr.w}px`,
  16. height: `${scale ? scale * attr.h : attr.h}px`
  17. }
  18. }
  19. // 锚点位置
  20. export const usePointStyle = (
  21. point: string,
  22. index: number,
  23. attr: PickCreateComponentType<'attr'>,
  24. cursorResize: string[]
  25. ) => {
  26. const { w: width, h: height } = attr
  27. const isTop = /t/.test(point)
  28. const isBottom = /b/.test(point)
  29. const isLeft = /l/.test(point)
  30. const isRight = /r/.test(point)
  31. let newLeft = 0
  32. let newTop = 0
  33. // 四个角的点
  34. if (point.length === 2) {
  35. newLeft = isLeft ? 0 : width
  36. newTop = isTop ? 0 : height
  37. } else {
  38. // 上下两点的点,宽度居中
  39. if (isTop || isBottom) {
  40. newLeft = width / 2
  41. newTop = isTop ? 0 : height
  42. }
  43. // 左右两边的点,高度居中
  44. if (isLeft || isRight) {
  45. newLeft = isLeft ? 0 : width
  46. newTop = Math.floor(height / 2)
  47. }
  48. }
  49. const style = {
  50. left: `${newLeft}px`,
  51. top: `${newTop}px`,
  52. cursor: cursorResize[index] + '-resize'
  53. }
  54. return style
  55. }