Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

bounds-bg.tsx 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { useRef } from 'react'
  2. import state, { useSelector } from 'state'
  3. import inputs from 'state/inputs'
  4. import styled from 'styles'
  5. import tld from 'utils/tld'
  6. function handlePointerDown(e: React.PointerEvent<SVGRectElement>) {
  7. if (!inputs.canAccept(e.pointerId)) return
  8. e.stopPropagation()
  9. e.currentTarget.setPointerCapture(e.pointerId)
  10. const info = inputs.pointerDown(e, 'bounds')
  11. if (e.button === 0) {
  12. state.send('POINTED_BOUNDS', info)
  13. } else if (e.button === 2) {
  14. state.send('RIGHT_POINTED', info)
  15. }
  16. }
  17. function handlePointerUp(e: React.PointerEvent<SVGRectElement>) {
  18. if (!inputs.canAccept(e.pointerId)) return
  19. e.stopPropagation()
  20. e.currentTarget.releasePointerCapture(e.pointerId)
  21. state.send('STOPPED_POINTING', inputs.pointerUp(e, 'bounds'))
  22. }
  23. export default function BoundsBg(): JSX.Element {
  24. const rBounds = useRef<SVGRectElement>(null)
  25. const bounds = useSelector((state) => state.values.selectedBounds)
  26. const shouldDisplay = useSelector((s) =>
  27. s.isInAny('selecting', 'selectPinching')
  28. )
  29. const rotation = useSelector((s) => s.values.selectedRotation)
  30. const isAllHandles = useSelector((s) => {
  31. const selectedIds = s.values.selectedIds
  32. if (selectedIds.length === 1) {
  33. const page = tld.getPage(s.data)
  34. const selected = selectedIds[0]
  35. return (
  36. selectedIds.length === 1 && page.shapes[selected]?.handles !== undefined
  37. )
  38. }
  39. })
  40. if (isAllHandles) return null
  41. if (!bounds) return null
  42. if (!shouldDisplay) return null
  43. const { width, height } = bounds
  44. return (
  45. <StyledBoundsBg
  46. ref={rBounds}
  47. width={Math.max(1, width)}
  48. height={Math.max(1, height)}
  49. transform={`
  50. rotate(${rotation * (180 / Math.PI)},
  51. ${(bounds.minX + bounds.maxX) / 2},
  52. ${(bounds.minY + bounds.maxY) / 2})
  53. translate(${bounds.minX},${bounds.minY})
  54. rotate(${(bounds.rotation || 0) * (180 / Math.PI)}, 0, 0)`}
  55. onPointerDown={handlePointerDown}
  56. onPointerUp={handlePointerUp}
  57. pointerEvents="all"
  58. />
  59. )
  60. }
  61. const StyledBoundsBg = styled('rect', {
  62. fill: '$boundsBg',
  63. })