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

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