Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/space usage tool: improve the browse view on mobile
Adorian Doran6847f42c5794
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1dd367a3b1..869b301b65 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3121,6 +3121,7 @@"status_attachments": "Attachments","status_deleted_items": "Deleted items","selection_hint": "Tap an item for details",+ "show_selection_details": "Show selection 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/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsxindex f23c7ecfb8..459ca575e2 100644--- a/apps/client/src/widgets/react/ActionButton.tsx+++ b/apps/client/src/widgets/react/ActionButton.tsx@@ -17,6 +17,13 @@ export interface ActionButtonProps extends Pick<HTMLAttributes<HTMLButtonElementtooltipHtml?: boolean;icon: string;className?: string;+ /**+ * Keeps the tooltip off a touch screen, where there is no hover to open it with: the tap that+ * presses the button opens it instead, and it then sits over whatever the press brought up until+ * something else takes the focus. For a button whose meaning is already plain where it stands.+ * The label is still given to assistive technology, which is what the tooltip was carrying it for.+ */+ noTooltipOnTouch?: boolean;triggerCommand?: CommandNames;noIconActionClass?: boolean;frame?: boolean;@@ -26,7 +33,7 @@ export interface ActionButtonProps extends Pick<HTMLAttributes<HTMLButtonElementconst cachedIsMobile = isMobile();-export default function ActionButton({ text, icon, className, triggerCommand, titlePosition, tooltipClass, tooltipHtml, noIconActionClass, frame, active, disabled, ...restProps }: ActionButtonProps) {+export default function ActionButton({ text, icon, className, triggerCommand, titlePosition, tooltipClass, tooltipHtml, noIconActionClass, noTooltipOnTouch, frame, active, disabled, ...restProps }: ActionButtonProps) {const buttonRef = useRef<HTMLButtonElement>(null);const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();@@ -35,7 +42,8 @@ export default function ActionButton({ text, icon, className, triggerCommand, ti: text;const titleRef = useRef(title);titleRef.current = title;- const hasTitle = !!title && title.length > 0;+ const declined = !!noTooltipOnTouch && cachedIsMobile;+ const hasTitle = !!title && title.length > 0 && !declined;// The tooltip is recreated only when its structural options (or its presence) change — not when// the label text changes. A plain text change is pushed into the live tooltip via setContent@@ -74,6 +82,9 @@ export default function ActionButton({ text, icon, className, triggerCommand, titype="button"class={`${className ?? ""} ${!noIconActionClass ? "icon-action" : "btn"} ${icon} ${frame ? "btn btn-primary" : ""} ${disabled ? "disabled" : ""} ${active ? "active" : ""}`}data-trigger-command={triggerCommand}+ // Only where the tooltip that would otherwise carry it has been declined: elsewhere the+ // tooltip is the button's name, and a second one here would be read out beside it.+ aria-label={declined ? title : undefined}disabled={disabled}{...restProps}/>;diff --git a/apps/client/src/widgets/react/ScrollableLabel.tsx b/apps/client/src/widgets/react/ScrollableLabel.tsxindex 498c0117ad..163cc57124 100644--- a/apps/client/src/widgets/react/ScrollableLabel.tsx+++ b/apps/client/src/widgets/react/ScrollableLabel.tsx@@ -36,6 +36,11 @@ interface ScrollableLabelProps {export default function ScrollableLabel({ children, className, autoScroll }: ScrollableLabelProps) {const ref = useRef<HTMLDivElement>(null);const [ fades, setFades ] = useState({ start: false, end: false });+ // How much of the line lies outside the box. Kept because the walk below starts over whenever it+ // changes, which is what walks a label whose words arrived after it did — a path waiting on its+ // titles — without asking the caller to say so, and without restarting on a render that changed+ // nothing (children given as elements rather than as text are a new value every time).+ const [ overflow, setOverflow ] = useState(0);// Set by the reader's own first scroll, and cleared only by a new label: a walk they interrupted// must not start again while the same line is still on screen.const handedOver = useRef(false);@@ -54,6 +59,9 @@ export default function ScrollableLabel({ children, className, autoScroll }: Scr// same line towards negative offsets, and both ends are the same question either way.const travelled = Math.abs(element.scrollLeft);const total = element.scrollWidth - element.clientWidth;++ setOverflow(total);+const next = {start: travelled > EDGE_EPSILON,end: travelled < total - EDGE_EPSILON@@ -130,9 +138,9 @@ export default function ScrollableLabel({ children, className, autoScroll }: Scrframe = requestAnimationFrame(step);return () => cancelAnimationFrame(frame);- // Restarted per label, which is also how a path that arrived after its own strip did gets- // walked: the titles land as a second render.- }, [ autoScroll, children ]);+ // Restarted when the line's length changes rather than when its markup does, so a label+ // rendered again with the same words is left walking where it was.+ }, [ autoScroll, overflow ]);return (<divdiff --git a/apps/client/src/widgets/react/charts/chart_tooltip.tsx b/apps/client/src/widgets/react/charts/chart_tooltip.tsxindex da9f211787..5d70cb655d 100644--- a/apps/client/src/widgets/react/charts/chart_tooltip.tsx+++ b/apps/client/src/widgets/react/charts/chart_tooltip.tsx@@ -3,6 +3,16 @@ import "./chart_tooltip.css";import clsx from "clsx";import { useRef, useState } from "preact/hooks";+import { isMobile } from "../../../services/utils";++/**+ * A touch screen has no pointer for the bubble to follow: the tap that stands in for a hover leaves it+ * behind, over the chart, until something else is tapped. What it was there to say is said by the+ * selection strip instead, which names the mark that was actually chosen. Read once, as the app's+ * other phone branches do.+ */+const SHOWS_TOOLTIPS = !isMobile();+interface ChartTooltipState {text: string;/** Viewport coordinates: the bubble is positioned fixed, so it escapes any clipping ancestor. */@@ -41,9 +51,9 @@ export function useChartTooltip<T extends HTMLElement>() {setTooltip((current) => current && { ...current, ...positionOf(event) }),onMouseLeave: () => setTooltip(null)},- /** Called by a mark on hover; a mark without tooltip text shows nothing. */+ /** Called by a mark on hover; a mark without tooltip text, or a phone, shows nothing. */showTooltip: (text: string | undefined, event: { clientX: number, clientY: number }) =>- setTooltip(text ? { text, ...positionOf(event) } : null),+ setTooltip(text && SHOWS_TOOLTIPS ? { text, ...positionOf(event) } : null),hideTooltip: () => setTooltip(null),/** Render inside the chart, after its marks. */tooltipNode: tooltip && (diff --git a/apps/client/src/widgets/type_widgets/space_usage/browse.css b/apps/client/src/widgets/type_widgets/space_usage/browse.cssindex b6b2cc669a..d99677a8f6 100644--- a/apps/client/src/widgets/type_widgets/space_usage/browse.css+++ b/apps/client/src/widgets/type_widgets/space_usage/browse.css@@ -5,14 +5,18 @@min-height: 0;.space-usage-breadcrumb {- display: flex;flex: 0 0 auto;- justify-content: center;+ padding-block: 6px;+ }++ /* The line itself, which is what scrolls: centred while it fits, and left where it starts once it+ does not — `safe` is what keeps the first crumb reachable, since a centred overflow pushes the+ beginning out of the scrollport with no way back to it. */+ .space-usage-breadcrumb-track {+ display: flex;+ justify-content: safe center;align-items: center;gap: 0.35em;- padding-block: 6px;- overflow: hidden;- white-space: nowrap;}/* Names what the path leads to, so a lone root crumb still reads as a location. */@@ -23,6 +27,7 @@.space-usage-crumb {display: inline-block;+ flex: 0 0 auto;overflow: hidden;max-width: 14em;border: none;@@ -59,6 +64,41 @@padding-block: 8px;}+ /* The way into the chosen child, for a reader who has not found the second tap that does the+ same. Its line is held whether or not anything is chosen, so the chart above is laid out once+ and a tap on the ring never resizes it; one line always, the label being short enough that a+ narrow screen should cut it rather than fold the row open. */+ .space-usage-browse-details {+ display: flex;+ flex: 0 0 auto;+ justify-content: center;+ align-items: center;+ /* Clear of the strip below, whose own rule would otherwise sit right under the label. */+ margin-bottom: 1.5em;+ /* The line it holds, at the leading stated below, plus the room around it. */+ min-height: calc(1.2em + 12px);+ padding-block: 6px;+ overflow: hidden;+ line-height: 1.2;+ white-space: nowrap;+ }++ /* Reads as the link it is, like the note's name in the middle of the chart. */+ .space-usage-details-link {+ overflow: hidden;+ border: none;+ background: none;+ padding: 0;+ color: var(--link-color);+ text-overflow: ellipsis;+ white-space: nowrap;+ cursor: pointer;+ }++ .space-usage-details-link:hover {+ text-decoration: underline;+ }+/* At the root there is nowhere to go back to: the arrow disappears but keeps its box, so thecenter text doesn't jump between levels. */.space-usage-back.disabled {diff --git a/apps/client/src/widgets/type_widgets/space_usage/browse.tsx b/apps/client/src/widgets/type_widgets/space_usage/browse.tsxindex 9b118f8382..1063fbd72d 100644--- a/apps/client/src/widgets/type_widgets/space_usage/browse.tsx+++ b/apps/client/src/widgets/type_widgets/space_usage/browse.tsx@@ -13,6 +13,7 @@ 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 ScrollableLabel from "../../react/ScrollableLabel";import NoteUsageDonut, { segmentTooltip } from "./note_usage_donut";import SpaceUsagePlaceholder from "./placeholder";import type { SpaceUsageSelection } from "./selection";@@ -28,8 +29,11 @@ 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;+ /**+ * The mark the page is holding as chosen, drawn as such and offered by the link under the chart.+ * See {@link onSelect}.+ */+ selection?: SpaceUsageSelection | null;/*** 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@@ -52,7 +56,7 @@ interface BrowseProps {* Usage can drop the user straight onto a note here.*/export default function Browse({- path, onPathChange, refreshToken, onContentChanged, selectedMarkId, onSelect, onLoadingChange+ path, onPathChange, refreshToken, onContentChanged, selection, onSelect, onLoadingChange}: BrowseProps) {const noteId = path[path.length - 1];const { data: usage, failed, loading } = useFetch<SpaceUsageNoteResponse>(@@ -85,7 +89,8 @@ export default function Browse({const childId = segment.data?.noteId;if (onSelect) {- const selection = selectionOf(segment, path, menuFor);+ const selection = selectionOf(segment, path, menuFor,+ (id) => onPathChange([ ...path, id ]));if (selection) {onSelect(selection);@@ -111,21 +116,27 @@ export default function Browse({return (<div className="space-usage-browse"><nav className="space-usage-breadcrumb">- <span className="space-usage-crumb-label">{t("space_usage.current_note")}</span>- {path.map((id, index) => (- <Fragment key={`${index}/${id}`}>- {index > 0 && <span className="space-usage-crumb-separator" aria-hidden="true">›</span>}- {index < path.length - 1 ? (- <button- type="button"- className="space-usage-crumb"- onClick={() => onPathChange(path.slice(0, index + 1))}- >{getTitle(id)}</button>- ) : (- <span className="space-usage-crumb space-usage-crumb-current">{getTitle(id)}</span>- )}- </Fragment>- ))}+ {/* The whole line travels together — what it says and where it leads are one sentence+ — so it is swiped rather than cut off, and fades at whichever end it carries on+ past. A deep path is otherwise clipped at both ends by a row that centres itself,+ and the end it loses is the note being looked at. */}+ <ScrollableLabel className="space-usage-breadcrumb-track" autoScroll>+ <span className="space-usage-crumb-label">{t("space_usage.current_note")}</span>+ {path.map((id, index) => (+ <Fragment key={`${index}/${id}`}>+ {index > 0 && <span className="space-usage-crumb-separator" aria-hidden="true">›</span>}+ {index < path.length - 1 ? (+ <button+ type="button"+ className="space-usage-crumb"+ onClick={() => onPathChange(path.slice(0, index + 1))}+ >{getTitle(id)}</button>+ ) : (+ <span className="space-usage-crumb space-usage-crumb-current">{getTitle(id)}</span>+ )}+ </Fragment>+ ))}+ </ScrollableLabel></nav>{usage ? (@@ -135,7 +146,7 @@ export default function Browse({title={getTitle(usage.noteId)}notePath={path}outerRings={[ childrenRing ]}- selectedSegmentId={selectedMarkId}+ selectedSegmentId={selection?.markId}onSelectSegment={onSelect}onTitleContextMenu={(event) =>void openSpaceUsageContextMenu(event, path, onPathChange, onContentChanged)}@@ -144,6 +155,10 @@ export default function Browse({className="space-usage-back"icon="bx bx-arrow-back"text={t("space_usage.back")}+ // An arrow in the middle of the chart it walks back up: there is+ // nothing a tooltip could add, and on a touch screen the tap that+ // presses it would leave one standing over the donut.+ noTooltipOnTouchdisabled={path.length === 1}onClick={() => path.length > 1 && onPathChange(path.slice(0, -1))}/>@@ -153,6 +168,22 @@ export default function Browse({) : (<SpaceUsagePlaceholder failed={failed} />)}++ {/* What the second tap on a chosen child does, said in words: the tap is quick once it is+ known about, and nothing on the ring says it is there. Offered only where a selection+ is being kept at all, and kept in the layout whether or not one is — a line arriving+ and leaving would resize the chart above it at every tap. */}+ {onSelect && (+ <div className="space-usage-browse-details">+ {selection?.onOpen && (+ <button+ type="button"+ className="space-usage-details-link"+ onClick={selection.onOpen}+ >{t("space_usage.show_selection_details")}</button>+ )}+ </div>+ )}</div>);}@@ -166,7 +197,8 @@ export default function Browse({function selectionOf(segment: DonutSegment<UsageSegmentData>,path: string[],- menuFor: (childId: string) => (event: MouseEvent) => void+ menuFor: (childId: string) => (event: MouseEvent) => void,+ onDescend: (childId: string) => void): SpaceUsageSelection | null {const childId = segment.data?.noteId;@@ -175,7 +207,10 @@ function selectionOf(markId: segment.id,notePath: [ ...path, childId ],size: segment.value,- onActivate: menuFor(childId)+ onActivate: menuFor(childId),+ // Walking in is what a click does here on a desktop, and a second tap is what a touch+ // screen has left once the first has been spent naming the child.+ onOpen: () => onDescend(childId)};}diff --git a/apps/client/src/widgets/type_widgets/space_usage/index.tsx b/apps/client/src/widgets/type_widgets/space_usage/index.tsxindex a91fb238ca..14e5acd2a4 100644--- a/apps/client/src/widgets/type_widgets/space_usage/index.tsx+++ b/apps/client/src/widgets/type_widgets/space_usage/index.tsx@@ -46,10 +46,18 @@ export default function SpaceUsage() {// 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), []);+ // The first tap names the mark; a second on the same one opens it, which is how a touch screen+ // gets both without a gesture to guess at. A mark with nothing to open lets go instead, which is+ // also what puts the strip back to its hint without something else having to be picked.+ const select = useCallback((next: SpaceUsageSelection) => setSelection((current) => {+ if (current?.markId !== next.markId) {+ return next;+ }++ next.onOpen?.();++ return next.onOpen ? current : null;+ }), []);// 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 ]);@@ -112,7 +120,7 @@ export default function SpaceUsage() {onPathChange={setBrowsePath}refreshToken={refreshToken}onContentChanged={refresh}- selectedMarkId={selection?.markId}+ selection={selection}onSelect={IS_MOBILE ? select : undefined}onLoadingChange={setBrowseLoading}/>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.tsxindex 509b35bf90..7ad793ae39 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@@ -143,6 +143,11 @@ export default function NoteUsageDonut({* 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.+ *+ * None of them carries an {@link SpaceUsageSelection.onOpen}: this ring is only ever selected from+ * where a tap has to stand in for a hover, and a mark there opens something only where the opening is+ * a move within the chart. Walking into a child is that; throwing a popup over the chart from a+ * second tap on a segment is not, so the segments name themselves and leave it at that.*/function selectionOf(segment: DonutSegment<UsageSegmentData>,diff --git a/apps/client/src/widgets/type_widgets/space_usage/overview.tsx b/apps/client/src/widgets/type_widgets/space_usage/overview.tsxindex d03cc05ad4..e33aa47fa3 100644--- a/apps/client/src/widgets/type_widgets/space_usage/overview.tsx+++ b/apps/client/src/widgets/type_widgets/space_usage/overview.tsx@@ -148,7 +148,9 @@ function selectionOf(...(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+ onActivate,+ // The same the note answers a click with on a desktop, where no tap had to name it first.+ ...(notePath?.length && !bucket ? { onOpen: () => quickEditNote(notePath) } : {})};}diff --git a/apps/client/src/widgets/type_widgets/space_usage/selection.tsx b/apps/client/src/widgets/type_widgets/space_usage/selection.tsxindex b20c0b3029..e362582932 100644--- a/apps/client/src/widgets/type_widgets/space_usage/selection.tsx+++ b/apps/client/src/widgets/type_widgets/space_usage/selection.tsx@@ -28,6 +28,13 @@ export interface SpaceUsageSelection {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 a second tap on the mark itself does, once it is the chosen one: walking into a child in+ * Browse, opening the note in Overview. This is what keeps a touch screen's first tap free to+ * name the mark rather than act on it — naming it would otherwise cost the reader the action.+ * Absent for a mark that stands for a crowd, which has nothing to be opened.+ */+ onOpen?: () => void;}/**