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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(function ShapeOutline({
  23. id,
  24. isSelected,
  25. }: {
  26. id: string
  27. isSelected: boolean
  28. }) {
  29. const rIndicator = useRef<SVGUseElement>(null)
  30. const shape = useSelector(({ data }) => getPage(data).shapes[id])
  31. const events = useShapeEvents(id, rIndicator)
  32. if (!shape) return null
  33. const transform = `
  34. rotate(${shape.rotation * (180 / Math.PI)},
  35. ${getShapeUtils(shape).getCenter(shape)})
  36. translate(${shape.point})
  37. `
  38. return (
  39. <SelectIndicator
  40. ref={rIndicator}
  41. as="use"
  42. href={'#' + id}
  43. transform={transform}
  44. isLocked={shape.isLocked}
  45. {...events}
  46. />
  47. )
  48. })
  49. const SelectIndicator = styled('path', {
  50. zStrokeWidth: 3,
  51. strokeLineCap: 'round',
  52. strokeLinejoin: 'round',
  53. stroke: '$selected',
  54. fill: 'transparent',
  55. pointerEvents: 'none',
  56. paintOrder: 'stroke fill markers',
  57. variants: {
  58. isLocked: {
  59. true: {
  60. zDash: 2,
  61. },
  62. false: {},
  63. },
  64. variant: {},
  65. },
  66. })