您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

shape.tsx 5.4KB

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