您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

defs.tsx 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { getShapeStyle } from 'state/shape-styles'
  2. import { getShapeUtils } from 'state/shape-utils'
  3. import React from 'react'
  4. import { useSelector } from 'state'
  5. import { getCurrentCamera } from 'utils'
  6. import { DotCircle, Handle } from './misc'
  7. import useShapeDef from 'hooks/useShape'
  8. import useShapesToRender from 'hooks/useShapesToRender'
  9. export default function Defs(): JSX.Element {
  10. const shapeIdsToRender = useShapesToRender()
  11. return (
  12. <defs>
  13. <DotCircle id="dot" r={4} />
  14. <Handle id="handle" r={4} />
  15. <ExpandDef />
  16. {shapeIdsToRender.map((id) => (
  17. <Def key={id} id={id} />
  18. ))}
  19. </defs>
  20. )
  21. }
  22. function Def({ id }: { id: string }) {
  23. const shape = useShapeDef(id)
  24. if (!shape) return null
  25. const style = getShapeStyle(shape.style)
  26. return (
  27. <>
  28. {React.cloneElement(
  29. getShapeUtils(shape).render(shape, { isEditing: false }),
  30. { id, ...style, strokeWidth: undefined }
  31. )}
  32. </>
  33. )
  34. }
  35. function ExpandDef() {
  36. const zoom = useSelector((s) => getCurrentCamera(s.data).zoom)
  37. return (
  38. <filter id="expand">
  39. <feMorphology operator="dilate" radius={2 / zoom} />
  40. </filter>
  41. )
  42. }