@@ -2,7 +2,15 @@ import { createStore, del, get, set, update } from "idb-keyval";
22import { get as getFromStore } from "svelte/store" ;
33
44import { type Editor } from "@graphite/editor" ;
5- import { TriggerIndexedDbWriteDocument , TriggerIndexedDbRemoveDocument , TriggerSavePreferences , TriggerLoadAutoSaveDocuments , TriggerLoadPreferences } from "@graphite/messages" ;
5+ import {
6+ TriggerIndexedDbWriteDocument ,
7+ TriggerIndexedDbRemoveDocument ,
8+ TriggerSavePreferences ,
9+ TriggerLoadPreferences ,
10+ TriggerLoadFirstAutoSaveDocument ,
11+ TriggerLoadRestAutoSaveDocuments ,
12+ TriggerSaveActiveDocument ,
13+ } from "@graphite/messages" ;
614import { type PortfolioState } from "@graphite/state-providers/portfolio" ;
715
816const graphiteStore = createStore ( "graphite" , "store" ) ;
@@ -12,10 +20,13 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
1220
1321 async function storeDocumentOrder ( ) {
1422 const documentOrder = getFromStore ( portfolio ) . documents . map ( ( doc ) => String ( doc . id ) ) ;
15-
1623 await set ( "documents_tab_order" , documentOrder , graphiteStore ) ;
1724 }
1825
26+ async function storeCurrentDocumentId ( documentId : string ) {
27+ await set ( "current_document_id" , String ( documentId ) , graphiteStore ) ;
28+ }
29+
1930 async function storeDocument ( autoSaveDocument : TriggerIndexedDbWriteDocument ) {
2031 await update < Record < string , TriggerIndexedDbWriteDocument > > (
2132 "documents" ,
@@ -28,6 +39,7 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
2839 ) ;
2940
3041 await storeDocumentOrder ( ) ;
42+ await storeCurrentDocumentId ( autoSaveDocument . details . id ) ;
3143 }
3244
3345 async function removeDocument ( id : string ) {
@@ -41,19 +53,80 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
4153 graphiteStore ,
4254 ) ;
4355
56+ const documentCount = getFromStore ( portfolio ) . documents . length ;
57+ if ( documentCount > 0 ) {
58+ const documentIndex = getFromStore ( portfolio ) . activeDocumentIndex ;
59+ const documentId = getFromStore ( portfolio ) . documents [ documentIndex ] . id ;
60+
61+ await storeCurrentDocumentId ( String ( documentId ) ) ;
62+ } else {
63+ await del ( "current_document_id" , graphiteStore ) ;
64+ }
65+
4466 await storeDocumentOrder ( ) ;
4567 }
4668
47- async function loadDocuments ( ) {
69+ async function loadFirstDocument ( ) {
70+ const previouslySavedDocuments = await get < Record < string , TriggerIndexedDbWriteDocument > > ( "documents" , graphiteStore ) ;
71+ const documentOrder = await get < string [ ] > ( "documents_tab_order" , graphiteStore ) ;
72+ const currentDocumentId = await get < string > ( "current_document_id" , graphiteStore ) ;
73+ if ( ! previouslySavedDocuments || ! documentOrder ) return ;
74+
75+ const orderedSavedDocuments = documentOrder . flatMap ( ( id ) => ( previouslySavedDocuments [ id ] ? [ previouslySavedDocuments [ id ] ] : [ ] ) ) ;
76+
77+ if ( currentDocumentId ) {
78+ const doc = previouslySavedDocuments [ currentDocumentId ] ;
79+ editor . handle . openAutoSavedDocument ( BigInt ( doc . details . id ) , doc . details . name , doc . details . isSaved , doc . document , false ) ;
80+ editor . handle . selectDocument ( BigInt ( currentDocumentId ) ) ;
81+ } else {
82+ const len = orderedSavedDocuments . length ;
83+ if ( len > 0 ) {
84+ const doc = orderedSavedDocuments [ len - 1 ] ;
85+ editor . handle . openAutoSavedDocument ( BigInt ( doc . details . id ) , doc . details . name , doc . details . isSaved , doc . document , false ) ;
86+ editor . handle . selectDocument ( BigInt ( doc . details . id ) ) ;
87+ }
88+ }
89+ }
90+
91+ async function loadRestDocuments ( ) {
4892 const previouslySavedDocuments = await get < Record < string , TriggerIndexedDbWriteDocument > > ( "documents" , graphiteStore ) ;
4993 const documentOrder = await get < string [ ] > ( "documents_tab_order" , graphiteStore ) ;
94+ const currentDocumentId = await get < string > ( "current_document_id" , graphiteStore ) ;
5095 if ( ! previouslySavedDocuments || ! documentOrder ) return ;
5196
5297 const orderedSavedDocuments = documentOrder . flatMap ( ( id ) => ( previouslySavedDocuments [ id ] ? [ previouslySavedDocuments [ id ] ] : [ ] ) ) ;
5398
54- orderedSavedDocuments ?. forEach ( async ( doc : TriggerIndexedDbWriteDocument ) => {
55- editor . handle . openAutoSavedDocument ( BigInt ( doc . details . id ) , doc . details . name , doc . details . isSaved , doc . document ) ;
56- } ) ;
99+ if ( currentDocumentId ) {
100+ const currentIndex = orderedSavedDocuments . findIndex ( ( doc ) => doc . details . id === currentDocumentId ) ;
101+ const beforeCurrentIndex = currentIndex - 1 ;
102+ const afterCurrentIndex = currentIndex + 1 ;
103+
104+ for ( let i = beforeCurrentIndex ; i >= 0 ; i -- ) {
105+ const { document, details } = orderedSavedDocuments [ i ] ;
106+ const { id, name, isSaved } = details ;
107+ editor . handle . openAutoSavedDocument ( BigInt ( id ) , name , isSaved , document , true ) ;
108+ }
109+ for ( let i = afterCurrentIndex ; i < orderedSavedDocuments . length ; i ++ ) {
110+ const { document, details } = orderedSavedDocuments [ i ] ;
111+ const { id, name, isSaved } = details ;
112+ editor . handle . openAutoSavedDocument ( BigInt ( id ) , name , isSaved , document , false ) ;
113+ }
114+
115+ editor . handle . selectDocument ( BigInt ( currentDocumentId ) ) ;
116+ } else {
117+ const length = orderedSavedDocuments . length ;
118+
119+ for ( let i = length - 2 ; i >= 0 ; i -- ) {
120+ const { document, details } = orderedSavedDocuments [ i ] ;
121+ const { id, name, isSaved } = details ;
122+ editor . handle . openAutoSavedDocument ( BigInt ( id ) , name , isSaved , document , true ) ;
123+ }
124+
125+ if ( length > 0 ) {
126+ const id = orderedSavedDocuments [ length - 1 ] . details . id ;
127+ editor . handle . selectDocument ( BigInt ( id ) ) ;
128+ }
129+ }
57130 }
58131
59132 // PREFERENCES
@@ -84,12 +157,24 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
84157 editor . subscriptions . subscribeJsMessage ( TriggerIndexedDbRemoveDocument , async ( removeAutoSaveDocument ) => {
85158 await removeDocument ( removeAutoSaveDocument . documentId ) ;
86159 } ) ;
87- editor . subscriptions . subscribeJsMessage ( TriggerLoadAutoSaveDocuments , async ( ) => {
88- await loadDocuments ( ) ;
160+ editor . subscriptions . subscribeJsMessage ( TriggerLoadFirstAutoSaveDocument , async ( ) => {
161+ await loadFirstDocument ( ) ;
162+ } ) ;
163+ editor . subscriptions . subscribeJsMessage ( TriggerLoadRestAutoSaveDocuments , async ( ) => {
164+ await loadRestDocuments ( ) ;
165+ } ) ;
166+ editor . subscriptions . subscribeJsMessage ( TriggerSaveActiveDocument , async ( triggerSaveActiveDocument ) => {
167+ const documentId = String ( triggerSaveActiveDocument . documentId ) ;
168+ const previouslySavedDocuments = await get < Record < string , TriggerIndexedDbWriteDocument > > ( "documents" , graphiteStore ) ;
169+ if ( ! previouslySavedDocuments ) return ;
170+ if ( documentId in previouslySavedDocuments ) {
171+ await storeCurrentDocumentId ( documentId ) ;
172+ }
89173 } ) ;
90174}
91175
92176export async function wipeDocuments ( ) {
93177 await del ( "documents_tab_order" , graphiteStore ) ;
178+ await del ( "current_document_id" , graphiteStore ) ;
94179 await del ( "documents" , graphiteStore ) ;
95180}
0 commit comments