Skip to content

Build your personal knowledge base with Trilium Notes

feat(tree): fold Protect and Unprotect subtree into one split row

Elian Doran42745f338d9d
diff --git a/apps/client/src/menus/tree_context_menu.spec.ts b/apps/client/src/menus/tree_context_menu.spec.ts
index d0c3f987ab..d1de9f9b62 100644
--- a/apps/client/src/menus/tree_context_menu.spec.ts
+++ b/apps/client/src/menus/tree_context_menu.spec.ts
@@ -12,7 +12,8 @@ vi.mock("../services/clipboard", () => ({ default: { isClipboardEmpty: vi.fn(()
vi.mock("../components/app_context", () => ({ default: { tabManager: { getActiveContext: () => undefined } } }));
describe("buildTreeContextMenuItems", () => {
- it("offers the paste actions only while something is cut or copied", async () => {
+ /** The menu for a child note, with the note type data the insert submenus read stubbed. */
+ function setUp() {
vi.spyOn(noteTypesService, "loadNoteTypeData")
.mockResolvedValue({ builtInTemplateNotes: [], userTemplateNotes: [], newTemplates: new Set() });
const parent = buildNote({ title: "Parent", children: [ { title: "Child" } ] });
@@ -20,10 +21,17 @@ describe("buildTreeContextMenuItems", () => {
const [ branch ] = child?.getParentBranches() ?? [];
if (!child || !branch) throw new Error("expected the child and its branch");
+ const build = () => buildTreeContextMenuItems({
+ note: child, branch, notePath: `${parent.noteId}/${child.noteId}`, component: {} as Component
+ });
+ return { child, build };
+ }
+
+ it("offers the paste actions only while something is cut or copied", async () => {
+ const { build } = setUp();
+
async function actions() {
- const items = await buildTreeContextMenuItems({
- note: child, branch, notePath: `${parent.noteId}/${child.noteId}`, component: {} as Component
- });
+ const items = await build();
const row = items.find((item): item is Extract<MenuItem<TreeCommandNames>, { kind: "actions" }> =>
"kind" in item && item.kind === "actions");
return row?.items.map((item) => item.command);
@@ -37,6 +45,23 @@ describe("buildTreeContextMenuItems", () => {
"deleteNotes"
]);
});
+
+ it("protects or unprotects the subtree after Archive, as the note stands, with both in its submenu", async () => {
+ const { child, build } = setUp();
+
+ async function protection() {
+ const items = await build();
+ const archive = items.findIndex((item) => "uiIcon" in item && item.uiIcon === "bx bx-archive");
+ const row = items[archive + 1];
+ if (!row || "kind" in row) throw new Error("expected a row after Archive");
+ return [ row.command, row.items?.map((item) => "command" in item && item.command) ];
+ }
+
+ const both = [ "protectSubtree", "unprotectSubtree" ];
+ expect(await protection()).toStrictEqual([ "protectSubtree", both ]);
+ child.isProtected = true;
+ expect(await protection()).toStrictEqual([ "unprotectSubtree", both ]);
+ });
});
describe("getDuplicateItems", () => {
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts
index ddb7054b2a..3412809b81 100644
--- a/apps/client/src/menus/tree_context_menu.ts
+++ b/apps/client/src/menus/tree_context_menu.ts
@@ -140,6 +140,9 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Pr
? noteTypesService.buildNoteTypeItems(noteTypeData, "insertChildNote")
: null;
+ const protectItem: MenuCommandItem<TreeCommandNames> = { title: t("tree-context-menu.protect-subtree"), command: "protectSubtree", uiIcon: "bx bx-check-shield", enabled: noSelectedNotes };
+ const unprotectItem: MenuCommandItem<TreeCommandNames> = { title: t("tree-context-menu.unprotect-subtree"), command: "unprotectSubtree", uiIcon: "bx bx-shield", enabled: noSelectedNotes };
+
const items: (MenuItem<TreeCommandNames> | null)[] = [
{
kind: "actions",
@@ -224,12 +227,6 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Pr
{ kind: "separator" },
- { title: t("tree-context-menu.protect-subtree"), command: "protectSubtree", uiIcon: "bx bx-check-shield", enabled: noSelectedNotes },
-
- { title: t("tree-context-menu.unprotect-subtree"), command: "unprotectSubtree", uiIcon: "bx bx-shield", enabled: noSelectedNotes },
-
- { kind: "separator" },
-
{
title: t("tree-context-menu.advanced"),
uiIcon: "bx bxs-wrench",
@@ -338,6 +335,13 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Pr
}
},
+ // Runs what the note's own state calls for; the submenu keeps both, for a subtree that is
+ // protected only in part.
+ {
+ ...(note.isProtected ? unprotectItem : protectItem),
+ items: [ protectItem, unprotectItem ]
+ },
+
{ kind: "separator" },
(notOptionsOrHelp && selectedNotes.length === 1) ? {