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 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Data } 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 } 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. // rotate the delta ?
  30. // rotate the handle ?
  31. // rotate the shape around the previous center point
  32. getShapeUtils(shape).onHandleChange(shape, {
  33. [handleId]: {
  34. ...handles[handleId],
  35. point: vec.add(handles[handleId].point, delta), // vec.rot(delta, shape.rotation)),
  36. },
  37. })
  38. }
  39. cancel(data: Data) {
  40. const { currentPageId, handleId, initialShape } = this.snapshot
  41. getPage(data, currentPageId).shapes[initialShape.id] = initialShape
  42. }
  43. complete(data: Data) {
  44. commands.handle(
  45. data,
  46. this.snapshot,
  47. getHandleSnapshot(
  48. data,
  49. this.snapshot.initialShape.id,
  50. this.snapshot.handleId
  51. )
  52. )
  53. }
  54. }
  55. export function getHandleSnapshot(
  56. data: Data,
  57. shapeId: string,
  58. handleId: string
  59. ) {
  60. const initialShape = getPage(current(data)).shapes[shapeId]
  61. return {
  62. currentPageId: data.currentPageId,
  63. handleId,
  64. initialShape,
  65. }
  66. }
  67. export type HandleSnapshot = ReturnType<typeof getHandleSnapshot>