You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

group.tsx 4.8KB

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