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.

direction-session.ts 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Data, LineShape, RayShape } 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 DirectionSession extends BaseSession {
  7. delta = [0, 0]
  8. origin: number[]
  9. snapshot: DirectionSnapshot
  10. constructor(data: Data, point: number[]) {
  11. super(data)
  12. this.origin = point
  13. this.snapshot = getDirectionSnapshot(data)
  14. }
  15. update(data: Data, point: number[]) {
  16. const { currentPageId, shapes } = this.snapshot
  17. const { document } = data
  18. for (let { id } of shapes) {
  19. const shape = document.pages[currentPageId].shapes[id] as
  20. | RayShape
  21. | LineShape
  22. shape.direction = vec.uni(vec.vec(shape.point, point))
  23. }
  24. }
  25. cancel(data: Data) {
  26. const { document } = data
  27. for (let { id, direction } of this.snapshot.shapes) {
  28. const shape = document.pages[this.snapshot.currentPageId].shapes[id] as
  29. | RayShape
  30. | LineShape
  31. shape.direction = direction
  32. }
  33. }
  34. complete(data: Data) {
  35. commands.direct(data, this.snapshot, getDirectionSnapshot(data))
  36. }
  37. }
  38. export function getDirectionSnapshot(data: Data) {
  39. const {
  40. document: { pages },
  41. currentPageId,
  42. } = current(data)
  43. const { shapes } = pages[currentPageId]
  44. let snapshapes: { id: string; direction: number[] }[] = []
  45. data.selectedIds.forEach((id) => {
  46. const shape = shapes[id]
  47. if ("direction" in shape) {
  48. snapshapes.push({ id: shape.id, direction: shape.direction })
  49. }
  50. })
  51. return {
  52. currentPageId,
  53. shapes: snapshapes,
  54. }
  55. }
  56. export type DirectionSnapshot = ReturnType<typeof getDirectionSnapshot>