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.6KB

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