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): name a role the way a reader would, and drop the media type
Elian Doranc257799fc1da
diff --git a/apps/client/src/services/attachment_role_names.spec.ts b/apps/client/src/services/attachment_role_names.spec.tsnew file mode 100644index 0000000000..957baf2df3--- /dev/null+++ b/apps/client/src/services/attachment_role_names.spec.ts@@ -0,0 +1,34 @@+import { ATTACHMENT_ROLES } from "@triliumnext/commons";+import { describe, expect, it, vi } from "vitest";++// Nothing initializes i18next for the tests, so the wording is stood in for by the key — which is+// what the mapping here is actually about.+vi.mock("./i18n.js", () => ({ t: (key: string) => key }));++const { attachmentRoleName } = await import("./attachment_role_names.js");++describe("attachmentRoleName", () => {+ it("names every role the app creates", () => {+ // The types already refuse a role that arrives without a name; this says the same for a role+ // added to neither table, which would otherwise reach a reader as its own identifier.+ for (const role of Object.keys(ATTACHMENT_ROLES)) {+ expect(attachmentRoleName(role), role).toBe(`attachment_roles.${role}`);+ }+ });++ it("shows a role it has never heard of as it was stored", () => {+ // Not a name anyone chose, but it is all that is known about the attachment, and a blank+ // would leave a gap where every other row says something.+ expect(attachmentRoleName("somethingAScriptInvented")).toBe("somethingAScriptInvented");+ // The role is whatever was stored, so these reach the lookup too; read off the table rather+ // than checked against it, they would answer with something from its prototype.+ expect(attachmentRoleName("constructor")).toBe("constructor");+ expect(attachmentRoleName("toString")).toBe("toString");+ });++ it("has nothing to say for an attachment without a role", () => {+ expect(attachmentRoleName(undefined)).toBe("");+ expect(attachmentRoleName(null)).toBe("");+ expect(attachmentRoleName("")).toBe("");+ });+});diff --git a/apps/client/src/services/attachment_role_names.ts b/apps/client/src/services/attachment_role_names.tsnew file mode 100644index 0000000000..177cc62ee6--- /dev/null+++ b/apps/client/src/services/attachment_role_names.ts@@ -0,0 +1,46 @@+import type { AttachmentRole } from "@triliumnext/commons";++import { t } from "./i18n.js";++/**+ * What each role is called where a reader sees it.+ *+ * A `Record` over the roles rather than a lookup with a default, so a role added to+ * `ATTACHMENT_ROLES` does not compile until it has been given a name — the same arrangement the+ * user/system split uses. Naming is not derivable from the traits that live with the roles+ * themselves, and it is a question only the client has to answer, which is why the names live here+ * and not beside the roles.+ *+ * Each entry calls `t` on a literal rather than holding the key for a caller to resolve: the tooling+ * that finds translatable strings reads call sites, and a key handed through a variable is invisible+ * to it. Thunks rather than strings so the name is read at render, and a language changed under the+ * app is not left showing the one loaded at startup.+ */+const ATTACHMENT_ROLE_NAMES: Record<AttachmentRole, () => string> = {+ image: () => t("attachment_roles.image"),+ file: () => t("attachment_roles.file"),+ favicon: () => t("attachment_roles.favicon"),+ coverImage: () => t("attachment_roles.coverImage"),+ viewConfig: () => t("attachment_roles.viewConfig"),+ canvasLibraryItem: () => t("attachment_roles.canvasLibraryItem"),+ importSource: () => t("attachment_roles.importSource")+};++/**+ * What to call an attachment of this role.+ *+ * A role nobody here has heard of — a script's own, or one from a newer version reached over sync —+ * is shown as it was stored. It is not a name anyone chose, but it is the only thing known about the+ * attachment, and saying nothing would leave a row with a gap where every other row says something.+ */+export function attachmentRoleName(role: string | undefined | null): string {+ if (!role) {+ return "";+ }++ // `hasOwn` rather than a plain lookup: the role is whatever was stored, so `"constructor"`+ // reaches this, and reading it off the object would answer with something from its prototype.+ return Object.hasOwn(ATTACHMENT_ROLE_NAMES, role)+ ? ATTACHMENT_ROLE_NAMES[role as AttachmentRole]()+ : role;+}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 5bdb8dc0a3..0ad6b1543a 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2234,10 +2234,19 @@"deletion_badge": "Deleted in {{time}}","deletion_badge_soon": "Deleted soon","deletion_badge_tooltip": "This attachment will be deleted automatically, because it is not linked in the note's content. To keep it, add the attachment link back into the content or convert the attachment into a note.",- "role_and_size": "Role: {{role}}, size: {{size}}, MIME: {{- mimeType}}",+ "kind_and_size": "{{role}} · {{size}}","link_copied": "Attachment reference copied to clipboard.","unrecognized_role": "Unrecognized attachment role '{{role}}'."},+ "attachment_roles": {+ "image": "Image",+ "file": "File",+ "favicon": "Site icon",+ "coverImage": "Cover image",+ "viewConfig": "View settings",+ "canvasLibraryItem": "Canvas library",+ "importSource": "Import source"+ },"bookmark_switch": {"bookmark": "Bookmark","bookmark_this_note": "Bookmark this note to the left side panel",diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex e47c6008c2..c332131342 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -10,6 +10,7 @@ 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 { attachmentRoleName } from "../../services/attachment_role_names";import content_renderer from "../../services/content_renderer";import dialog from "../../services/dialog";import froca from "../../services/froca";@@ -305,11 +306,13 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view/>) : title}</h4>+ {/* The media type used to sit here too, spelled out. On a card whose title ends in+ the extension and whose content is drawn right below, `image/svg+xml` told a+ reader nothing they could not already see. */}<div className="attachment-details">- {t("attachment_detail_2.role_and_size", {- role: attachment.role,- size: utils.formatSize(attachment.contentLength),- mimeType: attachment.mime+ {t("attachment_detail_2.kind_and_size", {+ role: attachmentRoleName(attachment.role),+ size: utils.formatSize(attachment.contentLength)})}</div>{scheduledForErasureSince && <DeletionBadge utcDateScheduledForErasureSince={scheduledForErasureSince} />}