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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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(
  41. ({ id, style }: { id: string; style: ReturnType<typeof getShapeStyle> }) => {
  42. return <StyledShape as="use" href={'#' + id} {...style} />
  43. }
  44. )
  45. const StyledShape = styled('path', {
  46. strokeLinecap: 'round',
  47. strokeLinejoin: 'round',
  48. pointerEvents: 'none',
  49. })
  50. const HoverIndicator = styled('path', {
  51. stroke: '$selected',
  52. strokeLinecap: 'round',
  53. strokeLinejoin: 'round',
  54. transform: 'all .2s',
  55. fill: 'transparent',
  56. filter: 'url(#expand)',
  57. variants: {
  58. variant: {
  59. hollow: {
  60. pointerEvents: 'stroke',
  61. },
  62. filled: {
  63. pointerEvents: 'all',
  64. },
  65. },
  66. },
  67. })
  68. const StyledGroup = styled('g', {
  69. [`& ${HoverIndicator}`]: {
  70. opacity: '0',
  71. },
  72. variants: {
  73. isSelected: {
  74. true: {
  75. [`& ${HoverIndicator}`]: {
  76. opacity: '0.2',
  77. },
  78. [`&:hover ${HoverIndicator}`]: {
  79. opacity: '0.3',
  80. },
  81. [`&:active ${HoverIndicator}`]: {
  82. opacity: '0.3',
  83. },
  84. },
  85. false: {
  86. [`& ${HoverIndicator}`]: {
  87. opacity: '0',
  88. },
  89. [`&:hover ${HoverIndicator}`]: {
  90. opacity: '0.16',
  91. },
  92. },
  93. },
  94. },
  95. })
  96. function Label({ text }: { text: string }) {
  97. return (
  98. <text
  99. y={4}
  100. x={4}
  101. fontSize={18}
  102. fill="black"
  103. stroke="none"
  104. alignmentBaseline="text-before-edge"
  105. pointerEvents="none"
  106. >
  107. {text}
  108. </text>
  109. )
  110. }
  111. export { HoverIndicator }
  112. export default memo(Shape)