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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 { DashStyle, ShapeStyles } from 'types'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. import { getShapeStyle } from 'lib/shape-styles'
  9. function Shape({ id, isSelecting }: { id: string; isSelecting: boolean }) {
  10. const isSelected = useSelector((s) => s.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. `
  24. const style = getShapeStyle(shape.style)
  25. return (
  26. <StyledGroup ref={rGroup} isSelected={isSelected} transform={transform}>
  27. {isSelecting && (
  28. <HoverIndicator
  29. as="use"
  30. href={'#' + id}
  31. strokeWidth={+style.strokeWidth + 4}
  32. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  33. {...events}
  34. />
  35. )}
  36. {!shape.isHidden && <RealShape id={id} style={style} />}
  37. </StyledGroup>
  38. )
  39. }
  40. const RealShape = memo(function RealShape({
  41. id,
  42. style,
  43. }: {
  44. id: string
  45. style: ReturnType<typeof getShapeStyle>
  46. }) {
  47. return <StyledShape as="use" href={'#' + id} {...style} />
  48. })
  49. const StyledShape = styled('path', {
  50. strokeLinecap: 'round',
  51. strokeLinejoin: 'round',
  52. pointerEvents: 'none',
  53. })
  54. const HoverIndicator = styled('path', {
  55. stroke: '$selected',
  56. strokeLinecap: 'round',
  57. strokeLinejoin: 'round',
  58. transform: 'all .2s',
  59. fill: 'transparent',
  60. filter: 'url(#expand)',
  61. variants: {
  62. variant: {
  63. hollow: {
  64. pointerEvents: 'stroke',
  65. },
  66. filled: {
  67. pointerEvents: 'all',
  68. },
  69. },
  70. },
  71. })
  72. const StyledGroup = styled('g', {
  73. [`& ${HoverIndicator}`]: {
  74. opacity: '0',
  75. },
  76. variants: {
  77. isSelected: {
  78. true: {
  79. [`& ${HoverIndicator}`]: {
  80. opacity: '0.2',
  81. },
  82. [`&:hover ${HoverIndicator}`]: {
  83. opacity: '0.3',
  84. },
  85. [`&:active ${HoverIndicator}`]: {
  86. opacity: '0.3',
  87. },
  88. },
  89. false: {
  90. [`& ${HoverIndicator}`]: {
  91. opacity: '0',
  92. },
  93. [`&:hover ${HoverIndicator}`]: {
  94. opacity: '0.16',
  95. },
  96. },
  97. },
  98. },
  99. })
  100. function Label({ text }: { text: string }) {
  101. return (
  102. <text
  103. y={4}
  104. x={4}
  105. fontSize={18}
  106. fill="black"
  107. stroke="none"
  108. alignmentBaseline="text-before-edge"
  109. pointerEvents="none"
  110. >
  111. {text}
  112. </text>
  113. )
  114. }
  115. export { HoverIndicator }
  116. export default memo(Shape)