Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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