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

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