Skip to content

Build your personal knowledge base with Trilium Notes

feat(tabs): open link/middle-click tabs next to the current tab

Elian Doran64fef95f1917
diff --git a/apps/client/src/components/tab_manager.spec.ts b/apps/client/src/components/tab_manager.spec.ts
new file mode 100644
index 0000000000..2efe84da33
--- /dev/null
+++ b/apps/client/src/components/tab_manager.spec.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from "vitest";
+
+import TabManager from "./tab_manager.js";
+
+describe("TabManager tab placement", () => {
+ it("opens a blank tab at the end of the row, regardless of which tab is active", async () => {
+ const tm = new TabManager();
+ const [a, b, c] = await openEmptyTabs(tm, 3);
+ tm.activeNtxId = b.ntxId; // active tab sits in the middle
+
+ const blank = await tm.openEmptyTab();
+
+ expect(ntxOrder(tm)).toEqual([a.ntxId, b.ntxId, c.ntxId, blank.ntxId]);
+ });
+
+ it("opens a tab spawned from a link right after the active tab", async () => {
+ const tm = new TabManager();
+ const [a, b, c] = await openEmptyTabs(tm, 3);
+ tm.activeNtxId = b.ntxId; // active tab sits in the middle
+
+ // A link / middle-click open goes through openContextWithNote. A null notePath keeps the
+ // test focused on placement — the position is decided before any note loads.
+ const fromLink = await tm.openContextWithNote(null, { placement: "afterCurrent" });
+
+ expect(ntxOrder(tm)).toEqual([a.ntxId, b.ntxId, fromLink.ntxId, c.ntxId]);
+ });
+
+ it("inserts the new tab after the active tab and all of its splits", async () => {
+ const tm = new TabManager();
+ const [a] = await openEmptyTabs(tm, 1);
+ // tab "a" gets two extra splits, then a plain tab "b" follows it
+ const aSplit1 = await tm.openEmptyTab(null, "root", a.ntxId);
+ const aSplit2 = await tm.openEmptyTab(null, "root", a.ntxId);
+ const b = await tm.openEmptyTab();
+ tm.activeNtxId = a.ntxId; // the split tab is active
+
+ const fromLink = await tm.openContextWithNote(null, { placement: "afterCurrent" });
+
+ // the new tab lands after the whole "a" group (main + both splits), before "b"
+ expect(tm.children.map((nc) => nc.ntxId)).toEqual([
+ a.ntxId, aSplit1.ntxId, aSplit2.ntxId, fromLink.ntxId, b.ntxId
+ ]);
+ });
+
+ it("never inserts an unpinned tab inside the pinned group", async () => {
+ const tm = new TabManager();
+ const p1 = await tm.openEmptyTab(null, "root", null, true); // pinned
+ const p2 = await tm.openEmptyTab(null, "root", null, true); // pinned
+ const a = await tm.openEmptyTab();
+ tm.activeNtxId = p1.ntxId; // the first pinned tab is active
+
+ const fromLink = await tm.openContextWithNote(null, { placement: "afterCurrent" });
+
+ // inserting right after p1 would split the pinned group, so it clamps past the group
+ expect(ntxOrder(tm)).toEqual([p1.ntxId, p2.ntxId, fromLink.ntxId, a.ntxId]);
+ });
+});
+
+function openEmptyTabs(tm: TabManager, count: number) {
+ return Promise.all(Array.from({ length: count }, () => tm.openEmptyTab()));
+}
+
+function ntxOrder(tm: TabManager) {
+ return tm.mainNoteContexts.map((nc) => nc.ntxId);
+}
diff --git a/apps/client/src/components/tab_manager.ts b/apps/client/src/components/tab_manager.ts
index aedbce2d44..aec9148515 100644
--- a/apps/client/src/components/tab_manager.ts
+++ b/apps/client/src/components/tab_manager.ts
@@ -12,6 +12,9 @@ import { partitionPinnedFirst } from "../services/tab_pinning.js";
import type { EventData } from "./app_context.js";
import type FNote from "../entities/fnote.js";
+/** Where a newly created tab is inserted in the row: appended to the end, or right after the active tab. */
+type TabPlacement = "end" | "afterCurrent";
+
interface TabState {
contexts: NoteContext[];
position: number;
@@ -272,7 +275,8 @@ export default class TabManager extends Component {
ntxId: string | null = null,
hoistedNoteId: string = "root",
mainNtxId: string | null = null,
- pinned: boolean = false
+ pinned: boolean = false,
+ placement: TabPlacement = "end"
): Promise<NoteContext> {
const noteContext = new NoteContext(ntxId, hoistedNoteId, mainNtxId);
// set before setEmpty/newNoteContextCreated so the tab renders in its pinned state from the start
@@ -286,13 +290,55 @@ export default class TabManager extends Component {
return existingNoteContext;
}
- this.child(noteContext);
+ noteContext.setParent(this);
+ this.children.splice(this.getNewTabInsertionIndex(placement), 0, noteContext);
await this.triggerEvent("newNoteContextCreated", { noteContext });
return noteContext;
}
+ /**
+ * Index in the flat `children` array at which a newly created (unpinned) tab should be inserted.
+ * `"end"` appends; `"afterCurrent"` inserts right after the active tab and all of its splits,
+ * clamped so an unpinned tab never lands inside the pinned group (which `reorderPinnedFirst`
+ * keeps grouped at the front of the row).
+ */
+ private getNewTabInsertionIndex(placement: TabPlacement): number {
+ if (placement === "end") {
+ return this.children.length;
+ }
+
+ const activeMainNtxId = this.getActiveMainContext()?.ntxId;
+ const activeIndex = activeMainNtxId
+ ? this.children.findIndex((nc) => nc.ntxId === activeMainNtxId)
+ : -1;
+
+ if (activeIndex === -1) {
+ return this.children.length;
+ }
+
+ // Skip past the active tab's main context and all of its splits.
+ let index = activeIndex + 1;
+ while (index < this.children.length && this.children[index].mainNtxId === activeMainNtxId) {
+ index++;
+ }
+
+ // Pinned tabs stay grouped at the front, so never insert the new (unpinned) tab inside them.
+ const firstUnpinnedIndex = this.children.findIndex((nc) => !this.isPartOfPinnedTab(nc));
+ const minIndex = firstUnpinnedIndex === -1 ? this.children.length : firstUnpinnedIndex;
+
+ return Math.max(index, minIndex);
+ }
+
+ private isPartOfPinnedTab(noteContext: NoteContext): boolean {
+ const mainContext = noteContext.mainNtxId
+ ? this.children.find((nc) => nc.ntxId === noteContext.mainNtxId)
+ : noteContext;
+
+ return !!mainContext?.pinned;
+ }
+
async openInNewTab(targetNoteId: string, hoistedNoteId: string | null = null, activate: boolean = false) {
const noteContext = await this.openEmptyTab(null, hoistedNoteId || this.getActiveContext()?.hoistedNoteId);
@@ -323,6 +369,7 @@ export default class TabManager extends Component {
mainNtxId?: string | null;
hoistedNoteId?: string | null;
viewScope?: Record<string, any> | null;
+ placement?: TabPlacement | null;
} = {}
): Promise<NoteContext> {
const noteContext = this.getActiveContext();
@@ -350,6 +397,8 @@ export default class TabManager extends Component {
hoistedNoteId?: string | null;
viewScope?: Record<string, any> | null;
pinned?: boolean | null;
+ /** Where the new tab is placed in the row. Defaults to `"end"`; link/middle-click opens pass `"afterCurrent"`. */
+ placement?: TabPlacement | null;
} = {}
): Promise<NoteContext> {
const activate = !!opts.activate;
@@ -358,7 +407,13 @@ export default class TabManager extends Component {
const hoistedNoteId = opts.hoistedNoteId || "root";
const viewScope = opts.viewScope || { viewMode: "default" };
- const noteContext = await this.openEmptyTab(ntxId, hoistedNoteId, mainNtxId, !!opts.pinned);
+ const noteContext = await this.openEmptyTab(
+ ntxId,
+ hoistedNoteId,
+ mainNtxId,
+ !!opts.pinned,
+ opts.placement ?? "end"
+ );
if (notePath) {
await noteContext.setNote(notePath, {
// if activate is false, then send normal noteSwitched event
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts
index 6bbc638adf..b664e7e54f 100644
--- a/apps/client/src/menus/tree_context_menu.ts
+++ b/apps/client/src/menus/tree_context_menu.ts
@@ -342,7 +342,7 @@ export async function handleTreeContextMenuSelect(
resolved.onBeforeCommand?.();
if (command === "openInTab") {
- appContext.tabManager.openTabWithNoteWithHoisting(notePath);
+ appContext.tabManager.openTabWithNoteWithHoisting(notePath, { placement: "afterCurrent" });
} else if (command === "insertNoteAfter") {
const parentNotePath = parentNotePathOf(notePath);
const parentNote = await froca.getNote(branch.parentNoteId);
diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.ts
index 2462e4ebdc..2dd242977f 100644
--- a/apps/client/src/services/link.ts
+++ b/apps/client/src/services/link.ts
@@ -329,7 +329,8 @@ export function goToLinkExt(evt: MouseEvent | JQuery.ClickEvent | JQuery.MouseDo
} else if (openInNewTab) {
appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
activate: activate ? true : targetIsBlank,
- viewScope
+ viewScope,
+ placement: "afterCurrent"
});
} else if (isLeftClick) {
openInCurrentNoteContext(evt, notePath, viewScope);
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts
index f4b68f08a8..e1a3c9dc8c 100644
--- a/apps/client/src/widgets/note_tree.ts
+++ b/apps/client/src/widgets/note_tree.ts
@@ -244,7 +244,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
e.preventDefault();
appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
- activate: !!e.shiftKey
+ activate: !!e.shiftKey,
+ placement: "afterCurrent"
});
}
}
@@ -410,7 +411,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
} else if (ctrlKey) {
const notePath = treeService.getNotePath(node);
appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
- activate: !!event.shiftKey
+ activate: !!event.shiftKey,
+ placement: "afterCurrent"
});
} else if (event.altKey) {
node.setSelected(!node.isSelected());
diff --git a/apps/client/src/widgets/tab_row.ts b/apps/client/src/widgets/tab_row.ts
index 38dacbb2d6..8d2e954c44 100644
--- a/apps/client/src/widgets/tab_row.ts
+++ b/apps/client/src/widgets/tab_row.ts
@@ -614,6 +614,9 @@ export default class TabRowWidget extends BasicWidget {
setTimeout(() => $tab.removeClass("note-tab-was-just-added"), 500);
this.$containerAnchor.before($tab);
+ // The model may insert this tab before the end (e.g. opened from a link, after the
+ // active tab), so mirror the model order in the DOM rather than leaving it appended.
+ this.syncTabOrder();
this.setVisibility();
this.setTabCloseEvent($tab);
this.updateTitle($tab, t("tab_row.new_tab"));