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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import React, { useRef, memo, useEffect } from 'react'
  2. import { useSelector } from 'state'
  3. import styled from 'styles'
  4. import { getShapeUtils } from 'state/shape-utils'
  5. import { deepCompareArrays, getPage, getShape } from 'utils'
  6. import useShapeEvents from 'hooks/useShapeEvents'
  7. import vec from 'utils/vec'
  8. import { getShapeStyle } from 'state/shape-styles'
  9. import useShapeDef from 'hooks/useShape'
  10. interface ShapeProps {
  11. id: string
  12. isSelecting: boolean
  13. }
  14. function Shape({ id, isSelecting }: ShapeProps): JSX.Element {
  15. const rGroup = useRef<SVGGElement>(null)
  16. const shapeUtils = useSelector((s) => {
  17. const shape = getShape(s.data, id)
  18. return getShapeUtils(shape)
  19. })
  20. const isHidden = useSelector((s) => {
  21. const shape = getShape(s.data, id)
  22. return shape.isHidden
  23. })
  24. const children = useSelector((s) => {
  25. const shape = getShape(s.data, id)
  26. return shape.children
  27. }, deepCompareArrays)
  28. const isParent = shapeUtils.isParent
  29. const isForeignObject = shapeUtils.isForeignObject
  30. const strokeWidth = useSelector((s) => {
  31. const shape = getShape(s.data, id)
  32. const style = getShapeStyle(shape.style)
  33. return +style.strokeWidth
  34. })
  35. const transform = useSelector((s) => {
  36. const shape = getShape(s.data, id)
  37. const center = shapeUtils.getCenter(shape)
  38. const rotation = shape.rotation * (180 / Math.PI)
  39. const parentPoint = getShape(s.data, shape.parentId)?.point || [0, 0]
  40. return `
  41. translate(${vec.neg(parentPoint)})
  42. rotate(${rotation}, ${center})
  43. translate(${shape.point})
  44. `
  45. })
  46. const events = useShapeEvents(id, isParent, rGroup)
  47. return (
  48. <StyledGroup
  49. id={id + '-group'}
  50. ref={rGroup}
  51. transform={transform}
  52. {...events}
  53. >
  54. {isSelecting &&
  55. (isForeignObject ? (
  56. <ForeignObjectHover id={id} />
  57. ) : (
  58. <EventSoak
  59. as="use"
  60. href={'#' + id}
  61. strokeWidth={strokeWidth + 8}
  62. variant={shapeUtils.canStyleFill ? 'filled' : 'hollow'}
  63. />
  64. ))}
  65. {!isHidden &&
  66. (isForeignObject ? (
  67. <ForeignObjectRender id={id} />
  68. ) : (
  69. <RealShape id={id} isParent={isParent} strokeWidth={strokeWidth} />
  70. ))}
  71. {isParent &&
  72. children.map((shapeId) => (
  73. <Shape key={shapeId} id={shapeId} isSelecting={isSelecting} />
  74. ))}
  75. </StyledGroup>
  76. )
  77. }
  78. interface RealShapeProps {
  79. id: string
  80. isParent: boolean
  81. strokeWidth: number
  82. }
  83. const RealShape = memo(function RealShape({
  84. id,
  85. isParent,
  86. strokeWidth,
  87. }: RealShapeProps) {
  88. return (
  89. <StyledShape
  90. as="use"
  91. data-shy={isParent}
  92. href={'#' + id}
  93. strokeWidth={strokeWidth}
  94. />
  95. )
  96. })
  97. const ForeignObjectHover = memo(function ForeignObjectHover({
  98. id,
  99. }: {
  100. id: string
  101. }) {
  102. const size = useSelector((s) => {
  103. const shape = getPage(s.data).shapes[id]
  104. const bounds = getShapeUtils(shape).getBounds(shape)
  105. return [bounds.width, bounds.height]
  106. }, deepCompareArrays)
  107. return (
  108. <EventSoak
  109. as="rect"
  110. width={size[0]}
  111. height={size[1]}
  112. strokeWidth={1.5}
  113. variant={'ghost'}
  114. />
  115. )
  116. })
  117. const ForeignObjectRender = memo(function ForeignObjectRender({
  118. id,
  119. }: {
  120. id: string
  121. }) {
  122. const shape = useShapeDef(id)
  123. const rFocusable = useRef<HTMLTextAreaElement>(null)
  124. const isEditing = useSelector((s) => s.data.editingId === id)
  125. const shapeUtils = getShapeUtils(shape)
  126. useEffect(() => {
  127. if (isEditing) {
  128. setTimeout(() => {
  129. const elm = rFocusable.current
  130. if (!elm) return
  131. elm.focus()
  132. }, 0)
  133. }
  134. }, [isEditing])
  135. return shapeUtils.render(shape, { isEditing, ref: rFocusable })
  136. })
  137. const StyledShape = styled('path', {
  138. strokeLinecap: 'round',
  139. strokeLinejoin: 'round',
  140. pointerEvents: 'none',
  141. })
  142. const EventSoak = styled('use', {
  143. opacity: 0,
  144. strokeLinecap: 'round',
  145. strokeLinejoin: 'round',
  146. variants: {
  147. variant: {
  148. ghost: {
  149. pointerEvents: 'all',
  150. filter: 'none',
  151. opacity: 0,
  152. },
  153. hollow: {
  154. pointerEvents: 'stroke',
  155. },
  156. filled: {
  157. pointerEvents: 'all',
  158. },
  159. },
  160. },
  161. })
  162. const StyledGroup = styled('g', {
  163. outline: 'none',
  164. })
  165. export default memo(Shape)