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(attachments): say what is missing where only Trilium's own are left
Elian Doran9457af6570e6
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 9da07847bd..4b16f6b1cc 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1479,6 +1479,7 @@"owning_note": "Owning note: ","upload_attachments": "Upload attachments","no_attachments": "This note has no attachments.",+ "no_user_attachments": "This note has no attachments of its own.\nThe ones Trilium generated for it are below.","system_group": "Generated by Trilium ({{amount}})"},"book": {diff --git a/apps/client/src/widgets/type_widgets/Attachment.css b/apps/client/src/widgets/type_widgets/Attachment.cssindex 2efc2ece42..1ba08e7301 100644--- a/apps/client/src/widgets/type_widgets/Attachment.css+++ b/apps/client/src/widgets/type_widgets/Attachment.css@@ -175,11 +175,17 @@/* The placeholder stands in for the listing rather than for the pane. Its own `height: 100%` ismeasured against the whole body, which on a note whose only attachments are the app's would- push the row offering them past the bottom edge; growing into whatever that row leaves over- centres it in exactly the same way, and identically where there is no such row. */+ push the row offering them past the bottom edge. */> .no-items {height: auto;}++ /* And where there is such a row, the placeholder stops growing into the space above it as well, so+ that the two read as the one answer they are rather than as a message and, an inch of nothing+ later, an unrelated line at the foot of the pane. */+ > .no-items.beside-folded-away {+ flex-grow: 0;+ }}/*diff --git a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx b/apps/client/src/widgets/type_widgets/Attachment.spec.tsxindex dee9c953b5..c1ac7b4d91 100644--- a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx@@ -3,6 +3,14 @@ import { describe, expect, it, vi } from "vitest";import FAttachment from "../../entities/fattachment";import { renderInto } from "../../test/render";+// Nothing initialises i18next in a unit test, and an uninitialised one answers every lookup with an+// empty string — which would make the two placeholder messages indistinguishable. Answering with the+// key instead is enough to tell which of them the list reached for.+vi.mock("i18next", () => {+ const t = (key: string) => key;+ return { default: { t }, t };+});+// The cards' content is drawn imperatively; nothing here asks what a preview looks like, only how many// were built at all.vi.mock("../../services/content_renderer", () => ({@@ -46,9 +54,22 @@ describe("AttachmentList", () => {const container = await mount([ role("favicon"), role("viewConfig") ]);expect(cards(container)).toHaveLength(0);- expect(container.querySelector(".no-items")).not.toBeNull();+ // Sized and placed to sit with the row under it rather than to fill the pane over it, and+ // saying what is actually absent: the note's own, not attachments as such.+ const placeholder = container.querySelector(".no-items.no-items-small.beside-folded-away");+ expect(placeholder?.textContent).toBe("attachment_list.no_user_attachments");expect(container.querySelector(".attachment-system-group .collapsible-title")?.getAttribute("aria-expanded")).toBe("false");});++ it("says plainly that there is nothing where a note carries nothing at all", async () => {+ const container = await mount([]);++ // Nothing here is qualified by anything: the full-size placeholder fills the pane as it always+ // did, with no row underneath to leave room for and nothing to correct about the word it uses.+ expect(container.querySelector(".attachment-system-group")).toBeNull();+ expect(container.querySelector(".no-items")?.textContent).toBe("attachment_list.no_attachments");+ expect(container.querySelector(".no-items-small, .beside-folded-away")).toBeNull();+ });});function role(role: string) {@@ -75,9 +96,12 @@ async function mount(attachments: FAttachment[]) {const container = renderInto(<AttachmentList {...props} />);// The load is asynchronous, and its first render — before anything has arrived — is indistinguishable- // from a note with no attachments at all. Every case here carries at least one, so waiting for- // something the empty render cannot produce is enough.- await vi.waitFor(() => expect(container.querySelector(".attachment-detail-widget, .attachment-system-group")).not.toBeNull());+ // from a note with no attachments at all: which is why waiting for something that first render+ // cannot produce settles a note that has any, and a note that has none needs no waiting, its final+ // state being the one it started in.+ if (attachments.length) {+ await vi.waitFor(() => expect(container.querySelector(".attachment-detail-widget, .attachment-system-group")).not.toBeNull());+ }return container;}diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex 8ffd708951..456f342cab 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -48,6 +48,9 @@ import { TypeWidgetProps } from "./type_widget";export function AttachmentList({ note }: TypeWidgetProps) {const attachments = useAttachments(note);const groups = useMemo(() => partitionAttachmentsByGroup(attachments), [ attachments ]);+ // Nothing to fold away until the app has made an attachment of its own, which most notes never give+ // it reason to — so an ordinary note's list looks exactly as it did.+ const hasFoldedAway = groups.system.length > 0;return (<div className="attachment-list-container">@@ -56,13 +59,26 @@ export function AttachmentList({ note }: TypeWidgetProps) {{groups.user.length ? (<AttachmentGrid attachments={groups.user} />) : (- <NoItems icon="bx bx-unlink" text={t("attachment_list.no_attachments")} />+ <NoItems+ icon="bx bx-unlink"+ // "No attachments" would be a plain untruth on a note carrying a preview's+ // pictures, and the row saying otherwise is folded shut directly underneath —+ // so where there is one, the placeholder says what is missing (anything of the+ // note's own) and where the rest of it went.+ text={hasFoldedAway ? t("attachment_list.no_user_attachments") : t("attachment_list.no_attachments")}+ // Where something is folded away underneath, the placeholder gives up the room+ // it would otherwise fill and settles directly above the row offering it: the two+ // are one answer to what the note is carrying, and a placeholder centred in the+ // pane leaves the other half of that answer at the bottom edge. Small for the+ // same reason — a 4em mark over a single line, with the row tucked under it,+ // is a lot of furniture for a note that has nothing of its own.+ size={hasFoldedAway ? "small" : "normal"}+ className={hasFoldedAway ? "beside-folded-away" : undefined}+ />)}- {/* Nothing to fold away until the app has made an attachment of its own, which most- notes never give it reason to — so an ordinary note's list looks exactly as it did.- Keyed by the note so that moving to another one starts folded again, whatever was+ {/* Keyed by the note so that moving to another one starts folded again, whatever wasopened on the last. */}- {groups.system.length > 0 && <SystemAttachments key={note.noteId} attachments={groups.system} />}+ {hasFoldedAway && <SystemAttachments key={note.noteId} attachments={groups.system} />}</div></div>);