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

selected.tsx 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import styled from 'styles'
  2. import { useSelector } from 'state'
  3. import {
  4. deepCompareArrays,
  5. getBoundsCenter,
  6. getPage,
  7. getSelectedShapes,
  8. } from 'utils/utils'
  9. import * as vec from 'utils/vec'
  10. import { getShapeUtils } from 'lib/shape-utils'
  11. import { Bounds } from 'types'
  12. import useShapeEvents from 'hooks/useShapeEvents'
  13. import { useRef } from 'react'
  14. export default function Selected({ bounds }: { bounds: Bounds }) {
  15. const currentPageShapeIds = useSelector(({ data }) => {
  16. return Array.from(data.selectedIds.values())
  17. }, deepCompareArrays)
  18. return (
  19. <g>
  20. {currentPageShapeIds.map((id) => (
  21. <ShapeOutline key={id} id={id} bounds={bounds} />
  22. ))}
  23. </g>
  24. )
  25. }
  26. export function ShapeOutline({ id, bounds }: { id: string; bounds: Bounds }) {
  27. const rIndicator = useRef<SVGUseElement>(null)
  28. const shape = useSelector(({ data }) => getPage(data).shapes[id])
  29. const shapeBounds = getShapeUtils(shape).getBounds(shape)
  30. const events = useShapeEvents(id, rIndicator)
  31. return (
  32. <Indicator
  33. ref={rIndicator}
  34. as="use"
  35. href={'#' + id}
  36. transform={`rotate(${shape.rotation * (180 / Math.PI)},${getBoundsCenter(
  37. shapeBounds
  38. )}) translate(${vec.sub(shape.point, [bounds.minX, bounds.minY])})`}
  39. {...events}
  40. />
  41. )
  42. }
  43. const Indicator = styled('path', {
  44. zStrokeWidth: 1,
  45. strokeLineCap: 'round',
  46. strokeLinejoin: 'round',
  47. stroke: '$selected',
  48. fill: 'transparent',
  49. pointerEvents: 'all',
  50. })