Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { useCallback, useRef } from "react"
  2. import state, { useSelector } from "state"
  3. import inputs from "state/inputs"
  4. import styled from "styles"
  5. import { getPage } from "utils/utils"
  6. function handlePointerDown(e: React.PointerEvent<SVGRectElement>) {
  7. if (e.buttons !== 1) return
  8. e.stopPropagation()
  9. e.currentTarget.setPointerCapture(e.pointerId)
  10. state.send("POINTED_BOUNDS", inputs.pointerDown(e, "bounds"))
  11. }
  12. function handlePointerUp(e: React.PointerEvent<SVGRectElement>) {
  13. if (e.buttons !== 1) return
  14. e.stopPropagation()
  15. e.currentTarget.releasePointerCapture(e.pointerId)
  16. state.send("STOPPED_POINTING", inputs.pointerUp(e))
  17. }
  18. export default function BoundsBg() {
  19. const rBounds = useRef<SVGRectElement>(null)
  20. const bounds = useSelector((state) => state.values.selectedBounds)
  21. const isSelecting = useSelector((s) => s.isIn("selecting"))
  22. const rotation = useSelector((s) => {
  23. if (s.data.selectedIds.size === 1) {
  24. const { shapes } = getPage(s.data)
  25. const selected = Array.from(s.data.selectedIds.values())[0]
  26. return shapes[selected].rotation
  27. } else {
  28. return 0
  29. }
  30. })
  31. if (!bounds) return null
  32. if (!isSelecting) return null
  33. const { width, height } = bounds
  34. return (
  35. <StyledBoundsBg
  36. ref={rBounds}
  37. width={Math.max(1, width)}
  38. height={Math.max(1, height)}
  39. transform={`
  40. rotate(${rotation * (180 / Math.PI)},
  41. ${(bounds.minX + bounds.maxX) / 2},
  42. ${(bounds.minY + bounds.maxY) / 2})
  43. translate(${bounds.minX},${bounds.minY})`}
  44. onPointerDown={handlePointerDown}
  45. onPointerUp={handlePointerUp}
  46. />
  47. )
  48. }
  49. const StyledBoundsBg = styled("rect", {
  50. fill: "$boundsBg",
  51. })