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

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