From d2f43e5253cf0a6c77df12c80b3f8a80d3d304fd Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Fri, 25 Jun 2021 12:14:42 +0100 Subject: [PATCH] Storage now restores full state as well as document --- state/storage.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/state/storage.ts b/state/storage.ts index 57d25a31f..925cc1948 100644 --- a/state/storage.ts +++ b/state/storage.ts @@ -25,22 +25,20 @@ class Storage { // 1. Load Document from Local Storage // Using the "last opened file id" in local storage. if (lastOpenedFileId !== null) { - // Load document from local storage - const savedDocument = localStorage.getItem( - storageId(lastOpenedFileId, 'document', lastOpenedFileId) + // Load state from local storage + const savedState = localStorage.getItem( + storageId(lastOpenedFileId, 'document-state', lastOpenedFileId) ) - if (savedDocument === null) { - // If no document found, create a fresh random id. + if (savedState === null) { + // If no state with that document was found, create a fresh random id. data.document.id = uniqueId() } else { - // If we did find a document, load it into state. - const restoredDocument: TLDocument = JSON.parse( - decompress(savedDocument) - ) + // If we did find a state and document, load it into state. + const restoredDocument: Data = JSON.parse(decompress(savedState)) // 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), compress(JSON.stringify(document)) ) + localStorage.setItem( + storageId(data.document.id, 'document-state', data.document.id), + compress(JSON.stringify(data)) + ) } getCompleteDocument = (data: Data) => { @@ -168,6 +170,11 @@ class Storage { compress(JSON.stringify(data.document)) ) + localStorage.setItem( + storageId(data.document.id, 'document-state', data.document.id), + compress(JSON.stringify(data)) + ) + // 4.1 // Also save out copies of each page separately. Object.values(data.document.pages).forEach((page) => {