2021-06-03 13:10:54 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data } from 'types'
|
|
|
|
import { current } from 'immer'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-11 10:11:42 +00:00
|
|
|
import storage from 'state/storage'
|
2021-06-03 13:10:54 +00:00
|
|
|
|
2021-06-19 17:22:46 +00:00
|
|
|
export default function deletePage(data: Data, pageId: string) {
|
2021-06-03 13:10:54 +00:00
|
|
|
const snapshot = getSnapshot(data, pageId)
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
2021-06-19 17:22:46 +00:00
|
|
|
name: 'delete_page',
|
2021-06-03 13:10:54 +00:00
|
|
|
category: 'canvas',
|
|
|
|
do(data) {
|
|
|
|
data.currentPageId = snapshot.nextPageId
|
|
|
|
delete data.document.pages[pageId]
|
|
|
|
delete data.pageStates[pageId]
|
2021-06-11 10:11:42 +00:00
|
|
|
storage.loadPage(data, snapshot.nextPageId)
|
2021-06-03 13:10:54 +00:00
|
|
|
},
|
|
|
|
undo(data) {
|
|
|
|
data.currentPageId = snapshot.currentPageId
|
|
|
|
data.document.pages[pageId] = snapshot.page
|
|
|
|
data.pageStates[pageId] = snapshot.pageState
|
2021-06-11 10:11:42 +00:00
|
|
|
storage.loadPage(data, snapshot.currentPageId)
|
2021-06-03 13:10:54 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSnapshot(data: Data, pageId: string) {
|
|
|
|
const cData = current(data)
|
|
|
|
const { currentPageId, document } = cData
|
|
|
|
|
|
|
|
const page = document.pages[pageId]
|
|
|
|
const pageState = cData.pageStates[pageId]
|
|
|
|
|
|
|
|
const isCurrent = currentPageId === pageId
|
|
|
|
|
2021-06-11 10:11:42 +00:00
|
|
|
// const nextIndex = isCurrent
|
|
|
|
// ? page.childIndex === 0
|
|
|
|
// ? 1
|
|
|
|
// : page.childIndex - 1
|
|
|
|
// : document.pages[currentPageId].childIndex
|
2021-06-03 13:10:54 +00:00
|
|
|
|
|
|
|
const nextPageId = isCurrent
|
2021-06-11 10:11:42 +00:00
|
|
|
? Object.values(document.pages).filter((page) => page.id !== pageId)[0]?.id // TODO: should be at nextIndex
|
2021-06-03 13:10:54 +00:00
|
|
|
: cData.currentPageId
|
|
|
|
|
|
|
|
return {
|
|
|
|
nextPageId,
|
|
|
|
isCurrent,
|
|
|
|
currentPageId,
|
|
|
|
page,
|
|
|
|
pageState,
|
|
|
|
}
|
|
|
|
}
|