Skip to content

Commit

Permalink
Fixes last tab being opened instead of last active tab
Browse files Browse the repository at this point in the history
  • Loading branch information
mTvare6 committed Jan 13, 2025
1 parent 3582126 commit addf7e8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions frontend/src/io-managers/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
await set("documents_tab_order", documentOrder, graphiteStore);
}

async function storeCurrentDocumentIndex() {
const documentIndex = getFromStore(portfolio).activeDocumentIndex;
const documentId = getFromStore(portfolio).documents[documentIndex].id;

await storeDocumentDocumentByID(String(documentId));
}

async function storeDocumentDocumentByID(documentId: string) {
await set("current_document_id", String(documentId), graphiteStore);
}

async function storeDocument(autoSaveDocument: TriggerIndexedDbWriteDocument) {
await update<Record<string, TriggerIndexedDbWriteDocument>>(
"documents",
Expand All @@ -28,6 +39,7 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
);

await storeDocumentOrder();
await storeDocumentDocumentByID(autoSaveDocument.details.id);
}

async function removeDocument(id: string) {
Expand All @@ -42,18 +54,23 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
);

await storeDocumentOrder();
await storeCurrentDocumentIndex();
}

async function loadDocuments() {
const previouslySavedDocuments = await get<Record<string, TriggerIndexedDbWriteDocument>>("documents", graphiteStore);
const documentOrder = await get<string[]>("documents_tab_order", graphiteStore);
const currentDocumentId = await get<string>("current_document_id", graphiteStore);
if (!previouslySavedDocuments || !documentOrder) return;

const orderedSavedDocuments = documentOrder.flatMap((id) => (previouslySavedDocuments[id] ? [previouslySavedDocuments[id]] : []));

orderedSavedDocuments?.forEach(async (doc: TriggerIndexedDbWriteDocument) => {
editor.handle.openAutoSavedDocument(BigInt(doc.details.id), doc.details.name, doc.details.isSaved, doc.document);
});
if (currentDocumentId) {
editor.handle.selectDocument(BigInt(currentDocumentId));
}
}

// PREFERENCES
Expand Down Expand Up @@ -91,5 +108,6 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta

export async function wipeDocuments() {
await del("documents_tab_order", graphiteStore);
await del("current_document_id", graphiteStore);
await del("documents", graphiteStore);
}

0 comments on commit addf7e8

Please sign in to comment.