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
fix(attributes): show the other notes as a bottom sheet on a phone
Elian Doran5c91bfe3b62a
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.tsindex 420b59b647..6bcae0467c 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts@@ -202,6 +202,54 @@ describe("attribute detail popup naming", () => {});});+describe("related notes menu", () => {+ it("lists the notes and the search entry as the dropdown does, titles escaped for the menu", async () => {+ const { showRelatedNotesMenu } = await import("./attribute_detail");+ const { default: contextMenu } = await import("../../menus/context_menu");+ const { default: appContext } = await import("../../components/app_context");++ const show = vi.spyOn(contextMenu, "show").mockResolvedValue();+ const triggerCommand = vi.spyOn(appContext, "triggerCommand").mockResolvedValue(undefined);+ // The tab manager only exists once the app has started, so the stub stands it in whole.+ const setNote = vi.fn();+ const previousTabManager = appContext.tabManager;+ appContext.tabManager = { getActiveContext: () => ({ setNote }) } as unknown as typeof appContext.tabManager;++ showRelatedNotesMenu({ pageX: 12, pageY: 34 } as MouseEvent, {+ notes: [{ notePath: "root/abc", icon: "tn-icon bx bx-file", title: "Tolkien & <sons>" }],+ otherCount: 5+ }, { type: "label", name: "author", value: "" });++ expect(show).toHaveBeenCalledOnce();+ const options = show.mock.calls[0][0];+ expect(options).toMatchObject({ x: 12, y: 34 });++ const [ noteItem, separator, searchItem ] = options.items;+ // The menu reads a title as HTML, which the note's own is not.+ expect(noteItem).toMatchObject({+ title: "Tolkien & <sons>",+ uiIcon: "tn-icon bx bx-file"+ });+ expect(separator).toEqual({ kind: "separator" });+ expect(searchItem).toMatchObject({ uiIcon: "bx bx-search-alt" });++ // A note entry navigates to it; the search entry searches for every carrier of the name.+ const pressEvent = {} as JQuery.MouseDownEvent<HTMLElement, undefined, HTMLElement, HTMLElement>;+ if ("handler" in noteItem) {+ noteItem.handler?.(noteItem, pressEvent);+ }+ expect(setNote).toHaveBeenCalledWith("root/abc");++ if ("handler" in searchItem) {+ searchItem.handler?.(searchItem, pressEvent);+ }+ expect(triggerCommand).toHaveBeenCalledWith("searchNotes", { searchString: "#author" });++ appContext.tabManager = previousTabManager;+ vi.restoreAllMocks();+ });+});+const NO_OFFSET = { top: 0, left: 0 };function opts(overrides: Partial<AttributeDetailOpts>): AttributeDetailOpts {diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex a86b96adf9..0a64f6d5d1 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -7,6 +7,7 @@ import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, uimport appContext from "../../components/app_context.js";import { isDefinitionName } from "../../entities/fattribute.js";+import contextMenu, { MenuItem } from "../../menus/context_menu.js";import type { Attribute } from "../../services/attribute_parser.js";import { getBuiltinLabelSelectOptions, getBuiltinLabelValueType, isBuiltinAttribute } from "../../services/attributes.js";import { isExperimentalFeatureEnabled } from "../../services/experimental_features.js";@@ -870,7 +871,7 @@ interface SearchRelatedResponse {interface RelatedNotesResult {/** The first {@link DISPLAYED_NOTES} other notes carrying the attribute, by best path. */- notes: { notePath: string; icon: string }[];+ notes: { notePath: string; icon: string; title: string }[];/** How many notes other than the current one carry the attribute, however many of them are listed. */otherCount: number;}@@ -905,7 +906,8 @@ function RelatedNotesBadge({ attribute, currentNoteId }: { attribute: Attribute;setRelated({notes: notes.map((note) => ({notePath: note.getBestNotePathString(hoistedNoteId),- icon: note.getIcon()+ icon: note.getIcon(),+ title: note.title})),// The server counts every match, the current note included, but only returns the first// twenty of them — so past twenty matches, a current note that did not make the cut leaves@@ -943,6 +945,20 @@ function RelatedNotesBadge({ attribute, currentNoteId }: { attribute: Attribute;);}+ // On a phone the list is a bottom-sheet menu, as everything a thumb presses opens one: the+ // dropdown is nested in what is a sliding pane of the note attributes modal there, whose+ // scroll containers and slide transforms clip and misplace it. No tooltip — nothing hovers.+ if (IS_MOBILE) {+ return (+ <Badge+ className="related-notes-badge"+ icon="bx bx-file"+ text={t("attribute_detail.other_notes_count", { count: related.otherCount })}+ onClick={(e) => showRelatedNotesMenu(e, related, attribute)}+ />+ );+ }+return (<BadgeWithDropdownclassName="related-notes-badge"@@ -971,6 +987,41 @@ function RelatedNotesBadge({ attribute, currentNoteId }: { attribute: Attribute;);}+/** Whether the badge opens a menu rather than a dropdown, read once: it does not change under the app. */+const IS_MOBILE = utils.isMobile();++/**+ * The related notes as the bottom-sheet menu the phone presents its menus in (the context menu+ * service puts it there itself, behind a cover). One entry per note, and the search entry after+ * them, exactly as the desktop dropdown lists them. Navigating needs no closing of its own:+ * setNote closes the active dialog, the note attributes modal included, and searchNotes+ * navigates to the search note the same way.+ */+export function showRelatedNotesMenu(e: MouseEvent, related: RelatedNotesResult, attribute: Attribute) {+ const items: MenuItem<string>[] = [+ // The title is HTML to the menu, which the note's title is not.+ ...related.notes.map(({ notePath, icon, title }) => ({+ title: utils.escapeHtml(title),+ uiIcon: icon,+ handler: () => void appContext.tabManager.getActiveContext()?.setNote(notePath)+ })),+ { kind: "separator" as const },+ {+ title: t("attribute_detail.show_all_in_search"),+ uiIcon: "bx bx-search-alt",+ handler: () => void appContext.triggerCommand("searchNotes", { searchString: formatAttributeForSearch(attribute) })+ }+ ];++ // The coordinates go unused at the bottom of the screen, where the menu is placed on a phone.+ void contextMenu.show({+ x: e.pageX,+ y: e.pageY,+ items,+ selectMenuItemHandler: () => {}+ });+}+/** The query for every note carrying the attribute, mirroring the name-only search behind the count. */export function formatAttributeForSearch({ type, name }: Attribute) {// Names are filtered as they are typed, so there is nothing in one that would need quoting.