Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

shape.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { ShapeStyles } from 'types'
  7. import useShapeEvents from 'hooks/useShapeEvents'
  8. function Shape({ id, isSelecting }: { id: string; isSelecting: boolean }) {
  9. const isHovered = useSelector((state) => state.data.hoveredId === id)
  10. const isSelected = useSelector((state) => state.values.selectedIds.has(id))
  11. const shape = useSelector(({ data }) => getPage(data).shapes[id])
  12. const rGroup = useRef<SVGGElement>(null)
  13. const events = useShapeEvents(id, rGroup)
  14. // This is a problem with deleted shapes. The hooks in this component
  15. // may sometimes run before the hook in the Page component, which means
  16. // a deleted shape will still be pulled here before the page component
  17. // detects the change and pulls this component.
  18. if (!shape) return null
  19. const center = getShapeUtils(shape).getCenter(shape)
  20. const transform = `
  21. rotate(${shape.rotation * (180 / Math.PI)}, ${center})
  22. translate(${shape.point})`
  23. return (
  24. <StyledGroup
  25. ref={rGroup}
  26. isHovered={isHovered}
  27. isSelected={isSelected}
  28. transform={transform}
  29. {...events}
  30. >
  31. {isSelecting && (
  32. <HoverIndicator
  33. as="use"
  34. href={'#' + id}
  35. strokeWidth={+shape.style.strokeWidth + 8}
  36. />
  37. )}
  38. {!shape.isHidden && <StyledShape id={id} style={shape.style} />}
  39. </StyledGroup>
  40. )
  41. }
  42. const StyledShape = memo(
  43. ({ id, style }: { id: string; style: ShapeStyles }) => {
  44. return <use href={'#' + id} {...style} />
  45. }
  46. )
  47. function Label({ text }: { text: string }) {
  48. return (
  49. <text
  50. y={4}
  51. x={4}
  52. fontSize={18}
  53. fill="black"
  54. stroke="none"
  55. alignmentBaseline="text-before-edge"
  56. pointerEvents="none"
  57. >
  58. {text}
  59. </text>
  60. )
  61. }
  62. const HoverIndicator = styled('path', {
  63. fill: 'none',
  64. stroke: 'transparent',
  65. pointerEvents: 'all',
  66. strokeLinecap: 'round',
  67. strokeLinejoin: 'round',
  68. transform: 'all .2s',
  69. })
  70. const StyledGroup = styled('g', {
  71. [`& ${HoverIndicator}`]: {
  72. opacity: '0',
  73. },
  74. variants: {
  75. isSelected: {
  76. true: {},
  77. false: {},
  78. },
  79. isHovered: {
  80. true: {},
  81. false: {},
  82. },
  83. },
  84. compoundVariants: [
  85. {
  86. isSelected: true,
  87. isHovered: true,
  88. css: {
  89. [`& ${HoverIndicator}`]: {
  90. opacity: '1',
  91. stroke: '$hint',
  92. fill: '$hint',
  93. // zStrokeWidth: [8, 4],
  94. },
  95. },
  96. },
  97. {
  98. isSelected: true,
  99. isHovered: false,
  100. css: {
  101. [`& ${HoverIndicator}`]: {
  102. opacity: '1',
  103. stroke: '$hint',
  104. fill: '$hint',
  105. // zStrokeWidth: [6, 3],
  106. },
  107. },
  108. },
  109. {
  110. isSelected: false,
  111. isHovered: true,
  112. css: {
  113. [`& ${HoverIndicator}`]: {
  114. opacity: '1',
  115. stroke: '$hint',
  116. fill: '$hint',
  117. // zStrokeWidth: [8, 4],
  118. },
  119. },
  120. },
  121. ],
  122. })
  123. export { HoverIndicator }
  124. export default memo(Shape)