您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

shape.tsx 2.9KB

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