Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React, { useRef, memo } from 'react'
  2. import { useSelector } from 'state'
  3. import styled from 'styles'
  4. import { getShapeUtils } from 'lib/shape-utils'
  5. import { getPage } from 'utils/utils'
  6. import { ShapeStyles } from 'types'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. function Shape({ id, isSelecting }: { id: string; isSelecting: boolean }) {
  9. const isHovered = useSelector((state) => state.data.hoveredId === id)
  10. const isSelected = useSelector((state) => state.values.selectedIds.has(id))
  11. const shape = useSelector(({ data }) => getPage(data).shapes[id])
  12. const rGroup = useRef<SVGGElement>(null)
  13. const events = useShapeEvents(id, rGroup)
  14. // This is a problem with deleted shapes. The hooks in this component
  15. // may sometimes run before the hook in the Page component, which means
  16. // a deleted shape will still be pulled here before the page component
  17. // detects the change and pulls this component.
  18. if (!shape) return null
  19. const transform = `
  20. rotate(${shape.rotation * (180 / Math.PI)},
  21. ${getShapeUtils(shape).getCenter(shape)})
  22. translate(${shape.point})`
  23. return (
  24. <StyledGroup
  25. ref={rGroup}
  26. isHovered={isHovered}
  27. isSelected={isSelected}
  28. transform={transform}
  29. {...events}
  30. >
  31. {isSelecting && <HoverIndicator as="use" href={'#' + id} />}
  32. <StyledShape id={id} style={shape.style} />
  33. </StyledGroup>
  34. )
  35. }
  36. const StyledShape = memo(
  37. ({ id, style }: { id: string; style: ShapeStyles }) => {
  38. return <MainShape as="use" href={'#' + id} {...style} />
  39. }
  40. )
  41. const MainShape = styled('use', {
  42. zStrokeWidth: 1,
  43. })
  44. const HoverIndicator = styled('path', {
  45. fill: 'none',
  46. stroke: 'transparent',
  47. pointerEvents: 'all',
  48. strokeLinecap: 'round',
  49. strokeLinejoin: 'round',
  50. transform: 'all .2s',
  51. })
  52. const StyledGroup = styled('g', {
  53. [`& ${HoverIndicator}`]: {
  54. opacity: '0',
  55. },
  56. variants: {
  57. isSelected: {
  58. true: {},
  59. false: {},
  60. },
  61. isHovered: {
  62. true: {},
  63. false: {},
  64. },
  65. },
  66. compoundVariants: [
  67. {
  68. isSelected: true,
  69. isHovered: true,
  70. css: {
  71. [`& ${HoverIndicator}`]: {
  72. opacity: '1',
  73. stroke: '$hint',
  74. zStrokeWidth: [8, 4],
  75. },
  76. },
  77. },
  78. {
  79. isSelected: true,
  80. isHovered: false,
  81. css: {
  82. [`& ${HoverIndicator}`]: {
  83. opacity: '1',
  84. stroke: '$hint',
  85. zStrokeWidth: [6, 3],
  86. },
  87. },
  88. },
  89. {
  90. isSelected: false,
  91. isHovered: true,
  92. css: {
  93. [`& ${HoverIndicator}`]: {
  94. opacity: '1',
  95. stroke: '$hint',
  96. zStrokeWidth: [8, 4],
  97. },
  98. },
  99. },
  100. ],
  101. })
  102. export { HoverIndicator }
  103. export default memo(Shape)