Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

translate-session.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import { v4 as uuid } from "uuid"
  7. export default class TranslateSession extends BaseSession {
  8. delta = [0, 0]
  9. origin: number[]
  10. snapshot: TranslateSnapshot
  11. isCloning = false
  12. constructor(data: Data, point: number[], isCloning = false) {
  13. super(data)
  14. this.origin = point
  15. this.snapshot = getTranslateSnapshot(data)
  16. }
  17. update(data: Data, point: number[], isAligned: boolean, isCloning: boolean) {
  18. const { currentPageId, clones, initialShapes } = this.snapshot
  19. const { shapes } = data.document.pages[currentPageId]
  20. const delta = vec.vec(this.origin, point)
  21. if (isAligned) {
  22. if (Math.abs(delta[0]) < Math.abs(delta[1])) {
  23. delta[0] = 0
  24. } else {
  25. delta[1] = 0
  26. }
  27. }
  28. if (isCloning) {
  29. if (!this.isCloning) {
  30. this.isCloning = true
  31. data.selectedIds.clear()
  32. for (const { id, point } of initialShapes) {
  33. shapes[id].point = point
  34. }
  35. for (const clone of clones) {
  36. shapes[clone.id] = { ...clone }
  37. data.selectedIds.add(clone.id)
  38. }
  39. }
  40. for (const { id, point } of clones) {
  41. shapes[id].point = vec.add(point, delta)
  42. }
  43. } else {
  44. if (this.isCloning) {
  45. this.isCloning = false
  46. data.selectedIds.clear()
  47. for (const { id } of initialShapes) {
  48. data.selectedIds.add(id)
  49. }
  50. for (const clone of clones) {
  51. delete shapes[clone.id]
  52. }
  53. }
  54. for (const { id, point } of initialShapes) {
  55. shapes[id].point = vec.add(point, delta)
  56. }
  57. }
  58. }
  59. cancel(data: Data) {
  60. const { initialShapes, clones, currentPageId } = this.snapshot
  61. const { shapes } = data.document.pages[currentPageId]
  62. for (const { id, point } of initialShapes) {
  63. shapes[id].point = point
  64. }
  65. for (const { id } of clones) {
  66. delete shapes[id]
  67. }
  68. }
  69. complete(data: Data) {
  70. commands.translate(
  71. data,
  72. this.snapshot,
  73. getTranslateSnapshot(data),
  74. this.isCloning
  75. )
  76. }
  77. }
  78. export function getTranslateSnapshot(data: Data) {
  79. const { document, selectedIds, currentPageId } = current(data)
  80. const shapes = Array.from(selectedIds.values()).map(
  81. (id) => document.pages[currentPageId].shapes[id]
  82. )
  83. return {
  84. currentPageId,
  85. initialShapes: shapes.map(({ id, point }) => ({ id, point })),
  86. clones: shapes.map((shape) => ({ ...shape, id: uuid() })),
  87. }
  88. }
  89. export type TranslateSnapshot = ReturnType<typeof getTranslateSnapshot>