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

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