tldraw/components/canvas/selected.tsx

80 lines
1.9 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-06-04 16:08:43 +00:00
import { ShapeType } from 'types'
import * as vec from 'utils/vec'
2021-05-28 14:37:23 +00:00
2021-05-28 16:25:43 +00:00
export default function Selected() {
2021-06-04 16:08:43 +00:00
const currentSelectedShapeIds = useSelector(({ data }) => {
2021-05-28 14:37:23 +00:00
return Array.from(data.selectedIds.values())
}, deepCompareArrays)
2021-06-05 06:36:39 +00:00
const isSelecting = useSelector((s) => s.isInAny('notPointing', 'pinching'))
2021-05-28 16:25:43 +00:00
if (!isSelecting) return null
2021-05-28 14:37:23 +00:00
return (
<g>
2021-06-04 16:08:43 +00:00
{currentSelectedShapeIds.map((id) => (
<ShapeOutline key={id} id={id} />
2021-05-28 14:37:23 +00:00
))}
</g>
)
}
2021-06-04 16:08:43 +00:00
export const ShapeOutline = memo(function ShapeOutline({ id }: { id: string }) {
2021-06-03 21:23:16 +00:00
const rIndicator = useRef<SVGUseElement>(null)
2021-05-28 14:37:23 +00:00
2021-06-04 16:08:43 +00:00
const shape = useSelector((s) => getPage(s.data).shapes[id])
2021-05-28 14:37:23 +00:00
2021-06-04 16:08:43 +00:00
const events = useShapeEvents(id, shape?.type === ShapeType.Group, rIndicator)
2021-05-28 14:37:23 +00:00
2021-06-03 21:23:16 +00:00
if (!shape) return null
2021-05-28 16:25:43 +00:00
2021-06-04 16:08:43 +00:00
// This needs computation from state, similar to bounds, in order
// to handle parent rotation.
const center = getShapeUtils(shape).getCenter(shape)
const bounds = getShapeUtils(shape).getBounds(shape)
2021-06-03 21:23:16 +00:00
const transform = `
2021-06-04 16:08:43 +00:00
rotate(${shape.rotation * (180 / Math.PI)},
${center})
translate(${bounds.minX},${bounds.minY})
2021-05-30 13:20:25 +00:00
`
2021-05-28 16:25:43 +00:00
2021-06-03 21:23:16 +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', {
2021-06-04 16:08:43 +00:00
zStrokeWidth: 1,
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
})