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.

transform-session.ts 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { Data, Edge, Corner } from 'types'
  2. import vec from 'utils/vec'
  3. import BaseSession from './base-session'
  4. import commands from 'state/commands'
  5. import { getShapeUtils } from 'state/shape-utils'
  6. import {
  7. deepClone,
  8. getBoundsCenter,
  9. getBoundsFromPoints,
  10. getCommonBounds,
  11. getDocumentBranch,
  12. getPage,
  13. getRelativeTransformedBoundingBox,
  14. getSelectedIds,
  15. getTransformedBoundingBox,
  16. setToArray,
  17. updateParents,
  18. } from 'utils'
  19. export default class TransformSession extends BaseSession {
  20. scaleX = 1
  21. scaleY = 1
  22. transformType: Edge | Corner
  23. origin: number[]
  24. snapshot: TransformSnapshot
  25. constructor(data: Data, transformType: Corner | Edge, point: number[]) {
  26. super(data)
  27. this.origin = point
  28. this.transformType = transformType
  29. this.snapshot = getTransformSnapshot(data, transformType)
  30. }
  31. update(data: Data, point: number[], isAspectRatioLocked = false): void {
  32. const { transformType } = this
  33. const { shapeBounds, initialBounds, isAllAspectRatioLocked } = this.snapshot
  34. const { shapes } = getPage(data)
  35. const newBoundingBox = getTransformedBoundingBox(
  36. initialBounds,
  37. transformType,
  38. vec.vec(this.origin, point),
  39. data.boundsRotation,
  40. isAspectRatioLocked || isAllAspectRatioLocked
  41. )
  42. this.scaleX = newBoundingBox.scaleX
  43. this.scaleY = newBoundingBox.scaleY
  44. // Now work backward to calculate a new bounding box for each of the shapes.
  45. for (const id in shapeBounds) {
  46. const { initialShape, initialShapeBounds, transformOrigin } =
  47. shapeBounds[id]
  48. const newShapeBounds = getRelativeTransformedBoundingBox(
  49. newBoundingBox,
  50. initialBounds,
  51. initialShapeBounds,
  52. this.scaleX < 0,
  53. this.scaleY < 0
  54. )
  55. const shape = shapes[id]
  56. getShapeUtils(shape).transform(shape, newShapeBounds, {
  57. type: this.transformType,
  58. initialShape,
  59. scaleX: this.scaleX,
  60. scaleY: this.scaleY,
  61. transformOrigin,
  62. })
  63. shapes[id] = { ...shape }
  64. }
  65. updateParents(data, Object.keys(shapeBounds))
  66. }
  67. cancel(data: Data): void {
  68. const { shapeBounds } = this.snapshot
  69. const { shapes } = getPage(data)
  70. for (const id in shapeBounds) {
  71. const shape = shapes[id]
  72. const { initialShape, initialShapeBounds, transformOrigin } =
  73. shapeBounds[id]
  74. getShapeUtils(shape).transform(shape, initialShapeBounds, {
  75. type: this.transformType,
  76. initialShape,
  77. scaleX: 1,
  78. scaleY: 1,
  79. transformOrigin,
  80. })
  81. updateParents(data, Object.keys(shapeBounds))
  82. }
  83. }
  84. complete(data: Data): void {
  85. const { initialShapes, hasUnlockedShapes } = this.snapshot
  86. if (!hasUnlockedShapes) return
  87. const page = getPage(data)
  88. const finalShapes = initialShapes.map((shape) =>
  89. deepClone(page.shapes[shape.id])
  90. )
  91. commands.mutate(data, initialShapes, finalShapes)
  92. }
  93. }
  94. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  95. export function getTransformSnapshot(data: Data, transformType: Edge | Corner) {
  96. const { currentPageId } = data
  97. const page = getPage(data)
  98. const initialShapes = setToArray(getSelectedIds(data))
  99. .flatMap((id) => getDocumentBranch(data, id).map((id) => page.shapes[id]))
  100. .filter((shape) => !shape.isLocked)
  101. .map((shape) => deepClone(shape))
  102. const hasUnlockedShapes = initialShapes.length > 0
  103. const isAllAspectRatioLocked = initialShapes.every(
  104. (shape) =>
  105. shape.isAspectRatioLocked || !getShapeUtils(shape).canChangeAspectRatio
  106. )
  107. const shapesBounds = Object.fromEntries(
  108. initialShapes.map((shape) => [
  109. shape.id,
  110. getShapeUtils(shape).getBounds(shape),
  111. ])
  112. )
  113. const boundsArr = Object.values(shapesBounds)
  114. const commonBounds = getCommonBounds(...boundsArr)
  115. const initialInnerBounds = getBoundsFromPoints(boundsArr.map(getBoundsCenter))
  116. // Return a mapping of shapes to bounds together with the relative
  117. // positions of the shape's bounds within the common bounds shape.
  118. return {
  119. type: transformType,
  120. hasUnlockedShapes,
  121. isAllAspectRatioLocked,
  122. currentPageId,
  123. initialShapes,
  124. initialBounds: commonBounds,
  125. shapeBounds: Object.fromEntries(
  126. initialShapes.map((shape) => {
  127. const initialShapeBounds = shapesBounds[shape.id]
  128. const ic = getBoundsCenter(initialShapeBounds)
  129. const ix = (ic[0] - initialInnerBounds.minX) / initialInnerBounds.width
  130. const iy = (ic[1] - initialInnerBounds.minY) / initialInnerBounds.height
  131. return [
  132. shape.id,
  133. {
  134. initialShape: shape,
  135. initialShapeBounds,
  136. transformOrigin: [ix, iy],
  137. },
  138. ]
  139. })
  140. ),
  141. }
  142. }
  143. export type TransformSnapshot = ReturnType<typeof getTransformSnapshot>
  144. // const transformOrigins = {
  145. // [Edge.Top]: [0.5, 1],
  146. // [Edge.Right]: [0, 0.5],
  147. // [Edge.Bottom]: [0.5, 0],
  148. // [Edge.Left]: [1, 0.5],
  149. // [Corner.TopLeft]: [1, 1],
  150. // [Corner.TopRight]: [0, 1],
  151. // [Corner.BottomLeft]: [1, 0],
  152. // [Corner.BottomRight]: [0, 0],
  153. // }
  154. // const origin = transformOrigins[this.transformType]
  155. // function resizeDescendants(data: Data, shapeId: string, bounds: Bounds) {
  156. // const { initialShape, initialShapeBounds, transformOrigin } =
  157. // shapeBounds[id]
  158. // const newShapeBounds = getRelativeTransformedBoundingBox(
  159. // newBoundingBox,
  160. // initialBounds,
  161. // initialShapeBounds,
  162. // this.scaleX < 0,
  163. // this.scaleY < 0
  164. // )
  165. // const shape = shapes[id]
  166. // getShapeUtils(shape).transform(shape, newShapeBounds, {
  167. // type: this.transformType,
  168. // initialShape,
  169. // scaleX: this.scaleX,
  170. // scaleY: this.scaleY,
  171. // transformOrigin,
  172. // })
  173. // }