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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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'
  6. import useShapeEvents from 'hooks/useShapeEvents'
  7. import { Shape as _Shape } from 'types'
  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. {isSelecting && !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={shapeUtils.canStyleFill ? 'filled' : 'hollow'}
  73. />
  74. )}
  75. </>
  76. )}
  77. {!shape.isHidden &&
  78. (isForeignObject ? (
  79. shapeUtils.render(shape, { isEditing, ref: rFocusable })
  80. ) : (
  81. <RealShape id={id} isParent={isParent} shape={shape} />
  82. ))}
  83. {isParent &&
  84. shape.children.map((shapeId) => (
  85. <Shape
  86. key={shapeId}
  87. id={shapeId}
  88. isSelecting={isSelecting}
  89. parentPoint={shape.point}
  90. />
  91. ))}
  92. </StyledGroup>
  93. )
  94. }
  95. interface RealShapeProps {
  96. id: string
  97. shape: _Shape
  98. isParent: boolean
  99. }
  100. const RealShape = memo(function RealShape({ id, isParent }: RealShapeProps) {
  101. return <StyledShape as="use" data-shy={isParent} href={'#' + id} />
  102. })
  103. const StyledShape = styled('path', {
  104. strokeLinecap: 'round',
  105. strokeLinejoin: 'round',
  106. pointerEvents: 'none',
  107. })
  108. const HoverIndicator = styled('path', {
  109. stroke: '$selected',
  110. strokeLinecap: 'round',
  111. strokeLinejoin: 'round',
  112. fill: 'transparent',
  113. filter: 'url(#expand)',
  114. variants: {
  115. variant: {
  116. ghost: {
  117. pointerEvents: 'all',
  118. filter: 'none',
  119. opacity: 0,
  120. },
  121. hollow: {
  122. pointerEvents: 'stroke',
  123. },
  124. filled: {
  125. pointerEvents: 'all',
  126. },
  127. },
  128. },
  129. })
  130. const StyledGroup = styled('g', {
  131. outline: 'none',
  132. [`& *[data-shy="true"]`]: {
  133. opacity: '0',
  134. },
  135. [`& ${HoverIndicator}`]: {
  136. opacity: '0',
  137. },
  138. variants: {
  139. device: {
  140. mobile: {},
  141. desktop: {},
  142. },
  143. isSelected: {
  144. true: {
  145. [`& *[data-shy="true"]`]: {
  146. opacity: '1',
  147. },
  148. [`& ${HoverIndicator}`]: {
  149. opacity: '0.2',
  150. },
  151. },
  152. false: {
  153. [`& ${HoverIndicator}`]: {
  154. opacity: '0',
  155. },
  156. },
  157. },
  158. },
  159. compoundVariants: [
  160. {
  161. device: 'desktop',
  162. isSelected: 'false',
  163. css: {
  164. [`&:hover ${HoverIndicator}`]: {
  165. opacity: '0.16',
  166. },
  167. [`&:hover *[data-shy="true"]`]: {
  168. opacity: '1',
  169. },
  170. },
  171. },
  172. {
  173. device: 'desktop',
  174. isSelected: 'true',
  175. css: {
  176. [`&:hover ${HoverIndicator}`]: {
  177. opacity: '0.25',
  178. },
  179. [`&:active ${HoverIndicator}`]: {
  180. opacity: '0.25',
  181. },
  182. },
  183. },
  184. ],
  185. })
  186. // function Label({ children }: { children: React.ReactNode }) {
  187. // return (
  188. // <text
  189. // y={4}
  190. // x={4}
  191. // fontSize={12}
  192. // fill="black"
  193. // stroke="none"
  194. // alignmentBaseline="text-before-edge"
  195. // pointerEvents="none"
  196. // >
  197. // {children}
  198. // </text>
  199. // )
  200. // }
  201. // function pp(n: number[]) {
  202. // return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
  203. // }
  204. export { HoverIndicator }
  205. export default memo(Shape)