Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

group.tsx 4.7KB

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