@@ -119,62 +119,30 @@ export async function removeDocument(id: string, portfolio: PortfolioStore) {
119119 } ) ;
120120}
121121
122- export async function loadFirstDocument ( editor : EditorWrapper ) {
122+ export async function loadDocuments ( editor : EditorWrapper ) {
123123 await migrateToNewFormat ( ) ;
124124 await garbageCollectDocuments ( ) ;
125125
126126 const state = await databaseGet < PersistedState > ( "state" ) ;
127127 const documentContents = await databaseGet < Record < string , string > > ( "documents" ) ;
128128 if ( ! state || ! documentContents || state . documents . length === 0 ) return ;
129129
130- // Find and open the current document (or fall back to the last document in the list)
130+ // Find the current document (or fall back to the last document in the list)
131131 const currentId = state . current_document ;
132132 const currentEntry = currentId !== undefined ? state . documents . find ( ( doc ) => doc . id === currentId ) : undefined ;
133- const entryToOpen = currentEntry || state . documents [ state . documents . length - 1 ] ;
134-
135- const content = documentContents [ String ( entryToOpen . id ) ] ;
136- if ( content === undefined ) return ;
137-
138- editor . openAutoSavedDocument ( entryToOpen . id , entryToOpen . name , entryToOpen . is_saved , content , false ) ;
139- editor . selectDocument ( entryToOpen . id ) ;
140- }
141-
142- export async function loadRestDocuments ( editor : EditorWrapper ) {
143- const state = await databaseGet < PersistedState > ( "state" ) ;
144- const documentContents = await databaseGet < Record < string , string > > ( "documents" ) ;
145- if ( ! state || ! documentContents || state . documents . length === 0 ) return ;
146-
147- const currentId = state . current_document ;
148- const currentIndex = currentId !== undefined ? state . documents . findIndex ( ( doc ) => doc . id === currentId ) : - 1 ;
133+ const current = currentEntry || state . documents [ state . documents . length - 1 ] ;
134+ const currentIndex = state . documents . indexOf ( current ) ;
149135
150136 // Open documents in order around the current document, placing earlier ones before it and later ones after
151- if ( currentIndex !== - 1 && currentId !== undefined ) {
152- for ( let i = currentIndex - 1 ; i >= 0 ; i -- ) {
153- const entry = state . documents [ i ] ;
154- const content = documentContents [ String ( entry . id ) ] ;
155- if ( content !== undefined ) editor . openAutoSavedDocument ( entry . id , entry . name , entry . is_saved , content , true ) ;
156- }
157-
158- for ( let i = currentIndex + 1 ; i < state . documents . length ; i ++ ) {
159- const entry = state . documents [ i ] ;
160- const content = documentContents [ String ( entry . id ) ] ;
161- if ( content !== undefined ) editor . openAutoSavedDocument ( entry . id , entry . name , entry . is_saved , content , false ) ;
162- }
137+ state . documents . forEach ( ( entry , index ) => {
138+ const content = documentContents [ String ( entry . id ) ] ;
139+ if ( content === undefined ) return ;
163140
164- editor . selectDocument ( currentId ) ;
165- }
166- // No valid current document: open all remaining documents and select the last one
167- else {
168- const length = state . documents . length ;
169-
170- for ( let i = length - 2 ; i >= 0 ; i -- ) {
171- const entry = state . documents [ i ] ;
172- const content = documentContents [ String ( entry . id ) ] ;
173- if ( content !== undefined ) editor . openAutoSavedDocument ( entry . id , entry . name , entry . is_saved , content , true ) ;
174- }
141+ const toFront = index < currentIndex ;
142+ editor . openAutoSavedDocument ( entry . id , entry . name , entry . is_saved , content , toFront ) ;
143+ } ) ;
175144
176- if ( length > 0 ) editor . selectDocument ( state . documents [ length - 1 ] . id ) ;
177- }
145+ editor . selectDocument ( current . id ) ;
178146}
179147
180148export async function saveActiveDocument ( documentId : bigint ) {
@@ -244,7 +212,7 @@ async function wipeOldFormat() {
244212}
245213
246214// TODO: Eventually remove this document upgrade code
247- export async function migrateToNewFormat ( ) {
215+ async function migrateToNewFormat ( ) {
248216 // Detect the old format by checking for the existence of the "documents_tab_order" key
249217 const oldTabOrder = await databaseGet < string [ ] > ( "documents_tab_order" ) ;
250218 if ( oldTabOrder === undefined ) return ;
0 commit comments