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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Data, Shape } from 'types'
  2. import vec from 'utils/vec'
  3. import BaseSession from './base-session'
  4. import commands from 'state/commands'
  5. import tld from 'utils/tld'
  6. import { deepClone } from 'utils'
  7. import { getShapeUtils } from 'state/shape-utils'
  8. export default class DirectionSession extends BaseSession {
  9. delta = [0, 0]
  10. origin: number[]
  11. snapshot: DirectionSnapshot
  12. constructor(data: Data, point: number[]) {
  13. super(data)
  14. this.origin = point
  15. this.snapshot = getDirectionSnapshot(data)
  16. }
  17. update(data: Data, point: number[]): void {
  18. const page = tld.getPage(data)
  19. this.snapshot.forEach((initialShape) => {
  20. const shape = page.shapes[initialShape.id]
  21. if ('direction' in shape) {
  22. getShapeUtils(shape).setProperty(
  23. shape,
  24. 'direction',
  25. vec.uni(vec.vec(shape.point, point))
  26. )
  27. }
  28. })
  29. }
  30. cancel(data: Data): void {
  31. const page = tld.getPage(data)
  32. this.snapshot.forEach((initialShape) => {
  33. const shape = page.shapes[initialShape.id]
  34. if ('direction' in shape && 'direction' in initialShape) {
  35. getShapeUtils(shape).setProperty(
  36. shape,
  37. 'direction',
  38. initialShape.direction
  39. )
  40. }
  41. })
  42. }
  43. complete(data: Data): void {
  44. commands.mutate(
  45. data,
  46. this.snapshot,
  47. getDirectionSnapshot(data),
  48. 'change_direction'
  49. )
  50. }
  51. }
  52. export function getDirectionSnapshot(data: Data): Shape[] {
  53. return tld
  54. .getSelectedShapes(data)
  55. .filter((shape) => 'direction' in shape)
  56. .map((shape) => deepClone(shape))
  57. }
  58. export type DirectionSnapshot = ReturnType<typeof getDirectionSnapshot>