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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 { ShapeType } from 'types'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. import * as vec from 'utils/vec'
  9. import { getShapeStyle } from 'lib/shape-styles'
  10. interface ShapeProps {
  11. id: string
  12. isSelecting: boolean
  13. parentPoint: number[]
  14. parentRotation: number
  15. }
  16. function Shape({ id, isSelecting, parentPoint, parentRotation }: ShapeProps) {
  17. const shape = useSelector(({ data }) => getPage(data).shapes[id])
  18. const rGroup = useRef<SVGGElement>(null)
  19. const events = useShapeEvents(id, shape?.type === ShapeType.Group, rGroup)
  20. // This is a problem with deleted shapes. The hooks in this component
  21. // may sometimes run before the hook in the Page component, which means
  22. // a deleted shape will still be pulled here before the page component
  23. // detects the change and pulls this component.
  24. if (!shape) return null
  25. const isGroup = shape.type === ShapeType.Group
  26. const center = getShapeUtils(shape).getCenter(shape)
  27. const transform = `
  28. rotate(${shape.rotation * (180 / Math.PI)}, ${vec.sub(center, parentPoint)})
  29. translate(${vec.sub(shape.point, parentPoint)})
  30. `
  31. const style = getShapeStyle(shape.style)
  32. return (
  33. <>
  34. <StyledGroup ref={rGroup} transform={transform}>
  35. {isSelecting && !isGroup && (
  36. <HoverIndicator
  37. as="use"
  38. href={'#' + id}
  39. strokeWidth={+style.strokeWidth + 4}
  40. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  41. {...events}
  42. />
  43. )}
  44. {!shape.isHidden && <StyledShape as="use" href={'#' + id} {...style} />}
  45. {isGroup &&
  46. shape.children.map((shapeId) => (
  47. <Shape
  48. key={shapeId}
  49. id={shapeId}
  50. isSelecting={isSelecting}
  51. parentPoint={shape.point}
  52. parentRotation={shape.rotation}
  53. />
  54. ))}
  55. </StyledGroup>
  56. </>
  57. )
  58. }
  59. const StyledShape = styled('path', {
  60. strokeLinecap: 'round',
  61. strokeLinejoin: 'round',
  62. pointerEvents: 'none',
  63. })
  64. const HoverIndicator = styled('path', {
  65. stroke: '$selected',
  66. strokeLinecap: 'round',
  67. strokeLinejoin: 'round',
  68. transform: 'all .2s',
  69. fill: 'transparent',
  70. filter: 'url(#expand)',
  71. variants: {
  72. variant: {
  73. hollow: {
  74. pointerEvents: 'stroke',
  75. },
  76. filled: {
  77. pointerEvents: 'all',
  78. },
  79. },
  80. },
  81. })
  82. const StyledGroup = styled('g', {
  83. [`& ${HoverIndicator}`]: {
  84. opacity: '0',
  85. },
  86. [`&:hover ${HoverIndicator}`]: {
  87. opacity: '0.16',
  88. },
  89. variants: {
  90. isSelected: {
  91. true: {
  92. [`& ${HoverIndicator}`]: {
  93. opacity: '0.2',
  94. },
  95. [`&:hover ${HoverIndicator}`]: {
  96. opacity: '0.3',
  97. },
  98. [`&:active ${HoverIndicator}`]: {
  99. opacity: '0.3',
  100. },
  101. },
  102. false: {
  103. [`& ${HoverIndicator}`]: {
  104. opacity: '0',
  105. },
  106. },
  107. },
  108. },
  109. })
  110. function Label({ children }: { children: React.ReactNode }) {
  111. return (
  112. <text
  113. y={4}
  114. x={4}
  115. fontSize={12}
  116. fill="black"
  117. stroke="none"
  118. alignmentBaseline="text-before-edge"
  119. pointerEvents="none"
  120. >
  121. {children}
  122. </text>
  123. )
  124. }
  125. export { HoverIndicator }
  126. export default memo(Shape)
  127. function pp(n: number[]) {
  128. return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
  129. }