Skip to content

Build your personal knowledge base with Trilium Notes

client/space usage tool: allow items be selected on mobile, show selection info on the bottom of the screen

Adorian Doran30c2712d7ff4
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index a0752f5e1b..6a93a01143 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3116,6 +3116,7 @@
"status_content_hint": "Space held by all notes (hidden ones included), their attachments and their revisions, with duplicated content counted once",
"status_deleted": "{{size}} in deleted notes and attachments",
"status_deleted_hint": "Space still held by deleted notes and by deleted attachments (whose note may still exist); erasing them reclaims it",
+ "selection_hint": "Tap an item for details",
"menu_show_details": "Show details",
"menu_erase_deleted": "Erase deleted data",
"erase_deleted_message": "Deleted data has been erased.",
diff --git a/apps/client/src/widgets/react/charts/DonutChart.css b/apps/client/src/widgets/react/charts/DonutChart.css
index 1f947e5e0c..7022ef36e8 100644
--- a/apps/client/src/widgets/react/charts/DonutChart.css
+++ b/apps/client/src/widgets/react/charts/DonutChart.css
@@ -6,6 +6,11 @@
aspect-ratio: 1 / 1;
max-width: 100%;
+ /* How a chosen segment's trace is struck from its own hue — see the rule below. Knobs for a
+ theme, matching the treemap's, so the two charts mark a choice the same way. */
+ --donut-selected-saturation: 100%;
+ --donut-selected-lightness: 50%;
+
svg {
display: block;
width: 100%;
@@ -40,13 +45,38 @@
/* An outline that traces the arc rather than its box: `outline` on a dashed circle would frame
the entire ring, since that is the element's extent. Blur-less shadows in the four directions
hug the painted shape instead, and stay inside the inter-segment gap. */
- .donut-segment:hover {
+ /* Only where a pointer can hover: a tapped arc on a touch screen stays hovered, and would wear
+ this ring alongside the chosen segment's. */
+ @media (hover: hover) {
+ .donut-segment:hover {
+ filter:
+ brightness(1.12)
+ drop-shadow(2px 0 0 var(--input-focus-outline-color))
+ drop-shadow(-2px 0 0 var(--input-focus-outline-color))
+ drop-shadow(0 2px 0 var(--input-focus-outline-color))
+ drop-shadow(0 -2px 0 var(--input-focus-outline-color));
+ }
+ }
+
+ /* The chosen segment, traced the same way and for the same reason as the hover ring above: an
+ `outline` would frame the whole ring rather than the arc. The offsets are viewBox units, which
+ the chart scales down to roughly the three device pixels the treemap's chosen cell borders
+ itself with. */
+ .donut-segment.donut-segment-selected {
filter:
- brightness(1.12)
- drop-shadow(2px 0 0 var(--input-focus-outline-color))
- drop-shadow(-2px 0 0 var(--input-focus-outline-color))
- drop-shadow(0 2px 0 var(--input-focus-outline-color))
- drop-shadow(0 -2px 0 var(--input-focus-outline-color));
+ drop-shadow(3px 0 0 var(--donut-segment-selected-color, var(--main-text-color)))
+ drop-shadow(-3px 0 0 var(--donut-segment-selected-color, var(--main-text-color)))
+ drop-shadow(0 3px 0 var(--donut-segment-selected-color, var(--main-text-color)))
+ drop-shadow(0 -3px 0 var(--donut-segment-selected-color, var(--main-text-color)));
+ }
+
+ /* The segment's own hue at full strength, as the treemap strikes a chosen cell's border. A
+ segment painted from a semantic colour rather than a hue sets this variable itself; one with
+ neither keeps the plain default above. */
+ .donut-segment.donut-colored.donut-segment-selected {
+ --donut-segment-selected-color: hsl(
+ var(--donut-hue) var(--donut-selected-saturation) var(--donut-selected-lightness)
+ );
}
.donut-clickable {
diff --git a/apps/client/src/widgets/react/charts/DonutChart.tsx b/apps/client/src/widgets/react/charts/DonutChart.tsx
index 5a959a7718..53afb469d2 100644
--- a/apps/client/src/widgets/react/charts/DonutChart.tsx
+++ b/apps/client/src/widgets/react/charts/DonutChart.tsx
@@ -11,6 +11,13 @@ export interface DonutSegment<T = unknown> {
value: number;
/** Tooltip text, shown in the chart's own cursor-following tooltip while the segment is hovered. */
tooltip?: string;
+ /**
+ * The mark's plain name, for whatever names it away from the chart: a selection strip, a legend,
+ * an accessible name. The {@link tooltip} is this name with its figure worked into a sentence,
+ * which is why the two are not the same string. Left unset by a mark standing for several things
+ * at once, which has no one name to give.
+ */
+ label?: string;
/**
* Categorical tint (0–359), mixed into the theme surface exactly like the treemap cells.
* Absent = the plain surface color, unless a {@link className} paints its own.
@@ -40,6 +47,12 @@ export interface DonutRing<T = unknown> {
interface DonutChartProps<T> {
rings: DonutRing<T>[];
+ /**
+ * The segment drawn as the chosen one, by {@link DonutSegment.id}, which is why those must be
+ * unique across the chart's rings rather than only within one. For a consumer that keeps a
+ * selection rather than leaving identity to the hover, as a touch screen has to.
+ */
+ selectedSegmentId?: string;
className?: string;
/**
* SVG paint servers (gradients, patterns) for segments whose class points `--donut-segment-color`
@@ -67,7 +80,7 @@ const SEGMENT_GAP = 3;
* the element: a dashed circle's extent is the whole ring, so an anchored tooltip would hover far
* from the segment it describes.
*/
-export default function DonutChart<T>({ rings, className, defs, children }: DonutChartProps<T>) {
+export default function DonutChart<T>({ rings, selectedSegmentId, className, defs, children }: DonutChartProps<T>) {
const half = DONUT_VIEW_SIZE / 2;
const { containerRef, containerProps, showTooltip, hideTooltip, tooltipNode } = useChartTooltip<HTMLDivElement>();
@@ -83,6 +96,7 @@ export default function DonutChart<T>({ rings, className, defs, children }: Donu
<Ring
key={ring.id}
ring={ring}
+ selectedSegmentId={selectedSegmentId}
onSegmentHover={(segment, event) => segment && event
? showTooltip(segment.tooltip, event)
: hideTooltip()}
@@ -96,8 +110,9 @@ export default function DonutChart<T>({ rings, className, defs, children }: Donu
);
}
-function Ring<T>({ ring, onSegmentHover }: {
+function Ring<T>({ ring, selectedSegmentId, onSegmentHover }: {
ring: DonutRing<T>,
+ selectedSegmentId?: string,
onSegmentHover: (segment: DonutSegment<T> | null, event?: { clientX: number, clientY: number }) => void
}) {
const circumference = 2 * Math.PI * ring.radius;
@@ -123,6 +138,7 @@ function Ring<T>({ ring, onSegmentHover }: {
"donut-segment",
segment.hue !== undefined && "donut-colored",
ring.onSegmentClick && "donut-clickable",
+ segment.id === selectedSegmentId && "donut-segment-selected",
segment.className
)}
r={ring.radius}
diff --git a/apps/client/src/widgets/react/charts/Treemap.css b/apps/client/src/widgets/react/charts/Treemap.css
index 621d99b5e1..3f791c516a 100644
--- a/apps/client/src/widgets/react/charts/Treemap.css
+++ b/apps/client/src/widgets/react/charts/Treemap.css
@@ -12,6 +12,12 @@
--treemap-icon-lightness: 30%;
--treemap-icon-tint: 40%;
+ /* How a chosen cell's border is struck from the cell's own hue: that hue at full strength, which
+ reads as belonging to the cell while standing well clear of the tint mixed into the theme's
+ surface. Knobs of the same kind as the icon's above, for a theme to retune. */
+ --treemap-selected-saturation: 100%;
+ --treemap-selected-lightness: 50%;
+
/* Painted through a variable, not a competing rule: consumers set `--treemap-cell-background`
and never race this stylesheet's cascade position. */
/* The corner rounding is per cell — a cell rounds only the corners its block owns — so it is
@@ -54,9 +60,30 @@
);
}
- .treemap-cell:hover {
- outline: 2px solid var(--main-text-color);
- outline-offset: -1px;
- z-index: 1;
+ /* Only where a pointer can actually hover: a tap leaves a touch screen's cell "hovered" until
+ the next one is tapped, which would put a second ring on the map beside the chosen cell's. */
+ @media (hover: hover) {
+ .treemap-cell:hover {
+ outline: 2px solid var(--main-text-color);
+ outline-offset: -1px;
+ z-index: 1;
+ }
+ }
+
+ /* The chosen cell, where identity is a selection rather than a hover. A border rather than an
+ outline: it is drawn inside the cell's own rect, which leaves the geometry alone (the cells are
+ border-box) and keeps all four sides on the outermost cells, where the container clips and the
+ map's inset is thinner than this. */
+ .treemap-cell.treemap-cell-selected {
+ z-index: 2;
+ border: 3px solid var(--treemap-cell-selected-color, var(--main-text-color));
+ }
+
+ /* The cell's own hue at full strength. A cell without one — a consumer's bucket, standing for a
+ crowd rather than a thing — has no hue to strike this from and keeps the plain default above. */
+ .treemap-cell.treemap-colored.treemap-cell-selected {
+ --treemap-cell-selected-color: hsl(
+ var(--treemap-hue) var(--treemap-selected-saturation) var(--treemap-selected-lightness)
+ );
}
}
diff --git a/apps/client/src/widgets/react/charts/Treemap.tsx b/apps/client/src/widgets/react/charts/Treemap.tsx
index 2a0892616a..9831b4953b 100644
--- a/apps/client/src/widgets/react/charts/Treemap.tsx
+++ b/apps/client/src/widgets/react/charts/Treemap.tsx
@@ -38,6 +38,12 @@ export interface TreemapItem<T = unknown> {
interface TreemapProps<T> {
root: TreemapItem<T>;
+ /**
+ * The cell drawn as the chosen one, by {@link TreemapItem.id}. For a consumer that keeps a
+ * selection rather than leaving identity to the hover: a touch screen has no hover, so the mark
+ * has to stay marked while whatever names it is read elsewhere on the screen.
+ */
+ selectedItemId?: string;
onItemClick?: (item: TreemapItem<T>) => void;
onItemContextMenu?: (item: TreemapItem<T>, event: MouseEvent) => void;
className?: string;
@@ -84,7 +90,7 @@ const MIN_ICON_SIZE = 10;
* tooltips behave like everywhere else in the app. The hue tint is mixed into the theme's own
* surface color, which keeps the map coherent under any user theme.
*/
-export default function Treemap<T>({ root, onItemClick, onItemContextMenu, className }: TreemapProps<T>) {
+export default function Treemap<T>({ root, selectedItemId, onItemClick, onItemContextMenu, className }: TreemapProps<T>) {
const { containerRef, containerProps, showTooltip, hideTooltip, tooltipNode } = useChartTooltip<HTMLDivElement>();
const size = useElementSize(containerRef);
const width = Math.floor(size?.width ?? 0);
@@ -129,7 +135,12 @@ export default function Treemap<T>({ root, onItemClick, onItemContextMenu, class
return (
<div
key={item.id}
- className={clsx("treemap-cell", item.hue !== undefined && "treemap-colored", item.className)}
+ className={clsx(
+ "treemap-cell",
+ item.hue !== undefined && "treemap-colored",
+ item.id === selectedItemId && "treemap-cell-selected",
+ item.className
+ )}
// Geometry and tint are computed per layout run, so they cannot live in a stylesheet.
style={{
left: cell.x0,
diff --git a/apps/client/src/widgets/type_widgets/space_usage/browse.tsx b/apps/client/src/widgets/type_widgets/space_usage/browse.tsx
index 84846f687d..9b118f8382 100644
--- a/apps/client/src/widgets/type_widgets/space_usage/browse.tsx
+++ b/apps/client/src/widgets/type_widgets/space_usage/browse.tsx
@@ -8,13 +8,14 @@ import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
import { formatSize } from "../../../services/utils";
import ActionButton from "../../react/ActionButton";
-import type { DonutRing } from "../../react/charts/DonutChart";
+import type { DonutRing, DonutSegment } from "../../react/charts/DonutChart";
import { useFetch } from "../../react/use_fetch";
import { type ContentChangedHandler, openSpaceUsageContextMenu } from "./context_menu";
import { buildChildrenSegments, type UsageSegmentData } from "./donut_segments";
import { deletedEntitiesLabel } from "./labels";
import NoteUsageDonut, { segmentTooltip } from "./note_usage_donut";
import SpaceUsagePlaceholder from "./placeholder";
+import type { SpaceUsageSelection } from "./selection";
const CHILDREN_RING_RADIUS = 180;
const CHILDREN_RING_THICKNESS = 46;
@@ -27,6 +28,14 @@ interface BrowseProps {
refreshToken: number;
/** Called once the menu deleted something, so the donut stops drawing what is no longer there. */
onContentChanged: ContentChangedHandler;
+ /** The segment drawn as chosen, where the page keeps a selection. See {@link onSelect}. */
+ selectedMarkId?: string;
+ /**
+ * Where given, a click names the segment in the page's strip instead of descending into it: a
+ * touch screen cannot hover a segment to find out what it is, so a tap has to say. Walking into
+ * the child is then what "Show details" does in the menu the strip raises.
+ */
+ onSelect?: (selection: SpaceUsageSelection) => void;
/**
* Reports whether this view is measuring, so the section can keep its refresh button out while
* it is — this view's reading is its own request, which the section cannot otherwise see.
@@ -43,7 +52,7 @@ interface BrowseProps {
* Usage can drop the user straight onto a note here.
*/
export default function Browse({
- path, onPathChange, refreshToken, onContentChanged, onLoadingChange
+ path, onPathChange, refreshToken, onContentChanged, selectedMarkId, onSelect, onLoadingChange
}: BrowseProps) {
const noteId = path[path.length - 1];
const { data: usage, failed, loading } = useFetch<SpaceUsageNoteResponse>(
@@ -56,6 +65,10 @@ export default function Browse({
const titles = useNoteTitles(path, usage);
const getTitle = useCallback((id: string) => titles.get(id) ?? id, [ titles ]);
+ const menuFor = useCallback((childId: string) => (event: MouseEvent) =>
+ void openSpaceUsageContextMenu(event, [ ...path, childId ], onPathChange, onContentChanged),
+ [ path, onPathChange, onContentChanged ]);
+
const childrenRing: DonutRing<UsageSegmentData> = useMemo(() => ({
id: "children",
radius: CHILDREN_RING_RADIUS,
@@ -71,6 +84,16 @@ export default function Browse({
onSegmentClick: (segment) => {
const childId = segment.data?.noteId;
+ if (onSelect) {
+ const selection = selectionOf(segment, path, menuFor);
+
+ if (selection) {
+ onSelect(selection);
+ }
+
+ return;
+ }
+
if (childId) {
onPathChange([ ...path, childId ]);
}
@@ -83,7 +106,7 @@ export default function Browse({
void openSpaceUsageContextMenu(event, [ ...path, childId ], onPathChange, onContentChanged);
}
}
- }), [ usage, getTitle, path, onPathChange, onContentChanged ]);
+ }), [ usage, getTitle, path, onPathChange, onContentChanged, onSelect, menuFor ]);
return (
<div className="space-usage-browse">
@@ -112,6 +135,8 @@ export default function Browse({
title={getTitle(usage.noteId)}
notePath={path}
outerRings={[ childrenRing ]}
+ selectedSegmentId={selectedMarkId}
+ onSelectSegment={onSelect}
onTitleContextMenu={(event) =>
void openSpaceUsageContextMenu(event, path, onPathChange, onContentChanged)}
centerActions={
@@ -132,6 +157,33 @@ export default function Browse({
);
}
+/**
+ * What the strip says about a segment of the children ring: the child note, with the path that
+ * identifies its placement and the menu that acts on it, or one of the two figures closing the ring,
+ * named by what the segment calls itself. "Others" stands for several children at once and answers
+ * nothing, in the strip as in the chart.
+ */
+function selectionOf(
+ segment: DonutSegment<UsageSegmentData>,
+ path: string[],
+ menuFor: (childId: string) => (event: MouseEvent) => void
+): SpaceUsageSelection | null {
+ const childId = segment.data?.noteId;
+
+ if (childId) {
+ return {
+ markId: segment.id,
+ notePath: [ ...path, childId ],
+ size: segment.value,
+ onActivate: menuFor(childId)
+ };
+ }
+
+ return segment.label
+ ? { markId: segment.id, label: segment.label, size: segment.value }
+ : null;
+}
+
/**
* Batch-loads the titles the view needs — the breadcrumb's path and the children ring's tooltips.
* Until (or unless) a title arrives, the ID stands in.
diff --git a/apps/client/src/widgets/type_widgets/space_usage/donut_segments.ts b/apps/client/src/widgets/type_widgets/space_usage/donut_segments.ts
index 86e35f3314..a74a80332d 100644
--- a/apps/client/src/widgets/type_widgets/space_usage/donut_segments.ts
+++ b/apps/client/src/widgets/type_widgets/space_usage/donut_segments.ts
@@ -51,6 +51,7 @@ export function buildCompositionSegments(
id: "body",
value: usage.ownSize,
className: "space-usage-segment-body",
+ label: bodyLabel,
tooltip: makeTooltip("plain", bodyLabel, usage.ownSize),
data: { noteId: usage.noteId }
});
@@ -65,6 +66,7 @@ export function buildCompositionSegments(
id: `attachment/${attachment.attachmentId}`,
value: attachment.size,
className: "space-usage-segment-attachment",
+ label: attachment.title,
tooltip: makeTooltip("attachment", attachment.title, attachment.size),
data: { attachmentId: attachment.attachmentId }
});
@@ -101,6 +103,7 @@ export function buildChildrenSegments(
id: `child/${child.noteId}`,
value: child.subtreeSize,
hue: hueOf(child.noteId),
+ label: getTitle(child.noteId),
tooltip: makeTooltip("child", getTitle(child.noteId), child.subtreeSize),
data: { noteId: child.noteId }
}));
@@ -113,6 +116,7 @@ export function buildChildrenSegments(
id: "revisions",
value: revisionsSize,
className: "space-usage-segment-revisions",
+ label: revisionsLabel,
tooltip: makeTooltip("revisions", revisionsLabel, revisionsSize)
});
}
@@ -122,6 +126,7 @@ export function buildChildrenSegments(
id: "/deleted-notes",
value: usage.deletedNotes.size,
className: "space-usage-segment-deleted",
+ label: deletedNotesLabel,
tooltip: makeTooltip("plain", deletedNotesLabel, usage.deletedNotes.size),
data: {}
});
diff --git a/apps/client/src/widgets/type_widgets/space_usage/index.css b/apps/client/src/widgets/type_widgets/space_usage/index.css
index 4c0558958b..e350eaae04 100644
--- a/apps/client/src/widgets/type_widgets/space_usage/index.css
+++ b/apps/client/src/widgets/type_widgets/space_usage/index.css
@@ -118,10 +118,17 @@ body.mobile {
}
/* Sized by the map it holds rather than by what is left of the screen: the page scrolls, so
- there is no height here to fill. */
+ there is no height here to fill. The room below is the strip's own height, measured by the
+ page: the strip stands over the map until the end of the scroll, and every cell has to be
+ reachable clear of it. */
.space-usage-overview {
order: 2;
flex: 0 0 auto;
+ padding-bottom: calc(8px + var(--space-usage-strip-height, 0px));
+ }
+
+ .space-usage-selection {
+ order: 3;
}
}
diff --git a/apps/client/src/widgets/type_widgets/space_usage/index.tsx b/apps/client/src/widgets/type_widgets/space_usage/index.tsx
index e1211a8577..c856e46e62 100644
--- a/apps/client/src/widgets/type_widgets/space_usage/index.tsx
+++ b/apps/client/src/widgets/type_widgets/space_usage/index.tsx
@@ -1,22 +1,31 @@
import "./index.css";
import type { SpaceUsageOverviewResponse } from "@triliumnext/commons";
-import { useCallback, useMemo, useRef, useState } from "preact/hooks";
+import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n";
-import { formatSize } from "../../../services/utils";
+import { formatSize, isMobile } from "../../../services/utils";
import ActionButton from "../../react/ActionButton";
-import { useStaticTooltip } from "../../react/hooks";
+import { useElementSize, useStaticTooltip } from "../../react/hooks";
import SegmentedChoice from "../../react/SegmentedChoice";
import { useFetch } from "../../react/use_fetch";
import Browse from "./browse";
import { showCleanupDialog } from "./cleanup_dialog";
import Overview from "./overview";
import SpaceUsagePlaceholder from "./placeholder";
+import SelectionStrip, { type SpaceUsageSelection } from "./selection";
/** Matches the server default; the treemap could not label more cells anyway. */
const OVERVIEW_LIMIT = 500;
+/**
+ * Whether a tap on a mark names it rather than acting on it. A touch screen has no hover to identify
+ * a cell or a segment with, so there the charts keep a selection and the strip along the foot of the
+ * page says what is chosen; a pointer needs none of that and keeps its hover and its right-click.
+ * Read once, as the app's other phone branches do.
+ */
+const TAP_SELECTS = isMobile();
+
type SpaceUsageView = "overview" | "browse";
export default function SpaceUsage() {
@@ -33,6 +42,20 @@ export default function SpaceUsage() {
// Browse's position lives here so that "Show details", offered on any note either view draws,
// can land the user on that note — switching the view along the way when it comes from Overview.
const [ browsePath, setBrowsePath ] = useState([ "root" ]);
+ // The chosen mark, of whichever chart is on show: the strip naming it belongs to the page, and
+ // both views fill the same one.
+ const [ selection, setSelection ] = useState<SpaceUsageSelection | null>(null);
+ // Tapping the chosen mark again lets it go, which is what puts the strip back to its hint
+ // without something else having to be picked.
+ const select = useCallback((next: SpaceUsageSelection) =>
+ setSelection((current) => current?.markId === next.markId ? null : next), []);
+ // A selection stands for a mark on the chart in front of the user: switching views, walking to
+ // another note, or taking a fresh reading each leave it naming something that is no longer drawn.
+ useEffect(() => setSelection(null), [ view, browsePath, refreshToken ]);
+ // The strip is measured rather than guessed at: the map is given exactly its height as room to
+ // scroll clear of it, and how tall it stands depends on the theme's own text size.
+ const stripRef = useRef<HTMLDivElement>(null);
+ const stripSize = useElementSize(stripRef);
const showDetails = useCallback((notePath: string[]) => {
setBrowsePath(notePath);
setView("browse");
@@ -44,7 +67,12 @@ export default function SpaceUsage() {
`space-usage/overview?limit=${OVERVIEW_LIMIT}`, refreshToken);
return (
- <div className="space-usage-page">
+ <div
+ className="space-usage-page"
+ // Measured at runtime, so it cannot be a stylesheet value; the map reads it as the room
+ // it leaves below itself (see the stylesheet).
+ style={stripSize ? { "--space-usage-strip-height": `${stripSize.height}px` } : undefined}
+ >
{/* Above the views rather than in the note's own title row, so the controls stay put as
whichever view is on show is redrawn beneath them. */}
<div className="space-usage-toolbar">
@@ -83,11 +111,19 @@ export default function SpaceUsage() {
onPathChange={setBrowsePath}
refreshToken={refreshToken}
onContentChanged={refresh}
+ selectedMarkId={selection?.markId}
+ onSelect={TAP_SELECTS ? select : undefined}
onLoadingChange={setBrowseLoading}
/>
)}
{view === "overview" && (overview
- ? <Overview overview={overview} onShowDetails={showDetails} onContentChanged={refresh} />
+ ? <Overview
+ overview={overview}
+ selectedMarkId={selection?.markId}
+ onSelect={TAP_SELECTS ? select : undefined}
+ onShowDetails={showDetails}
+ onContentChanged={refresh}
+ />
: <SpaceUsagePlaceholder failed={failed} />)}
{/* Overview only: these are whole-database totals, and beside a single note's donut
@@ -115,6 +151,10 @@ export default function SpaceUsage() {
/>
</footer>
)}
+
+ {/* Kept on show with nothing chosen as well, so the map is laid out with room for it
+ from the start and a tap always has somewhere to report to. */}
+ {TAP_SELECTS && <SelectionStrip selection={selection} containerRef={stripRef} />}
</div>
);
}
diff --git a/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.css b/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.css
index 3fbb434cfe..315d74b7a8 100644
--- a/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.css
+++ b/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.css
@@ -9,12 +9,19 @@
ring thickness without becoming a second color. */
--space-usage-revisions-stripe-color: color-mix(in srgb, var(--accented-background-color) 40%, #8f959c 60%);
+ /* These two are painted from a semantic colour rather than from a hue, so they name the hue that
+ colour is built on for the chosen trace to be struck from: the fills above are that hue mixed
+ most of the way into the theme's surface, and this is the same hue at full strength. The
+ revisions gray stands for a tier rather than a thing and has no hue to give, so its trace
+ keeps the primitive's plain default. */
.space-usage-segment-body {
--donut-segment-color: var(--space-usage-body-color);
+ --donut-segment-selected-color: hsl(217 100% 50%);
}
.space-usage-segment-attachment {
--donut-segment-color: var(--space-usage-attachments-color);
+ --donut-segment-selected-color: hsl(42 100% 50%);
}
/* Stroked with the hatch rather than a flat fill: history is not content the user put there,
diff --git a/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.tsx b/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.tsx
index 550b0a0310..509b35bf90 100644
--- a/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.tsx
+++ b/apps/client/src/widgets/type_widgets/space_usage/note_usage_donut.tsx
@@ -9,9 +9,10 @@ import { Trans } from "react-i18next";
import { t } from "../../../services/i18n";
import { formatSize } from "../../../services/utils";
-import DonutChart, { type DonutRing } from "../../react/charts/DonutChart";
+import DonutChart, { type DonutRing, type DonutSegment } from "../../react/charts/DonutChart";
import ContextualHelp from "../../react/ContextualHelp";
import { quickEditAttachment, quickEditNote } from "./context_menu";
+import type { SpaceUsageSelection } from "./selection";
import {
buildCompositionSegments,
noteWeight,
@@ -32,6 +33,13 @@ interface NoteUsageDonutProps {
notePath: string[];
/** Extra rings drawn around the composition ring, e.g. Browse's children ring. */
outerRings?: DonutRing<UsageSegmentData>[];
+ /** The segment drawn as chosen, in any of the rings. See {@link onSelectSegment}. */
+ selectedSegmentId?: string;
+ /**
+ * Where given, a click on a composition segment names it in the page's strip instead of opening
+ * what it stands for: how a touch screen, which cannot hover a segment, reads the ring.
+ */
+ onSelectSegment?: (selection: SpaceUsageSelection) => void;
/** Rendered centered above the title in the hole — e.g. Browse's back button. */
centerActions?: ComponentChildren;
/** Right-clicking the center title, which stands for the note itself. */
@@ -46,7 +54,10 @@ interface NoteUsageDonutProps {
* user was reading. Reusable wherever one note's usage needs breaking down; Browse wraps it with
* its children ring via {@link outerRings}, which is also where history is drawn.
*/
-export default function NoteUsageDonut({ usage, title, notePath, outerRings = [], centerActions, onTitleContextMenu, className }: NoteUsageDonutProps) {
+export default function NoteUsageDonut({
+ usage, title, notePath, outerRings = [], selectedSegmentId, onSelectSegment, centerActions,
+ onTitleContextMenu, className
+}: NoteUsageDonutProps) {
const compositionRing: DonutRing<UsageSegmentData> = useMemo(() => ({
id: "composition",
radius: COMPOSITION_RING_RADIUS,
@@ -61,18 +72,29 @@ export default function NoteUsageDonut({ usage, title, notePath, outerRings = []
// each other segment one of its attachments. The consolidated "Others" bucket stands for
// several at once, so it opens nothing.
onSegmentClick: (segment) => {
+ if (onSelectSegment) {
+ const selection = selectionOf(segment, notePath, onTitleContextMenu);
+
+ if (selection) {
+ onSelectSegment(selection);
+ }
+
+ return;
+ }
+
if (segment.data?.attachmentId) {
quickEditAttachment(notePath, segment.data.attachmentId);
} else if (segment.data?.noteId) {
quickEditNote(notePath);
}
}
- }), [ usage, notePath ]);
+ }), [ usage, notePath, onSelectSegment, onTitleContextMenu ]);
return (
<DonutChart<UsageSegmentData>
rings={[ compositionRing, ...outerRings ]}
+ selectedSegmentId={selectedSegmentId}
className={clsx("note-usage-donut", className)}
defs={<RevisionsHatch />}
>
@@ -117,6 +139,39 @@ export default function NoteUsageDonut({ usage, title, notePath, outerRings = []
);
}
+/**
+ * What the strip says about a composition segment: the note itself for its body, which answers the
+ * same menu as the center title standing for it, and the attachment for one of its own, which the
+ * popup can show. "Others" stands for several attachments at once and answers nothing.
+ */
+function selectionOf(
+ segment: DonutSegment<UsageSegmentData>,
+ notePath: string[],
+ onTitleContextMenu: ((event: MouseEvent) => void) | undefined
+): SpaceUsageSelection | null {
+ const attachmentId = segment.data?.attachmentId;
+
+ if (attachmentId) {
+ return {
+ markId: segment.id,
+ label: segment.label ?? "",
+ size: segment.value,
+ onActivate: () => quickEditAttachment(notePath, attachmentId)
+ };
+ }
+
+ if (segment.data?.noteId) {
+ return {
+ markId: segment.id,
+ notePath,
+ size: segment.value,
+ onActivate: onTitleContextMenu
+ };
+ }
+
+ return null;
+}
+
/**
* The hatch the revisions segment is stroked with — history reads as the same gray as everything
* else that is not live content, textured so it is told apart from a note at a glance.
diff --git a/apps/client/src/widgets/type_widgets/space_usage/overview.tsx b/apps/client/src/widgets/type_widgets/space_usage/overview.tsx
index 1cd1fe6b76..d03cc05ad4 100644
--- a/apps/client/src/widgets/type_widgets/space_usage/overview.tsx
+++ b/apps/client/src/widgets/type_widgets/space_usage/overview.tsx
@@ -1,5 +1,5 @@
import type { SpaceUsageOverviewResponse } from "@triliumnext/commons";
-import { useEffect, useMemo, useState } from "preact/hooks";
+import { useCallback, useEffect, useMemo, useState } from "preact/hooks";
import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
@@ -15,6 +15,7 @@ import {
} from "./context_menu";
import { deletedEntitiesLabel } from "./labels";
import { bucketWeight, buildOverviewModel, type OverviewCell } from "./overview_model";
+import type { SpaceUsageSelection } from "./selection";
/**
* A cell's area covers a note's body and attachments, leaving history out; its tooltip says the
@@ -26,30 +27,42 @@ const INCLUDE_REVISIONS = false;
interface OverviewProps {
overview: SpaceUsageOverviewResponse;
+ /** The cell drawn as chosen, where the page keeps a selection. See {@link onSelect}. */
+ selectedMarkId?: string;
+ /**
+ * Where given, a click names the cell in the page's strip instead of opening the note: the way a
+ * touch screen reads a map whose cells cannot be hovered to find out what they are.
+ */
+ onSelect?: (selection: SpaceUsageSelection) => void;
onShowDetails: ShowDetailsHandler;
/** Called once the menu deleted something, so the map stops drawing what is no longer there. */
onContentChanged: ContentChangedHandler;
}
/** The treemap over the whole database: every large note at its tree location. */
-export default function Overview({ overview, onShowDetails, onContentChanged }: OverviewProps) {
+export default function Overview({
+ overview, selectedMarkId, onSelect, onShowDetails, onContentChanged
+}: OverviewProps) {
const icons = useNoteIcons(overview);
+ // What each bucket is called, on its own: the tooltips below add the size to the name, and the
+ // strip prints that in a column of its own, so the name has to be available without one.
+ const bucketNames = useMemo(() => ({
+ other: t("space_usage.other_notes", { count: overview.otherNotes.noteCount }),
+ hidden: t("space_usage.hidden_notes", { count: overview.hiddenNotes.noteCount }),
+ // Not a crowd of notes but a tier of content, so it names itself rather than a count.
+ revisions: t("space_usage.revisions"),
+ deleted: deletedEntitiesLabel(overview.deletedNotes)
+ }), [ overview ]);
+
// 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 and how much they take.
const model = useMemo(() => buildOverviewModel(overview, {
- 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)
- ),
- // Not a crowd of notes but a tier of content, so it names itself rather than a count — and
- // the figure is the deduplicated one the status line quotes, which is what its cell draws.
- revisionsLabel: withSize(t("space_usage.revisions"), overview.content.revisionsSize),
- deletedNotesLabel: withSize(deletedEntitiesLabel(overview.deletedNotes), overview.deletedNotes.size),
+ otherNotesLabel: withSize(bucketNames.other, bucketWeight(overview.otherNotes, INCLUDE_REVISIONS)),
+ hiddenNotesLabel: withSize(bucketNames.hidden, bucketWeight(overview.hiddenNotes, INCLUDE_REVISIONS)),
+ // The figure is the deduplicated one the status line quotes, which is what its cell draws.
+ revisionsLabel: withSize(bucketNames.revisions, overview.content.revisionsSize),
+ deletedNotesLabel: withSize(bucketNames.deleted, overview.deletedNotes.size),
includeRevisions: INCLUDE_REVISIONS,
getIcon: (noteId) => icons.get(noteId),
// A cell's area covers the note's body and its attachments together; where attachments are
@@ -60,34 +73,85 @@ export default function Overview({ overview, onShowDetails, onContentChanged }:
attachmentsSize: formatSize(attachmentsSize)
})
: t("space_usage.cell_size", { size: formatSize(size) })
- }), [ overview, icons ]);
+ }), [ overview, bucketNames, icons ]);
+
+ /**
+ * What a cell answers a menu request with: the note menu, or, for the buckets that have anything
+ * to offer, what can be done to the crowd they stand for. One builder for the two ways of asking,
+ * a right-click on the cell and a tap on the strip naming it.
+ */
+ const menuFor = useCallback((item: TreemapItem<OverviewCell>) => {
+ const bucket = item.data?.bucket;
+
+ if (bucket === "deleted") {
+ return (event: MouseEvent) => void openDeletedNotesContextMenu(event, onContentChanged);
+ }
+
+ if (bucket === "revisions") {
+ return (event: MouseEvent) => void openRevisionsContextMenu(event, onContentChanged);
+ }
+
+ const notePath = item.data?.notePath;
+
+ // The other two buckets stand for crowds nothing can be done to as a whole.
+ if (bucket || !notePath?.length) {
+ return undefined;
+ }
+
+ return (event: MouseEvent) =>
+ void openSpaceUsageContextMenu(event, notePath, onShowDetails, onContentChanged);
+ }, [ onShowDetails, onContentChanged ]);
return (
<div className="space-usage-overview">
<Treemap<OverviewCell>
root={model}
- onItemClick={(item) => withCellPath(item, quickEditNote)}
- onItemContextMenu={(item, event) => {
- // The buckets name no note, so those with something to offer answer with what
- // can be done to the crowd they stand for rather than with the note menu.
- if (item.data?.bucket === "deleted") {
- void openDeletedNotesContextMenu(event, onContentChanged);
+ selectedItemId={selectedMarkId}
+ onItemClick={(item) => {
+ if (!onSelect) {
+ withCellPath(item, quickEditNote);
return;
}
- if (item.data?.bucket === "revisions") {
- void openRevisionsContextMenu(event, onContentChanged);
- return;
- }
+ // A tap names the cell rather than acting on it: the strip is where a touch
+ // screen reads what a pointer would have hovered.
+ const selection = selectionOf(item, bucketNames, menuFor(item));
- withCellPath(item, (notePath) =>
- void openSpaceUsageContextMenu(event, notePath, onShowDetails, onContentChanged));
+ if (selection) {
+ onSelect(selection);
+ }
}}
+ onItemContextMenu={(item, event) => menuFor(item)?.(event)}
/>
</div>
);
}
+/**
+ * What the strip says about a cell: the note it stands for, or the name of the crowd it stands for,
+ * and the size its area encodes either way. A cell naming neither is not worth selecting.
+ */
+function selectionOf(
+ item: TreemapItem<OverviewCell>,
+ bucketNames: Record<NonNullable<OverviewCell["bucket"]>, string>,
+ onActivate: ((event: MouseEvent) => void) | undefined
+): SpaceUsageSelection | null {
+ const bucket = item.data?.bucket;
+ const notePath = item.data?.notePath;
+
+ if (!bucket && !notePath?.length) {
+ return null;
+ }
+
+ return {
+ markId: item.id,
+ ...(bucket ? { label: bucketNames[bucket] } : { notePath }),
+ // The weight the cell was laid out with, which is what its area stands for.
+ size: item.value ?? 0,
+ onActivate
+ };
+}
+
/**
* Batch-loads the icons the cells wear — one froca call for the whole ranking, not one per cell.
* The map draws immediately and picks the icons up on the next render: an icon is an extra reading
diff --git a/apps/client/src/widgets/type_widgets/space_usage/selection.css b/apps/client/src/widgets/type_widgets/space_usage/selection.css
new file mode 100644
index 0000000000..f07ac088b6
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/space_usage/selection.css
@@ -0,0 +1,85 @@
+/*
+ * The chosen mark, named along the foot of the screen. Two rows of text with the size beside them:
+ * where the note is, what it is called, and how much it takes.
+ *
+ * Sticky rather than fixed, and the last thing on the page, so at the end of the scroll it settles
+ * below the map instead of over it. The page leaves the map room for it besides (see index.css).
+ */
+.space-usage-selection {
+ display: grid;
+ position: sticky;
+ bottom: 0;
+ /* With the map, which lifts a chosen cell to 2, and under the toolbar's own 10. */
+ z-index: 3;
+ grid-template-columns: minmax(0, 1fr) auto;
+ align-items: center;
+ column-gap: 12px;
+ /* Two rows of text plus the box around them, held whether or not both rows are filled: the strip
+ is always on screen and the map is given room for exactly this much, so its height must not
+ follow what is in it. Both rows state their own leading (1.2) precisely so this can be added
+ up: 0.75em and 1.2em of text come to 2.34em, and the box adds the name's 4px lead, the 16px
+ padding and the rule. Themes set the page's leading themselves, which is why the rows do not
+ inherit it here. */
+ min-height: calc(2.34em + 21px);
+ border-top: 1px solid var(--main-border-color);
+ background: var(--main-background-color);
+ padding-block: 8px;
+}
+
+/* What to do, while nothing has been tapped: the strip is the map's legend on a touch screen, and it
+ has to say so before it can be used. Set against both rows, which is what holds the height. */
+.space-usage-selection-hint {
+ grid-row: 1 / span 2;
+ grid-column: 1 / span 2;
+ color: var(--muted-text-color);
+ text-align: center;
+}
+
+.space-usage-selection-actionable {
+ cursor: pointer;
+}
+
+/*
+ * Where the note sits, as its ancestors' names. Scrolled by dragging it: a path is as long as it is,
+ * and cutting it short would take off the end that says which of several like-named notes this is.
+ * The bar itself is hidden, having no room of its own to take on a line this size.
+ */
+.space-usage-selection-path {
+ grid-row: 1;
+ grid-column: 1;
+ overflow-x: auto;
+ color: var(--muted-text-color);
+ font-size: 0.75em;
+ line-height: 1.2;
+ white-space: nowrap;
+ text-transform: uppercase;
+ scrollbar-width: none;
+}
+
+.space-usage-selection-path::-webkit-scrollbar {
+ display: none;
+}
+
+/* The note itself, so it is the line the eye lands on. Cut short rather than scrolled: this is one
+ name, and its beginning is the part that identifies it. */
+.space-usage-selection-name {
+ grid-row: 2;
+ grid-column: 1;
+ margin-block-start: 4px;
+ overflow: hidden;
+ font-size: 1.2em;
+ font-weight: 500;
+ line-height: 1.2;
+ white-space: nowrap;
+ text-overflow: ellipsis;
+}
+
+/* Set against both rows, being what the whole strip is about. */
+.space-usage-selection-size {
+ grid-row: 1 / span 2;
+ grid-column: 2;
+ margin-inline-start: 12px;
+ font-weight: 600;
+ letter-spacing: .4pt;
+ white-space: nowrap;
+}
diff --git a/apps/client/src/widgets/type_widgets/space_usage/selection.tsx b/apps/client/src/widgets/type_widgets/space_usage/selection.tsx
new file mode 100644
index 0000000000..edef4f74a3
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/space_usage/selection.tsx
@@ -0,0 +1,102 @@
+import "./selection.css";
+
+import clsx from "clsx";
+import type { RefObject } from "preact";
+import { useEffect, useState } from "preact/hooks";
+
+import froca from "../../../services/froca";
+import { t } from "../../../services/i18n";
+import { formatSize } from "../../../services/utils";
+
+/**
+ * A mark the user picked out of a chart: which one it is, what it stands for, and what can be done
+ * with it. Held by the page rather than by either view, since the strip naming it belongs to the
+ * page and both views fill it.
+ */
+export interface SpaceUsageSelection {
+ /** The mark's own id, which is how the chart knows to draw it as chosen. */
+ markId: string;
+ /**
+ * Note IDs from the root (inclusive) down to the note, where the mark stands for one — the strip
+ * reads the name and the path it shows out of this.
+ */
+ notePath?: string[];
+ /** What to call a mark that stands for no note: a bucket, or one of a note's attachments. */
+ label?: string;
+ /** The size the mark's area encodes, in bytes. */
+ size: number;
+ /** What a tap on the strip raises. Absent for a mark with nothing to offer, such as a crowd. */
+ onActivate?: (event: MouseEvent) => void;
+}
+
+/**
+ * What the chosen mark is, along the foot of the screen: where the mark is, what it is called, and
+ * how much it takes. This is how a chart identifies itself where there is no hover to do it, a phone,
+ * so it says what the tooltip would have said, and a tap on it raises the same menu a right-click
+ * raises on a desktop.
+ *
+ * It stands there with nothing chosen too, saying what to do instead: the strip is the map's legend
+ * on a touch screen, and a legend that appears only once you have guessed the gesture is no legend.
+ */
+export default function SelectionStrip({ selection, containerRef }: {
+ selection: SpaceUsageSelection | null,
+ /** Held by the page, which measures the strip to leave the map room to scroll clear of it. */
+ containerRef?: RefObject<HTMLDivElement>
+}) {
+ const titles = usePathTitles(selection?.notePath);
+ // The note's own title is the last of them; anything above it is where the note sits. A mark
+ // standing for no note carries its name instead, and has no location to show.
+ const name = selection?.notePath ? titles[titles.length - 1] ?? "" : selection?.label ?? "";
+ const path = titles.slice(0, -1);
+
+ return (
+ <div
+ ref={containerRef}
+ className={clsx("space-usage-selection", selection?.onActivate && "space-usage-selection-actionable")}
+ onClick={(event) => selection?.onActivate?.(event)}
+ >
+ {selection ? (
+ <>
+ <span className="space-usage-selection-path">{path.join(" › ")}</span>
+ <span className="space-usage-selection-name">{name}</span>
+ <span className="space-usage-selection-size">{formatSize(selection.size)}</span>
+ </>
+ ) : (
+ <span className="space-usage-selection-hint">{t("space_usage.selection_hint")}</span>
+ )}
+ </div>
+ );
+}
+
+/**
+ * The titles along the selected path, in path order, standing in with the note IDs until they
+ * arrive. One froca call for the whole chain, and one selection is ever shown at a time, so the
+ * strip asks again only when a different mark is picked.
+ */
+function usePathTitles(notePath: string[] | undefined) {
+ const [ titles, setTitles ] = useState<string[]>([]);
+
+ useEffect(() => {
+ if (!notePath) {
+ setTitles([]);
+ return;
+ }
+
+ let cancelled = false;
+
+ // Silent: the reading the mark came from predates the tap, so the note may already be gone.
+ void froca.getNotes(notePath, true).then((notes) => {
+ if (!cancelled) {
+ const byNoteId = new Map(notes.map((note) => [ note.noteId, note.title ]));
+
+ setTitles(notePath.map((noteId) => byNoteId.get(noteId) ?? noteId));
+ }
+ });
+
+ return () => {
+ cancelled = true;
+ };
+ }, [ notePath ]);
+
+ return titles;
+}