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.9KB

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. import { ShapeType } from 'types'
  8. import * as vec from 'utils/vec'
  9. export default function Selected() {
  10. const currentSelectedShapeIds = useSelector(({ data }) => {
  11. return Array.from(data.selectedIds.values())
  12. }, deepCompareArrays)
  13. const isSelecting = useSelector((s) => s.isIn('selecting'))
  14. if (!isSelecting) return null
  15. return (
  16. <g>
  17. {currentSelectedShapeIds.map((id) => (
  18. <ShapeOutline key={id} id={id} />
  19. ))}
  20. </g>
  21. )
  22. }
  23. export const ShapeOutline = memo(function ShapeOutline({ id }: { id: string }) {
  24. const rIndicator = useRef<SVGUseElement>(null)
  25. const shape = useSelector((s) => getPage(s.data).shapes[id])
  26. const events = useShapeEvents(id, shape?.type === ShapeType.Group, rIndicator)
  27. if (!shape) return null
  28. // This needs computation from state, similar to bounds, in order
  29. // to handle parent rotation.
  30. const center = getShapeUtils(shape).getCenter(shape)
  31. const bounds = getShapeUtils(shape).getBounds(shape)
  32. const transform = `
  33. rotate(${shape.rotation * (180 / Math.PI)},
  34. ${center})
  35. translate(${bounds.minX},${bounds.minY})
  36. `
  37. return (
  38. <SelectIndicator
  39. ref={rIndicator}
  40. as="use"
  41. href={'#' + id}
  42. transform={transform}
  43. isLocked={shape.isLocked}
  44. {...events}
  45. />
  46. )
  47. })
  48. const SelectIndicator = styled('path', {
  49. zStrokeWidth: 1,
  50. strokeLineCap: 'round',
  51. strokeLinejoin: 'round',
  52. stroke: '$selected',
  53. fill: 'transparent',
  54. pointerEvents: 'none',
  55. paintOrder: 'stroke fill markers',
  56. variants: {
  57. isLocked: {
  58. true: {
  59. zDash: 2,
  60. },
  61. false: {},
  62. },
  63. variant: {},
  64. },
  65. })