Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

shape.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import React, { useRef, memo } 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 * as vec from 'utils/vec'
  9. import { getShapeStyle } from 'lib/shape-styles'
  10. import ContextMenu from 'components/context-menu'
  11. interface ShapeProps {
  12. id: string
  13. isSelecting: boolean
  14. parentPoint: number[]
  15. }
  16. function Shape({ id, isSelecting, parentPoint }: ShapeProps) {
  17. const shape = useSelector((s) => getPage(s.data).shapes[id])
  18. const isEditing = useSelector((s) => s.data.editingId === id)
  19. const rGroup = useRef<SVGGElement>(null)
  20. const events = useShapeEvents(id, getShapeUtils(shape)?.isParent, rGroup)
  21. // This is a problem with deleted shapes. The hooks in this component
  22. // may sometimes run before the hook in the Page component, which means
  23. // a deleted shape will still be pulled here before the page component
  24. // detects the change and pulls this component.
  25. if (!shape) return null
  26. const utils = getShapeUtils(shape)
  27. const style = getShapeStyle(shape.style)
  28. const shapeUtils = getShapeUtils(shape)
  29. const { isShy, isParent, isForeignObject } = shapeUtils
  30. const bounds = shapeUtils.getBounds(shape)
  31. const center = shapeUtils.getCenter(shape)
  32. const rotation = shape.rotation * (180 / Math.PI)
  33. const transform = `
  34. translate(${vec.neg(parentPoint)})
  35. rotate(${rotation}, ${center})
  36. translate(${shape.point})
  37. `
  38. return (
  39. <StyledGroup
  40. ref={rGroup}
  41. transform={transform}
  42. onBlur={() => state.send('BLURRED_SHAPE', { target: id })}
  43. >
  44. {isSelecting &&
  45. !isShy &&
  46. (isForeignObject ? (
  47. <HoverIndicator
  48. as="rect"
  49. width={bounds.width}
  50. height={bounds.height}
  51. strokeWidth={1.5}
  52. variant={'ghost'}
  53. {...events}
  54. />
  55. ) : (
  56. <HoverIndicator
  57. as="use"
  58. href={'#' + id}
  59. strokeWidth={+style.strokeWidth + 4}
  60. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  61. {...events}
  62. />
  63. ))}
  64. {!shape.isHidden &&
  65. (isForeignObject ? (
  66. shapeUtils.render(shape, { isEditing })
  67. ) : (
  68. <RealShape
  69. isParent={isParent}
  70. id={id}
  71. style={style}
  72. isEditing={isEditing}
  73. />
  74. ))}
  75. {isParent &&
  76. shape.children.map((shapeId) => (
  77. <Shape
  78. key={shapeId}
  79. id={shapeId}
  80. isSelecting={isSelecting}
  81. parentPoint={shape.point}
  82. />
  83. ))}
  84. </StyledGroup>
  85. )
  86. }
  87. interface RealShapeProps {
  88. id: string
  89. style: Partial<React.SVGProps<SVGUseElement>>
  90. isParent: boolean
  91. isEditing: boolean
  92. }
  93. const RealShape = memo(function RealShape({
  94. id,
  95. style,
  96. isParent,
  97. isEditing,
  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)