tldraw/components/canvas/selected.tsx

76 lines
1.7 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 { memo, useRef } from 'react'
2021-05-28 14:37:23 +00:00
2021-05-28 16:25:43 +00:00
export default function Selected() {
const selectedIds = useSelector((s) => s.data.selectedIds)
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) => (
<ShapeOutline key={id} id={id} isSelected={selectedIds.has(id)} />
2021-05-28 14:37:23 +00:00
))}
</g>
)
}
export const ShapeOutline = memo(
({ id, isSelected }: { id: string; isSelected: boolean }) => {
const rIndicator = useRef<SVGUseElement>(null)
2021-05-28 14:37:23 +00:00
const shape = useSelector(({ data }) => getPage(data).shapes[id])
2021-05-28 14:37:23 +00:00
const events = useShapeEvents(id, rIndicator)
2021-05-28 14:37:23 +00:00
if (!shape) return null
2021-05-28 16:25:43 +00:00
const transform = `
2021-05-30 13:20:25 +00:00
rotate(${shape.rotation * (180 / Math.PI)},
${getShapeUtils(shape).getCenter(shape)})
translate(${shape.point})
`
2021-05-28 16:25:43 +00:00
return (
<SelectIndicator
ref={rIndicator}
as="use"
href={'#' + id}
transform={transform}
isLocked={shape.isLocked}
{...events}
/>
)
}
)
2021-05-28 14:37:23 +00:00
2021-06-01 21:49:32 +00:00
const SelectIndicator = styled('path', {
zStrokeWidth: 3,
2021-05-28 14:37:23 +00:00
strokeLineCap: 'round',
strokeLinejoin: 'round',
stroke: '$selected',
fill: 'transparent',
2021-06-01 21:49:32 +00:00
pointerEvents: 'none',
paintOrder: 'stroke fill markers',
2021-05-29 12:40:41 +00:00
variants: {
isLocked: {
true: {
zDash: 2,
},
false: {},
},
2021-06-01 21:49:32 +00:00
variant: {},
2021-05-29 12:40:41 +00:00
},
2021-05-28 14:37:23 +00:00
})