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.

shape.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 center = getShapeUtils(shape).getCenter(shape)
  20. const transform = `
  21. rotate(${shape.rotation * (180 / Math.PI)}, ${center})
  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. {/*
  34. <text
  35. y={4}
  36. x={4}
  37. fontSize={18}
  38. fill="black"
  39. stroke="none"
  40. alignmentBaseline="text-before-edge"
  41. pointerEvents="none"
  42. >
  43. {center.toString()}
  44. </text> */}
  45. </StyledGroup>
  46. )
  47. }
  48. const StyledShape = memo(
  49. ({ id, style }: { id: string; style: ShapeStyles }) => {
  50. return (
  51. <MainShape
  52. as="use"
  53. href={'#' + id}
  54. {...style}
  55. // css={{ zStrokeWidth: Number(style.strokeWidth) }}
  56. />
  57. )
  58. }
  59. )
  60. const MainShape = styled('use', {
  61. // zStrokeWidth: 1,
  62. })
  63. const HoverIndicator = styled('path', {
  64. fill: 'none',
  65. stroke: 'transparent',
  66. pointerEvents: 'all',
  67. strokeLinecap: 'round',
  68. strokeLinejoin: 'round',
  69. transform: 'all .2s',
  70. })
  71. const StyledGroup = styled('g', {
  72. [`& ${HoverIndicator}`]: {
  73. opacity: '0',
  74. },
  75. variants: {
  76. isSelected: {
  77. true: {},
  78. false: {},
  79. },
  80. isHovered: {
  81. true: {},
  82. false: {},
  83. },
  84. },
  85. compoundVariants: [
  86. {
  87. isSelected: true,
  88. isHovered: true,
  89. css: {
  90. [`& ${HoverIndicator}`]: {
  91. opacity: '1',
  92. stroke: '$hint',
  93. zStrokeWidth: [8, 4],
  94. },
  95. },
  96. },
  97. {
  98. isSelected: true,
  99. isHovered: false,
  100. css: {
  101. [`& ${HoverIndicator}`]: {
  102. opacity: '1',
  103. stroke: '$hint',
  104. zStrokeWidth: [6, 3],
  105. },
  106. },
  107. },
  108. {
  109. isSelected: false,
  110. isHovered: true,
  111. css: {
  112. [`& ${HoverIndicator}`]: {
  113. opacity: '1',
  114. stroke: '$hint',
  115. zStrokeWidth: [8, 4],
  116. },
  117. },
  118. },
  119. ],
  120. })
  121. export { HoverIndicator }
  122. export default memo(Shape)