feat(window): record openNoteContents of recently closed windows

This commit is contained in:
SiriusXT 2025-12-29 14:33:34 +08:00
parent 7740154bdc
commit 3353d4f436
3 changed files with 72 additions and 13 deletions

View File

@ -41,9 +41,6 @@ export default class TabManager extends Component {
this.recentlyClosedTabs = [];
this.tabsUpdate = new SpacedUpdate(async () => {
if (!appContext.isMainWindow) {
return;
}
if (options.is("databaseReadonly")) {
return;
}
@ -52,9 +49,21 @@ export default class TabManager extends Component {
.map((nc) => nc.getPojoState())
.filter((t) => !!t);
await server.put("options", {
openNoteContexts: JSON.stringify(openNoteContexts)
});
// Update the current windows openNoteContexts in options
const savedWindows = options.getJson("openNoteContexts");
const win = savedWindows.find(w => w.windowId === appContext.windowId);
if (win) {
win.contexts = openNoteContexts;
} else {
savedWindows.push({
windowId: appContext.windowId,
createdAt: Date.now(),
closedAt: null,
contexts: savedWindows
});
}
await options.save("openNoteContexts", JSON.stringify(savedWindows));
});
appContext.addBeforeUnloadListener(this);
@ -69,8 +78,13 @@ export default class TabManager extends Component {
}
async loadTabs() {
// Get the current windows openNoteContexts
const savedWindows = options.getJson("openNoteContexts");
const currentWin = savedWindows.find(w => w.windowId === appContext.windowId);
const openNoteContexts = currentWin ? currentWin.contexts : undefined;
try {
const noteContextsToOpen = (appContext.isMainWindow && options.getJson("openNoteContexts")) || [];
const noteContextsToOpen = openNoteContexts || [];
// preload all notes at once
await froca.getNotes([...noteContextsToOpen.flatMap((tab: NoteContextState) =>
@ -119,6 +133,30 @@ export default class TabManager extends Component {
}
});
// Save window contents
if (currentWin) {
currentWin.createdAt = Date.now(),
currentWin.closedAt = null;
currentWin.contexts = filteredNoteContexts;
} else {
// Filter out the oldest entry (excluding the main window)
if (savedWindows.length >= 10) {
const candidates = savedWindows.filter(w => w.windowId !== "main");
const oldestIndex = candidates.reduce((a, b) =>
a.createdAt < b.createdAt ? a : b
);
savedWindows.splice(savedWindows.indexOf(oldestIndex), 1);
}
savedWindows.push({
windowId: appContext.windowId,
createdAt: Date.now(),
closedAt: null,
contexts: filteredNoteContexts
});
}
await options.save("openNoteContexts", JSON.stringify(savedWindows));
// if there's a notePath in the URL, make sure it's open and active
// (useful, for e.g., opening clipped notes from clipper or opening link in an extra window)
if (parsedFromUrl.notePath) {

View File

@ -45,8 +45,15 @@ async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts =
"openNoteContexts",
JSON.stringify([
{
notePath: "root",
active: true
windowId: "main",
createdAt: 0,
closedAt: null,
contexts: [
{
notePath: "root",
active: true
}
]
}
]),
false
@ -251,8 +258,15 @@ function initStartupOptions() {
"openNoteContexts",
JSON.stringify([
{
notePath: process.env.TRILIUM_START_NOTE_ID || "root",
active: true
windowId: "main",
createdAt: 0,
closedAt: null,
contexts: [
{
notePath: process.env.TRILIUM_START_NOTE_ID || "root",
active: true
}
]
}
])
);

View File

@ -147,8 +147,15 @@ async function createInitialDatabase(skipDemoDb?: boolean) {
"openNoteContexts",
JSON.stringify([
{
notePath: startNoteId,
active: true
windowId: "main",
createdAt: 0,
closedAt: null,
contexts: [
{
notePath: startNoteId,
active: true
}
]
}
])
);