Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

group.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { uniqueId } from '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. create(props) {
  14. return {
  15. id: uniqueId(),
  16. type: ShapeType.Group,
  17. isGenerated: false,
  18. name: 'Group',
  19. parentId: 'page1',
  20. childIndex: 0,
  21. point: [0, 0],
  22. size: [1, 1],
  23. radius: 2,
  24. rotation: 0,
  25. isAspectRatioLocked: false,
  26. isLocked: false,
  27. isHidden: false,
  28. style: defaultStyle,
  29. children: [],
  30. ...props,
  31. }
  32. },
  33. render(shape) {
  34. const { id, size } = shape
  35. return (
  36. <StyledGroupShape
  37. id={id}
  38. width={size[0]}
  39. height={size[1]}
  40. data-shy={true}
  41. />
  42. )
  43. },
  44. getBounds(shape) {
  45. if (!this.boundsCache.has(shape)) {
  46. const [width, height] = shape.size
  47. const bounds = {
  48. minX: 0,
  49. maxX: width,
  50. minY: 0,
  51. maxY: height,
  52. width,
  53. height,
  54. }
  55. this.boundsCache.set(shape, bounds)
  56. }
  57. return translateBounds(this.boundsCache.get(shape), shape.point)
  58. },
  59. hitTest() {
  60. return false
  61. },
  62. hitTestBounds() {
  63. return false
  64. },
  65. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  66. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  67. shape.size = [bounds.width, bounds.height]
  68. shape.point = [bounds.minX, bounds.minY]
  69. } else {
  70. shape.size = vec.mul(
  71. initialShape.size,
  72. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  73. )
  74. shape.point = [
  75. bounds.minX +
  76. (bounds.width - shape.size[0]) *
  77. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  78. bounds.minY +
  79. (bounds.height - shape.size[1]) *
  80. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  81. ]
  82. shape.rotation =
  83. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  84. ? -initialShape.rotation
  85. : initialShape.rotation
  86. }
  87. return this
  88. },
  89. transformSingle(shape, bounds) {
  90. shape.size = [bounds.width, bounds.height]
  91. shape.point = [bounds.minX, bounds.minY]
  92. return this
  93. },
  94. onChildrenChange(shape, children) {
  95. if (shape.children.length === 0) return
  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