Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

transform-single-session.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Data, TransformEdge, TransformCorner } 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/shapes"
  7. import {
  8. getTransformedBoundingBox,
  9. getCommonBounds,
  10. getRotatedCorners,
  11. getTransformAnchor,
  12. } from "utils/utils"
  13. export default class TransformSingleSession extends BaseSession {
  14. transformType: TransformEdge | TransformCorner
  15. origin: number[]
  16. scaleX = 1
  17. scaleY = 1
  18. snapshot: TransformSingleSnapshot
  19. isCreating: boolean
  20. constructor(
  21. data: Data,
  22. transformType: TransformCorner | TransformEdge,
  23. point: number[],
  24. isCreating = false
  25. ) {
  26. super(data)
  27. this.origin = point
  28. this.transformType = transformType
  29. this.snapshot = getTransformSingleSnapshot(data, transformType)
  30. this.isCreating = isCreating
  31. }
  32. update(data: Data, point: number[]) {
  33. const { transformType } = this
  34. const { initialShapeBounds, currentPageId, initialShape, id } =
  35. this.snapshot
  36. const shape = data.document.pages[currentPageId].shapes[id]
  37. const newBoundingBox = getTransformedBoundingBox(
  38. initialShapeBounds,
  39. transformType,
  40. vec.vec(this.origin, point),
  41. shape.rotation
  42. )
  43. this.scaleX = newBoundingBox.scaleX
  44. this.scaleY = newBoundingBox.scaleY
  45. getShapeUtils(shape).transformSingle(shape, newBoundingBox, {
  46. initialShape,
  47. type: this.transformType,
  48. scaleX: this.scaleX,
  49. scaleY: this.scaleY,
  50. })
  51. }
  52. cancel(data: Data) {
  53. const { id, initialShape, initialShapeBounds, currentPageId } =
  54. this.snapshot
  55. const { shapes } = data.document.pages[currentPageId]
  56. const shape = shapes[id]
  57. getShapeUtils(shape).transform(shape, initialShapeBounds, {
  58. initialShape,
  59. type: this.transformType,
  60. scaleX: this.scaleX,
  61. scaleY: this.scaleY,
  62. })
  63. }
  64. complete(data: Data) {
  65. commands.transformSingle(
  66. data,
  67. this.snapshot,
  68. getTransformSingleSnapshot(data, this.transformType),
  69. this.scaleX,
  70. this.scaleY,
  71. this.isCreating
  72. )
  73. }
  74. }
  75. export function getTransformSingleSnapshot(
  76. data: Data,
  77. transformType: TransformEdge | TransformCorner
  78. ) {
  79. const {
  80. document: { pages },
  81. selectedIds,
  82. currentPageId,
  83. } = current(data)
  84. const id = Array.from(selectedIds)[0]
  85. const shape = pages[currentPageId].shapes[id]
  86. const bounds = getShapeUtils(shape).getBounds(shape)
  87. return {
  88. id,
  89. currentPageId,
  90. type: transformType,
  91. initialShape: shape,
  92. initialShapeBounds: bounds,
  93. }
  94. }
  95. export type TransformSingleSnapshot = ReturnType<
  96. typeof getTransformSingleSnapshot
  97. >