Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager/space usage: display note sizes in the overview

Adorian Doran9425da2707dc
diff --git a/apps/client/src/services/note_tooltip.spec.ts b/apps/client/src/services/note_tooltip.spec.ts
index 02ef4d7557..1c63039455 100644
--- a/apps/client/src/services/note_tooltip.spec.ts
+++ b/apps/client/src/services/note_tooltip.spec.ts
@@ -243,6 +243,22 @@ describe("renderTooltip", () => {
expect(html).toContain('class="note-tooltip-attributes"');
});
+ it("places a contributed detail between the title and the attributes, escaping it", async () => {
+ const note = fakeNote({ noteId: "n1", bestPath: "root/n1" });
+ const html = (await renderTooltip(note, "Size: 1.2 MiB <>")) as string;
+
+ expect(html).toContain('<div class="note-tooltip-detail">Size: 1.2 MiB &lt;&gt;</div>');
+ expect(html.indexOf("note-tooltip-title")).toBeLessThan(html.indexOf("note-tooltip-detail"));
+ expect(html.indexOf("note-tooltip-detail")).toBeLessThan(html.indexOf("note-tooltip-attributes"));
+ });
+
+ it("adds nothing when no detail was contributed", async () => {
+ const note = fakeNote({ noteId: "n1", bestPath: "root/n1" });
+
+ expect((await renderTooltip(note)) as string).not.toContain("note-tooltip-detail");
+ expect((await renderTooltip(note, "")) as string).not.toContain("note-tooltip-detail");
+ });
+
it("omits the title heading when the path suffix resolves to empty", async () => {
getNoteTitleWithPathAsSuffix.mockResolvedValue("");
const note = fakeNote({ noteId: "n3", bestPath: "root/n3" });
diff --git a/apps/client/src/services/note_tooltip.ts b/apps/client/src/services/note_tooltip.ts
index e674b0f43e..e8aba36c23 100644
--- a/apps/client/src/services/note_tooltip.ts
+++ b/apps/client/src/services/note_tooltip.ts
@@ -76,6 +76,10 @@ export async function mouseEnterHandler<T>(this: HTMLElement, e: JQuery.Triggere
return;
}
+ // An optional line the trigger element contributes about the note it links to — a size in a
+ // space-usage cell, say. Plain text: the element states a fact, it does not render markup.
+ const detail = $link.attr("data-tooltip-detail");
+
let renderPromise;
let note: FNote | null = null;
// In-page anchors and footnotes are always bare `#fragment` references; a `?` means this is a
@@ -91,12 +95,12 @@ export async function mouseEnterHandler<T>(this: HTMLElement, e: JQuery.Triggere
// transitively imports this module via the context menu).
const { default: DeletedFNote } = await import("../entities/deleted_fnote.js");
note = await DeletedFNote.load(noteId);
- renderPromise = renderTooltip(note);
+ renderPromise = renderTooltip(note, detail);
} else {
// Default route: a live note. A missing note (deleted/erased) renders the "note has been
// deleted" placeholder — content previews of deleted notes are opt-in via `data-note-deleted`.
note = await froca.getNote(noteId);
- renderPromise = renderTooltip(note);
+ renderPromise = renderTooltip(note, detail);
}
const [content] = await Promise.all([
@@ -162,7 +166,12 @@ export async function mouseEnterHandler<T>(this: HTMLElement, e: JQuery.Triggere
}
}
-export async function renderTooltip(note: FNote | null) {
+/**
+ * @param detail plain text the trigger element contributed about this note (see the `data-tooltip-detail`
+ * attribute), shown under the title. Absent on the vast majority of links, which say
+ * nothing beyond the note itself.
+ */
+export async function renderTooltip(note: FNote | null, detail?: string) {
if (!note) {
return `<div>${t("note_tooltip.note-has-been-deleted")}</div>`;
}
@@ -206,6 +215,10 @@ export async function renderTooltip(note: FNote | null) {
}
}
+ if (detail) {
+ content = `${content}<div class="note-tooltip-detail">${utils.escapeHtml(detail)}</div>`;
+ }
+
content = `${content}<div class="note-tooltip-attributes">${$renderedAttributes[0].outerHTML}</div>`;
if (!isContentEmpty) {
content += $renderedContent[0].outerHTML;
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index ee696cdb80..56ae74895a 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -912,6 +912,13 @@ table.promoted-attributes-in-tooltip th {
overflow: hidden;
}
+.note-tooltip-detail {
+ margin-top: -5px;
+ margin-bottom: 5px;
+ color: var(--muted-text-color);
+ font-size: small;
+}
+
.note-tooltip-content {
/* height needs to stay small because tooltip has problem when it can't fit to either top or bottom of the cursor */
max-height: 300px;
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 6a00b842da..00ee34fb7d 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2679,6 +2679,7 @@
"others_notes_other": "{{count}} more notes ({{size}})",
"others_attachments_one": "{{count}} more attachment ({{size}})",
"others_attachments_other": "{{count}} more attachments ({{size}})",
+ "cell_size": "Size: {{size}}",
"segment_tooltip": "{{- title}} ({{size}})",
"attachment_tooltip": "Attachment: {{- title}} ({{size}})",
"child_tooltip": "Child note: {{- title}} ({{size}})",
diff --git a/apps/client/src/widgets/react/charts/DonutChart.render.spec.tsx b/apps/client/src/widgets/react/charts/DonutChart.render.spec.tsx
index 80ff9df704..0369f47cc5 100644
--- a/apps/client/src/widgets/react/charts/DonutChart.render.spec.tsx
+++ b/apps/client/src/widgets/react/charts/DonutChart.render.spec.tsx
@@ -93,9 +93,11 @@ describe("DonutChart rendering", () => {
await flushRender();
const tooltip = probe.querySelector<HTMLElement>(".chart-tooltip");
expect(tooltip?.textContent).toBe("A tip");
- // The Trilium look depends on riding the real tooltip classes.
+ // The Trilium look depends on riding the real tooltip classes; `tooltip-top` is what keeps
+ // the bubble above the hovered mark, which lifts itself to draw its outline.
expect(tooltip?.classList.contains("tooltip")).toBe(true);
expect(tooltip?.classList.contains("show")).toBe(true);
+ expect(tooltip?.classList.contains("tooltip-top")).toBe(true);
// Positioned fixed, straight from the viewport coordinates, so no clipping ancestor cuts it.
expect(tooltip?.style.left).toBe("30px");
expect(tooltip?.style.top).toBe("40px");
diff --git a/apps/client/src/widgets/react/charts/chart_tooltip.tsx b/apps/client/src/widgets/react/charts/chart_tooltip.tsx
index 80e2283003..400b1bcd69 100644
--- a/apps/client/src/widgets/react/charts/chart_tooltip.tsx
+++ b/apps/client/src/widgets/react/charts/chart_tooltip.tsx
@@ -49,9 +49,12 @@ export function useChartTooltip<T extends HTMLElement>() {
tooltipNode: tooltip && (
// The `tooltip show` classes matter: the themes set the colours on `.tooltip` and scope
// the text colour to `.tooltip .tooltip-inner`, so a bare inner would inherit the page's.
+ // `tooltip-top` is the app's "raise above everything" class: the base `.tooltip` z-index
+ // is derived from a CKEditor variable that is out of scope here, leaving the bubble at
+ // `auto` — under the hovered mark, which lifts itself to draw its outline.
<div
className={clsx(
- "tooltip", "show", "chart-tooltip",
+ "tooltip", "show", "tooltip-top", "chart-tooltip",
tooltip.flipX && "chart-tooltip-flip-x",
tooltip.flipY && "chart-tooltip-flip-y"
)}
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
index 4892760df1..5bcd5a30dc 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
@@ -21,7 +21,10 @@ type SpaceUsageView = "overview" | "browse";
export default function SpaceUsage({ sectionSwitcher }: ContentManagerSectionProps) {
const [ view, setView ] = useState<SpaceUsageView>("overview");
// Fetched in both views: the treemap consumes it, and the status line is view-independent.
- const overview = useSpaceUsageFetch<SpaceUsageOverviewResponse>(`space-usage/overview?limit=${OVERVIEW_LIMIT}`);
+ // Revisions count towards the ranking because the treemap shows them in a cell's area — asking
+ // for one basis and drawing the other would hide notes whose weight is all history.
+ const overview = useSpaceUsageFetch<SpaceUsageOverviewResponse>(
+ `space-usage/overview?limit=${OVERVIEW_LIMIT}&includeRevisions=true`);
return (
<div className="space-usage-section">
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx
index ae2c896e2b..e112b83b17 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx
@@ -3,18 +3,32 @@ import { useMemo } from "preact/hooks";
import appContext from "../../../../../components/app_context";
import { t } from "../../../../../services/i18n";
+import { formatSize } from "../../../../../services/utils";
import Treemap, { type TreemapItem } from "../../../../react/charts/Treemap";
-import { buildOverviewModel, type OverviewCell } from "./overview_model";
+import { bucketWeight, buildOverviewModel, type OverviewCell } from "./overview_model";
+
+/** A cell's area covers a note's whole weight, history included; its tooltip says the same. */
+const INCLUDE_REVISIONS = true;
/** The treemap over the whole database: every large note at its tree location. */
export default function Overview({ overview }: { overview: SpaceUsageOverviewResponse }) {
// The labels are formatted here rather than in the model, which stays free of i18n: a bucket
- // stands for a crowd, so its tooltip names how many notes it holds.
+ // stands for a crowd, so its tooltip names how many notes it holds and how much they take.
const model = useMemo(() => buildOverviewModel(overview, {
- otherNotesLabel: t("space_usage.other_notes", { count: overview.otherNotes.noteCount }),
- hiddenNotesLabel: t("space_usage.hidden_notes", { count: overview.hiddenNotes.noteCount }),
- deletedNotesLabel: t("space_usage.deleted_notes", { count: overview.deletedNotes.noteCount }),
- includeRevisions: false
+ otherNotesLabel: withSize(
+ t("space_usage.other_notes", { count: overview.otherNotes.noteCount }),
+ bucketWeight(overview.otherNotes, INCLUDE_REVISIONS)
+ ),
+ hiddenNotesLabel: withSize(
+ t("space_usage.hidden_notes", { count: overview.hiddenNotes.noteCount }),
+ bucketWeight(overview.hiddenNotes, INCLUDE_REVISIONS)
+ ),
+ deletedNotesLabel: withSize(
+ t("space_usage.deleted_notes", { count: overview.deletedNotes.noteCount }),
+ overview.deletedNotes.size
+ ),
+ includeRevisions: INCLUDE_REVISIONS,
+ makeSizeDetail: (size) => t("space_usage.cell_size", { size: formatSize(size) })
}), [ overview ]);
return (
@@ -24,6 +38,15 @@ export default function Overview({ overview }: { overview: SpaceUsageOverviewRes
);
}
+/**
+ * "Other 1928 notes (512 MiB)" — the same "name (size)" wording the donut segments use, so a
+ * bucket reads alike wherever it appears. Composed here rather than baked into the count keys,
+ * which Browse reuses on its own and appends the size to itself.
+ */
+function withSize(name: string, size: number) {
+ return t("space_usage.segment_tooltip", { title: name, size: formatSize(size) });
+}
+
/**
* Opens the clicked note in a new tab, leaving the settings page in place. The bucket cells carry
* no note and do nothing.
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
index 76af076393..eef40c57fd 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
@@ -25,7 +25,8 @@ function build(overview: SpaceUsageOverviewResponse, includeRevisions = false) {
otherNotesLabel: "Other notes",
hiddenNotesLabel: "Hidden notes",
deletedNotesLabel: "Deleted notes",
- includeRevisions
+ includeRevisions,
+ makeSizeDetail: (size) => `size:${size}`
});
}
@@ -49,7 +50,25 @@ describe("buildOverviewModel", () => {
const leaf = childById(folder, "leaf");
expect(leaf.value).toBe(120);
expect(leaf.data).toEqual({ noteId: "leaf" });
- expect(leaf.attributes).toEqual({ "data-href": "#root/folder/leaf" });
+ // The size rides along in the note preview tooltip rather than a second tooltip of its own.
+ expect(leaf.attributes).toEqual({
+ "data-href": "#root/folder/leaf",
+ "data-tooltip-detail": "size:120"
+ });
+ });
+
+ it("states the size its area encodes, and stays silent when no wording is given", () => {
+ const overview = response([ entry("a", [ "a" ], { ownSize: 5, revisionsSize: 7 }) ]);
+
+ expect(childById(build(overview, true), "a").attributes?.["data-tooltip-detail"]).toBe("size:12");
+
+ const silent = buildOverviewModel(overview, {
+ otherNotesLabel: "Other notes",
+ hiddenNotesLabel: "Hidden notes",
+ deletedNotesLabel: "Deleted notes",
+ includeRevisions: true
+ });
+ expect(childById(silent, "a").attributes).toEqual({ "data-href": "#root/a" });
});
it("colors cells by their parent: siblings share a hue, other groups get their own", () => {
@@ -80,7 +99,10 @@ describe("buildOverviewModel", () => {
expect(selfCell.value).toBe(50);
expect(selfCell.hue).toBe(hueOf("parent"));
expect(selfCell.data).toEqual({ noteId: "parent" });
- expect(selfCell.attributes).toEqual({ "data-href": "#root/parent" });
+ expect(selfCell.attributes).toEqual({
+ "data-href": "#root/parent",
+ "data-tooltip-detail": "size:50"
+ });
expect(childById(parent, "child").value).toBe(10);
});
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts
index 1508acea9b..a2beb6cc68 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts
@@ -1,4 +1,4 @@
-import type { SpaceUsageOverviewNote, SpaceUsageOverviewResponse } from "@triliumnext/commons";
+import type { SpaceUsageBucket, SpaceUsageOverviewNote, SpaceUsageOverviewResponse } from "@triliumnext/commons";
import type { TreemapItem } from "../../../../react/charts/Treemap";
@@ -13,6 +13,11 @@ interface OverviewModelOptions {
deletedNotesLabel: string;
/** Folds revision sizes into each cell's weight. */
includeRevisions: boolean;
+ /**
+ * Wording for the size a note cell contributes to its hover tooltip, given the weight its area
+ * encodes. Omit to leave the cells silent about their size.
+ */
+ makeSizeDetail?: (size: number) => string;
}
/**
@@ -28,7 +33,7 @@ interface OverviewModelOptions {
*/
export function buildOverviewModel(
overview: SpaceUsageOverviewResponse,
- { otherNotesLabel, hiddenNotesLabel, deletedNotesLabel, includeRevisions }: OverviewModelOptions
+ { otherNotesLabel, hiddenNotesLabel, deletedNotesLabel, includeRevisions, makeSizeDetail }: OverviewModelOptions
): TreemapItem<OverviewCell> {
const root: TreemapItem<OverviewCell> = { id: "root", children: [] };
const nodesByNoteId = new Map<string, TreemapItem<OverviewCell>>();
@@ -64,12 +69,18 @@ export function buildOverviewModel(
// nested self-cell depends on whether some later entry turned it into a group.
for (const { node, entry, path } of entries) {
const weight = weightOf(entry, includeRevisions);
- const attributes = { "data-href": `#root/${path.join("/")}` };
if (weight <= 0) {
continue;
}
+ const attributes = {
+ "data-href": `#root/${path.join("/")}`,
+ // The note preview tooltip shows this under the title, which is how a cell states its
+ // size without any text of its own — and without a second tooltip competing with it.
+ ...(makeSizeDetail ? { "data-tooltip-detail": makeSizeDetail(weight) } : {})
+ };
+
if (node.children?.length) {
node.children.push({
id: `${entry.noteId}/own`,
@@ -90,14 +101,14 @@ export function buildOverviewModel(
root.children?.push(
{
id: "/other-notes",
- value: overview.otherNotes.size + (includeRevisions ? overview.otherNotes.revisionsSize : 0),
+ value: bucketWeight(overview.otherNotes, includeRevisions),
className: "treemap-cell-other",
tooltip: otherNotesLabel,
data: {}
},
{
id: "/hidden-notes",
- value: overview.hiddenNotes.size + (includeRevisions ? overview.hiddenNotes.revisionsSize : 0),
+ value: bucketWeight(overview.hiddenNotes, includeRevisions),
className: "treemap-cell-hidden",
tooltip: hiddenNotesLabel,
data: {}
@@ -114,6 +125,14 @@ export function buildOverviewModel(
return root;
}
+/**
+ * The area a bucket's cell takes. Exported so the caller's tooltip can quote the same figure the
+ * cell draws, rather than reproducing the rule and drifting from it.
+ */
+export function bucketWeight(bucket: SpaceUsageBucket, includeRevisions: boolean) {
+ return bucket.size + (includeRevisions ? bucket.revisionsSize : 0);
+}
+
/**
* The tint shared by all cells under one parent: an arbitrary but stable hue hashed from the
* parent's ID, so the coloring survives reloads and refreshes unchanged.