Skip to content

Build your personal knowledge base with Trilium Notes

feat(attachments): split the list into the reader's attachments and the app's

Elian Doran9431b0d5d12c
diff --git a/apps/client/src/services/attachment_groups.spec.ts b/apps/client/src/services/attachment_groups.spec.ts
new file mode 100644
index 0000000000..c5ea4e8aaf
--- /dev/null
+++ b/apps/client/src/services/attachment_groups.spec.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from "vitest";
+
+import { attachmentGroupForRole, partitionAttachmentsByGroup, SYSTEM_ATTACHMENT_ROLES } from "./attachment_groups.js";
+
+describe("attachmentGroupForRole", () => {
+ it("puts what the user placed on one side and what the app made on the other", () => {
+ expect(attachmentGroupForRole("image")).toBe("user");
+ expect(attachmentGroupForRole("file")).toBe("user");
+
+ for (const role of SYSTEM_ATTACHMENT_ROLES) {
+ expect(attachmentGroupForRole(role), role).toBe("system");
+ }
+ });
+
+ it("counts a role it has never heard of as the user's", () => {
+ // The default view is the user's, so an unrecognised role — a script's own, or one added to
+ // the app since — shows up rather than being buried under a group nobody thought to open.
+ expect(attachmentGroupForRole("somethingAScriptInvented")).toBe("user");
+ expect(attachmentGroupForRole("unknownRole")).toBe("user");
+ expect(attachmentGroupForRole(undefined)).toBe("user");
+ expect(attachmentGroupForRole("")).toBe("user");
+ });
+
+ it("matches a role exactly, rather than by resemblance", () => {
+ expect(attachmentGroupForRole("Favicon")).toBe("user");
+ expect(attachmentGroupForRole("favicons")).toBe("user");
+ });
+});
+
+describe("partitionAttachmentsByGroup", () => {
+ it("splits in two, keeping each side in the order it arrived", () => {
+ const attachments = [
+ { role: "favicon", id: 1 },
+ { role: "image", id: 2 },
+ { role: "coverImage", id: 3 },
+ { role: "file", id: 4 },
+ { role: "image", id: 5 }
+ ];
+
+ expect(partitionAttachmentsByGroup(attachments)).toStrictEqual({
+ user: [ { role: "image", id: 2 }, { role: "file", id: 4 }, { role: "image", id: 5 } ],
+ system: [ { role: "favicon", id: 1 }, { role: "coverImage", id: 3 } ]
+ });
+ });
+
+ it("always returns both halves, so an empty one can still be counted", () => {
+ // What the switcher reads to say a second group exists at all — an absent key would leave it
+ // unable to tell "no system attachments" from "not looked yet".
+ expect(partitionAttachmentsByGroup([])).toStrictEqual({ user: [], system: [] });
+ expect(partitionAttachmentsByGroup([ { role: "image" } ])).toStrictEqual({
+ user: [ { role: "image" } ],
+ system: []
+ });
+ });
+});
diff --git a/apps/client/src/services/attachment_groups.ts b/apps/client/src/services/attachment_groups.ts
new file mode 100644
index 0000000000..563c70ff37
--- /dev/null
+++ b/apps/client/src/services/attachment_groups.ts
@@ -0,0 +1,58 @@
+/**
+ * Which half of the attachment list an attachment belongs in.
+ *
+ * `user` is what someone put on the note — the pictures and files they pasted, dropped or uploaded.
+ * `system` is what the app made for itself while doing something else: the two pictures a link
+ * preview fetches, the JSON a collection keeps its view state in, the original file an import was
+ * read from. Both are attachments of the same note and both take up space, but only the first kind
+ * is anything the reader chose, and a note with a handful of links can carry more of the second
+ * kind than the first.
+ */
+export type AttachmentGroup = "user" | "system";
+
+/**
+ * The roles the app creates for its own purposes.
+ *
+ * Listed rather than derived because there is no field that says so: a role is just a string, and
+ * whether it names something the user placed is knowledge about the role, not about the attachment.
+ *
+ * Named as the *system* half, so that a role nobody here has heard of — a script's own, a role added
+ * to the app since — counts as the user's and stays in the view that opens by default. The opposite
+ * arrangement would file each new role under `system` and quietly bury an attachment its owner is
+ * looking for; this way the worst an unknown role costs is one extra row among the user's own.
+ */
+export const SYSTEM_ATTACHMENT_ROLES: readonly string[] = [
+ // The two pictures a link preview fetches: a site's icon and a page's thumbnail. Neither is
+ // chosen, and a note that links a lot of pages accumulates a lot of them.
+ "favicon",
+ "coverImage",
+ // How a collection remembers the way it is being looked at, and how the PDF viewer remembers
+ // where the reader had got to.
+ "viewConfig",
+ // Shapes saved into an Excalidraw canvas's library. Kept by the canvas for the canvas, rather
+ // than attached to the note by hand.
+ "canvasLibraryItem",
+ // The file an import was read from, kept so an import that went wrong can be looked at again.
+ "importSource"
+];
+
+/** Which half of the list an attachment of this role belongs in. */
+export function attachmentGroupForRole(role: string | undefined | null): AttachmentGroup {
+ return role && SYSTEM_ATTACHMENT_ROLES.includes(role) ? "system" : "user";
+}
+
+/**
+ * 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.
+ */
+export function partitionAttachmentsByGroup<T extends { role: string }>(attachments: readonly T[]): Record<AttachmentGroup, T[]> {
+ const groups: Record<AttachmentGroup, T[]> = { user: [], system: [] };
+
+ for (const attachment of attachments) {
+ groups[attachmentGroupForRole(attachment.role)].push(attachment);
+ }
+
+ return groups;
+}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index beb7a8590d..84e3de3180 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1478,7 +1478,10 @@
"open_help_page": "Open help page on attachments",
"owning_note": "Owning note: ",
"upload_attachments": "Upload attachments",
- "no_attachments": "This note has no attachments."
+ "no_attachments": "This note has no attachments.",
+ "group_user": "Yours ({{total}})",
+ "group_system": "System ({{total}})",
+ "no_attachments_in_group": "No attachments of this kind."
},
"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 d432179ca2..05a3ec7859 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.css
+++ b/apps/client/src/widgets/type_widgets/Attachment.css
@@ -17,6 +17,19 @@
gap: 0.5em;
}
}
+/*
+ * The switcher between the reader's own attachments and the app's, which only appears once there is a
+ * second group to switch to. Sits under the header rather than in it: the header's row is already
+ * shared between the owning note and the upload actions, and on a narrow window that row wraps into a
+ * column the choice would land in the middle of.
+ */
+.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. Sized to its labels instead, as the app's other segmented choices
+ are. */
+ align-self: flex-start;
+ margin-bottom: 12px;
+}
/* #endregion */
/* #region Attachment info */
diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsx
index 9a828397ce..b77e2ae811 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.tsx
+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx
@@ -2,13 +2,14 @@ import "./Attachment.css";
import { ConvertAttachmentToNoteResponse, isImageAttachmentRole } from "@triliumnext/commons";
import { t } from "i18next";
-import { useContext, useEffect, useRef, useState } from "preact/hooks";
+import { useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
import appContext from "../../components/app_context";
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 content_renderer from "../../services/content_renderer";
import dialog from "../../services/dialog";
import froca from "../../services/froca";
@@ -34,6 +35,7 @@ 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";
@@ -44,24 +46,59 @@ 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%"}}>
<AttachmentListHeader noteId={note.noteId} />
+ {isGrouped && <AttachmentGroupChoice groups={groups} currentGroup={group} onChange={setGroup} />}
<div style={{overflow: "auto", flexGrow: 1}}>
- {attachments.length ? (
+ {shown.length ? (
<div className="attachment-list-wrapper">
- {attachments.map(attachment => <AttachmentInfo key={attachment.attachmentId} attachment={attachment} />)}
+ {shown.map(attachment => <AttachmentInfo key={attachment.attachmentId} attachment={attachment} />)}
</div>
) : (
- <NoItems icon="bx bx-unlink" text={t("attachment_list.no_attachments")} />
+ <NoItems
+ icon="bx bx-unlink"
+ text={isGrouped ? t("attachment_list.no_attachments_in_group") : t("attachment_list.no_attachments")}
+ />
)}
</div>
</div>
);
}
+/**
+ * Which half of the list is on show. Both halves are named with their size, 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.
+ */
+function AttachmentGroupChoice({ groups, currentGroup, onChange }: {
+ groups: Record<AttachmentGroup, FAttachment[]>,
+ currentGroup: AttachmentGroup,
+ onChange: (group: AttachmentGroup) => void
+}) {
+ return (
+ <div className="attachment-group-choice">
+ <SegmentedChoice
+ options={[
+ { value: "user", label: t("attachment_list.group_user", { total: groups.user.length }) },
+ { value: "system", label: t("attachment_list.group_system", { total: groups.system.length }) }
+ ]}
+ currentValue={currentGroup}
+ onChange={onChange}
+ />
+ </div>
+ );
+}
+
export function useAttachments(note: FNote) {
const [ attachments, setAttachments ] = useState<FAttachment[]>([]);