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

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