Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make document auto-saving include the active tab index instead of just restoring the last tab #2194

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions editor/src/messages/portfolio/portfolio_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,8 @@ impl PortfolioMessageHandler {
responses.add(PortfolioMessage::LoadDocumentResources { document_id });
responses.add(PortfolioMessage::UpdateDocumentWidgets);
responses.add(ToolMessage::InitTools);
responses.add(NodeGraphMessage::Init);
responses.add(NavigationMessage::CanvasPan { delta: (0., 0.).into() });
responses.add(PropertiesPanelMessage::Clear);
responses.add(NodeGraphMessage::UpdateNewNodeGraph);
}

/// Returns an iterator over the open documents in order.
Expand Down
24 changes: 24 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 storeCurrentDocumentByID(String(documentId));
}

async function storeCurrentDocumentByID(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 storeCurrentDocumentByID(autoSaveDocument.details.id);
}

async function removeDocument(id: string) {
Expand All @@ -41,19 +53,30 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
graphiteStore,
);

const documentCount = getFromStore(portfolio).documents.length;
if (documentCount > 0) {
await storeCurrentDocumentIndex();
} else {
await del("current_document_id", graphiteStore);
}

await storeDocumentOrder();
}

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 +114,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);
}
Loading