Skip to content

Build your personal knowledge base with Trilium Notes

feat(attachments): fold Trilium's own attachments away in the list

Elian Doran238e8bb8d221
diff --git a/apps/client/src/services/attachment_groups.ts b/apps/client/src/services/attachment_groups.ts
index 5774386307..2e04f08896 100644
--- a/apps/client/src/services/attachment_groups.ts
+++ b/apps/client/src/services/attachment_groups.ts
@@ -56,7 +56,8 @@ export function attachmentGroupForRole(role: string | undefined | null): Attachm
* Splits attachments into the two groups, each keeping the order it arrived in.
*
* Both halves are always returned, empty ones included, so a caller can count what it is not showing
- * — which is how the switcher says there is a second group to look at at all.
+ * — which is how the list knows whether to offer the app's own attachments at all, and how the row
+ * that offers them says how much is behind it without unfolding.
*/
export function partitionAttachmentsByGroup<T extends { role: string }>(attachments: readonly T[]): Record<AttachmentGroup, T[]> {
const groups: Record<AttachmentGroup, T[]> = { user: [], system: [] };
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 284ddda18a..9da07847bd 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1479,9 +1479,7 @@
"owning_note": "Owning note: ",
"upload_attachments": "Upload attachments",
"no_attachments": "This note has no attachments.",
- "group_user": "Yours",
- "group_system": "System",
- "no_attachments_in_group": "No attachments of this kind."
+ "system_group": "Generated by Trilium ({{amount}})"
},
"book": {
"no_children_help": "This collection doesn't have any child notes so there's nothing to display.",
diff --git a/apps/client/src/widgets/type_widgets/Attachment.css b/apps/client/src/widgets/type_widgets/Attachment.css
index fa896d2110..2efc2ece42 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.css
+++ b/apps/client/src/widgets/type_widgets/Attachment.css
@@ -154,28 +154,51 @@
}
}
-.attachment-list .attachment-group-choice {
- /* A flex item of the list's column, so without this it is stretched the width of the pane and the
- two options grow into banners. Centred over the list instead. */
- align-self: center;
- margin-bottom: 12px;
-
- /* Both halves are given the same width, and more of it than their labels ask for: the labels carry
- a count, so sizing each to its own text would move the divider — and the whole control, being
- centred — every time an attachment is added or removed.
- Sized on the group rather than per button: the theme resets `min-width` on anything inside a
- button group (`:root div.btn-group button.btn.btn-secondary` in theme-next/forms.css), which
- outweighs anything scoped from here that is still worth reading. A minimum rather than a width,
- so a locale whose labels run longer than English's grows the control instead of clipping it. */
- .btn-group {
- min-width: 20em;
+/* The header stays put and only the listing under it scrolls, so the owning note and the upload actions
+ are still reachable from the bottom of a long list. */
+.attachment-list .attachment-list-container {
+ display: flex;
+ flex-direction: column;
+ height: 100%;
+}
+
+.attachment-list .attachment-list-body {
+ flex-grow: 1;
+ overflow: auto;
+ display: flex;
+ flex-direction: column;
+
+ /* Nothing in here is squeezed to fit the pane — a listing longer than it scrolls instead. */
+ > * {
+ flex-shrink: 0;
}
- /* Equal halves, which flex-basis decides rather than the labels' own widths — Bootstrap's
- `flex: 1 1 auto` would hand each the space its text leaves over, making the wider label wider
- still. */
- .btn {
- flex: 1 1 0;
+ /* The placeholder stands in for the listing rather than for the pane. Its own `height: 100%` is
+ measured 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. */
+ > .no-items {
+ height: auto;
+ }
+}
+
+/*
+ * The app's own attachments, folded away under the reader's.
+ *
+ * Dressed as an aside rather than as a control: a muted line the width of its own words, set off from the
+ * listing above it but given no frame, no fill and no place of its own in the layout. What is behind it is
+ * a footnote to the note's attachments, and anything heavier — the button group this replaced, centred and
+ * 20em wide — reads as a question the reader has to answer before the list means anything.
+ */
+.attachment-list .attachment-system-group {
+ margin-top: 20px;
+
+ .collapsible-title {
+ /* `Collapsible` lays its button out as a flex row, which makes it block-level and hands it the
+ whole pane — a hover tint the width of the listing, for a line of words. */
+ width: fit-content;
+ color: var(--muted-text-color);
+ font-size: .9em;
}
}
/* #endregion */
diff --git a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
new file mode 100644
index 0000000000..dee9c953b5
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
@@ -0,0 +1,86 @@
+import { describe, expect, it, vi } from "vitest";
+
+import FAttachment from "../../entities/fattachment";
+import { renderInto } from "../../test/render";
+
+// 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", () => ({
+ default: {
+ getRenderedContent: async () => ({ $renderedContent: [] }),
+ disposeInteractiveContent: () => {}
+ }
+}));
+// Bootstrap's dropdown and the note link both reach well past this widget; the compression dialog only
+// exists to be opened from a menu that is stubbed out anyway.
+vi.mock("../react/Dropdown", () => ({ default: () => <div class="dropdown-stub" /> }));
+vi.mock("../react/NoteLink", () => ({ default: () => <span class="note-link-stub" /> }));
+vi.mock("../dialogs/image_compression/image_compression_dialog", () => ({ showImageCompressionDialog: vi.fn() }));
+
+const { AttachmentList } = await import("./Attachment");
+
+describe("AttachmentList", () => {
+ it("lists what the reader placed, with nothing to unfold where the app has made nothing", async () => {
+ const container = await mount([ role("image"), role("file") ]);
+
+ expect(cards(container)).toHaveLength(2);
+ expect(container.querySelector(".attachment-system-group")).toBeNull();
+ });
+
+ it("folds the app's own away, and does not build them until they are asked for", async () => {
+ const container = await mount([ role("image"), role("favicon"), role("coverImage") ]);
+
+ // Only the reader's own is listed; the other two are behind the disclosure, and behind it they
+ // are not rendered at all — each card would otherwise fetch and draw its content unprompted.
+ expect(cards(container)).toHaveLength(1);
+ const disclosure = container.querySelector<HTMLButtonElement>(".attachment-system-group .collapsible-title");
+ expect(disclosure?.getAttribute("aria-expanded")).toBe("false");
+
+ disclosure?.click();
+ await vi.waitFor(() => expect(cards(container)).toHaveLength(3));
+ expect(disclosure?.getAttribute("aria-expanded")).toBe("true");
+ expect(container.querySelectorAll(".attachment-system-group .attachment-detail-widget")).toHaveLength(2);
+ });
+
+ it("stays folded on a note whose attachments are all the app's, saying so in its place", async () => {
+ const container = await mount([ role("favicon"), role("viewConfig") ]);
+
+ expect(cards(container)).toHaveLength(0);
+ expect(container.querySelector(".no-items")).not.toBeNull();
+ expect(container.querySelector(".attachment-system-group .collapsible-title")?.getAttribute("aria-expanded")).toBe("false");
+ });
+});
+
+function role(role: string) {
+ return {
+ attachmentId: `att-${role}-${Math.random().toString(36).slice(2)}`,
+ ownerId: "note1",
+ title: `${role}.dat`,
+ role,
+ mime: "application/octet-stream",
+ contentLength: 10,
+ utcDateModified: "2026-01-01 00:00:00Z",
+ getBlob: async () => null
+ } as unknown as FAttachment;
+}
+
+/** Renders the list for a note carrying the given attachments, once the asynchronous load has settled. */
+async function mount(attachments: FAttachment[]) {
+ const props = {
+ note: {
+ noteId: "note1",
+ getAttachments: async () => attachments
+ }
+ } as unknown as Parameters<typeof AttachmentList>[0];
+ 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());
+ return container;
+}
+
+function cards(container: HTMLElement) {
+ return container.querySelectorAll(".attachment-detail-widget");
+}
diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsx
index eab0f6efb0..8ffd708951 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.tsx
+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx
@@ -9,7 +9,7 @@ import type NoteContext from "../../components/note_context";
import FAttachment from "../../entities/fattachment";
import FNote from "../../entities/fnote";
import imageContextMenu from "../../menus/image_context_menu";
-import { type AttachmentGroup, partitionAttachmentsByGroup } from "../../services/attachment_groups";
+import { partitionAttachmentsByGroup } from "../../services/attachment_groups";
import { attachmentRoleLabel } from "../../services/attachment_role_names";
import content_renderer from "../../services/content_renderer";
import dialog from "../../services/dialog";
@@ -26,6 +26,7 @@ import { showImageCompressionDialog } from "../dialogs/image_compression/image_c
import ActionButton from "../react/ActionButton";
import { Badge } from "../react/Badge";
import Button from "../react/Button";
+import { ExternallyControlledCollapsible } from "../react/Collapsible";
import Dropdown from "../react/Dropdown";
import FormFileUpload from "../react/FormFileUpload";
import { FormDropdownDivider, FormListItem } from "../react/FormList";
@@ -36,7 +37,6 @@ import ImageViewer from "../react/ImageViewer";
import NoItems from "../react/NoItems";
import NoteLink from "../react/NoteLink";
import { ParentComponent, refToJQuerySelector } from "../react/react_utils";
-import SegmentedChoice from "../react/SegmentedChoice";
import SiblingNavigator from "../react/SiblingNavigator";
import { TextPreview } from "./File";
import MediaPreview from "./file/MediaPreview";
@@ -47,55 +47,68 @@ import { TypeWidgetProps } from "./type_widget";
*/
export function AttachmentList({ note }: TypeWidgetProps) {
const attachments = useAttachments(note);
- const [ group, setGroup ] = useState<AttachmentGroup>("user");
const groups = useMemo(() => partitionAttachmentsByGroup(attachments), [ attachments ]);
- // Nothing to choose between 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 isGrouped = groups.system.length > 0;
- const shown = isGrouped ? groups[group] : attachments;
- // TODO: Extract inline styles to CSS
return (
- <div style={{display: "flex", flexDirection: "column", height: "100%"}}>
+ <div className="attachment-list-container">
<AttachmentListHeader noteId={note.noteId} />
- {isGrouped && <AttachmentGroupChoice groups={groups} currentGroup={group} onChange={setGroup} />}
- <div style={{overflow: "auto", flexGrow: 1}}>
- {shown.length ? (
- <div className="attachment-list-wrapper">
- {shown.map(attachment => <AttachmentInfo key={attachment.attachmentId} attachment={attachment} />)}
- </div>
+ <div className="attachment-list-body">
+ {groups.user.length ? (
+ <AttachmentGrid attachments={groups.user} />
) : (
- <NoItems
- icon="bx bx-unlink"
- text={isGrouped ? t("attachment_list.no_attachments_in_group") : t("attachment_list.no_attachments")}
- />
+ <NoItems icon="bx bx-unlink" text={t("attachment_list.no_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.
+ Keyed by the note so that moving to another one starts folded again, whatever was
+ opened on the last. */}
+ {groups.system.length > 0 && <SystemAttachments key={note.noteId} attachments={groups.system} />}
</div>
</div>
);
}
+/** The cards themselves, in the order the group came in. */
+function AttachmentGrid({ attachments }: { attachments: FAttachment[] }) {
+ return (
+ <div className="attachment-list-wrapper">
+ {attachments.map(attachment => <AttachmentInfo key={attachment.attachmentId} attachment={attachment} />)}
+ </div>
+ );
+}
+
/**
- * Which half of the list is on show. Both halves carry a count, so the group not being looked at still
- * says how much is in it — the point of the switcher being that a note can carry far more of the app's
- * own attachments than of the reader's, and neither number is guessable from the other's list.
+ * What the app made for itself, folded away under what the reader placed.
+ *
+ * A disclosure rather than a second half of a switcher: these are not an alternative view of the note's
+ * attachments but a footnote to them, and giving them a button of their own equal to the reader's own
+ * made every note carrying a single link preview open on a mode chooser. Folded always, on every note —
+ * the count says how much is behind it, which is as much as someone who is not looking for them needs.
+ *
+ * The cards are not built until it is first opened. Each one fetches its content and renders a preview,
+ * and a link-heavy note carries more of these than of anything else; a collapsed section that has
+ * loaded all of them costs the same as the old switcher did on the tab nobody chose.
*/
-function AttachmentGroupChoice({ groups, currentGroup, onChange }: {
- groups: Record<AttachmentGroup, FAttachment[]>,
- currentGroup: AttachmentGroup,
- onChange: (group: AttachmentGroup) => void
-}) {
+function SystemAttachments({ attachments }: { attachments: FAttachment[] }) {
+ const [ expanded, setExpanded ] = useState(false);
+ // Kept mounted once opened, so folding it back does not throw away what was just loaded.
+ const [ everExpanded, setEverExpanded ] = useState(false);
+
return (
- <div className="attachment-group-choice">
- <SegmentedChoice
- options={[
- { value: "user", label: t("attachment_list.group_user"), count: groups.user.length },
- { value: "system", label: t("attachment_list.group_system"), count: groups.system.length }
- ]}
- currentValue={currentGroup}
- onChange={onChange}
- />
- </div>
+ <ExternallyControlledCollapsible
+ className="attachment-system-group"
+ /* `amount` rather than i18next's `count`, which would pull the label into the plural
+ machinery — the number here is parenthetical, not a counted noun, so the sentence is the
+ same either way and the pair of identical English forms would be noise for translators. */
+ title={t("attachment_list.system_group", { amount: attachments.length })}
+ expanded={expanded}
+ setExpanded={(newExpanded) => {
+ if (newExpanded) setEverExpanded(true);
+ setExpanded(newExpanded);
+ }}
+ >
+ {everExpanded && <AttachmentGrid attachments={attachments} />}
+ </ExternallyControlledCollapsible>
);
}