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

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