Skip to content
Merged
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
25 changes: 25 additions & 0 deletions src/lib/state/settings.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,31 @@ class Settings {
this.commit();
}

/** A new context goes last, so the ones before it keep their shortcuts. */
contextCreated(existing: { name: string }[], name: string): void {
const order = this.pinnedOrder(existing).filter((n) => n !== name);
this.setContextOrder([...order, name]);
}

/** A renamed context keeps its place, and its shortcut. */
contextRenamed(existing: { name: string }[], from: string, to: string): void {
const order = this.pinnedOrder(existing).filter((n) => n !== to);
this.setContextOrder(order.map((n) => (n === from ? to : n)));
}

/**
* The order as shown now, written out in full. Names of deleted contexts stay, so one
* restored from Trash returns to its old place.
*/
private pinnedOrder(existing: { name: string }[]): string[] {
const order = [...this.contextOrder];
const ranked = new Set(order);
for (const c of this.orderContexts(existing)) {
if (!ranked.has(c.name)) order.push(c.name);
}
return order;
}

orderContexts<T extends { name: string }>(contexts: T[]): T[] {
const rank = new Map(this.contextOrder.map((name, i) => [name, i]));
return [...contexts].sort((a, b) => {
Expand Down
18 changes: 18 additions & 0 deletions src/lib/state/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,21 @@ describe("settings loaded from storage", () => {

/** The blue preset's light value, the first entry of `SURFACE_TINTS`. */
const SURFACE_TINTS_COLOR = "oklch(52% 0.21 266)";

describe("context order", () => {
const named = (...names: string[]) => names.map((name) => ({ name }));

it("puts a new context last, even before any reordering", async () => {
const { settings } = await loadSettings({});
settings.contextCreated(named("Work", "Personal"), "Archive");
const shown = settings.orderContexts(named("Work", "Personal", "Archive"));
expect(shown.map((c) => c.name)).toEqual(["Personal", "Work", "Archive"]);
});

it("keeps a renamed context in its place before any reordering", async () => {
const { settings } = await loadSettings({});
settings.contextRenamed(named("Apple", "Banana", "Cherry"), "Apple", "Zebra");
const shown = settings.orderContexts(named("Zebra", "Banana", "Cherry"));
expect(shown.map((c) => c.name)).toEqual(["Zebra", "Banana", "Cherry"]);
});
});
2 changes: 2 additions & 0 deletions src/lib/state/workspace.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ class Workspace {
toasts.error(checkFolder(`Couldn't create the context “${name}”.`));
return null;
}
settings.contextCreated(this.contexts, created);
await this.refresh();
await this.switchContext(created);
return created;
Expand All @@ -614,6 +615,7 @@ class Workspace {
}

viewState.renameContext(from, renamed);
settings.contextRenamed(this.contexts, from, renamed);
const here = this.activeContext === from;
if (here) this.setContext(renamed);
await this.refresh();
Expand Down