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

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