Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/note splits: remember the last focused split when switching between tabs
Adorian Doranfefe54fbe1c9
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.tsindex aa2a51178b..13f697f374 100644--- a/apps/client/src/components/note_context.ts+++ b/apps/client/src/components/note_context.ts@@ -72,6 +72,9 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">/** When true, this tab stays on its current note: navigations to a different note open a new tab instead. Only meaningful on a main context. */pinned = false;+ /** ntxId of the split most recently focused within this tab; re-activating the tab restores it. Only meaningful on a main context. */+ lastActiveNtxId?: string | null;+notePath?: string | null;noteId?: string | null;parentNoteId?: string | null;diff --git a/apps/client/src/components/root_command_executor.ts b/apps/client/src/components/root_command_executor.tsindex 5bb692e9bc..24c372d349 100644--- a/apps/client/src/components/root_command_executor.ts+++ b/apps/client/src/components/root_command_executor.ts@@ -248,7 +248,7 @@ export default class RootCommandExecutor extends Component {const tab = mainNoteContexts[index];if (tab) {- appContext.tabManager.activateNoteContext(tab.ntxId);+ appContext.tabManager.activateTabContext(tab.ntxId);}}diff --git a/apps/client/src/components/tab_manager.ts b/apps/client/src/components/tab_manager.tsindex fea026f656..37b43024de 100644--- a/apps/client/src/components/tab_manager.ts+++ b/apps/client/src/components/tab_manager.ts@@ -397,6 +397,12 @@ export default class TabManager extends Component {this.activeNtxId = ntxId;+ // remember which split is focused within its tab, so re-activating the tab restores it+ const activatedContext = this.noteContexts.find((nc) => nc.ntxId === ntxId);+ if (activatedContext) {+ activatedContext.getMainContext().lastActiveNtxId = ntxId;+ }+if (triggerEvent) {await this.triggerEvent("activeContextChanged", {noteContext: this.getNoteContextById(ntxId)@@ -408,6 +414,21 @@ export default class TabManager extends Component {this.setCurrentNavigationStateToHash();}+ /** Activates a tab, restoring focus to the split that was last focused within it (or its main split). */+ async activateTabContext(mainNtxId: string | null) {+ if (!mainNtxId) {+ return;+ }++ const mainContext = this.noteContexts.find((nc) => nc.ntxId === mainNtxId);+ const remembered = mainContext?.lastActiveNtxId;+ const targetNtxId = remembered && this.noteContexts.some((nc) => nc.ntxId === remembered)+ ? remembered+ : mainNtxId;++ await this.activateNoteContext(targetNtxId);+ }+async removeNoteContext(ntxId: string | null): Promise<boolean> {// removing note context is an async process which can take some time, if users presses CTRL-W quickly, two// close events could interleave which would then lead to attempting to activate already removed context.@@ -561,7 +582,7 @@ export default class TabManager extends Component {const oldIdx = this.mainNoteContexts.findIndex((nc) => nc.ntxId === activeMainNtxId);const newActiveNtxId = this.mainNoteContexts[oldIdx === this.mainNoteContexts.length - 1 ? 0 : oldIdx + 1].ntxId;- await this.activateNoteContext(newActiveNtxId);+ await this.activateTabContext(newActiveNtxId);}async activatePreviousTabCommand() {@@ -571,7 +592,7 @@ export default class TabManager extends Component {const oldIdx = this.mainNoteContexts.findIndex((nc) => nc.ntxId === activeMainNtxId);const newActiveNtxId = this.mainNoteContexts[oldIdx === 0 ? this.mainNoteContexts.length - 1 : oldIdx - 1].ntxId;- await this.activateNoteContext(newActiveNtxId);+ await this.activateTabContext(newActiveNtxId);}async closeActiveTabCommand() {diff --git a/apps/client/src/widgets/tab_row.ts b/apps/client/src/widgets/tab_row.tsindex 0d96843568..943742566d 100644--- a/apps/client/src/widgets/tab_row.ts+++ b/apps/client/src/widgets/tab_row.ts@@ -725,7 +725,8 @@ export default class TabRowWidget extends BasicWidget {this.draggabillies.push(draggabilly);draggabilly.on("staticClick", () => {- appContext.tabManager.activateNoteContext(tabEl.getAttribute("data-ntx-id"));+ // restore the tab's last-focused split rather than always jumping to its main split+ appContext.tabManager.activateTabContext(tabEl.getAttribute("data-ntx-id"));});draggabilly.on("dragStart", () => {@@ -929,12 +930,14 @@ export default class TabRowWidget extends BasicWidget {$tab.find(".note-tab-wrapper").removeAttr("style");}- await this.updateTabTitle($tab, mainContext);+ const subContexts = mainContext.getSubContexts();+ const focusedNtxId = this.getFocusedNtxId(mainContext, subContexts);++ await this.updateTabTitle($tab, subContexts, focusedNtxId);- // The icon and type classes follow the focused split when this tab is active; otherwise they- // fall back to its main (first) split.- const activeNtxId = appContext.tabManager.activeNtxId;- const displayContext = mainContext.getSubContexts().find((ctx) => ctx.ntxId === activeNtxId) ?? mainContext;+ // The icon and type classes follow the tab's focused split (remembered across activations),+ // falling back to its main (first) split.+ const displayContext = subContexts.find((ctx) => ctx.ntxId === focusedNtxId) ?? mainContext;const note = displayContext.note;if (note) {$tab.addClass(note.getCssClass());@@ -955,14 +958,11 @@ export default class TabRowWidget extends BasicWidget {* Builds the tab title from every split in the tab, e.g. "Note A • Note B • Note C", with the* currently focused split emphasized. The full text is also set as the tooltip.*/- async updateTabTitle($tab: JQuery<HTMLElement>, mainContext: NoteContext) {- const activeNtxId = appContext.tabManager.activeNtxId;- const subContexts = mainContext.getSubContexts();-+ async updateTabTitle($tab: JQuery<HTMLElement>, subContexts: NoteContext[], focusedNtxId: string | null) {const splits = await Promise.all(subContexts.map(async (ctx) => ({title: ctx.note ? await ctx.getNavigationTitle() : null,- active: !!ctx.ntxId && ctx.ntxId === activeNtxId+ active: !!ctx.ntxId && ctx.ntxId === focusedNtxId})));@@ -983,6 +983,15 @@ export default class TabRowWidget extends BasicWidget {});}+ /** The split a tab currently represents: its remembered last-focused split (if still open), else its main split. */+ getFocusedNtxId(mainContext: NoteContext, subContexts: NoteContext[]): string | null {+ const remembered = mainContext.lastActiveNtxId;+ if (remembered && subContexts.some((ctx) => ctx.ntxId === remembered)) {+ return remembered;+ }+ return mainContext.ntxId;+ }+/** Rebuilds a tab in place (title, icon, classes) without scrolling it into view. */refreshTab(ntxId: string | null | undefined) {if (!ntxId) {