You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

selected.tsx 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import styled from 'styles'
  2. import { useSelector } from 'state'
  3. import { deepCompareArrays, getPage } from 'utils/utils'
  4. import { getShapeUtils } from 'lib/shape-utils'
  5. import useShapeEvents from 'hooks/useShapeEvents'
  6. import { memo, useRef } from 'react'
  7. export default function Selected() {
  8. const selectedIds = useSelector((s) => s.data.selectedIds)
  9. const currentPageShapeIds = useSelector(({ data }) => {
  10. return Array.from(data.selectedIds.values())
  11. }, deepCompareArrays)
  12. const isSelecting = useSelector((s) => s.isIn('selecting'))
  13. if (!isSelecting) return null
  14. return (
  15. <g>
  16. {currentPageShapeIds.map((id) => (
  17. <ShapeOutline key={id} id={id} isSelected={selectedIds.has(id)} />
  18. ))}
  19. </g>
  20. )
  21. }
  22. export const ShapeOutline = memo(
  23. ({ id, isSelected }: { id: string; isSelected: boolean }) => {
  24. const rIndicator = useRef<SVGUseElement>(null)
  25. const shape = useSelector(({ data }) => getPage(data).shapes[id])
  26. const events = useShapeEvents(id, rIndicator)
  27. if (!shape) return null
  28. const transform = `
  29. rotate(${shape.rotation * (180 / Math.PI)},
  30. ${getShapeUtils(shape).getCenter(shape)})
  31. translate(${shape.point})
  32. `
  33. return (
  34. <SelectIndicator
  35. ref={rIndicator}
  36. as="use"
  37. href={'#' + id}
  38. transform={transform}
  39. isLocked={shape.isLocked}
  40. {...events}
  41. />
  42. )
  43. }
  44. )
  45. const SelectIndicator = styled('path', {
  46. zStrokeWidth: 3,
  47. strokeLineCap: 'round',
  48. strokeLinejoin: 'round',
  49. stroke: '$selected',
  50. fill: 'transparent',
  51. pointerEvents: 'none',
  52. paintOrder: 'stroke fill markers',
  53. variants: {
  54. isLocked: {
  55. true: {
  56. zDash: 2,
  57. },
  58. false: {},
  59. },
  60. variant: {},
  61. },
  62. })