index.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { memo } from 'react'
  2. import { useViewport } from 'reactflow'
  3. import { useStore } from '../store'
  4. import type {
  5. HelpLineHorizontalPosition,
  6. HelpLineVerticalPosition,
  7. } from './types'
  8. const HelpLineHorizontal = memo(({
  9. top,
  10. left,
  11. width,
  12. }: HelpLineHorizontalPosition) => {
  13. const { x, y, zoom } = useViewport()
  14. return (
  15. <div
  16. className='absolute h-[1px] bg-primary-300 z-[9]'
  17. style={{
  18. top: top * zoom + y,
  19. left: left * zoom + x,
  20. width: width * zoom,
  21. }}
  22. />
  23. )
  24. })
  25. HelpLineHorizontal.displayName = 'HelpLineBase'
  26. const HelpLineVertical = memo(({
  27. top,
  28. left,
  29. height,
  30. }: HelpLineVerticalPosition) => {
  31. const { x, y, zoom } = useViewport()
  32. return (
  33. <div
  34. className='absolute w-[1px] bg-primary-300 z-[9]'
  35. style={{
  36. top: top * zoom + y,
  37. left: left * zoom + x,
  38. height: height * zoom,
  39. }}
  40. />
  41. )
  42. })
  43. HelpLineVertical.displayName = 'HelpLineVertical'
  44. const HelpLine = () => {
  45. const helpLineHorizontal = useStore(s => s.helpLineHorizontal)
  46. const helpLineVertical = useStore(s => s.helpLineVertical)
  47. if (!helpLineHorizontal && !helpLineVertical)
  48. return null
  49. return (
  50. <>
  51. {
  52. helpLineHorizontal && (
  53. <HelpLineHorizontal {...helpLineHorizontal} />
  54. )
  55. }
  56. {
  57. helpLineVertical && (
  58. <HelpLineVertical {...helpLineVertical} />
  59. )
  60. }
  61. </>
  62. )
  63. }
  64. export default memo(HelpLine)