tldraw/components/canvas/defs.tsx

46 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { getShapeStyle } from 'state/shape-styles'
import { getShapeUtils } from 'state/shape-utils'
2021-06-20 22:01:40 +00:00
import React, { memo } from 'react'
2021-05-28 16:25:43 +00:00
import { useSelector } from 'state'
import { getCurrentCamera } from 'utils'
2021-06-05 06:36:39 +00:00
import { DotCircle, Handle } from './misc'
2021-06-26 11:52:36 +00:00
import useShapeDef from 'hooks/useShape'
import useShapesToRender from 'hooks/useShapesToRender'
2021-05-28 13:08:51 +00:00
2021-06-21 21:35:28 +00:00
export default function Defs(): JSX.Element {
const shapeIdsToRender = useShapesToRender()
2021-05-28 13:08:51 +00:00
return (
<defs>
{shapeIdsToRender.map((id) => (
2021-05-28 13:08:51 +00:00
<Def key={id} id={id} />
))}
2021-06-05 06:36:39 +00:00
<DotCircle id="dot" r={4} />
<Handle id="handle" r={4} />
<ExpandDef />
2021-05-28 13:08:51 +00:00
</defs>
)
}
2021-06-03 21:23:16 +00:00
const Def = memo(function Def({ id }: { id: string }) {
2021-06-26 11:52:36 +00:00
const shape = useShapeDef(id)
2021-05-28 16:25:43 +00:00
if (!shape) return null
2021-06-20 22:01:40 +00:00
const style = getShapeStyle(shape.style)
return React.cloneElement(
getShapeUtils(shape).render(shape, { isEditing: false }),
{ id, ...style }
2021-06-20 22:01:40 +00:00
)
})
function ExpandDef() {
const zoom = useSelector((s) => getCurrentCamera(s.data).zoom)
return (
<filter id="expand">
<feMorphology operator="dilate" radius={2 / zoom} />
</filter>
)
}