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.

delete-page.ts 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data } from 'types'
  4. import { current } from 'immer'
  5. import { getPage, getSelectedShapes } from 'utils/utils'
  6. import { getShapeUtils } from 'lib/shape-utils'
  7. import * as vec from 'utils/vec'
  8. export default function changePage(data: Data, pageId: string) {
  9. const snapshot = getSnapshot(data, pageId)
  10. history.execute(
  11. data,
  12. new Command({
  13. name: 'change_page',
  14. category: 'canvas',
  15. do(data) {
  16. data.currentPageId = snapshot.nextPageId
  17. delete data.document.pages[pageId]
  18. delete data.pageStates[pageId]
  19. },
  20. undo(data) {
  21. data.currentPageId = snapshot.currentPageId
  22. data.document.pages[pageId] = snapshot.page
  23. data.pageStates[pageId] = snapshot.pageState
  24. },
  25. })
  26. )
  27. }
  28. function getSnapshot(data: Data, pageId: string) {
  29. const cData = current(data)
  30. const { currentPageId, document } = cData
  31. const page = document.pages[pageId]
  32. const pageState = cData.pageStates[pageId]
  33. const isCurrent = currentPageId === pageId
  34. const nextIndex = isCurrent
  35. ? page.childIndex === 0
  36. ? 1
  37. : page.childIndex - 1
  38. : document.pages[currentPageId].childIndex
  39. const nextPageId = isCurrent
  40. ? Object.values(document.pages).find(
  41. (page) => page.childIndex === nextIndex
  42. )!.id
  43. : cData.currentPageId
  44. return {
  45. nextPageId,
  46. isCurrent,
  47. currentPageId,
  48. page,
  49. pageState,
  50. }
  51. }