Improve names on creating page

This commit is contained in:
Steve Ruiz 2021-09-04 16:12:29 +01:00
parent 8ecddcbdbc
commit 5948fe467d
2 changed files with 34 additions and 14 deletions

View file

@ -1,9 +1,37 @@
import type { Data, TLDrawCommand } from '~types' import type { Data, TLDrawCommand, TLDrawPage } from '~types'
import { Utils } from '@tldraw/core' import { Utils, TLPageState } from '@tldraw/core'
export function createPage(data: Data, pageId = Utils.uniqueId()): TLDrawCommand { export function createPage(data: Data, pageId = Utils.uniqueId()): TLDrawCommand {
const { currentPageId } = data.appState const { currentPageId } = data.appState
const topPage = Object.values(data.document.pages).sort(
(a, b) => (b.childIndex || 0) - (a.childIndex || 0)
)[0]
const nextChildIndex = topPage?.childIndex ? topPage?.childIndex + 1 : 1
// TODO: Iterate the name better
const nextName = `Page ${nextChildIndex}`
const page: TLDrawPage = {
id: pageId,
name: nextName,
shapes: {},
childIndex: nextChildIndex,
bindings: {},
}
const pageState: TLPageState = {
id: pageId,
selectedIds: [],
camera: { point: [-window.innerWidth / 2, -window.innerHeight / 2], zoom: 1 },
currentParentId: pageId,
editingId: undefined,
bindingId: undefined,
hoveredId: undefined,
pointedId: undefined,
}
return { return {
id: 'create_page', id: 'create_page',
before: { before: {
@ -21,23 +49,14 @@ export function createPage(data: Data, pageId = Utils.uniqueId()): TLDrawCommand
}, },
after: { after: {
appState: { appState: {
currentPageId: pageId, currentPageId: page.id,
}, },
document: { document: {
pages: { pages: {
[pageId]: { id: pageId, shapes: {}, bindings: {} }, [pageId]: page,
}, },
pageStates: { pageStates: {
[pageId]: { [pageId]: pageState,
id: pageId,
selectedIds: [],
camera: { point: [-window.innerWidth / 2, -window.innerHeight / 2], zoom: 1 },
currentParentId: pageId,
editingId: undefined,
bindingId: undefined,
hoveredId: undefined,
pointedId: undefined,
},
}, },
}, },
}, },

View file

@ -47,6 +47,7 @@ const defaultDocument: TLDrawDocument = {
pages: { pages: {
page: { page: {
id: 'page', id: 'page',
name: 'Page 1',
childIndex: 1, childIndex: 1,
shapes: {}, shapes: {},
bindings: {}, bindings: {},