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

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