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

shape.tsx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import React, { useRef, memo, useEffect } from 'react'
  2. import state, { useSelector } from 'state'
  3. import styled from 'styles'
  4. import { getShapeUtils } from 'lib/shape-utils'
  5. import { getBoundsCenter, getPage, isMobile } from 'utils/utils'
  6. import { ShapeStyles, ShapeType, Shape as _Shape } from 'types'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. import vec from 'utils/vec'
  9. import { getShapeStyle } from 'lib/shape-styles'
  10. import ContextMenu from 'components/canvas/context-menu/context-menu'
  11. const isMobileDevice = isMobile()
  12. interface ShapeProps {
  13. id: string
  14. isSelecting: boolean
  15. parentPoint: number[]
  16. }
  17. function Shape({ id, isSelecting, parentPoint }: ShapeProps) {
  18. const rGroup = useRef<SVGGElement>(null)
  19. const rFocusable = useRef<HTMLTextAreaElement>(null)
  20. const isEditing = useSelector((s) => s.data.editingId === 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. device={isMobileDevice ? 'mobile' : 'desktop'}
  54. >
  55. {isSelecting && !isShy && (
  56. <>
  57. {isForeignObject ? (
  58. <HoverIndicator
  59. as="rect"
  60. width={bounds.width}
  61. height={bounds.height}
  62. strokeWidth={1.5}
  63. variant={'ghost'}
  64. {...events}
  65. />
  66. ) : (
  67. <HoverIndicator
  68. as="use"
  69. href={'#' + id}
  70. strokeWidth={+style.strokeWidth + 4}
  71. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  72. {...events}
  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({
  109. id,
  110. shape,
  111. style,
  112. isParent,
  113. }: RealShapeProps) {
  114. return <StyledShape as="use" data-shy={isParent} href={'#' + id} />
  115. })
  116. const StyledShape = styled('path', {
  117. strokeLinecap: 'round',
  118. strokeLinejoin: 'round',
  119. pointerEvents: 'none',
  120. })
  121. const HoverIndicator = styled('path', {
  122. stroke: '$selected',
  123. strokeLinecap: 'round',
  124. strokeLinejoin: 'round',
  125. fill: 'transparent',
  126. filter: 'url(#expand)',
  127. variants: {
  128. variant: {
  129. ghost: {
  130. pointerEvents: 'all',
  131. filter: 'none',
  132. opacity: 0,
  133. },
  134. hollow: {
  135. pointerEvents: 'stroke',
  136. },
  137. filled: {
  138. pointerEvents: 'all',
  139. },
  140. },
  141. },
  142. })
  143. const StyledGroup = styled('g', {
  144. outline: 'none',
  145. [`& *[data-shy="true"]`]: {
  146. opacity: '0',
  147. },
  148. [`& ${HoverIndicator}`]: {
  149. opacity: '0',
  150. },
  151. variants: {
  152. device: {
  153. mobile: {},
  154. desktop: {},
  155. },
  156. isSelected: {
  157. true: {
  158. [`& *[data-shy="true"]`]: {
  159. opacity: '1',
  160. },
  161. [`& ${HoverIndicator}`]: {
  162. opacity: '0.2',
  163. },
  164. },
  165. false: {
  166. [`& ${HoverIndicator}`]: {
  167. opacity: '0',
  168. },
  169. },
  170. },
  171. },
  172. compoundVariants: [
  173. {
  174. device: 'desktop',
  175. isSelected: 'false',
  176. css: {
  177. [`&:hover ${HoverIndicator}`]: {
  178. opacity: '0.16',
  179. },
  180. [`&:hover *[data-shy="true"]`]: {
  181. opacity: '1',
  182. },
  183. },
  184. },
  185. {
  186. device: 'desktop',
  187. isSelected: 'true',
  188. css: {
  189. [`&:hover ${HoverIndicator}`]: {
  190. opacity: '0.3',
  191. },
  192. [`&:active ${HoverIndicator}`]: {
  193. opacity: '0.3',
  194. },
  195. },
  196. },
  197. ],
  198. })
  199. function Label({ children }: { children: React.ReactNode }) {
  200. return (
  201. <text
  202. y={4}
  203. x={4}
  204. fontSize={12}
  205. fill="black"
  206. stroke="none"
  207. alignmentBaseline="text-before-edge"
  208. pointerEvents="none"
  209. >
  210. {children}
  211. </text>
  212. )
  213. }
  214. function pp(n: number[]) {
  215. return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
  216. }
  217. export { HoverIndicator }
  218. export default memo(Shape)