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
feat(attachments): give every attachment an icon
Elian Doranba748b4afa27
diff --git a/apps/client/src/widgets/type_widgets/Attachment.css b/apps/client/src/widgets/type_widgets/Attachment.cssindex 06372b5892..d66d7e1e87 100644--- a/apps/client/src/widgets/type_widgets/Attachment.css+++ b/apps/client/src/widgets/type_widgets/Attachment.css@@ -202,6 +202,14 @@}}+/* What the attachment is, ahead of its name — the role's own mark where the role says something, and+ the media type's where it only says the reader put it there. Sized and placed as the note icon on a+ collection's cards is, since the two sit in the same kind of header. */+.attachment-icon {+ flex-shrink: 0;+ font-size: 1.2em;+}+.attachment-title-line {display: flex;align-items: center;diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex a3ff03cef6..46b53c723f 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -1,6 +1,6 @@import "./Attachment.css";-import { ConvertAttachmentToNoteResponse, isImageAttachmentRole } from "@triliumnext/commons";+import { attachmentIcon, ConvertAttachmentToNoteResponse, isImageAttachmentRole } from "@triliumnext/commons";import { t } from "i18next";import { useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";@@ -294,6 +294,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewprocessUrl: `ocr/process-attachment/${attachment.attachmentId}`}) : undefined}/>+ <Icon className="attachment-icon" icon={attachmentIcon(attachment.role, attachment.mime)} /><h4 className="attachment-title">{!isFullDetail ? (<NoteLinkdiff --git a/packages/commons/src/lib/attachment_roles.spec.ts b/packages/commons/src/lib/attachment_roles.spec.tsindex aaf9e04e08..be851090c4 100644--- a/packages/commons/src/lib/attachment_roles.spec.ts+++ b/packages/commons/src/lib/attachment_roles.spec.ts@@ -1,6 +1,7 @@import { describe, expect, it } from "vitest";-import { ATTACHMENT_ROLES, attachmentRoleTraits, IMAGE_ATTACHMENT_ROLES, isDeduplicatedAttachmentRole, isEmbeddedAttachmentRole, isImageAttachmentRole } from "./attachment_roles.js";+import { ATTACHMENT_ROLES, attachmentIcon, attachmentRoleTraits, IMAGE_ATTACHMENT_ROLES, isDeduplicatedAttachmentRole, isEmbeddedAttachmentRole, isImageAttachmentRole } from "./attachment_roles.js";+import { getNoteIcon } from "./notes.js";describe("isImageAttachmentRole", () => {it("recognises every picture role and nothing else", () => {@@ -61,7 +62,7 @@ describe("attachmentRoleTraits", () => {// The types already refuse a role that arrives without one; this says the same at runtime,// for the shape the table is actually read with.for (const [ role, traits ] of Object.entries(ATTACHMENT_ROLES)) {- expect(Object.keys(traits).sort(), role).toStrictEqual([ "copiedAs", "deduplicated", "embedded", "picture" ]);+ expect(Object.keys(traits).sort(), role).toStrictEqual([ "copiedAs", "deduplicated", "embedded", "icon", "picture" ]);}});@@ -108,6 +109,61 @@ describe("copiedAs", () => {});});+describe("attachmentIcon", () => {+ it("reads the media type for what a reader put there themselves", () => {+ // Between a spreadsheet, a PDF and a video, the difference worth showing is the content's:+ // "file" is the same word for all three.+ expect(attachmentIcon("file", "application/pdf")).toBe("bx bxs-file-pdf");+ expect(attachmentIcon("file", "video/mp4")).toBe("bx bx-video");+ expect(attachmentIcon("file", "audio/mpeg")).toBe("bx bx-music");+ expect(attachmentIcon("image", "image/gif")).toBe("bx bxs-file-gif");+ expect(attachmentIcon("image", "image/png")).toBe("bx bx-image");+ });++ it("gives an attachment the icon a note of the same content would have", () => {+ // The point of routing both through one table: a PDF is a PDF however it got here.+ for (const [ type, mime ] of [ [ "file", "application/pdf" ], [ "file", "video/mp4" ], [ "image", "image/gif" ] ] as const) {+ expect(attachmentIcon(type, mime), mime).toBe(getNoteIcon({+ noteId: "abc123", type, mime, iconClass: undefined, workspaceIconClass: undefined, isFolder: () => false+ }));+ }+ });++ it("names the app's own doing for the roles the app created", () => {+ // Their content is an implementation detail — a favicon is a PNG, a saved view is JSON — and+ // what a reader wants told is which part of the app put it there.+ expect(attachmentIcon("favicon", "image/png")).toBe("bx bx-globe");+ expect(attachmentIcon("coverImage", "image/jpeg")).toBe("bx bx-image-alt");+ expect(attachmentIcon("viewConfig", "application/json")).toBe("bx bx-cog");+ expect(attachmentIcon("canvasLibraryItem", "application/json")).toBe("bx bx-shape-square");+ expect(attachmentIcon("importSource", "application/zip")).toBe("bx bx-import");+ });++ it("treats a role it has never heard of as the reader's own upload", () => {+ // Whatever a script attached, the reader is still looking at a file.+ expect(attachmentIcon("somethingAScriptInvented", "application/pdf")).toBe("bx bxs-file-pdf");+ // The role and the media type are both whatever was stored, so these reach the lookups; read+ // off a table rather than checked against it, they would answer with one of its prototype's+ // functions, which survives the fallback and is handed on as an icon class.+ expect(attachmentIcon("constructor", "image/gif")).toBe("bx bxs-file-gif");+ expect(attachmentIcon("file", "constructor")).toBe("bx bx-file");+ expect(attachmentIcon("file", "toString")).toBe("bx bx-file");+ });++ it("always answers, whatever is missing", () => {+ // An attachment with no media type recorded still has a row in the list to fill.+ expect(attachmentIcon("file", undefined)).toBe("bx bx-file");+ expect(attachmentIcon(undefined, undefined)).toBe("bx bx-file");+ expect(attachmentIcon(null, "")).toBe("bx bx-file");+ });++ it("gives every role an icon, or says plainly that it has none", () => {+ for (const [ role, traits ] of Object.entries(ATTACHMENT_ROLES)) {+ expect(attachmentIcon(role, "application/pdf"), role).toBe(traits.icon ?? "bx bxs-file-pdf");+ }+ });+});+describe("isDeduplicatedAttachmentRole", () => {it("deduplicates only what the app named itself", () => {// A site's icon: one per site rather than one per link to it.diff --git a/packages/commons/src/lib/attachment_roles.ts b/packages/commons/src/lib/attachment_roles.tsindex f9ff11fea8..49a8dbfb80 100644--- a/packages/commons/src/lib/attachment_roles.ts+++ b/packages/commons/src/lib/attachment_roles.ts@@ -1,3 +1,5 @@+import { getMimeIcon } from "./notes.js";+/*** What the app knows about an attachment of a given role.*@@ -52,6 +54,19 @@ export interface AttachmentRoleTraits {* the app's roles would only move the problem. A picture stays a picture: see the spec.*/copiedAs: "image" | "file";+ /**+ * The icon standing for an attachment of this role, or `null` where the role has nothing to say+ * about what the thing is.+ *+ * Null for exactly the two roles that mean "someone put this here": between a spreadsheet, a PDF+ * and a video the difference a reader is looking for is the content's, not the role's, so those+ * read their media type instead — and get the same icon a note of that content would.+ *+ * A role the app created is the opposite case. Its content is an implementation detail (a+ * favicon is a PNG, a saved view is JSON) and what the reader wants told is which of the app's+ * doings put it there, which is the role and nothing else.+ */+ icon: string | null;}/**@@ -63,9 +78,9 @@ export interface AttachmentRoleTraits {*/export const ATTACHMENT_ROLES = {/** A picture the user placed in the note. */- image: { picture: true, deduplicated: false, embedded: true, copiedAs: "image" },+ image: { picture: true, deduplicated: false, embedded: true, copiedAs: "image", icon: null },/** A file the user attached to the note. */- file: { picture: false, deduplicated: false, embedded: true, copiedAs: "file" },+ file: { picture: false, deduplicated: false, embedded: true, copiedAs: "file", icon: null },/*** A link preview's two pictures, kept apart from `image` so they can be told from one the user* chose. Both arrive already sized by the server (a 16x16 icon, a 256px thumbnail), so the@@ -74,14 +89,14 @@ export const ATTACHMENT_ROLES = {* what eventually takes them with it. Carried into a note by hand, though, they are pictures like* any other — the preview they belonged to stayed behind.*/- favicon: { picture: true, deduplicated: true, embedded: true, copiedAs: "image" },- coverImage: { picture: true, deduplicated: true, embedded: true, copiedAs: "image" },+ favicon: { picture: true, deduplicated: true, embedded: true, copiedAs: "image", icon: "bx bx-globe" },+ coverImage: { picture: true, deduplicated: true, embedded: true, copiedAs: "image", icon: "bx bx-image-alt" },/** How a collection remembers the way it is being looked at, and a PDF where the reader had got to. */- viewConfig: { picture: false, deduplicated: false, embedded: false, copiedAs: "file" },+ viewConfig: { picture: false, deduplicated: false, embedded: false, copiedAs: "file", icon: "bx bx-cog" },/** Shapes saved into an Excalidraw canvas's library. */- canvasLibraryItem: { picture: false, deduplicated: false, embedded: false, copiedAs: "file" },+ canvasLibraryItem: { picture: false, deduplicated: false, embedded: false, copiedAs: "file", icon: "bx bx-shape-square" },/** The file an import was read from, kept so an import that went wrong can be looked at again. */- importSource: { picture: false, deduplicated: false, embedded: false, copiedAs: "file" }+ importSource: { picture: false, deduplicated: false, embedded: false, copiedAs: "file", icon: "bx bx-import" }} as const satisfies Record<string, AttachmentRoleTraits>;/** A role the app itself creates. Arbitrary strings reach the same fields — see {@link ATTACHMENT_ROLES}. */@@ -111,6 +126,20 @@ export function isEmbeddedAttachmentRole(role: string | undefined | null): boolereturn attachmentRoleTraits(role)?.embedded ?? false;}+/**+ * The icon for an attachment, from its role where the role says what the thing is and from its media+ * type where it does not.+ *+ * A role nobody here has heard of — a script's own — is treated as the user's own upload rather than+ * given a mark for "unknown": whatever a script attached, the reader is still looking at a file, and+ * its media type describes it as well as it describes anyone else's.+ *+ * @see AttachmentRoleTraits.icon+ */+export function attachmentIcon(role: string | undefined | null, mime: string | undefined | null): string {+ return attachmentRoleTraits(role)?.icon ?? getMimeIcon(mime);+}+/*** What the app knows about this role, or nothing at all for one it does not recognise — a script's* own, or a role from a newer version reached over sync.diff --git a/packages/commons/src/lib/notes.spec.ts b/packages/commons/src/lib/notes.spec.tsindex 0783112515..196f9180df 100644--- a/packages/commons/src/lib/notes.spec.ts+++ b/packages/commons/src/lib/notes.spec.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { getImageAttachmentTitle, getNoteIcon, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS } from "./notes.js";+import { getImageAttachmentTitle, getMimeIcon, getNoteIcon, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS } from "./notes.js";import { NoteType } from "./rows.js";function buildArgs(overrides: {@@ -139,3 +139,29 @@ describe("getNoteIcon", () => {expect(icon).toBe("bx bx-book");});});++describe("getMimeIcon", () => {+ it("reads a media type the way a note of that content is read", () => {+ // Same answers as the file/image branches above, which now go through here: a PDF is a PDF+ // whether it arrived as a note or as an attachment.+ expect(getMimeIcon("application/pdf")).toBe("bx bxs-file-pdf");+ expect(getMimeIcon("video/mp4")).toBe("bx bx-video");+ expect(getMimeIcon("audio/mpeg")).toBe("bx bx-music");+ expect(getMimeIcon("image/gif")).toBe("bx bxs-file-gif");+ expect(getMimeIcon("image/png")).toBe("bx bx-image");+ expect(getMimeIcon("text/plain")).toBe("bx bx-file");+ });++ it("falls back to the file icon when there is no media type to read", () => {+ expect(getMimeIcon(undefined)).toBe("bx bx-file");+ expect(getMimeIcon(null)).toBe("bx bx-file");+ expect(getMimeIcon("")).toBe("bx bx-file");+ });++ it("does not answer with something off the mapping tables' prototype", () => {+ // The media type is whatever was stored. Read off a table rather than checked against it,+ // these return a function, which survives the `??` and is handed on as an icon class.+ expect(getMimeIcon("constructor")).toBe("bx bx-file");+ expect(getMimeIcon("toString")).toBe("bx bx-file");+ });+});diff --git a/packages/commons/src/lib/notes.ts b/packages/commons/src/lib/notes.tsindex 3d92f7a633..eb08c2c1b0 100644--- a/packages/commons/src/lib/notes.ts+++ b/packages/commons/src/lib/notes.ts@@ -90,12 +90,44 @@ export function getNoteIcon({ noteId, type, mime, iconClass, workspaceIconClass,const correspondingMimeType = MIME_TYPES_DICT.find(m => m.mime === mime);return correspondingMimeType?.icon ?? NOTE_TYPE_ICONS.code;} else if (type === "file") {- if (mime.startsWith("video/")) return "bx bx-video";- if (mime.startsWith("audio/")) return "bx bx-music";- return FILE_MIME_MAPPINGS[mime] ?? NOTE_TYPE_ICONS.file;+ return getFileMimeIcon(mime);} else if (type === "image") {- return IMAGE_MIME_MAPPINGS[mime] ?? NOTE_TYPE_ICONS.image;+ return getImageMimeIcon(mime);}return NOTE_TYPE_ICONS[type];}++/**+ * The icon for something known only by its media type — an attachment, or anything else the app holds+ * that is not a note and so has no type to ask about.+ *+ * A note of the same content gets the same icon, which is the point of routing both through here: a PDF+ * is a PDF whether it was uploaded as a note or attached to one.+ */+export function getMimeIcon(mime: string | undefined | null): string {+ if (!mime) {+ return NOTE_TYPE_ICONS.file;+ }++ return mime.startsWith("image/") ? getImageMimeIcon(mime) : getFileMimeIcon(mime);+}++function getFileMimeIcon(mime: string): string {+ if (mime.startsWith("video/")) return "bx bx-video";+ if (mime.startsWith("audio/")) return "bx bx-music";+ return lookUpMime(FILE_MIME_MAPPINGS, mime) ?? NOTE_TYPE_ICONS.file;+}++function getImageMimeIcon(mime: string): string {+ return lookUpMime(IMAGE_MIME_MAPPINGS, mime) ?? NOTE_TYPE_ICONS.image;+}++/**+ * `hasOwn` rather than a plain lookup: the media type is whatever was stored, so `"constructor"` reaches+ * these tables, and reading it off one would answer with something from its prototype — a function, which+ * survives the `??` and is handed on as an icon class.+ */+function lookUpMime(mappings: Record<string, string>, mime: string): string | undefined {+ return Object.hasOwn(mappings, mime) ? mappings[mime] : undefined;+}