@@ -16,6 +16,25 @@ function createDocumentInfo(id: bigint, name: string, isSaved: boolean): Persist
1616 return { id, name, is_saved : isSaved } ;
1717}
1818
19+ // Reorder document entries to match the given ID ordering, appending any unmentioned entries at the end
20+ function reorderDocuments ( documents : PersistedDocumentInfo [ ] , orderedIds : bigint [ ] ) : PersistedDocumentInfo [ ] {
21+ const byId = new Map ( documents . map ( ( entry ) => [ entry . id , entry ] ) ) ;
22+ const reordered : PersistedDocumentInfo [ ] = [ ] ;
23+
24+ orderedIds . forEach ( ( id ) => {
25+ const existing = byId . get ( id ) ;
26+ if ( existing ) {
27+ reordered . push ( existing ) ;
28+ byId . delete ( id ) ;
29+ }
30+ } ) ;
31+
32+ // Append any entries not yet present in the portfolio (e.g. documents still loading at startup)
33+ byId . forEach ( ( entry ) => reordered . push ( entry ) ) ;
34+
35+ return reordered ;
36+ }
37+
1938// ====================================
2039// State-based persistence (new format)
2140// ====================================
@@ -26,22 +45,7 @@ export async function storeDocumentTabOrder(portfolio: PortfolioStore) {
2645
2746 await databaseUpdate < PersistedState > ( "state" , ( old ) => {
2847 const state = old || emptyPersistedState ( ) ;
29-
30- // Reorder existing document entries to match the portfolio's tab order, preserving metadata
31- const byId = new Map ( state . documents . map ( ( entry ) => [ entry . id , entry ] ) ) ;
32- const reordered : PersistedDocumentInfo [ ] = [ ] ;
33- orderedIds . forEach ( ( id ) => {
34- const existing = byId . get ( id ) ;
35- if ( existing ) {
36- reordered . push ( existing ) ;
37- byId . delete ( id ) ;
38- }
39- } ) ;
40-
41- // Append any entries not yet present in the portfolio (e.g. documents still loading at startup)
42- byId . forEach ( ( entry ) => reordered . push ( entry ) ) ;
43-
44- return { ...state , documents : reordered } ;
48+ return { ...state , documents : reorderDocuments ( state . documents , orderedIds ) } ;
4549 } ) ;
4650}
4751
@@ -71,23 +75,9 @@ export async function storeDocument(autoSaveDocument: MessageBody<"TriggerPersis
7175 state . documents . push ( entry ) ;
7276 }
7377
74- // Reorder to match the portfolio's tab order
75- const byId = new Map ( state . documents . map ( ( doc ) => [ doc . id , doc ] ) ) ;
76- const reordered : PersistedDocumentInfo [ ] = [ ] ;
77- orderedIds . forEach ( ( id ) => {
78- const existing = byId . get ( id ) ;
79- if ( existing ) {
80- reordered . push ( existing ) ;
81- byId . delete ( id ) ;
82- }
83- } ) ;
84-
85- // Append any entries not yet present in the portfolio (e.g. documents still loading at startup)
86- byId . forEach ( ( entry ) => reordered . push ( entry ) ) ;
87-
8878 // eslint-disable-next-line camelcase
8979 state . current_document = documentId ;
90- state . documents = reordered ;
80+ state . documents = reorderDocuments ( state . documents , orderedIds ) ;
9181 return state ;
9282 } ) ;
9383}
@@ -131,15 +121,13 @@ export async function loadDocuments(editor: EditorWrapper) {
131121 const currentId = state . current_document ;
132122 const currentEntry = currentId !== undefined ? state . documents . find ( ( doc ) => doc . id === currentId ) : undefined ;
133123 const current = currentEntry || state . documents [ state . documents . length - 1 ] ;
134- const currentIndex = state . documents . indexOf ( current ) ;
135124
136- // Open documents in order around the current document, placing earlier ones before it and later ones after
137- state . documents . forEach ( ( entry , index ) => {
125+ // Open all documents in persisted tab order, then select the current one
126+ state . documents . forEach ( ( entry ) => {
138127 const content = documentContents [ String ( entry . id ) ] ;
139128 if ( content === undefined ) return ;
140129
141- const toFront = index < currentIndex ;
142- editor . openAutoSavedDocument ( entry . id , entry . name , entry . is_saved , content , toFront ) ;
130+ editor . openAutoSavedDocument ( entry . id , entry . name , entry . is_saved , content , false ) ;
143131 } ) ;
144132
145133 editor . selectDocument ( current . id ) ;
@@ -252,9 +240,9 @@ async function migrateToNewFormat() {
252240 let name = "" ;
253241 if ( "name" in details && typeof details . name === "string" ) name = details . name ;
254242
255- const status = savedStatusFromUnknown ( details ) ;
243+ const isSaved = extractIsSavedFromUnknown ( details ) ;
256244
257- newDocumentInfos . push ( createDocumentInfo ( id , name , status . isSaved ) ) ;
245+ newDocumentInfos . push ( createDocumentInfo ( id , name , isSaved ) ) ;
258246 } ) ;
259247 }
260248
@@ -271,20 +259,16 @@ async function migrateToNewFormat() {
271259}
272260
273261// TODO: Eventually remove this document upgrade code
274- function savedStatusFromUnknown ( details : unknown ) : { isSaved : boolean ; isAutoSaved : boolean } {
275- if ( typeof details !== "object" || details === null ) return { isSaved : false , isAutoSaved : false } ;
262+ function extractIsSavedFromUnknown ( details : unknown ) : boolean {
263+ if ( typeof details !== "object" || details === null ) return false ;
276264
277265 // Old camelCase format
278- if ( "isSaved" in details && "isAutoSaved" in details ) {
279- return { isSaved : Boolean ( details . isSaved ) , isAutoSaved : Boolean ( details . isAutoSaved ) } ;
280- }
266+ if ( "isSaved" in details ) return Boolean ( details . isSaved ) ;
281267
282268 // New snake_case format
283- if ( "is_saved" in details && "is_auto_saved" in details ) {
284- return { isSaved : Boolean ( details . is_saved ) , isAutoSaved : Boolean ( details . is_auto_saved ) } ;
285- }
269+ if ( "is_saved" in details ) return Boolean ( details . is_saved ) ;
286270
287- return { isSaved : false , isAutoSaved : false } ;
271+ return false ;
288272}
289273
290274// =================
0 commit comments