Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager: use custom context menu

Adorian Doran9cfb14945b2d
diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
index 5a1814add5..837029297c 100644
--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
@@ -44,6 +44,8 @@ export interface ListViewOptions {
pageSize?: number;
/** Rendered on the title row in place of the note's attributes. */
renderItemActions?: (note: FNote) => ComponentChildren;
+ /** Rendered in place of the item's "..." menu, for lists offering their own actions. */
+ renderItemMenu?: (note: FNote) => ComponentChildren;
}
export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens, showTextRepresentation, listOptions }: ViewModeProps<{}> & { listOptions?: ListViewOptions }) {
@@ -66,6 +68,7 @@ export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens,
searchResultsLayout={searchResultsLayout}
hideSubNotes={listOptions?.hideSubNotes}
renderItemActions={listOptions?.renderItemActions}
+ renderItemMenu={listOptions?.renderItemMenu}
showTextRepresentation={showTextRepresentation} />
))}
</Card>
@@ -121,7 +124,7 @@ function NoteList(props: NoteListProps) {
</div>
}
-function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, showTextRepresentation, searchResultsLayout, hideSubNotes, renderItemActions }: {
+function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, showTextRepresentation, searchResultsLayout, hideSubNotes, renderItemActions, renderItemMenu }: {
note: FNote,
parentNote: FNote,
currentLevel: number,
@@ -129,7 +132,7 @@ function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expan
highlightedTokens: string[] | null | undefined;
includeArchived: boolean;
showTextRepresentation?: boolean;
-} & Pick<ListViewOptions, "searchResultsLayout" | "hideSubNotes" | "renderItemActions">) {
+} & Pick<ListViewOptions, "searchResultsLayout" | "hideSubNotes" | "renderItemActions" | "renderItemMenu">) {
const [ isExpanded, setExpanded ] = useState(currentLevel <= expandDepth);
const notePath = getNotePath(parentNote, note, searchResultsLayout);
@@ -157,6 +160,7 @@ function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expan
expandDepth={expandDepth}
searchResultsLayout={searchResultsLayout}
renderItemActions={renderItemActions}
+ renderItemMenu={renderItemMenu}
includeArchived={includeArchived}
/>
)}
@@ -186,7 +190,7 @@ function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expan
{renderItemActions
? <div className="note-list-item-actions">{renderItemActions(note)}</div>
: <NoteAttributes note={note} />}
- <NoteMenuButton notePath={notePath} />
+ {renderItemMenu ? renderItemMenu(note) : <NoteMenuButton notePath={notePath} />}
</h5>
</CardSection>
);
@@ -326,14 +330,14 @@ export function NoteContent({ note, trim, noChildrenList, highlightedTokens, inc
return <div ref={contentRef} className={clsx("note-book-content", `type-${noteType}`, {"note-book-content-ready": ready})} />;
}
-function NoteChildren({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, searchResultsLayout, renderItemActions }: {
+function NoteChildren({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, searchResultsLayout, renderItemActions, renderItemMenu }: {
note: FNote,
parentNote: FNote,
currentLevel: number,
expandDepth: number,
highlightedTokens: string[] | null | undefined
includeArchived: boolean;
-} & Pick<ListViewOptions, "searchResultsLayout" | "renderItemActions">) {
+} & Pick<ListViewOptions, "searchResultsLayout" | "renderItemActions" | "renderItemMenu">) {
const [ childNotes, setChildNotes ] = useState<FNote[]>();
useEffect(() => {
@@ -349,6 +353,7 @@ function NoteChildren({ note, parentNote, highlightedTokens, currentLevel, expan
includeArchived={includeArchived}
searchResultsLayout={searchResultsLayout}
renderItemActions={renderItemActions}
+ renderItemMenu={renderItemMenu}
/>);
}
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.tsx b/apps/client/src/widgets/type_widgets/options/content_manager.tsx
index c142b887d1..51411b6d40 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager.tsx
@@ -1,9 +1,13 @@
import "./content_manager.css";
import clsx from "clsx";
+import type { TargetedMouseEvent } from "preact";
import { useCallback, useEffect, useState } from "preact/hooks";
+import appContext from "../../../components/app_context";
import type FNote from "../../../entities/fnote";
+import contextMenu, { type MenuItem } from "../../../menus/context_menu";
+import branches from "../../../services/branches";
import {
CONTENT_CATEGORIES,
type ContentCategory,
@@ -15,6 +19,7 @@ import {
} from "../../../services/content_manager";
import { t } from "../../../services/i18n";
import { ListView, type ListViewOptions } from "../../collections/legacy/ListOrGridView";
+import ActionButton from "../../react/ActionButton";
import Button, { ButtonGroup } from "../../react/Button";
import FormToggle from "../../react/FormToggle";
import { useTriliumEvent, useTriliumOption } from "../../react/hooks";
@@ -24,6 +29,50 @@ import OptionsPageHeader from "./components/OptionsPageHeader";
const NOOP = () => {};
+/**
+ * The item's "..." menu. The collection menu is replaced here because most of its entries act on a
+ * note in the context of its parent, which this list is not: these notes live all over the tree.
+ */
+function ContentItemMenu({ note }: { note: FNote }) {
+ const openMenu = useCallback((e: TargetedMouseEvent<HTMLElement>) => {
+ const items: MenuItem<ContentItemCommand>[] = [
+ { title: t("link_context_menu.open_note_in_new_tab"), command: "openNoteInNewTab", uiIcon: "bx bx-link-external" },
+ { title: t("tree-context-menu.delete"), command: "deleteNote", uiIcon: "bx bx-trash destructive-action-icon" }
+ ];
+
+ void contextMenu.show<ContentItemCommand>({
+ x: e.pageX,
+ y: e.pageY,
+ items,
+ selectMenuItemHandler: ({ command }) => {
+ if (command === "openNoteInNewTab") {
+ void appContext.tabManager.openContextWithNote(note.noteId, {
+ hoistedNoteId: appContext.tabManager.getActiveContext()?.hoistedNoteId ?? null
+ });
+ } else if (command === "deleteNote") {
+ // Search results carry virtual branches, which cannot be deleted.
+ const branchIds = note.getParentBranchIds().filter((branchId) => !branchId.startsWith("virt-"));
+ // `moveToParent` is off: the user is on the Content Manager, not on the note, so
+ // there is nothing to navigate away from.
+ void branches.deleteNotes(branchIds, false, false);
+ }
+ }
+ });
+
+ e.stopPropagation();
+ }, [ note ]);
+
+ return (
+ <ActionButton
+ className="note-book-item-menu"
+ icon="bx bx-dots-vertical-rounded" text=""
+ onClick={openMenu}
+ />
+ );
+}
+
+type ContentItemCommand = "openNoteInNewTab" | "deleteNote";
+
/** Renders a note's extra detail, e.g. `Trigger: backend startup, hourly; Mode: authentic`. */
function ContentProperties({ note, category }: { note: FNote, category: ContentCategory }) {
const properties = resolveProperties(note, category);
@@ -131,7 +180,8 @@ function CategoryList({ pageNote, categories }: { pageNote: FNote, categories: C
<ContentProperties note={note} category={category} />
<ContentToggle note={note} category={category} />
</>
- )
+ ),
+ renderItemMenu: (note) => <ContentItemMenu note={note} />
}}
/>
))}