Skip to content

Build your personal knowledge base with Trilium Notes

feat(collections/dashboard): add refresh and delete context menu items

Elian Doranbdaefe0d6909
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 3148490aea..d6e31553b6 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2347,6 +2347,10 @@
"edit-column-title": "Click to edit column title",
"column-already-exists": "This column already exists on the board."
},
+ "dashboard_view": {
+ "refresh-widget": "Refresh",
+ "remove-widget": "Remove from dashboard"
+ },
"presentation_view": {
"edit-slide": "Edit this slide",
"start-presentation": "Start presentation",
diff --git a/apps/client/src/widgets/collections/dashboard/context_menu.ts b/apps/client/src/widgets/collections/dashboard/context_menu.ts
new file mode 100644
index 0000000000..55503ec119
--- /dev/null
+++ b/apps/client/src/widgets/collections/dashboard/context_menu.ts
@@ -0,0 +1,29 @@
+import { type CommandNames } from "../../../components/app_context";
+import contextMenu, { type ContextMenuEvent, type MenuItem } from "../../../menus/context_menu";
+import link_context_menu from "../../../menus/link_context_menu";
+import branches from "../../../services/branches";
+import { t } from "../../../services/i18n";
+
+export default function openWidgetContextMenu(notePath: string, branchId: string, e: ContextMenuEvent, { onRefresh }: {
+ /** When provided (i.e. the widget is a render note), adds a "Refresh" item that re-renders it. */
+ onRefresh?: () => void;
+}) {
+ const items: MenuItem<CommandNames>[] = [
+ ...link_context_menu.getItems(e),
+ { kind: "separator" }
+ ];
+
+ if (onRefresh) {
+ items.push({ title: t("dashboard_view.refresh-widget"), uiIcon: "bx bx-refresh", handler: () => onRefresh() });
+ items.push({ kind: "separator" });
+ }
+
+ items.push({ title: t("dashboard_view.remove-widget"), uiIcon: "bx bx-trash", handler: () => branches.deleteNotes([ branchId ], false, false) });
+
+ contextMenu.show({
+ x: e.pageX,
+ y: e.pageY,
+ items,
+ selectMenuItemHandler: ({ command }) => link_context_menu.handleLinkContextMenuItem(command, e, notePath)
+ });
+}
diff --git a/apps/client/src/widgets/collections/dashboard/index.tsx b/apps/client/src/widgets/collections/dashboard/index.tsx
index 28c8376870..96ebcfc524 100644
--- a/apps/client/src/widgets/collections/dashboard/index.tsx
+++ b/apps/client/src/widgets/collections/dashboard/index.tsx
@@ -7,7 +7,6 @@ import { RefObject, TargetedMouseEvent } from "preact";
import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
import FNote from "../../../entities/fnote";
-import linkContextMenuService from "../../../menus/link_context_menu";
import branches from "../../../services/branches";
import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
@@ -18,6 +17,7 @@ import Icon from "../../react/Icon";
import NoteLink from "../../react/NoteLink";
import { ViewModeProps } from "../interface";
import { getNotePath, NoteContent } from "../legacy/ListOrGridView";
+import openWidgetContextMenu from "./context_menu";
interface DashboardWidgetLayout {
x: number;
@@ -269,6 +269,9 @@ interface DashboardWidgetProps {
function DashboardWidget({ note, parentNote, highlightedTokens, includeArchived, showTextRepresentation }: DashboardWidgetProps) {
const notePath = getNotePath(parentNote, note);
+ // Bumping the key remounts NoteContent, which re-runs the render — only meaningful for render notes.
+ const [ refreshKey, setRefreshKey ] = useState(0);
+ const isRenderNote = note.type === "render";
/* The outer .grid-stack-item class list must stay constant across renders so that Preact never
clobbers the classes gridstack adds there; dynamic classes go on the inner content element. */
@@ -286,12 +289,15 @@ function DashboardWidget({ note, parentNote, highlightedTokens, includeArchived,
<ActionButton className="note-book-item-menu"
icon="bx bx-dots-vertical-rounded" text=""
onClick={(e: TargetedMouseEvent<HTMLElement>) => {
- linkContextMenuService.openContextMenu(notePath, e);
+ openWidgetContextMenu(notePath, note.parentToBranch[parentNote.noteId], e, {
+ onRefresh: isRenderNote ? () => setRefreshKey((key) => key + 1) : undefined
+ });
e.stopPropagation();
}} />
</div>
<div className="dashboard-widget-content">
- <NoteContent note={note}
+ <NoteContent key={refreshKey}
+ note={note}
trim
highlightedTokens={highlightedTokens}
includeArchivedNotes={includeArchived}