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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import React, { useRef, memo, useEffect, useState } 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. return shape?.isHidden || false
  20. })
  21. const children = useSelector((s) => {
  22. const shape = tld.getShape(s.data, id)
  23. return shape?.children || []
  24. }, deepCompareArrays)
  25. const strokeWidth = useSelector((s) => {
  26. const shape = tld.getShape(s.data, id)
  27. const style = getShapeStyle(shape?.style)
  28. return +style.strokeWidth
  29. })
  30. const shapeUtils = useSelector((s) => {
  31. const shape = tld.getShape(s.data, id)
  32. return getShapeUtils(shape)
  33. })
  34. const transform = useSelector((s) => {
  35. const shape = tld.getShape(s.data, id)
  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 events = useShapeEvents(id, shapeUtils?.isParent, rGroup)
  46. const hasShape = useMissingShapeTest(id)
  47. if (!hasShape) return null
  48. const isParent = shapeUtils.isParent
  49. const isForeignObject = shapeUtils.isForeignObject
  50. return (
  51. <StyledGroup
  52. id={id + '-group'}
  53. ref={rGroup}
  54. transform={transform}
  55. {...events}
  56. >
  57. {isSelecting &&
  58. (isForeignObject ? (
  59. <ForeignObjectHover id={id} />
  60. ) : (
  61. <EventSoak
  62. as="use"
  63. href={'#' + id}
  64. strokeWidth={strokeWidth + 8}
  65. variant={shapeUtils.canStyleFill ? 'filled' : 'hollow'}
  66. />
  67. ))}
  68. {!isHidden &&
  69. (isForeignObject ? (
  70. <ForeignObjectRender id={id} />
  71. ) : (
  72. <RealShape id={id} isParent={isParent} strokeWidth={strokeWidth} />
  73. ))}
  74. {isParent &&
  75. children.map((shapeId) => (
  76. <Shape key={shapeId} id={shapeId} isSelecting={isSelecting} />
  77. ))}
  78. </StyledGroup>
  79. )
  80. }
  81. interface RealShapeProps {
  82. id: string
  83. isParent: boolean
  84. strokeWidth: number
  85. }
  86. const RealShape = memo(function RealShape({
  87. id,
  88. isParent,
  89. strokeWidth,
  90. }: RealShapeProps) {
  91. return (
  92. <StyledShape
  93. as="use"
  94. data-shy={isParent}
  95. href={'#' + id}
  96. strokeWidth={strokeWidth}
  97. />
  98. )
  99. })
  100. const ForeignObjectHover = memo(function ForeignObjectHover({
  101. id,
  102. }: {
  103. id: string
  104. }) {
  105. const size = useSelector((s) => {
  106. const shape = tld.getPage(s.data).shapes[id]
  107. const bounds = getShapeUtils(shape).getBounds(shape)
  108. return [bounds.width, bounds.height]
  109. }, deepCompareArrays)
  110. return (
  111. <EventSoak
  112. as="rect"
  113. width={size[0]}
  114. height={size[1]}
  115. strokeWidth={1.5}
  116. variant={'ghost'}
  117. />
  118. )
  119. })
  120. const ForeignObjectRender = memo(function ForeignObjectRender({
  121. id,
  122. }: {
  123. id: string
  124. }) {
  125. const shape = useShapeDef(id)
  126. const rFocusable = useRef<HTMLTextAreaElement>(null)
  127. const isEditing = useSelector((s) => s.data.editingId === id)
  128. const shapeUtils = getShapeUtils(shape)
  129. useEffect(() => {
  130. if (isEditing) {
  131. setTimeout(() => {
  132. const elm = rFocusable.current
  133. if (!elm) return
  134. elm.focus()
  135. }, 0)
  136. }
  137. }, [isEditing])
  138. return shapeUtils.render(shape, { isEditing, ref: rFocusable })
  139. })
  140. const StyledShape = styled('path', {
  141. strokeLinecap: 'round',
  142. strokeLinejoin: 'round',
  143. pointerEvents: 'none',
  144. })
  145. const EventSoak = styled('use', {
  146. opacity: 0,
  147. strokeLinecap: 'round',
  148. strokeLinejoin: 'round',
  149. variants: {
  150. variant: {
  151. ghost: {
  152. pointerEvents: 'all',
  153. filter: 'none',
  154. opacity: 0,
  155. },
  156. hollow: {
  157. pointerEvents: 'stroke',
  158. },
  159. filled: {
  160. pointerEvents: 'all',
  161. },
  162. },
  163. },
  164. })
  165. const StyledGroup = styled('g', {
  166. outline: 'none',
  167. })
  168. export default memo(Shape)
  169. function useMissingShapeTest(id: string) {
  170. const [isShape, setIsShape] = useState(true)
  171. useEffect(() => {
  172. return state.onUpdate((s) => {
  173. if (isShape && !tld.getShape(s.data, id)) {
  174. setIsShape(false)
  175. }
  176. })
  177. }, [isShape, id])
  178. return isShape
  179. }