tldraw/components/canvas/selected.tsx

83 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-05-28 14:37:23 +00:00
import styled from 'styles'
import { useSelector } from 'state'
import {
deepCompareArrays,
getPage,
getSelectedIds,
setToArray,
} 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 vec from 'utils/vec'
2021-05-28 14:37:23 +00:00
2021-05-28 16:25:43 +00:00
export default function Selected() {
const currentSelectedShapeIds = useSelector(
({ data }) => setToArray(getSelectedIds(data)),
deepCompareArrays
)
2021-05-28 14:37:23 +00:00
const isSelecting = useSelector((s) => s.isIn('selecting'))
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)
2021-06-03 21:23:16 +00:00
const transform = `
2021-06-17 21:50:04 +00:00
rotate(${shape.rotation * (180 / Math.PI)}, ${center})
translate(${shape.point})
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-15 12:20:22 +00:00
zStrokeWidth: 2,
2021-05-28 14:37:23 +00:00
strokeLineCap: 'round',
strokeLinejoin: 'round',
stroke: '$selected',
2021-06-01 21:49:32 +00:00
pointerEvents: 'none',
2021-06-17 21:50:04 +00:00
fill: 'transparent',
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
})