tldraw/components/canvas/defs.tsx

46 lines
1.3 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'
2021-06-24 08:18:14 +00:00
import { deepCompareArrays, getCurrentCamera, getPage } 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'
2021-05-28 13:08:51 +00:00
2021-06-21 21:35:28 +00:00
export default function Defs(): JSX.Element {
2021-06-03 12:06:39 +00:00
const zoom = useSelector((s) => getCurrentCamera(s.data).zoom)
2021-05-28 13:08:51 +00:00
const currentPageShapeIds = useSelector(({ data }) => {
return Object.values(getPage(data).shapes)
.filter(Boolean)
.filter((shape) => !getShapeUtils(shape).isForeignObject)
2021-05-28 13:08:51 +00:00
.sort((a, b) => a.childIndex - b.childIndex)
.map((shape) => shape.id)
}, deepCompareArrays)
return (
<defs>
{currentPageShapeIds.map((id) => (
<Def key={id} id={id} />
))}
2021-06-05 06:36:39 +00:00
<DotCircle id="dot" r={4} />
<Handle id="handle" r={4} />
<filter id="expand">
<feMorphology operator="dilate" radius={2 / zoom} />
</filter>
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
)
})