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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 } 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. interface ShapeProps {
  12. id: string
  13. isSelecting: boolean
  14. parentPoint: number[]
  15. }
  16. function Shape({ id, isSelecting, parentPoint }: ShapeProps) {
  17. const rGroup = useRef<SVGGElement>(null)
  18. const rFocusable = useRef<HTMLTextAreaElement>(null)
  19. const isEditing = useSelector((s) => s.data.editingId === id)
  20. const shape = useSelector((s) => getPage(s.data).shapes[id])
  21. const events = useShapeEvents(id, getShapeUtils(shape)?.isParent, rGroup)
  22. useEffect(() => {
  23. if (isEditing) {
  24. setTimeout(() => {
  25. const elm = rFocusable.current
  26. if (!elm) return
  27. elm.focus()
  28. }, 0)
  29. }
  30. }, [isEditing])
  31. // This is a problem with deleted shapes. The hooks in this component
  32. // may sometimes run before the hook in the Page component, which means
  33. // a deleted shape will still be pulled here before the page component
  34. // detects the change and pulls this component.
  35. if (!shape) return null
  36. const style = getShapeStyle(shape.style)
  37. const shapeUtils = getShapeUtils(shape)
  38. const { isShy, isParent, isForeignObject } = shapeUtils
  39. const bounds = shapeUtils.getBounds(shape)
  40. const center = shapeUtils.getCenter(shape)
  41. const rotation = shape.rotation * (180 / Math.PI)
  42. const transform = `
  43. translate(${vec.neg(parentPoint)})
  44. rotate(${rotation}, ${center})
  45. translate(${shape.point})
  46. `
  47. return (
  48. <StyledGroup ref={rGroup} transform={transform}>
  49. {isSelecting &&
  50. !isShy &&
  51. (isForeignObject ? (
  52. <HoverIndicator
  53. as="rect"
  54. width={bounds.width}
  55. height={bounds.height}
  56. strokeWidth={1.5}
  57. variant={'ghost'}
  58. {...events}
  59. />
  60. ) : (
  61. <HoverIndicator
  62. as="use"
  63. href={'#' + id}
  64. strokeWidth={+style.strokeWidth + 4}
  65. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  66. {...events}
  67. />
  68. ))}
  69. {!shape.isHidden &&
  70. (isForeignObject ? (
  71. shapeUtils.render(shape, { isEditing, ref: rFocusable })
  72. ) : (
  73. <RealShape
  74. isParent={isParent}
  75. id={id}
  76. style={style}
  77. isEditing={isEditing}
  78. />
  79. ))}
  80. {isParent &&
  81. shape.children.map((shapeId) => (
  82. <Shape
  83. key={shapeId}
  84. id={shapeId}
  85. isSelecting={isSelecting}
  86. parentPoint={shape.point}
  87. />
  88. ))}
  89. </StyledGroup>
  90. )
  91. }
  92. interface RealShapeProps {
  93. id: string
  94. style: Partial<React.SVGProps<SVGUseElement>>
  95. isParent: boolean
  96. isEditing: boolean
  97. }
  98. const RealShape = memo(function RealShape({
  99. id,
  100. style,
  101. isParent,
  102. }: RealShapeProps) {
  103. return <StyledShape as="use" data-shy={isParent} href={'#' + id} {...style} />
  104. })
  105. const StyledShape = styled('path', {
  106. strokeLinecap: 'round',
  107. strokeLinejoin: 'round',
  108. pointerEvents: 'none',
  109. })
  110. const HoverIndicator = styled('path', {
  111. stroke: '$selected',
  112. strokeLinecap: 'round',
  113. strokeLinejoin: 'round',
  114. fill: 'transparent',
  115. filter: 'url(#expand)',
  116. variants: {
  117. variant: {
  118. ghost: {
  119. pointerEvents: 'all',
  120. filter: 'none',
  121. opacity: 0,
  122. },
  123. hollow: {
  124. pointerEvents: 'stroke',
  125. },
  126. filled: {
  127. pointerEvents: 'all',
  128. },
  129. },
  130. },
  131. })
  132. const StyledGroup = styled('g', {
  133. outline: 'none',
  134. [`& *[data-shy="true"]`]: {
  135. opacity: '0',
  136. },
  137. [`& ${HoverIndicator}`]: {
  138. opacity: '0',
  139. },
  140. [`&:hover ${HoverIndicator}`]: {
  141. opacity: '0.16',
  142. },
  143. [`&:hover *[data-shy="true"]`]: {
  144. opacity: '1',
  145. },
  146. variants: {
  147. isSelected: {
  148. true: {
  149. [`& *[data-shy="true"]`]: {
  150. opacity: '1',
  151. },
  152. [`& ${HoverIndicator}`]: {
  153. opacity: '0.2',
  154. },
  155. [`&:hover ${HoverIndicator}`]: {
  156. opacity: '0.3',
  157. },
  158. [`&:active ${HoverIndicator}`]: {
  159. opacity: '0.3',
  160. },
  161. },
  162. false: {
  163. [`& ${HoverIndicator}`]: {
  164. opacity: '0',
  165. },
  166. },
  167. },
  168. },
  169. })
  170. function Label({ children }: { children: React.ReactNode }) {
  171. return (
  172. <text
  173. y={4}
  174. x={4}
  175. fontSize={12}
  176. fill="black"
  177. stroke="none"
  178. alignmentBaseline="text-before-edge"
  179. pointerEvents="none"
  180. >
  181. {children}
  182. </text>
  183. )
  184. }
  185. function pp(n: number[]) {
  186. return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
  187. }
  188. export { HoverIndicator }
  189. export default memo(Shape)