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.6KB

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