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.

edit-session.ts 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { Data, LineShape, RayShape, Shape } 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 {
  7. getPage,
  8. getPageState,
  9. getSelectedIds,
  10. getSelectedShapes,
  11. getShape,
  12. } from 'utils/utils'
  13. import { getShapeUtils } from 'lib/shape-utils'
  14. export default class EditSession extends BaseSession {
  15. snapshot: EditSnapshot
  16. constructor(data: Data) {
  17. super(data)
  18. this.snapshot = getEditSnapshot(data)
  19. }
  20. update(data: Data, change: Partial<Shape>) {
  21. const initialShape = this.snapshot.initialShape
  22. const shape = getShape(data, initialShape.id)
  23. const utils = getShapeUtils(shape)
  24. Object.entries(change).forEach(([key, value]) => {
  25. utils.setProperty(shape, key as keyof Shape, value as Shape[keyof Shape])
  26. })
  27. }
  28. cancel(data: Data) {
  29. const initialShape = this.snapshot.initialShape
  30. const page = getPage(data)
  31. page.shapes[initialShape.id] = initialShape
  32. }
  33. complete(data: Data) {
  34. commands.edit(data, this.snapshot, getEditSnapshot(data))
  35. }
  36. }
  37. export function getEditSnapshot(data: Data) {
  38. const initialShape = getSelectedShapes(current(data))[0]
  39. return {
  40. currentPageId: data.currentPageId,
  41. initialShape,
  42. }
  43. }
  44. export type EditSnapshot = ReturnType<typeof getEditSnapshot>