You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bounding-box.tsx 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import * as React from 'react'
  2. import { Edge, Corner } from 'types'
  3. import { useSelector } from 'state'
  4. import { getPage, getSelectedShapes, isMobile } from 'utils/utils'
  5. import CenterHandle from './center-handle'
  6. import CornerHandle from './corner-handle'
  7. import EdgeHandle from './edge-handle'
  8. import RotateHandle from './rotate-handle'
  9. export default function Bounds() {
  10. const isBrushing = useSelector((s) => s.isIn('brushSelecting'))
  11. const isSelecting = useSelector((s) => s.isIn('selecting'))
  12. const zoom = useSelector((s) => s.data.camera.zoom)
  13. const bounds = useSelector((s) => s.values.selectedBounds)
  14. const rotation = useSelector(({ data }) =>
  15. data.selectedIds.size === 1 ? getSelectedShapes(data)[0].rotation : 0
  16. )
  17. const isAllLocked = useSelector((s) => {
  18. const page = getPage(s.data)
  19. return Array.from(s.data.selectedIds.values()).every(
  20. (id) => page.shapes[id].isLocked
  21. )
  22. })
  23. if (!bounds) return null
  24. if (!isSelecting) return null
  25. const size = (isMobile().any ? 12 : 8) / zoom // Touch target size
  26. return (
  27. <g
  28. pointerEvents={isBrushing ? 'none' : 'all'}
  29. transform={`
  30. rotate(${rotation * (180 / Math.PI)},
  31. ${(bounds.minX + bounds.maxX) / 2},
  32. ${(bounds.minY + bounds.maxY) / 2})
  33. translate(${bounds.minX},${bounds.minY})`}
  34. >
  35. <CenterHandle bounds={bounds} isLocked={isAllLocked} />
  36. {!isAllLocked && (
  37. <>
  38. <EdgeHandle size={size} bounds={bounds} edge={Edge.Top} />
  39. <EdgeHandle size={size} bounds={bounds} edge={Edge.Right} />
  40. <EdgeHandle size={size} bounds={bounds} edge={Edge.Bottom} />
  41. <EdgeHandle size={size} bounds={bounds} edge={Edge.Left} />
  42. <CornerHandle size={size} bounds={bounds} corner={Corner.TopLeft} />
  43. <CornerHandle size={size} bounds={bounds} corner={Corner.TopRight} />
  44. <CornerHandle
  45. size={size}
  46. bounds={bounds}
  47. corner={Corner.BottomRight}
  48. />
  49. <CornerHandle
  50. size={size}
  51. bounds={bounds}
  52. corner={Corner.BottomLeft}
  53. />
  54. <RotateHandle size={size} bounds={bounds} />
  55. </>
  56. )}
  57. </g>
  58. )
  59. }