Skip to content

Build your personal knowledge base with Trilium Notes

fix(tabs): open launcher and link-menu tabs next to the current tab (closes #11458)

matt wilkie5856b4db6ec2
diff --git a/apps/client/src/components/tab_manager.spec.ts b/apps/client/src/components/tab_manager.spec.ts
index 669d914686..63dbf3e6cb 100644
--- a/apps/client/src/components/tab_manager.spec.ts
+++ b/apps/client/src/components/tab_manager.spec.ts
@@ -55,6 +55,23 @@ describe("TabManager tab placement", () => {
// 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]);
});
+
+ it("places a tab from openInNewTab() after the active tab only when asked to", async () => {
+ const tm = new TabManager();
+ const [a, b, c] = await openEmptyTabs(tm, 3);
+ tm.activeNtxId = b.ntxId;
+
+ // An empty note ID makes setNote() return early, so only the placement is exercised.
+ await tm.openInNewTab("", null, false, "afterCurrent");
+ const afterCurrent = tm.mainNoteContexts[2];
+ expect(ntxOrder(tm)).toEqual([a.ntxId, b.ntxId, afterCurrent.ntxId, c.ntxId]);
+
+ await tm.openInNewTab("", null);
+ const appended = tm.mainNoteContexts[4];
+ expect(ntxOrder(tm)).toEqual([
+ a.ntxId, b.ntxId, afterCurrent.ntxId, c.ntxId, appended.ntxId
+ ]);
+ });
});
describe("moving a tab with splits into a window of its own", () => {
diff --git a/apps/client/src/components/tab_manager.ts b/apps/client/src/components/tab_manager.ts
index ae266792b4..93b7d21c75 100644
--- a/apps/client/src/components/tab_manager.ts
+++ b/apps/client/src/components/tab_manager.ts
@@ -338,8 +338,19 @@ export default class TabManager extends Component {
return !!noteContext.getMainContext().pinned;
}
- async openInNewTab(targetNoteId: string, hoistedNoteId: string | null = null, activate: boolean = false) {
- const noteContext = await this.openEmptyTab(null, hoistedNoteId || this.getActiveContext()?.hoistedNoteId);
+ async openInNewTab(
+ targetNoteId: string,
+ hoistedNoteId: string | null = null,
+ activate: boolean = false,
+ placement: TabPlacement = "end"
+ ) {
+ const noteContext = await this.openEmptyTab(
+ null,
+ hoistedNoteId || this.getActiveContext()?.hoistedNoteId,
+ null,
+ false,
+ placement
+ );
await noteContext.setNote(targetNoteId);
diff --git a/apps/client/src/menus/link_context_menu.spec.ts b/apps/client/src/menus/link_context_menu.spec.ts
index ca34f29fdc..cd2cbd2cab 100644
--- a/apps/client/src/menus/link_context_menu.spec.ts
+++ b/apps/client/src/menus/link_context_menu.spec.ts
@@ -111,13 +111,15 @@ describe("handleLinkContextMenuItem", () => {
expect(handle("openNoteInNewTab", VIEW_SCOPE, "explicitHoist")).toBe(true);
expect(mocks.openContextWithNote).toHaveBeenCalledWith("root/n1", {
hoistedNoteId: "explicitHoist",
- viewScope: VIEW_SCOPE
+ viewScope: VIEW_SCOPE,
+ placement: "afterCurrent"
});
handle("openNoteInNewTab");
expect(mocks.openContextWithNote).toHaveBeenLastCalledWith("root/n1", {
hoistedNoteId: "hoistedNote",
- viewScope: {}
+ viewScope: {},
+ placement: "afterCurrent"
});
});
@@ -162,7 +164,8 @@ describe("handleLinkContextMenuItem", () => {
expect(handle("openNoteInNewTab")).toBe(true);
expect(mocks.openContextWithNote).toHaveBeenCalledWith("root/n1", {
hoistedNoteId: null,
- viewScope: {}
+ viewScope: {},
+ placement: "afterCurrent"
});
// On the desktop the split is resolved from that same missing tab.
diff --git a/apps/client/src/menus/link_context_menu.ts b/apps/client/src/menus/link_context_menu.ts
index 1e84f46b06..4811d4b7de 100644
--- a/apps/client/src/menus/link_context_menu.ts
+++ b/apps/client/src/menus/link_context_menu.ts
@@ -59,7 +59,9 @@ function handleLinkContextMenuItem(command: string | undefined, e: ContextMenuEv
}
if (command === "openNoteInNewTab") {
- appContext.tabManager.openContextWithNote(notePath, { hoistedNoteId, viewScope });
+ appContext.tabManager.openContextWithNote(notePath, {
+ hoistedNoteId, viewScope, placement: "afterCurrent"
+ });
return true;
} else if (command === "openNoteInNewSplit") {
const ntxId = getNtxId(e);
diff --git a/apps/client/src/widgets/launch_bar/GenericButtons.spec.tsx b/apps/client/src/widgets/launch_bar/GenericButtons.spec.tsx
new file mode 100644
index 0000000000..b742d59762
--- /dev/null
+++ b/apps/client/src/widgets/launch_bar/GenericButtons.spec.tsx
@@ -0,0 +1,62 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import type FNote from "../../entities/fnote";
+
+const mocks = vi.hoisted(() => ({
+ openInNewTab: vi.fn(),
+ openInSameTab: vi.fn()
+}));
+
+vi.mock("../../components/app_context", () => ({
+ default: {
+ tabManager: {
+ openInNewTab: mocks.openInNewTab,
+ openInSameTab: mocks.openInSameTab,
+ getActiveContext: () => ({ hoistedNoteId: "activeHoist" })
+ }
+ }
+}));
+
+import { launchCustomNoteLauncher } from "./GenericButtons";
+
+const { openInNewTab, openInSameTab } = mocks;
+const launcherNote = { noteId: "_lbLauncher" } as FNote;
+
+function launch(evt: Partial<MouseEvent>, targetNoteId: string | null = "n1", hoist?: string) {
+ return launchCustomNoteLauncher(evt as MouseEvent, {
+ launcherNote,
+ getTargetNoteId: () => targetNoteId,
+ getHoistedNoteId: hoist ? () => hoist : undefined
+ });
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+});
+
+describe("launchCustomNoteLauncher", () => {
+ it("opens a ctrl-click or a middle-click in a new tab after the active one", async () => {
+ await launch({ which: 1, ctrlKey: true });
+ expect(openInNewTab).toHaveBeenLastCalledWith("n1", "activeHoist", false, "afterCurrent");
+
+ await launch({ which: 2 }, "n2", "launcherHoist");
+ expect(openInNewTab).toHaveBeenLastCalledWith("n2", "launcherHoist", false, "afterCurrent");
+
+ // Shift activates the new tab.
+ await launch({ which: 2, shiftKey: true });
+ expect(openInNewTab).toHaveBeenLastCalledWith("n1", "activeHoist", true, "afterCurrent");
+
+ expect(openInNewTab).toHaveBeenCalledTimes(3);
+ expect(openInSameTab).not.toHaveBeenCalled();
+ });
+
+ it("opens a plain click in the same tab, and ignores a right click or no target", async () => {
+ await launch({ which: 1 });
+ expect(openInSameTab).toHaveBeenCalledExactlyOnceWith("n1", "activeHoist");
+
+ await launch({ which: 3 });
+ await launch({ which: 1, ctrlKey: true }, null);
+ expect(openInSameTab).toHaveBeenCalledTimes(1);
+ expect(openInNewTab).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/client/src/widgets/launch_bar/GenericButtons.tsx b/apps/client/src/widgets/launch_bar/GenericButtons.tsx
index 6933233a66..d09c2892b2 100644
--- a/apps/client/src/widgets/launch_bar/GenericButtons.tsx
+++ b/apps/client/src/widgets/launch_bar/GenericButtons.tsx
@@ -66,7 +66,9 @@ export async function launchCustomNoteLauncher(evt: MouseEvent | KeyboardEvent,
if ((evt.which === 1 && ctrlKey) || evt.which === 2) {
const activate = !!evt.shiftKey;
- await appContext.tabManager.openInNewTab(targetNoteId, hoistedNoteIdWithDefault, activate);
+ await appContext.tabManager.openInNewTab(
+ targetNoteId, hoistedNoteIdWithDefault, activate, "afterCurrent"
+ );
} else {
await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteIdWithDefault);
}