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.

arrow-session.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { ArrowShape, 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 { getBoundsFromPoints, getPage, updateParents } from 'utils/utils'
  7. import { getShapeUtils } from 'lib/shape-utils'
  8. export default class PointsSession extends BaseSession {
  9. points: number[][]
  10. origin: number[]
  11. snapshot: ArrowSnapshot
  12. isLocked: boolean
  13. lockedDirection: 'horizontal' | 'vertical'
  14. constructor(data: Data, id: string, point: number[], isLocked: boolean) {
  15. super(data)
  16. this.origin = point
  17. this.points = [[0, 0]]
  18. this.snapshot = getArrowSnapshot(data, id)
  19. }
  20. update(data: Data, point: number[], isLocked = false) {
  21. const { id } = this.snapshot
  22. const delta = vec.vec(this.origin, point)
  23. if (isLocked) {
  24. if (!this.isLocked && this.points.length > 1) {
  25. this.isLocked = true
  26. if (Math.abs(delta[0]) < Math.abs(delta[1])) {
  27. this.lockedDirection = 'vertical'
  28. } else {
  29. this.lockedDirection = 'horizontal'
  30. }
  31. }
  32. } else {
  33. if (this.isLocked) {
  34. this.isLocked = false
  35. }
  36. }
  37. if (this.isLocked) {
  38. if (this.lockedDirection === 'vertical') {
  39. point[0] = this.origin[0]
  40. } else {
  41. point[1] = this.origin[1]
  42. }
  43. }
  44. const shape = getPage(data).shapes[id] as ArrowShape
  45. getShapeUtils(shape).onHandleChange(shape, {
  46. end: {
  47. ...shape.handles.end,
  48. point: vec.sub(point, shape.point),
  49. },
  50. })
  51. updateParents(data, [shape])
  52. }
  53. cancel(data: Data) {
  54. const { id, initialShape } = this.snapshot
  55. const shape = getPage(data).shapes[id] as ArrowShape
  56. getShapeUtils(shape)
  57. .onHandleChange(shape, { end: initialShape.handles.end })
  58. .setProperty(shape, 'point', initialShape.point)
  59. updateParents(data, [shape])
  60. }
  61. complete(data: Data) {
  62. const { id } = this.snapshot
  63. const shape = getPage(data).shapes[id] as ArrowShape
  64. const { start, end, bend } = shape.handles
  65. // Normalize point and handles
  66. const bounds = getBoundsFromPoints([start.point, end.point])
  67. const corner = [bounds.minX, bounds.minY]
  68. const newPoint = vec.add(shape.point, corner)
  69. const nextHandles = {
  70. start: { ...start, point: vec.sub(start.point, corner) },
  71. end: { ...end, point: vec.sub(end.point, corner) },
  72. bend: { ...bend, point: vec.sub(bend.point, corner) },
  73. }
  74. getShapeUtils(shape)
  75. .setProperty(shape, 'points', [
  76. nextHandles.start.point,
  77. nextHandles.end.point,
  78. ])
  79. .setProperty(shape, 'handles', nextHandles)
  80. .setProperty(shape, 'point', newPoint)
  81. .onHandleChange(shape, nextHandles)
  82. commands.arrow(
  83. data,
  84. this.snapshot,
  85. getArrowSnapshot(data, this.snapshot.id)
  86. )
  87. }
  88. }
  89. export function getArrowSnapshot(data: Data, id: string) {
  90. const initialShape = getPage(current(data)).shapes[id] as ArrowShape
  91. return {
  92. id,
  93. initialShape,
  94. selectedIds: new Set(data.selectedIds),
  95. currentPageId: data.currentPageId,
  96. }
  97. }
  98. export type ArrowSnapshot = ReturnType<typeof getArrowSnapshot>