Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

selected.tsx 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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: '$selected',
  57. pointerEvents: 'none',
  58. fill: 'none',
  59. variants: {
  60. isLocked: {
  61. true: {
  62. zDash: 2,
  63. },
  64. false: {},
  65. },
  66. variant: {},
  67. },
  68. })