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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. rotate(${(bounds.rotation || 0) * (180 / Math.PI)}, 0, 0)
  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: 1,
  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. })