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
client: change the way the attributes without a display name are shown in the menus and the promoted attributes card
Adorian Doran2fb6e12a8831
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex bbbeb60d04..daf41c8474 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1386,6 +1386,8 @@"promoted_attributes": {"promoted_attributes": "Promoted Attributes","create_attribute": "Create a new attribute",+ "label_name": "Label: {{name}}",+ "relation_name": "Relationship: {{name}}","edit_attribute": "Edit this attribute","delete_attribute": "Delete this attribute","delete_confirmation": "Delete <strong>{{name}}</strong> and its values from every note in this collection? This cannot be undone.",diff --git a/apps/client/src/widgets/collections/promoted_attributes.spec.ts b/apps/client/src/widgets/collections/promoted_attributes.spec.tsindex f61344352b..88a1f0fca5 100644--- a/apps/client/src/widgets/collections/promoted_attributes.spec.ts+++ b/apps/client/src/widgets/collections/promoted_attributes.spec.ts@@ -9,6 +9,12 @@ import {const mocks = vi.hoisted(() => ({ bulk: vi.fn(async () => {}) }));vi.mock("../../services/bulk_action", () => ({ executeBulkActions: mocks.bulk }));+// i18next is never initialised under test, so `t` echoes the key with what it interpolates.+vi.mock("../../services/i18n", () => ({+ t: (key: string, values?: { name?: string }) =>+ values?.name ? `${key}:${values.name}` : key+}));+/** A definition as the collection note carries it, `label:dueDate` naming `dueDate`. */function definition(name: string, {alias, noteId = "board1", value = "promoted,single,text", isPromoted = true,@@ -40,7 +46,7 @@ const DEFINED = [];describe("resolvePromotedAttributes", () => {- it("reads what the note defines, naming each by its alias where it has one", () => {+ it("reads what the note defines, naming each by its alias or by its kind and name", () => {const resolved = resolvePromotedAttributes(collection(DEFINED), undefined);expect(resolved).toEqual([@@ -54,8 +60,16 @@ describe("resolvePromotedAttributes", () => {isOwned: true,isInheritable: true},- expect.objectContaining({ name: "requiresResearch", title: "requiresResearch" }),- expect.objectContaining({ name: "owner", type: "relation" })+ // No alias, so the name is prefixed by the kind that defines it.+ expect.objectContaining({+ name: "requiresResearch",+ title: "promoted_attributes.label_name:requiresResearch"+ }),+ expect.objectContaining({+ name: "owner",+ type: "relation",+ title: "promoted_attributes.relation_name:owner"+ })]);});diff --git a/apps/client/src/widgets/collections/promoted_attributes.ts b/apps/client/src/widgets/collections/promoted_attributes.tsindex db5159430e..e0da88b19d 100644--- a/apps/client/src/widgets/collections/promoted_attributes.ts+++ b/apps/client/src/widgets/collections/promoted_attributes.ts@@ -2,6 +2,7 @@ import type { BulkAction } from "@triliumnext/commons";import type FNote from "../../entities/fnote";import { executeBulkActions } from "../../services/bulk_action";+import { t } from "../../services/i18n";/** How one promoted attribute is shown, as a collection's view config stores it. */export interface PromotedAttributeSetting {@@ -18,7 +19,10 @@ export interface PromotedAttribute {/** The definition the collection note carries, `label:dueDate` for instance. */definitionName: string;type: "label" | "relation";- /** What the reader sees: the definition's alias, or the name where it gives none. */+ /**+ * What the reader sees: the definition's alias, or the name behind the kind that defines it+ * where it gives none. See {@link defaultTitle}.+ */title: string;/** Whether the attribute is kept off the items. */hidden: boolean;@@ -71,7 +75,7 @@ export function resolvePromotedAttributes(name,definitionName: definition.name,type,- title: parsed?.promotedAlias || name,+ title: parsed?.promotedAlias || defaultTitle(type, name),labelType: parsed?.labelType,selectOptions: parsed?.selectOptions,hidden: false,@@ -99,6 +103,16 @@ export function resolvePromotedAttributes(return ordered;}+/**+ * What an attribute with no alias is listed as: the name behind the kind that defines it, so a bare+ * `dueDate` reads as the field it is rather than as a name someone chose.+ */+function defaultTitle(type: "label" | "relation", name: string) {+ return type === "relation"+ ? t("promoted_attributes.relation_name", { name })+ : t("promoted_attributes.label_name", { name });+}+/** The attributes drawn on an item, in order, for a view showing their values. */export function visiblePromotedAttributeNames(attributes: PromotedAttribute[]) {return attributes.filter((attribute) => !attribute.hidden).map((attribute) => attribute.name);diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsxindex 96bb4b2806..4b42122ace 100644--- a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx+++ b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx@@ -10,13 +10,18 @@ import type { PromotedAttribute, PromotedAttributeSetting } from "../collectionsimport PromotedAttributesCard from "./PromotedAttributesCard";import { ParentComponent } from "./react_utils";-// i18next is never initialised under test, so every label would read as undefined. The command+// i18next is never initialised under test, so every label would read as undefined. `t` echoes the+// key with what it interpolates, which is how an attribute with no alias is named. The command// registry, reached through the attribute editor, waits on the translations before it builds.vi.mock("../../services/i18n", () => ({- t: (key: string) => key,+ t: (key: string, values?: { name?: string }) =>+ values?.name ? `${key}:${values.name}` : key,translationsInitializedPromise: Promise.resolve()}));+/** What a label with no alias is listed as, the editor naming it by its kind. */+const unnamed = (name: string) => `promoted_attributes.label_name:${name}`;+const mocks = vi.hoisted(() => ({setLabel: vi.fn(),removeOwned: vi.fn(async () => {}),@@ -97,7 +102,7 @@ describe("PromotedAttributesCard", () => {it("lists what the note defines, named as the definitions name them", () => {draw();- expect(names()).toEqual([ "Due", "owner" ]);+ expect(names()).toEqual([ "Due", unnamed("owner") ]);expect(shown()).toEqual([ true, true ]);});@@ -125,7 +130,7 @@ describe("PromotedAttributesCard", () => {settings = [ { name: "owner", hidden: true }, { name: "dueDate" } ];draw();- expect(names()).toEqual([ "owner", "Due" ]);+ expect(names()).toEqual([ unnamed("owner"), "Due" ]);expect(shown()).toEqual([ false, true ]);});@@ -135,7 +140,7 @@ describe("PromotedAttributesCard", () => {press(segments()[0], "ArrowDown", { ctrlKey: true });expect(stored.at(-1)?.map((attribute) => attribute.name)).toEqual([ "owner", "dueDate" ]);- expect(names()).toEqual([ "owner", "Due" ]);+ expect(names()).toEqual([ unnamed("owner"), "Due" ]);});it("hides an attribute without taking it off the list, and shows it again", () => {@@ -307,7 +312,7 @@ describe("PromotedAttributesCard", () => {expect(mocks.detail.opts?.focus).toBe("name");expect(mocks.detail.opts?.hideInheritance).toBe(true);// Nothing is listed until the note reports what was made.- expect(names()).toEqual([ "Due", "owner" ]);+ expect(names()).toEqual([ "Due", unnamed("owner") ]);expect(stored).toEqual([]);});@@ -322,7 +327,7 @@ describe("PromotedAttributesCard", () => {defined = [ ...defined, definition("label:priority") ];await reported();- expect(names()).toEqual([ "Due", "owner", "priority" ]);+ expect(names()).toEqual([ "Due", unnamed("owner"), unnamed("priority") ]);});/** A definition the reader left unpromoted is not one the items can show. */@@ -335,7 +340,7 @@ describe("PromotedAttributesCard", () => {});await reported();- expect(names()).toEqual([ "Due", "owner" ]);+ expect(names()).toEqual([ "Due", unnamed("owner") ]);});it("drops an attribute the note no longer defines, keeping the order of the rest", async () => {