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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Data, LineShape, RayShape } from 'types'
  2. import vec from 'utils/vec'
  3. import BaseSession from './base-session'
  4. import commands from 'state/commands'
  5. import { current } from 'immer'
  6. import { getPage, getSelectedIds } from '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[]): void {
  17. const { shapes } = this.snapshot
  18. const page = getPage(data)
  19. for (const { 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): void {
  25. const page = getPage(data)
  26. for (const { 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): void {
  32. commands.direct(data, this.snapshot, getDirectionSnapshot(data))
  33. }
  34. }
  35. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  36. export function getDirectionSnapshot(data: Data) {
  37. const { shapes } = getPage(current(data))
  38. const snapshapes: { id: string; direction: number[] }[] = []
  39. getSelectedIds(data).forEach((id) => {
  40. const shape = shapes[id]
  41. if ('direction' in shape) {
  42. snapshapes.push({ id: shape.id, direction: shape.direction })
  43. }
  44. })
  45. return {
  46. currentPageId: data.currentPageId,
  47. shapes: snapshapes,
  48. }
  49. }
  50. export type DirectionSnapshot = ReturnType<typeof getDirectionSnapshot>