2021-06-03 13:10:54 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
2021-06-07 11:18:50 +00:00
|
|
|
import { Data, Page, PageState } from 'types'
|
2021-06-16 12:09:45 +00:00
|
|
|
import { uniqueId } from 'utils/utils'
|
2021-06-03 13:10:54 +00:00
|
|
|
import { current } from 'immer'
|
2021-06-07 11:18:50 +00:00
|
|
|
import storage from 'state/storage'
|
2021-06-03 13:10:54 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default function createPage(data: Data, goToPage = true): void {
|
2021-06-03 13:10:54 +00:00
|
|
|
const snapshot = getSnapshot(data)
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
2021-06-19 17:22:46 +00:00
|
|
|
name: 'create_page',
|
2021-06-03 13:10:54 +00:00
|
|
|
category: 'canvas',
|
|
|
|
do(data) {
|
2021-06-17 12:03:08 +00:00
|
|
|
const { page, pageState, currentPageId } = snapshot
|
2021-06-03 13:10:54 +00:00
|
|
|
data.document.pages[page.id] = page
|
|
|
|
data.pageStates[page.id] = pageState
|
2021-06-17 12:03:08 +00:00
|
|
|
|
2021-06-03 13:10:54 +00:00
|
|
|
data.currentPageId = page.id
|
2021-06-11 10:11:42 +00:00
|
|
|
storage.savePage(data, data.document.id, page.id)
|
2021-06-16 12:09:45 +00:00
|
|
|
storage.saveDocumentToLocalStorage(data)
|
2021-06-17 12:03:08 +00:00
|
|
|
|
|
|
|
if (goToPage) {
|
|
|
|
data.currentPageId = page.id
|
|
|
|
} else {
|
|
|
|
data.currentPageId = currentPageId
|
|
|
|
}
|
2021-06-03 13:10:54 +00:00
|
|
|
},
|
|
|
|
undo(data) {
|
|
|
|
const { page, currentPageId } = snapshot
|
|
|
|
delete data.document.pages[page.id]
|
|
|
|
delete data.pageStates[page.id]
|
|
|
|
data.currentPageId = currentPageId
|
2021-06-16 12:09:45 +00:00
|
|
|
storage.saveDocumentToLocalStorage(data)
|
2021-06-03 13:10:54 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSnapshot(data: Data) {
|
|
|
|
const { currentPageId } = current(data)
|
|
|
|
|
|
|
|
const pages = Object.values(data.document.pages)
|
|
|
|
const unchanged = pages.filter((page) => page.name.startsWith('Page '))
|
2021-06-16 12:09:45 +00:00
|
|
|
const id = uniqueId()
|
2021-06-03 13:10:54 +00:00
|
|
|
|
|
|
|
const page: Page = {
|
|
|
|
type: 'page',
|
|
|
|
id,
|
|
|
|
name: `Page ${unchanged.length + 1}`,
|
|
|
|
childIndex: pages.length,
|
|
|
|
shapes: {},
|
|
|
|
}
|
2021-06-17 12:03:08 +00:00
|
|
|
|
2021-06-07 11:18:50 +00:00
|
|
|
const pageState: PageState = {
|
2021-06-16 12:09:45 +00:00
|
|
|
id,
|
|
|
|
selectedIds: new Set([]),
|
2021-06-03 13:10:54 +00:00
|
|
|
camera: {
|
|
|
|
point: [0, 0],
|
|
|
|
zoom: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
currentPageId,
|
|
|
|
page,
|
|
|
|
pageState,
|
|
|
|
}
|
|
|
|
}
|