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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import React, { useRef, memo, useEffect } from 'react'
  2. import { useSelector } from 'state'
  3. import styled from 'styles'
  4. import { getShapeUtils } from 'state/shape-utils'
  5. import { getPage, getSelectedIds, isMobile } from 'utils/utils'
  6. import { Shape as _Shape } from 'types'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. import vec from 'utils/vec'
  9. import { getShapeStyle } from 'state/shape-styles'
  10. const isMobileDevice = isMobile()
  11. interface ShapeProps {
  12. id: string
  13. isSelecting: boolean
  14. parentPoint: number[]
  15. }
  16. function Shape({ id, isSelecting, parentPoint }: ShapeProps): JSX.Element {
  17. const rGroup = useRef<SVGGElement>(null)
  18. const rFocusable = useRef<HTMLTextAreaElement>(null)
  19. const isEditing = useSelector((s) => s.data.editingId === id)
  20. const isSelected = useSelector((s) => getSelectedIds(s.data).has(id))
  21. const shape = useSelector((s) => getPage(s.data).shapes[id])
  22. const events = useShapeEvents(id, getShapeUtils(shape)?.isParent, rGroup)
  23. useEffect(() => {
  24. if (isEditing) {
  25. setTimeout(() => {
  26. const elm = rFocusable.current
  27. if (!elm) return
  28. elm.focus()
  29. }, 0)
  30. }
  31. }, [isEditing])
  32. // This is a problem with deleted shapes. The hooks in this component
  33. // may sometimes run before the hook in the Page component, which means
  34. // a deleted shape will still be pulled here before the page component
  35. // detects the change and pulls this component.
  36. if (!shape) return null
  37. const style = getShapeStyle(shape.style)
  38. const shapeUtils = getShapeUtils(shape)
  39. const { isShy, isParent, isForeignObject } = shapeUtils
  40. const bounds = shapeUtils.getBounds(shape)
  41. const center = shapeUtils.getCenter(shape)
  42. const rotation = shape.rotation * (180 / Math.PI)
  43. const transform = `
  44. translate(${vec.neg(parentPoint)})
  45. rotate(${rotation}, ${center})
  46. translate(${shape.point})
  47. `
  48. return (
  49. <StyledGroup
  50. id={id + '-group'}
  51. ref={rGroup}
  52. transform={transform}
  53. isSelected={isSelected}
  54. device={isMobileDevice ? 'mobile' : 'desktop'}
  55. {...events}
  56. >
  57. {!isShy && (
  58. <>
  59. {isForeignObject ? (
  60. <HoverIndicator
  61. as="rect"
  62. width={bounds.width}
  63. height={bounds.height}
  64. strokeWidth={1.5}
  65. variant={'ghost'}
  66. />
  67. ) : (
  68. <HoverIndicator
  69. as="use"
  70. href={'#' + id}
  71. strokeWidth={+style.strokeWidth + 5}
  72. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  73. />
  74. )}
  75. </>
  76. )}
  77. {!shape.isHidden &&
  78. (isForeignObject ? (
  79. shapeUtils.render(shape, { isEditing, ref: rFocusable })
  80. ) : (
  81. <RealShape
  82. isParent={isParent}
  83. id={id}
  84. shape={shape}
  85. style={style}
  86. isEditing={isEditing}
  87. />
  88. ))}
  89. {isParent &&
  90. shape.children.map((shapeId) => (
  91. <Shape
  92. key={shapeId}
  93. id={shapeId}
  94. isSelecting={isSelecting}
  95. parentPoint={shape.point}
  96. />
  97. ))}
  98. </StyledGroup>
  99. )
  100. }
  101. interface RealShapeProps {
  102. id: string
  103. style: Partial<React.SVGProps<SVGUseElement>>
  104. isParent: boolean
  105. shape: _Shape
  106. isEditing: boolean
  107. }
  108. const RealShape = memo(function RealShape({ id, isParent }: RealShapeProps) {
  109. return <StyledShape as="use" data-shy={isParent} href={'#' + id} />
  110. })
  111. const StyledShape = styled('path', {
  112. strokeLinecap: 'round',
  113. strokeLinejoin: 'round',
  114. pointerEvents: 'none',
  115. })
  116. const HoverIndicator = styled('path', {
  117. stroke: '$selected',
  118. strokeLinecap: 'round',
  119. strokeLinejoin: 'round',
  120. fill: 'transparent',
  121. filter: 'url(#expand)',
  122. variants: {
  123. variant: {
  124. ghost: {
  125. pointerEvents: 'all',
  126. filter: 'none',
  127. opacity: 0,
  128. },
  129. hollow: {
  130. pointerEvents: 'stroke',
  131. },
  132. filled: {
  133. pointerEvents: 'all',
  134. },
  135. },
  136. },
  137. })
  138. const StyledGroup = styled('g', {
  139. outline: 'none',
  140. [`& *[data-shy="true"]`]: {
  141. opacity: '0',
  142. },
  143. [`& ${HoverIndicator}`]: {
  144. opacity: '0',
  145. },
  146. variants: {
  147. device: {
  148. mobile: {},
  149. desktop: {},
  150. },
  151. isSelected: {
  152. true: {
  153. [`& *[data-shy="true"]`]: {
  154. opacity: '1',
  155. },
  156. [`& ${HoverIndicator}`]: {
  157. opacity: '0.2',
  158. },
  159. },
  160. false: {
  161. [`& ${HoverIndicator}`]: {
  162. opacity: '0',
  163. },
  164. },
  165. },
  166. },
  167. compoundVariants: [
  168. {
  169. device: 'desktop',
  170. isSelected: 'false',
  171. css: {
  172. [`&:hover ${HoverIndicator}`]: {
  173. opacity: '0.16',
  174. },
  175. [`&:hover *[data-shy="true"]`]: {
  176. opacity: '1',
  177. },
  178. },
  179. },
  180. {
  181. device: 'desktop',
  182. isSelected: 'true',
  183. css: {
  184. [`&:hover ${HoverIndicator}`]: {
  185. opacity: '0.25',
  186. },
  187. [`&:active ${HoverIndicator}`]: {
  188. opacity: '0.25',
  189. },
  190. },
  191. },
  192. ],
  193. })
  194. // function Label({ children }: { children: React.ReactNode }) {
  195. // return (
  196. // <text
  197. // y={4}
  198. // x={4}
  199. // fontSize={12}
  200. // fill="black"
  201. // stroke="none"
  202. // alignmentBaseline="text-before-edge"
  203. // pointerEvents="none"
  204. // >
  205. // {children}
  206. // </text>
  207. // )
  208. // }
  209. // function pp(n: number[]) {
  210. // return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
  211. // }
  212. export { HoverIndicator }
  213. export default memo(Shape)