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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import React, { useRef, memo, useEffect } from 'react'
  2. import state, { useSelector } from 'state'
  3. import styled from 'styles'
  4. import { getShapeUtils } from 'state/shape-utils'
  5. import { deepCompareArrays } from 'utils'
  6. import tld from 'utils/tld'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. import vec from 'utils/vec'
  9. import { getShapeStyle } from 'state/shape-styles'
  10. import useShapeDef from 'hooks/useShape'
  11. interface ShapeProps {
  12. id: string
  13. isSelecting: boolean
  14. }
  15. function Shape({ id, isSelecting }: ShapeProps): JSX.Element {
  16. const rGroup = useRef<SVGGElement>(null)
  17. const isHidden = useSelector((s) => {
  18. const shape = tld.getShape(s.data, id)
  19. if (shape === undefined) return true
  20. return shape?.isHidden
  21. })
  22. const children = useSelector((s) => {
  23. const shape = tld.getShape(s.data, id)
  24. if (shape === undefined) return []
  25. return shape?.children
  26. }, deepCompareArrays)
  27. const strokeWidth = useSelector((s) => {
  28. const shape = tld.getShape(s.data, id)
  29. if (shape === undefined) return 0
  30. const style = getShapeStyle(shape?.style)
  31. return +style.strokeWidth
  32. })
  33. const transform = useSelector((s) => {
  34. const shape = tld.getShape(s.data, id)
  35. if (shape === undefined) return ''
  36. const center = getShapeUtils(shape).getCenter(shape)
  37. const rotation = shape.rotation * (180 / Math.PI)
  38. const parentPoint = tld.getShape(s.data, shape.parentId)?.point || [0, 0]
  39. return `
  40. translate(${vec.neg(parentPoint)})
  41. rotate(${rotation}, ${center})
  42. translate(${shape.point})
  43. `
  44. })
  45. const isCurrentParent = useSelector((s) => {
  46. return s.data.currentParentId === id
  47. })
  48. const events = useShapeEvents(id, isCurrentParent, rGroup)
  49. const shape = tld.getShape(state.data, id)
  50. if (!shape) return null
  51. // From here on, not reactive—if we're here, we can trust that the
  52. // shape in state is a shape with changes that we need to render.
  53. const { isParent, isForeignObject, canStyleFill } = getShapeUtils(shape)
  54. return (
  55. <StyledGroup
  56. id={id + '-group'}
  57. ref={rGroup}
  58. transform={transform}
  59. isCurrentParent={isCurrentParent}
  60. {...events}
  61. >
  62. {isSelecting &&
  63. (isForeignObject ? (
  64. <ForeignObjectHover id={id} />
  65. ) : (
  66. <EventSoak
  67. as="use"
  68. href={'#' + id}
  69. strokeWidth={strokeWidth + 8}
  70. variant={canStyleFill ? 'filled' : 'hollow'}
  71. />
  72. ))}
  73. {!isHidden &&
  74. (isForeignObject ? (
  75. <ForeignObjectRender id={id} />
  76. ) : (
  77. <RealShape id={id} isParent={isParent} strokeWidth={strokeWidth} />
  78. ))}
  79. {isParent &&
  80. children.map((shapeId) => (
  81. <Shape key={shapeId} id={shapeId} isSelecting={isSelecting} />
  82. ))}
  83. </StyledGroup>
  84. )
  85. }
  86. export default memo(Shape)
  87. interface RealShapeProps {
  88. id: string
  89. isParent: boolean
  90. strokeWidth: number
  91. }
  92. const RealShape = memo(function RealShape({
  93. id,
  94. isParent,
  95. strokeWidth,
  96. }: RealShapeProps) {
  97. return (
  98. <StyledShape
  99. as="use"
  100. data-shy={isParent}
  101. href={'#' + id}
  102. strokeWidth={strokeWidth}
  103. />
  104. )
  105. })
  106. const ForeignObjectHover = memo(function ForeignObjectHover({
  107. id,
  108. }: {
  109. id: string
  110. }) {
  111. const size = useSelector((s) => {
  112. const shape = tld.getPage(s.data).shapes[id]
  113. if (shape === undefined) return [0, 0]
  114. const bounds = getShapeUtils(shape).getBounds(shape)
  115. return [bounds.width, bounds.height]
  116. }, deepCompareArrays)
  117. return (
  118. <EventSoak
  119. as="rect"
  120. width={size[0]}
  121. height={size[1]}
  122. strokeWidth={1.5}
  123. variant={'ghost'}
  124. />
  125. )
  126. })
  127. const ForeignObjectRender = memo(function ForeignObjectRender({
  128. id,
  129. }: {
  130. id: string
  131. }) {
  132. const shape = useShapeDef(id)
  133. const rFocusable = useRef<HTMLTextAreaElement>(null)
  134. const isEditing = useSelector((s) => s.data.editingId === id)
  135. useEffect(() => {
  136. if (isEditing) {
  137. setTimeout(() => {
  138. const elm = rFocusable.current
  139. if (!elm) return
  140. elm.focus()
  141. }, 0)
  142. }
  143. }, [isEditing])
  144. if (shape === undefined) return null
  145. return getShapeUtils(shape).render(shape, { isEditing, ref: rFocusable })
  146. })
  147. const StyledShape = styled('path', {
  148. strokeLinecap: 'round',
  149. strokeLinejoin: 'round',
  150. pointerEvents: 'none',
  151. })
  152. const EventSoak = styled('use', {
  153. opacity: 0,
  154. strokeLinecap: 'round',
  155. strokeLinejoin: 'round',
  156. variants: {
  157. variant: {
  158. ghost: {
  159. pointerEvents: 'all',
  160. filter: 'none',
  161. opacity: 0,
  162. },
  163. hollow: {
  164. pointerEvents: 'stroke',
  165. },
  166. filled: {
  167. pointerEvents: 'all',
  168. },
  169. },
  170. },
  171. })
  172. const StyledGroup = styled('g', {
  173. outline: 'none',
  174. '& > *[data-shy=true]': {
  175. opacity: 0,
  176. },
  177. '&:hover': {
  178. '& > *[data-shy=true]': {
  179. opacity: 1,
  180. },
  181. },
  182. variants: {
  183. isCurrentParent: {
  184. true: {
  185. '& > *[data-shy=true]': {
  186. opacity: 1,
  187. },
  188. },
  189. },
  190. },
  191. })