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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Data, Shape } from 'types'
  2. import BaseSession from './base-session'
  3. import commands from 'state/commands'
  4. import { current } from 'immer'
  5. import { getPage, getSelectedShapes, getShape } from 'utils'
  6. import { getShapeUtils } from 'state/shape-utils'
  7. export default class EditSession extends BaseSession {
  8. snapshot: EditSnapshot
  9. constructor(data: Data) {
  10. super(data)
  11. this.snapshot = getEditSnapshot(data)
  12. }
  13. update(data: Data, change: Partial<Shape>): void {
  14. const initialShape = this.snapshot.initialShape
  15. const shape = getShape(data, initialShape.id)
  16. const utils = getShapeUtils(shape)
  17. Object.entries(change).forEach(([key, value]) => {
  18. utils.setProperty(shape, key as keyof Shape, value as Shape[keyof Shape])
  19. })
  20. }
  21. cancel(data: Data): void {
  22. const initialShape = this.snapshot.initialShape
  23. const page = getPage(data)
  24. page.shapes[initialShape.id] = initialShape
  25. }
  26. complete(data: Data): void {
  27. commands.edit(data, this.snapshot, getEditSnapshot(data))
  28. }
  29. }
  30. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  31. export function getEditSnapshot(data: Data) {
  32. const initialShape = getSelectedShapes(current(data))[0]
  33. return {
  34. currentPageId: data.currentPageId,
  35. initialShape,
  36. }
  37. }
  38. export type EditSnapshot = ReturnType<typeof getEditSnapshot>