magnetInfo.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // import {App} from "vue";
  2. import {Magnet, MagnetInfo, MagnetSize} from "@/types/magnetType.ts";
  3. const cellWidth = 50;
  4. const cellMargin = 10;
  5. export const MagnetEvent = "magnet"
  6. // 时间组件
  7. export const timeMagnetInfo: MagnetInfo =
  8. {
  9. type: 'TimeMagnet',
  10. event: 'daySelect',
  11. defaultSize: MagnetSize.medium,
  12. sizes: {
  13. small: {
  14. width: 7,
  15. height: 3,
  16. },
  17. medium: {
  18. width: 7,
  19. height: 6,
  20. },
  21. },
  22. component: null
  23. }
  24. export function initTimeMagnetInfo(component: any): MagnetInfo{
  25. timeMagnetInfo.component = component
  26. return timeMagnetInfo
  27. }
  28. export function computeMagnetStyle(magnet: Magnet) {
  29. // console.log(magnet)
  30. // 计算磁贴样式 元素宽度为 50px, 间距为 5px
  31. return computeStyle(magnet.width, magnet.height, magnet.x, magnet.y);
  32. }
  33. export function computeStyle(w: number, h: number, x: number, y: number, sub: boolean = false) {
  34. let _w = cellWidth * w + (cellMargin * w - cellMargin);
  35. let _h = cellWidth * h + (cellMargin * h - cellMargin);
  36. // 计算元素位置 起始位置为 0 .
  37. let _x = cellWidth * x + (cellMargin * x + cellMargin);
  38. let _y = cellWidth * y + (cellMargin * y + cellMargin);
  39. // console.log(w, h, x, y)
  40. if (sub) {
  41. return {
  42. width: `${_w}px`,
  43. height: `${_h}px`,
  44. }
  45. }
  46. return {
  47. width: `${_w}px`,
  48. height: `${_h}px`,
  49. left: `${_x}px`,
  50. top: `${_y}px`
  51. }
  52. }
  53. // 通过元素位置反向计算xy值
  54. export function comXY(left: number, top: number){
  55. let x = Math.floor(left / (cellWidth + cellMargin));
  56. let y = Math.floor(top / (cellWidth + cellMargin));
  57. return {x, y}
  58. }
  59. // 定义vue组件
  60. export default {
  61. timeMagnetInfo,
  62. }