2021-05-30 13:14:35 +00:00
|
|
|
import { useCallback, useRef } from 'react'
|
|
|
|
import state, { useSelector } from 'state'
|
|
|
|
import inputs from 'state/inputs'
|
|
|
|
import styled from 'styles'
|
2021-05-31 19:13:43 +00:00
|
|
|
import { deepCompareArrays, getPage } from 'utils/utils'
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
function handlePointerDown(e: React.PointerEvent<SVGRectElement>) {
|
|
|
|
if (e.buttons !== 1) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
e.currentTarget.setPointerCapture(e.pointerId)
|
2021-05-30 13:14:35 +00:00
|
|
|
state.send('POINTED_BOUNDS', inputs.pointerDown(e, 'bounds'))
|
2021-05-22 15:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handlePointerUp(e: React.PointerEvent<SVGRectElement>) {
|
|
|
|
if (e.buttons !== 1) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
e.currentTarget.releasePointerCapture(e.pointerId)
|
2021-05-30 13:14:35 +00:00
|
|
|
state.send('STOPPED_POINTING', inputs.pointerUp(e))
|
2021-05-22 15:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function BoundsBg() {
|
|
|
|
const rBounds = useRef<SVGRectElement>(null)
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const bounds = useSelector((state) => state.values.selectedBounds)
|
2021-05-31 19:13:43 +00:00
|
|
|
|
2021-05-30 13:14:35 +00:00
|
|
|
const isSelecting = useSelector((s) => s.isIn('selecting'))
|
2021-05-31 19:13:43 +00:00
|
|
|
|
|
|
|
const selectedIds = useSelector(
|
|
|
|
(s) => Array.from(s.values.selectedIds.values()),
|
|
|
|
deepCompareArrays
|
|
|
|
)
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const rotation = useSelector((s) => {
|
2021-05-31 19:13:43 +00:00
|
|
|
if (selectedIds.length === 1) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const { shapes } = getPage(s.data)
|
2021-05-31 19:13:43 +00:00
|
|
|
const selected = Array.from(s.values.selectedIds.values())[0]
|
2021-05-22 15:45:24 +00:00
|
|
|
return shapes[selected].rotation
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-05-31 19:13:43 +00:00
|
|
|
const isAllHandles = useSelector((s) => {
|
|
|
|
const page = getPage(s.data)
|
2021-06-01 08:56:41 +00:00
|
|
|
const selectedIds = Array.from(s.values.selectedIds.values())
|
|
|
|
return (
|
|
|
|
selectedIds.length === 1 &&
|
|
|
|
page.shapes[selectedIds[0]]?.handles !== undefined
|
2021-05-31 19:13:43 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (isAllHandles) return null
|
2021-05-22 15:45:24 +00:00
|
|
|
if (!bounds) return null
|
|
|
|
if (!isSelecting) return null
|
|
|
|
|
|
|
|
const { width, height } = bounds
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StyledBoundsBg
|
|
|
|
ref={rBounds}
|
|
|
|
width={Math.max(1, width)}
|
|
|
|
height={Math.max(1, height)}
|
|
|
|
transform={`
|
|
|
|
rotate(${rotation * (180 / Math.PI)},
|
|
|
|
${(bounds.minX + bounds.maxX) / 2},
|
|
|
|
${(bounds.minY + bounds.maxY) / 2})
|
2021-06-04 16:08:43 +00:00
|
|
|
translate(${bounds.minX},${bounds.minY})
|
|
|
|
rotate(${(bounds.rotation || 0) * (180 / Math.PI)}, 0, 0)`}
|
2021-05-22 15:45:24 +00:00
|
|
|
onPointerDown={handlePointerDown}
|
|
|
|
onPointerUp={handlePointerUp}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-30 13:14:35 +00:00
|
|
|
const StyledBoundsBg = styled('rect', {
|
|
|
|
fill: '$boundsBg',
|
2021-05-22 15:45:24 +00:00
|
|
|
})
|