Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 { getPage } from 'utils/utils'
  6. import { ShapeType } from 'types'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. import * as vec from 'utils/vec'
  9. import { getShapeStyle } from 'lib/shape-styles'
  10. interface ShapeProps {
  11. id: string
  12. isSelecting: boolean
  13. parentPoint: number[]
  14. parentRotation: number
  15. }
  16. function Shape({ id, isSelecting, parentPoint, parentRotation }: ShapeProps) {
  17. const shape = useSelector(({ data }) => getPage(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 transform = `
  28. rotate(${shape.rotation * (180 / Math.PI)}, ${vec.sub(center, parentPoint)})
  29. translate(${vec.sub(shape.point, parentPoint)})
  30. `
  31. const style = getShapeStyle(shape.style)
  32. return (
  33. <StyledGroup ref={rGroup} transform={transform}>
  34. {isSelecting && !isGroup && (
  35. <HoverIndicator
  36. as="use"
  37. href={'#' + id}
  38. strokeWidth={+style.strokeWidth + 4}
  39. variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
  40. {...events}
  41. />
  42. )}
  43. {!shape.isHidden && (
  44. <StyledShape as="use" data-shy={isGroup} href={'#' + id} {...style} />
  45. )}
  46. {isGroup &&
  47. shape.children.map((shapeId) => (
  48. <Shape
  49. key={shapeId}
  50. id={shapeId}
  51. isSelecting={isSelecting}
  52. parentPoint={shape.point}
  53. parentRotation={shape.rotation}
  54. />
  55. ))}
  56. </StyledGroup>
  57. )
  58. }
  59. const StyledShape = styled('path', {
  60. strokeLinecap: 'round',
  61. strokeLinejoin: 'round',
  62. pointerEvents: 'none',
  63. })
  64. const HoverIndicator = styled('path', {
  65. stroke: '$selected',
  66. strokeLinecap: 'round',
  67. strokeLinejoin: 'round',
  68. transform: 'all .2s',
  69. fill: 'transparent',
  70. filter: 'url(#expand)',
  71. variants: {
  72. variant: {
  73. hollow: {
  74. pointerEvents: 'stroke',
  75. },
  76. filled: {
  77. pointerEvents: 'all',
  78. },
  79. },
  80. },
  81. })
  82. const StyledGroup = styled('g', {
  83. outline: 'none',
  84. [`& *[data-shy="true"]`]: {
  85. opacity: '0',
  86. },
  87. [`& ${HoverIndicator}`]: {
  88. opacity: '0',
  89. },
  90. [`&:hover ${HoverIndicator}`]: {
  91. opacity: '0.16',
  92. },
  93. [`&:hover *[data-shy="true"]`]: {
  94. opacity: '1',
  95. },
  96. variants: {
  97. isSelected: {
  98. true: {
  99. [`& *[data-shy="true"]`]: {
  100. opacity: '1',
  101. },
  102. [`& ${HoverIndicator}`]: {
  103. opacity: '0.2',
  104. },
  105. [`&:hover ${HoverIndicator}`]: {
  106. opacity: '0.3',
  107. },
  108. [`&:active ${HoverIndicator}`]: {
  109. opacity: '0.3',
  110. },
  111. },
  112. false: {
  113. [`& ${HoverIndicator}`]: {
  114. opacity: '0',
  115. },
  116. },
  117. },
  118. },
  119. })
  120. function Label({ children }: { children: React.ReactNode }) {
  121. return (
  122. <text
  123. y={4}
  124. x={4}
  125. fontSize={12}
  126. fill="black"
  127. stroke="none"
  128. alignmentBaseline="text-before-edge"
  129. pointerEvents="none"
  130. >
  131. {children}
  132. </text>
  133. )
  134. }
  135. export { HoverIndicator }
  136. export default memo(Shape)
  137. function pp(n: number[]) {
  138. return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
  139. }