Storage now restores full state as well as document

This commit is contained in:
Steve Ruiz 2021-06-25 12:14:42 +01:00
parent 3c26ffaae3
commit d2f43e5253

View file

@ -25,22 +25,20 @@ class Storage {
// 1. Load Document from Local Storage // 1. Load Document from Local Storage
// Using the "last opened file id" in local storage. // Using the "last opened file id" in local storage.
if (lastOpenedFileId !== null) { if (lastOpenedFileId !== null) {
// Load document from local storage // Load state from local storage
const savedDocument = localStorage.getItem( const savedState = localStorage.getItem(
storageId(lastOpenedFileId, 'document', lastOpenedFileId) storageId(lastOpenedFileId, 'document-state', lastOpenedFileId)
) )
if (savedDocument === null) { if (savedState === null) {
// If no document found, create a fresh random id. // If no state with that document was found, create a fresh random id.
data.document.id = uniqueId() data.document.id = uniqueId()
} else { } else {
// If we did find a document, load it into state. // If we did find a state and document, load it into state.
const restoredDocument: TLDocument = JSON.parse( const restoredDocument: Data = JSON.parse(decompress(savedState))
decompress(savedDocument)
)
// Merge restored data into state. // Merge restored data into state.
data.document = restoredDocument Object.assign(data, restoredDocument)
} }
} }
@ -58,6 +56,10 @@ class Storage {
storageId(data.document.id, 'document', data.document.id), storageId(data.document.id, 'document', data.document.id),
compress(JSON.stringify(document)) compress(JSON.stringify(document))
) )
localStorage.setItem(
storageId(data.document.id, 'document-state', data.document.id),
compress(JSON.stringify(data))
)
} }
getCompleteDocument = (data: Data) => { getCompleteDocument = (data: Data) => {
@ -168,6 +170,11 @@ class Storage {
compress(JSON.stringify(data.document)) compress(JSON.stringify(data.document))
) )
localStorage.setItem(
storageId(data.document.id, 'document-state', data.document.id),
compress(JSON.stringify(data))
)
// 4.1 // 4.1
// Also save out copies of each page separately. // Also save out copies of each page separately.
Object.values(data.document.pages).forEach((page) => { Object.values(data.document.pages).forEach((page) => {