Skip to content

Commit

Permalink
Make last active tab load before others
Browse files Browse the repository at this point in the history
  • Loading branch information
mTvare6 committed Jan 18, 2025
1 parent b0f039b commit 0c2d2af
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
3 changes: 2 additions & 1 deletion editor/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Dispatcher {
Message::NoOp => {}
Message::Init => {
// Load persistent data from the browser database
queue.add(FrontendMessage::TriggerLoadAutoSaveDocuments);
queue.add(FrontendMessage::TriggerLoadFirstAutoSaveDocument);
queue.add(FrontendMessage::TriggerLoadPreferences);

// Display the menu bar at the top of the window
Expand All @@ -153,6 +153,7 @@ impl Dispatcher {
node_descriptions: document_node_definitions::collect_node_descriptions(),
node_types: document_node_definitions::collect_node_types(),
});
queue.add(FrontendMessage::TriggerLoadRestAutoSaveDocuments);
}
Message::Batched(messages) => {
messages.iter().for_each(|message| self.handle_message(message.to_owned(), false));
Expand Down
3 changes: 2 additions & 1 deletion editor/src/messages/frontend/frontend_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ pub enum FrontendMessage {
document: String,
details: FrontendDocumentDetails,
},
TriggerLoadAutoSaveDocuments,
TriggerLoadFirstAutoSaveDocument,
TriggerLoadRestAutoSaveDocuments,
TriggerLoadPreferences,
TriggerOpenDocument,
TriggerPaste,
Expand Down
56 changes: 46 additions & 10 deletions frontend/src/io-managers/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { get as getFromStore } from "svelte/store";

import { type PortfolioState } from "@graphite/state-providers/portfolio";
import { type Editor } from "@graphite/wasm-communication/editor";
import { TriggerIndexedDbWriteDocument, TriggerIndexedDbRemoveDocument, TriggerSavePreferences, TriggerLoadAutoSaveDocuments, TriggerLoadPreferences } from "@graphite/wasm-communication/messages";
import {
TriggerIndexedDbWriteDocument,
TriggerIndexedDbRemoveDocument,
TriggerSavePreferences,
TriggerLoadFirstAutoSaveDocument,
TriggerLoadRestAutoSaveDocuments,
TriggerLoadPreferences,
} from "@graphite/wasm-communication/messages";

const graphiteStore = createStore("graphite", "store");

Expand Down Expand Up @@ -63,7 +70,7 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
await storeDocumentOrder();
}

async function loadDocuments() {
async function loadFirstDocument() {
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);
Expand All @@ -74,20 +81,46 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
if (currentDocumentId) {
const doc = previouslySavedDocuments[currentDocumentId];
editor.handle.openAutoSavedDocument(BigInt(doc.details.id), doc.details.name, doc.details.isSaved, doc.document, false);
const currentIndex = orderedSavedDocuments.findIndex(doc => doc.details.id === currentDocumentId);
for (let i = currentIndex-1; i >= 0; i--) {
editor.handle.selectDocument(BigInt(currentDocumentId));
} else {
const len = orderedSavedDocuments.length;
if (len > 0) {
const doc = orderedSavedDocuments[len - 1];
editor.handle.openAutoSavedDocument(BigInt(doc.details.id), doc.details.name, doc.details.isSaved, doc.document, false);
editor.handle.selectDocument(BigInt(doc.details.id));
}
}
}

async function loadRestDocuments() {
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]] : []));

if (currentDocumentId) {
const currentIndex = orderedSavedDocuments.findIndex((doc) => doc.details.id === currentDocumentId);
for (let i = currentIndex - 1; i >= 0; i--) {
const doc = orderedSavedDocuments[i];
editor.handle.openAutoSavedDocument(BigInt(doc.details.id), doc.details.name, doc.details.isSaved, doc.document, true);
}
for (let i = currentIndex+1; i < orderedSavedDocuments.length; i++) {
for (let i = currentIndex + 1; i < orderedSavedDocuments.length; i++) {
const doc = orderedSavedDocuments[i];
editor.handle.openAutoSavedDocument(BigInt(doc.details.id), doc.details.name, doc.details.isSaved, doc.document, false);
}
editor.handle.selectDocument(BigInt(currentDocumentId));
} else {
orderedSavedDocuments?.forEach(async (doc: TriggerIndexedDbWriteDocument) => {
editor.handle.openAutoSavedDocument(BigInt(doc.details.id), doc.details.name, doc.details.isSaved, doc.document, false);
});
const len = orderedSavedDocuments.length;
for (let i = len - 2; i >= 0; i--) {
const doc = orderedSavedDocuments[i];
editor.handle.openAutoSavedDocument(BigInt(doc.details.id), doc.details.name, doc.details.isSaved, doc.document, true);
}
if (len > 0) {
const doc = orderedSavedDocuments[len - 1];
editor.handle.selectDocument(BigInt(doc.details.id));
}
}
}

Expand Down Expand Up @@ -119,8 +152,11 @@ export function createPersistenceManager(editor: Editor, portfolio: PortfolioSta
editor.subscriptions.subscribeJsMessage(TriggerIndexedDbRemoveDocument, async (removeAutoSaveDocument) => {
await removeDocument(removeAutoSaveDocument.documentId);
});
editor.subscriptions.subscribeJsMessage(TriggerLoadAutoSaveDocuments, async () => {
await loadDocuments();
editor.subscriptions.subscribeJsMessage(TriggerLoadFirstAutoSaveDocument, async () => {
await loadFirstDocument();
});
editor.subscriptions.subscribeJsMessage(TriggerLoadRestAutoSaveDocuments, async () => {
await loadRestDocuments();
});
}

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/wasm-communication/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,8 @@ export class UpdateMouseCursor extends JsMessage {
readonly cursor!: MouseCursorIcon;
}

export class TriggerLoadAutoSaveDocuments extends JsMessage {}
export class TriggerLoadFirstAutoSaveDocument extends JsMessage {}
export class TriggerLoadRestAutoSaveDocuments extends JsMessage {}

export class TriggerLoadPreferences extends JsMessage {}

Expand Down Expand Up @@ -1583,7 +1584,8 @@ export const messageMakers: Record<string, MessageMaker> = {
TriggerImport,
TriggerIndexedDbRemoveDocument,
TriggerIndexedDbWriteDocument,
TriggerLoadAutoSaveDocuments,
TriggerLoadFirstAutoSaveDocument,
TriggerLoadRestAutoSaveDocuments,
TriggerLoadPreferences,
TriggerOpenDocument,
TriggerPaste,
Expand Down

0 comments on commit 0c2d2af

Please sign in to comment.