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

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