tldraw/components/canvas/selected.tsx

69 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-05-28 14:37:23 +00:00
import styled from 'styles'
import { useSelector } from 'state'
2021-05-28 16:25:43 +00:00
import { deepCompareArrays, getPage } from 'utils/utils'
2021-05-28 14:37:23 +00:00
import { getShapeUtils } from 'lib/shape-utils'
import useShapeEvents from 'hooks/useShapeEvents'
import { useRef } from 'react'
2021-05-28 16:25:43 +00:00
export default function Selected() {
2021-05-28 14:37:23 +00:00
const currentPageShapeIds = useSelector(({ data }) => {
return Array.from(data.selectedIds.values())
}, deepCompareArrays)
2021-05-28 16:25:43 +00:00
const isSelecting = useSelector((s) => s.isIn('selecting'))
if (!isSelecting) return null
2021-05-28 14:37:23 +00:00
return (
<g>
{currentPageShapeIds.map((id) => (
2021-05-28 16:25:43 +00:00
<ShapeOutline key={id} id={id} />
2021-05-28 14:37:23 +00:00
))}
</g>
)
}
2021-05-28 16:25:43 +00:00
export function ShapeOutline({ id }: { id: string }) {
2021-05-28 14:37:23 +00:00
const rIndicator = useRef<SVGUseElement>(null)
const shape = useSelector(({ data }) => getPage(data).shapes[id])
const events = useShapeEvents(id, rIndicator)
2021-05-28 16:25:43 +00:00
if (!shape) return null
const transform = `
rotate(${shape.rotation * (180 / Math.PI)},
${getShapeUtils(shape).getCenter(shape)})
translate(${shape.point})`
2021-05-28 14:37:23 +00:00
return (
<Indicator
ref={rIndicator}
as="use"
href={'#' + id}
2021-05-28 16:25:43 +00:00
transform={transform}
2021-05-29 12:40:41 +00:00
isLocked={shape.isLocked}
2021-05-28 14:37:23 +00:00
{...events}
/>
)
}
const Indicator = styled('path', {
zStrokeWidth: 1,
strokeLineCap: 'round',
strokeLinejoin: 'round',
stroke: '$selected',
fill: 'transparent',
pointerEvents: 'all',
2021-05-29 12:40:41 +00:00
variants: {
isLocked: {
true: {
zDash: 2,
},
false: {},
},
},
2021-05-28 14:37:23 +00:00
})