Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

shape.tsx 3.9KB

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