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

group.tsx 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import {
  4. GroupShape,
  5. RectangleShape,
  6. ShapeType,
  7. Bounds,
  8. Corner,
  9. Edge,
  10. } from 'types'
  11. import { getShapeUtils, registerShapeUtils } from './index'
  12. import {
  13. getBoundsCenter,
  14. getCommonBounds,
  15. getRotatedCorners,
  16. rotateBounds,
  17. translateBounds,
  18. } from 'utils/utils'
  19. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  20. import styled from 'styles'
  21. import { boundsContainPolygon } from 'utils/bounds'
  22. const group = registerShapeUtils<GroupShape>({
  23. boundsCache: new WeakMap([]),
  24. create(props) {
  25. return {
  26. id: uuid(),
  27. seed: Math.random(),
  28. type: ShapeType.Group,
  29. isGenerated: false,
  30. name: 'Group',
  31. parentId: 'page0',
  32. childIndex: 0,
  33. point: [0, 0],
  34. size: [1, 1],
  35. radius: 2,
  36. rotation: 0,
  37. isAspectRatioLocked: false,
  38. isLocked: false,
  39. isHidden: false,
  40. style: defaultStyle,
  41. children: [],
  42. ...props,
  43. }
  44. },
  45. render(shape) {
  46. const { id, size } = shape
  47. return (
  48. <StyledGroupShape
  49. id={id}
  50. width={size[0]}
  51. height={size[1]}
  52. data-shy={true}
  53. />
  54. )
  55. },
  56. translateTo(shape, point) {
  57. shape.point = point
  58. return this
  59. },
  60. getBounds(shape) {
  61. if (!this.boundsCache.has(shape)) {
  62. const [width, height] = shape.size
  63. const bounds = {
  64. minX: 0,
  65. maxX: width,
  66. minY: 0,
  67. maxY: height,
  68. width,
  69. height,
  70. }
  71. this.boundsCache.set(shape, bounds)
  72. }
  73. return translateBounds(this.boundsCache.get(shape), shape.point)
  74. },
  75. hitTest() {
  76. return false
  77. },
  78. hitTestBounds(shape, brushBounds) {
  79. return false
  80. },
  81. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  82. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  83. shape.size = [bounds.width, bounds.height]
  84. shape.point = [bounds.minX, bounds.minY]
  85. } else {
  86. shape.size = vec.mul(
  87. initialShape.size,
  88. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  89. )
  90. shape.point = [
  91. bounds.minX +
  92. (bounds.width - shape.size[0]) *
  93. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  94. bounds.minY +
  95. (bounds.height - shape.size[1]) *
  96. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  97. ]
  98. shape.rotation =
  99. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  100. ? -initialShape.rotation
  101. : initialShape.rotation
  102. }
  103. return this
  104. },
  105. transformSingle(shape, bounds) {
  106. shape.size = [bounds.width, bounds.height]
  107. shape.point = [bounds.minX, bounds.minY]
  108. return this
  109. },
  110. onChildrenChange(shape, children) {
  111. const childBounds = getCommonBounds(
  112. ...children.map((child) => getShapeUtils(child).getRotatedBounds(child))
  113. )
  114. // const c1 = this.getCenter(shape)
  115. // const c2 = getBoundsCenter(childBounds)
  116. // const [x0, y0] = vec.rotWith(shape.point, c1, shape.rotation)
  117. // const [w0, h0] = vec.rotWith(shape.size, c1, shape.rotation)
  118. // const [x1, y1] = vec.rotWith(
  119. // [childBounds.minX, childBounds.minY],
  120. // c2,
  121. // shape.rotation
  122. // )
  123. // const [w1, h1] = vec.rotWith(
  124. // [childBounds.width, childBounds.height],
  125. // c2,
  126. // shape.rotation
  127. // )
  128. // let delta: number[]
  129. // if (h0 === h1 && w0 !== w1) {
  130. // if (x0 < x1) {
  131. // // moving left edge, pin right edge
  132. // delta = vec.sub([x1 + w1, y1 + h1 / 2], [x0 + w0, y0 + h0 / 2])
  133. // } else {
  134. // // moving right edge, pin left edge
  135. // delta = vec.sub([x1, y1 + h1 / 2], [x0, y0 + h0 / 2])
  136. // }
  137. // } else if (h0 !== h1 && w0 === w1) {
  138. // if (y0 < y1) {
  139. // // moving top edge, pin bottom edge
  140. // delta = vec.sub([x1 + w1 / 2, y1 + h1], [x0 + w0 / 2, y0 + h0])
  141. // } else {
  142. // // moving bottom edge, pin top edge
  143. // delta = vec.sub([x1 + w1 / 2, y1], [x0 + w0 / 2, y0])
  144. // }
  145. // } else if (x0 !== x1) {
  146. // if (y0 !== y1) {
  147. // // moving top left, pin bottom right
  148. // delta = vec.sub([x1 + w1, y1 + h1], [x0 + w0, y0 + h0])
  149. // } else {
  150. // // moving bottom left, pin top right
  151. // delta = vec.sub([x1 + w1, y1], [x0 + w0, y0])
  152. // }
  153. // } else if (y0 !== y1) {
  154. // // moving top right, pin bottom left
  155. // delta = vec.sub([x1, y1 + h1], [x0, y0 + h0])
  156. // } else {
  157. // // moving bottom right, pin top left
  158. // delta = vec.sub([x1, y1], [x0, y0])
  159. // }
  160. // if (shape.rotation !== 0) {
  161. // shape.point = vec.sub(shape.point, delta)
  162. // }
  163. shape.point = [childBounds.minX, childBounds.minY] //vec.add([x1, y1], delta)
  164. shape.size = [childBounds.width, childBounds.height]
  165. return this
  166. },
  167. })
  168. const StyledGroupShape = styled('rect', {
  169. zDash: 5,
  170. zStrokeWidth: 1,
  171. })
  172. export default group