Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): stay on the tree after a menu action

Elian Doranbb8b12fdcc1e
diff --git a/apps/client/src/menus/tree_context_menu.spec.ts b/apps/client/src/menus/tree_context_menu.spec.ts
index ddbe38fa3f..bfe280a8ed 100644
--- a/apps/client/src/menus/tree_context_menu.spec.ts
+++ b/apps/client/src/menus/tree_context_menu.spec.ts
@@ -3,10 +3,11 @@ import { describe, expect, it, vi } from "vitest";
import type Component from "../components/component";
import clipboard from "../services/clipboard";
import froca from "../services/froca";
+import noteCreateService from "../services/note_create";
import noteTypesService from "../services/note_types";
import { buildNote } from "../test/easy-froca";
import type { MenuItem } from "./context_menu";
-import { buildTreeContextMenuItems, getDuplicateItems, type TreeCommandNames } from "./tree_context_menu";
+import { buildTreeContextMenuItems, getDuplicateItems, handleTreeContextMenuSelect, type TreeCommandNames } from "./tree_context_menu";
vi.mock("../services/clipboard", () => ({ default: { getContents: vi.fn(() => null) } }));
vi.mock("../components/app_context", () => ({ default: { tabManager: { getActiveContext: () => undefined } } }));
@@ -158,3 +159,31 @@ describe("getDuplicateItems", () => {
.toStrictEqual([ "duplicateSubtree", "duplicateNote" ]);
});
});
+
+describe("handleTreeContextMenuSelect", () => {
+ it("reveals the note only for the commands that open one", async () => {
+ const parent = buildNote({ title: "Parent", children: [ { title: "Child" } ] });
+ const child = froca.notes[parent.children[0] ?? ""];
+ const [ branch ] = child?.getParentBranches() ?? [];
+ if (!child || !branch) throw new Error("expected the child and its branch");
+ vi.spyOn(noteCreateService, "createNote").mockResolvedValue({ note: null, branch: undefined });
+
+ async function reveals(command: TreeCommandNames) {
+ const onRevealNote = vi.fn();
+ const triggerCommand = vi.fn();
+ await handleTreeContextMenuSelect({ title: command, command }, {
+ note: child, branch, notePath: `${parent.noteId}/${child.noteId}`,
+ component: { triggerCommand } as unknown as Component,
+ onRevealNote
+ });
+ return onRevealNote.mock.calls.length > 0;
+ }
+
+ for (const command of [ "insertChildNote", "duplicateSubtree", "openNoteInSplit", "searchInSubtree" ] as const) {
+ expect(await reveals(command), command).toBe(true);
+ }
+ for (const command of [ "copyNotesToClipboard", "cutNotesToClipboard", "deleteNotes", "toggleNoteHoisting", "moveNotesTo" ] as const) {
+ expect(await reveals(command), command).toBe(false);
+ }
+ });
+});
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts
index 003a40af9e..4b0ade5e20 100644
--- a/apps/client/src/menus/tree_context_menu.ts
+++ b/apps/client/src/menus/tree_context_menu.ts
@@ -74,10 +74,10 @@ export interface TreeContextMenuContext {
*/
target?: "desktop" | "mobile";
/**
- * Invoked right before any command's handler runs. Fancytree uses this to
- * switch to the detail screen on mobile so dialogs/navigations are visible.
+ * Invoked right before a command in `COMMANDS_THAT_SHOW_A_NOTE` runs. On mobile, callers
+ * switch to the detail screen here so that the note the command opens is visible.
*/
- onBeforeCommand?: () => void;
+ onRevealNote?: () => void;
/**
* Fancytree node for the target, when available. A handful of tree
* commands (cut/paste/export/expand/collapse/sort) read `node.data` or call
@@ -399,7 +399,9 @@ export async function handleTreeContextMenuSelect(
const resolved = resolveContext(ctx);
const { note, branch, notePath, component, selectedOrActiveBranchIds, selectedOrActiveNoteIds } = resolved;
- resolved.onBeforeCommand?.();
+ if (command && COMMANDS_THAT_SHOW_A_NOTE.has(command)) {
+ resolved.onRevealNote?.();
+ }
if (command === "openInTab") {
appContext.tabManager.openTabWithNoteWithHoisting(notePath, { placement: "afterCurrent" });
@@ -473,6 +475,19 @@ export async function handleTreeContextMenuSelect(
}
}
+/**
+ * The commands whose result is a note on the detail screen: a new note, a duplicate, a split or a
+ * search. Every other command acts on the tree, the clipboard or a dialog, so the tree stays shown.
+ */
+const COMMANDS_THAT_SHOW_A_NOTE = new Set<TreeCommandNames>([
+ "insertNoteAfter",
+ "insertChildNote",
+ "duplicateNote",
+ "duplicateSubtree",
+ "openNoteInSplit",
+ "searchInSubtree"
+]);
+
/**
* The choice between copying the children or not, offered only when a note to copy has children.
*/
@@ -548,7 +563,7 @@ export default class TreeContextMenu implements SelectMenuItemEventListener<Tree
selectedNotes,
isSpotlighted: this.node.extraClasses.includes("spotlighted-node"),
node: this.node,
- onBeforeCommand: () => {
+ onRevealNote: () => {
if (utils.isMobile()) {
this.treeWidget.triggerCommand("setActiveScreen", { screen: "detail" });
}
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx
index 58513d40c8..6eb8a96342 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx
+++ b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx
@@ -572,7 +572,7 @@ function buildNoteContextMenu(notePath: string, parentComponent: Component | nul
notePath,
component: parentComponent,
target: "mobile" as const,
- onBeforeCommand: () => {
+ onRevealNote: () => {
if (utils.isMobile()) {
parentComponent.triggerCommand("setActiveScreen", { screen: "detail" });
}