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.

translate-session.ts 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { Data } 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. export default class TranslateSession extends BaseSession {
  7. delta = [0, 0]
  8. origin: number[]
  9. snapshot: TranslateSnapshot
  10. constructor(data: Data, point: number[]) {
  11. super(data)
  12. this.origin = point
  13. this.snapshot = getTranslateSnapshot(data)
  14. }
  15. update(data: Data, point: number[]) {
  16. const { currentPageId, shapes } = this.snapshot
  17. const { document } = data
  18. const delta = vec.vec(this.origin, point)
  19. for (let shape of shapes) {
  20. document.pages[currentPageId].shapes[shape.id].point = vec.add(
  21. shape.point,
  22. delta
  23. )
  24. }
  25. }
  26. cancel(data: Data) {
  27. const { document } = data
  28. for (let shape of this.snapshot.shapes) {
  29. document.pages[this.snapshot.currentPageId].shapes[shape.id].point =
  30. shape.point
  31. }
  32. }
  33. complete(data: Data) {
  34. commands.translate(data, this.snapshot, getTranslateSnapshot(data))
  35. }
  36. }
  37. export function getTranslateSnapshot(data: Data) {
  38. const {
  39. document: { pages },
  40. currentPageId,
  41. } = current(data)
  42. const { shapes } = pages[currentPageId]
  43. return {
  44. currentPageId,
  45. shapes: Array.from(data.selectedIds.values())
  46. .map((id) => shapes[id])
  47. .map(({ id, point }) => ({ id, point })),
  48. }
  49. }
  50. export type TranslateSnapshot = ReturnType<typeof getTranslateSnapshot>