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.

handle-session.ts 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { getPage } from 'utils/utils'
  7. import { getShapeUtils } from 'lib/shape-utils'
  8. export default class HandleSession extends BaseSession {
  9. delta = [0, 0]
  10. origin: number[]
  11. snapshot: HandleSnapshot
  12. constructor(data: Data, shapeId: string, handleId: string, point: number[]) {
  13. super(data)
  14. this.origin = point
  15. this.snapshot = getHandleSnapshot(data, shapeId, handleId)
  16. }
  17. update(data: Data, point: number[], isAligned: boolean) {
  18. const { currentPageId, handleId, initialShape } = this.snapshot
  19. const shape = getPage(data, currentPageId).shapes[initialShape.id]
  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. const handles = initialShape.handles
  29. getShapeUtils(shape).onHandleMove(shape, {
  30. [handleId]: {
  31. ...handles[handleId],
  32. point: vec.add(handles[handleId].point, delta),
  33. },
  34. })
  35. }
  36. cancel(data: Data) {
  37. const { currentPageId, handleId, initialShape } = this.snapshot
  38. const shape = getPage(data, currentPageId).shapes[initialShape.id]
  39. }
  40. complete(data: Data) {
  41. commands.handle(
  42. data,
  43. this.snapshot,
  44. getHandleSnapshot(
  45. data,
  46. this.snapshot.initialShape.id,
  47. this.snapshot.handleId
  48. )
  49. )
  50. }
  51. }
  52. export function getHandleSnapshot(
  53. data: Data,
  54. shapeId: string,
  55. handleId: string
  56. ) {
  57. const initialShape = getPage(current(data)).shapes[shapeId]
  58. return {
  59. currentPageId: data.currentPageId,
  60. handleId,
  61. initialShape,
  62. }
  63. }
  64. export type HandleSnapshot = ReturnType<typeof getHandleSnapshot>