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.

rotate-session.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { Data, ShapeType } 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 {
  7. clampToRotationToSegments,
  8. getBoundsCenter,
  9. getCommonBounds,
  10. getPage,
  11. getSelectedShapes,
  12. getRotatedBounds,
  13. getShapeBounds,
  14. updateParents,
  15. getDocumentBranch,
  16. setToArray,
  17. getSelectedIds,
  18. } from 'utils/utils'
  19. import { getShapeUtils } from 'lib/shape-utils'
  20. const PI2 = Math.PI * 2
  21. export default class RotateSession extends BaseSession {
  22. delta = [0, 0]
  23. origin: number[]
  24. snapshot: RotateSnapshot
  25. prev = 0
  26. constructor(data: Data, point: number[]) {
  27. super(data)
  28. this.origin = point
  29. this.snapshot = getRotateSnapshot(data)
  30. }
  31. update(data: Data, point: number[], isLocked: boolean) {
  32. const { commonBoundsCenter, initialShapes } = this.snapshot
  33. const page = getPage(data)
  34. const a1 = vec.angle(commonBoundsCenter, this.origin)
  35. const a2 = vec.angle(commonBoundsCenter, point)
  36. let rot = a2 - a1
  37. const delta = rot - this.prev
  38. this.prev = rot
  39. if (isLocked) {
  40. rot = clampToRotationToSegments(rot, 24)
  41. }
  42. data.boundsRotation = (PI2 + (this.snapshot.boundsRotation + rot)) % PI2
  43. for (let { id, center, offset, rotation } of initialShapes) {
  44. const shape = page.shapes[id]
  45. const nextRotation =
  46. PI2 +
  47. ((isLocked
  48. ? clampToRotationToSegments(rotation + rot, 24)
  49. : rotation + rot) %
  50. PI2)
  51. const nextPoint = vec.sub(
  52. vec.rotWith(center, commonBoundsCenter, rot),
  53. offset
  54. )
  55. getShapeUtils(shape)
  56. .rotateTo(shape, nextRotation, delta)
  57. .translateTo(shape, nextPoint)
  58. }
  59. updateParents(
  60. data,
  61. initialShapes.map((s) => s.id)
  62. )
  63. }
  64. cancel(data: Data) {
  65. const { currentPageId, initialShapes } = this.snapshot
  66. const page = getPage(data, currentPageId)
  67. for (let { id, point, rotation } of initialShapes) {
  68. const shape = page.shapes[id]
  69. getShapeUtils(shape)
  70. .rotateTo(shape, rotation, rotation - shape.rotation)
  71. .translateTo(shape, point)
  72. }
  73. updateParents(
  74. data,
  75. initialShapes.map((s) => s.id)
  76. )
  77. }
  78. complete(data: Data) {
  79. if (!this.snapshot.hasUnlockedShapes) return
  80. commands.rotate(data, this.snapshot, getRotateSnapshot(data))
  81. }
  82. }
  83. export function getRotateSnapshot(data: Data) {
  84. const cData = current(data)
  85. const page = getPage(cData)
  86. const initialShapes = setToArray(getSelectedIds(data))
  87. .flatMap((id) => getDocumentBranch(cData, id).map((id) => page.shapes[id]))
  88. .filter((shape) => !shape.isLocked)
  89. const hasUnlockedShapes = initialShapes.length > 0
  90. const shapesBounds = Object.fromEntries(
  91. initialShapes.map((shape) => [shape.id, getShapeBounds(shape)])
  92. )
  93. const bounds = getCommonBounds(...Object.values(shapesBounds))
  94. const commonBoundsCenter = getBoundsCenter(bounds)
  95. return {
  96. hasUnlockedShapes,
  97. currentPageId: data.currentPageId,
  98. boundsRotation: data.boundsRotation,
  99. commonBoundsCenter,
  100. initialShapes: initialShapes
  101. .filter((shape) => shape.type !== ShapeType.Group)
  102. .map((shape) => {
  103. const bounds = shapesBounds[shape.id]
  104. const center = getBoundsCenter(bounds)
  105. const offset = vec.sub(center, shape.point)
  106. const rotationOffset = vec.sub(
  107. center,
  108. getBoundsCenter(getRotatedBounds(shape))
  109. )
  110. return {
  111. id: shape.id,
  112. point: shape.point,
  113. rotation: shape.rotation,
  114. offset,
  115. rotationOffset,
  116. center,
  117. }
  118. }),
  119. }
  120. }
  121. export type RotateSnapshot = ReturnType<typeof getRotateSnapshot>