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

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