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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. onDoubleClick={() => console.log('aux')}
  59. {...events}
  60. />
  61. ) : (
  62. <HoverIndicator
  63. as="use"
  64. href={'#' + id}
  65. strokeWidth={+style.strokeWidth + 4}
  66. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  67. {...events}
  68. />
  69. ))}
  70. {!shape.isHidden &&
  71. (isForeignObject ? (
  72. shapeUtils.render(shape, { isEditing, ref: rFocusable })
  73. ) : (
  74. <RealShape
  75. isParent={isParent}
  76. id={id}
  77. style={style}
  78. isEditing={isEditing}
  79. />
  80. ))}
  81. {isParent &&
  82. shape.children.map((shapeId) => (
  83. <Shape
  84. key={shapeId}
  85. id={shapeId}
  86. isSelecting={isSelecting}
  87. parentPoint={shape.point}
  88. />
  89. ))}
  90. </StyledGroup>
  91. )
  92. }
  93. interface RealShapeProps {
  94. id: string
  95. style: Partial<React.SVGProps<SVGUseElement>>
  96. isParent: boolean
  97. isEditing: boolean
  98. }
  99. const RealShape = memo(function RealShape({
  100. id,
  101. style,
  102. isParent,
  103. }: RealShapeProps) {
  104. return <StyledShape as="use" data-shy={isParent} href={'#' + id} {...style} />
  105. })
  106. const StyledShape = styled('path', {
  107. strokeLinecap: 'round',
  108. strokeLinejoin: 'round',
  109. pointerEvents: 'none',
  110. })
  111. const HoverIndicator = styled('path', {
  112. stroke: '$selected',
  113. strokeLinecap: 'round',
  114. strokeLinejoin: 'round',
  115. fill: 'transparent',
  116. filter: 'url(#expand)',
  117. variants: {
  118. variant: {
  119. ghost: {
  120. pointerEvents: 'all',
  121. filter: 'none',
  122. opacity: 0,
  123. },
  124. hollow: {
  125. pointerEvents: 'stroke',
  126. },
  127. filled: {
  128. pointerEvents: 'all',
  129. },
  130. },
  131. },
  132. })
  133. const StyledGroup = styled('g', {
  134. outline: 'none',
  135. [`& *[data-shy="true"]`]: {
  136. opacity: '0',
  137. },
  138. [`& ${HoverIndicator}`]: {
  139. opacity: '0',
  140. },
  141. [`&:hover ${HoverIndicator}`]: {
  142. opacity: '0.16',
  143. },
  144. [`&:hover *[data-shy="true"]`]: {
  145. opacity: '1',
  146. },
  147. variants: {
  148. isSelected: {
  149. true: {
  150. [`& *[data-shy="true"]`]: {
  151. opacity: '1',
  152. },
  153. [`& ${HoverIndicator}`]: {
  154. opacity: '0.2',
  155. },
  156. [`&:hover ${HoverIndicator}`]: {
  157. opacity: '0.3',
  158. },
  159. [`&:active ${HoverIndicator}`]: {
  160. opacity: '0.3',
  161. },
  162. },
  163. false: {
  164. [`& ${HoverIndicator}`]: {
  165. opacity: '0',
  166. },
  167. },
  168. },
  169. },
  170. })
  171. function Label({ children }: { children: React.ReactNode }) {
  172. return (
  173. <text
  174. y={4}
  175. x={4}
  176. fontSize={12}
  177. fill="black"
  178. stroke="none"
  179. alignmentBaseline="text-before-edge"
  180. pointerEvents="none"
  181. >
  182. {children}
  183. </text>
  184. )
  185. }
  186. function pp(n: number[]) {
  187. return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
  188. }
  189. export { HoverIndicator }
  190. export default memo(Shape)