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-06-24 08:18:14 +00:00
import { deepCompareArrays, getPage, getSelectedIds, setToArray } from 'utils'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
import { memo } from 'react'
2021-05-28 14:37:23 +00:00
2021-06-21 21:35:28 +00:00
export default function Selected(): JSX.Element {
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-20 22:01:40 +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-20 22:01:40 +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
2021-06-20 22:01:40 +00:00
// ref={rIndicator}
2021-06-03 21:23:16 +00:00
as="use"
href={'#' + id}
transform={transform}
isLocked={shape.isLocked}
2021-06-20 22:01:40 +00:00
// {...events}
2021-06-03 21:23:16 +00:00
/>
)
})
2021-05-28 14:37:23 +00:00
2021-06-01 21:49:32 +00:00
const SelectIndicator = styled('path', {
2021-06-20 22:01:40 +00:00
// zStrokeWidth: 2,
2021-05-28 14:37:23 +00:00
strokeLineCap: 'round',
strokeLinejoin: 'round',
2021-06-20 22:01:40 +00:00
stroke: 'red',
strokeWidth: '10',
2021-06-01 21:49:32 +00:00
pointerEvents: 'none',
2021-06-20 22:01:40 +00:00
fill: 'red',
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
})