Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/promoted attributes card: use icons matching the attribute type, display the attribute type

Adorian Doran5fd75c942d3d
diff --git a/apps/client/src/widgets/collections/promoted_attributes.ts b/apps/client/src/widgets/collections/promoted_attributes.ts
index 79790b1a96..400e752bc3 100644
--- a/apps/client/src/widgets/collections/promoted_attributes.ts
+++ b/apps/client/src/widgets/collections/promoted_attributes.ts
@@ -24,6 +24,8 @@ export interface PromotedAttribute {
hidden: boolean;
/** The definition as stored, which the attribute editor is handed to edit. */
definitionValue: string;
+ /** What the field holds: `text`, `date`, `boolean` and the rest. Absent for a relation. */
+ labelType?: string;
/** Whether the note that defines it is the collection itself rather than an ancestor. */
isOwned: boolean;
/**
@@ -62,11 +64,13 @@ export function resolvePromotedAttributes(
continue;
}
+ const parsed = definition.getDefinition();
defined.set(name, {
name,
definitionName: definition.name,
type,
- title: definition.getDefinition()?.promotedAlias || name,
+ title: parsed?.promotedAlias || name,
+ labelType: parsed?.labelType,
hidden: false,
definitionValue: definition.value,
isOwned: definition.noteId === note?.noteId,
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.css b/apps/client/src/widgets/react/PromotedAttributesCard.css
index 9720429585..5006d5c4fa 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.css
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.css
@@ -9,6 +9,13 @@
white-space: nowrap;
}
+ /* Reads as a note about the row rather than as something to press. */
+ .promoted-attribute-type {
+ flex-shrink: 0;
+ --color: var(--muted-text-color);
+ font-size: 0.8em;
+ }
+
.promoted-attribute-edit,
.promoted-attribute-delete {
flex-shrink: 0;
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
index 1364bb7c27..eac07ef62e 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
@@ -37,7 +37,10 @@ vi.mock("../../services/dialog", () => ({ default: { confirm: mocks.confirm } })
// The editor is tested where it lives; what the card answers for is what it hands over and what it
// writes once the editor reports a definition.
-vi.mock("../attribute_widgets/attribute_detail", () => ({
+vi.mock("../attribute_widgets/attribute_detail", async (importActual) => ({
+ // The kinds a definition can hold are read from the module itself, so the card wears the icons
+ // the editor picks them by.
+ ...(await importActual<typeof import("../attribute_widgets/attribute_detail")>()),
AttributeDetail: (props: { opts: AttributeDetailOpts | null }) => {
mocks.detail.opts = props.opts;
mocks.detail.callbacks = props as unknown as Record<string, Function>;
@@ -46,15 +49,15 @@ vi.mock("../attribute_widgets/attribute_detail", () => ({
}));
/** A definition as the collection note carries it. */
-function definition(name: string, { alias, value = "promoted,single,text" }: {
- alias?: string, value?: string
+function definition(name: string, { alias, value = "promoted,single,text", labelType }: {
+ alias?: string, value?: string, labelType?: string
} = {}) {
return {
name,
value,
noteId: "board1",
isInheritable: true,
- getDefinition: () => ({ isPromoted: true, promotedAlias: alias })
+ getDefinition: () => ({ isPromoted: true, promotedAlias: alias, labelType })
};
}
@@ -94,6 +97,26 @@ describe("PromotedAttributesCard", () => {
expect(shown()).toEqual([ true, true ]);
});
+ /** The same icons the editor picks a kind by, so a row reads as what it holds. */
+ it("wears the icon of what each attribute holds", () => {
+ defined = [
+ definition("label:dueDate", { labelType: "date" }),
+ definition("label:done", { labelType: "boolean" }),
+ definition("label:notes"),
+ definition("relation:owner")
+ ];
+ draw();
+
+ expect(icons()).toEqual([
+ "bx bx-calendar", "bx bx-toggle-left", "bx bx-text", "bx bx-transfer"
+ ]);
+ // And says which kind it is, in the words the editor lists them under.
+ expect(types()).toEqual([
+ "attribute_detail.date", "attribute_detail.boolean", "attribute_detail.text",
+ "attribute_detail.relation_type"
+ ]);
+ });
+
it("follows the order the settings give, and turns off what they hide", () => {
settings = [ { name: "owner", hidden: true }, { name: "dueDate" } ];
draw();
@@ -344,6 +367,16 @@ describe("PromotedAttributesCard", () => {
return [ ...container.querySelectorAll<HTMLElement>(".tn-sortable-segment") ];
}
+ function types() {
+ return segments().map((segment) =>
+ segment.querySelector(".promoted-attribute-type .text")?.textContent);
+ }
+
+ function icons() {
+ return segments().map((segment) =>
+ segment.querySelector(".tn-icon")?.className.replace(" tn-icon", ""));
+ }
+
function names() {
return segments().map((segment) =>
segment.querySelector(".promoted-attribute-name")?.textContent);
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
index 51b3fa392e..d638155807 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.tsx
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
@@ -9,18 +9,25 @@ import attributes, { removeOwnedAttributesByNameOrType } from "../../services/at
import dialog from "../../services/dialog";
import { t } from "../../services/i18n";
import {
- AttributeDetail, type AttributeDetailOpts
+ AttributeDetail, type AttributeDetailOpts, DEFINITION_TYPES, RELATION_DEFINITION_TYPE
} from "../attribute_widgets/attribute_detail";
import {
deleteAttributeInSubtree, type PromotedAttribute, type PromotedAttributeSetting,
renameAttributeInSubtree, resolvePromotedAttributes, storedPromotedAttributes
} from "../collections/promoted_attributes";
import ActionButton from "./ActionButton";
+import { Badge } from "./Badge";
import FormToggle from "./FormToggle";
import { useTriliumEvent } from "./hooks";
import Icon from "./Icon";
import { type SortableItem, SortableCard } from "./SortableCard";
+/** What each kind of definition is called and drawn as, as the editor picks it by. */
+const TYPES = new Map(DEFINITION_TYPES.map((type) => [ type.value, type ]));
+
+/** What a definition naming no kind holds, which is what the editor starts one as. */
+const DEFAULT_TYPE = TYPES.get("text") ?? DEFINITION_TYPES[0];
+
/** The definition a new attribute starts from, which the reader names in the editor. */
const NEW_DEFINITION: Attribute = {
type: "label",
@@ -74,7 +81,7 @@ export default function PromotedAttributesCard({
const items = useMemo(() => shown.map((attribute) => ({
key: attribute.name,
caption: attribute.title,
- icon: attribute.type === "relation" ? "bx bx-transfer" : "bx bx-tag"
+ icon: typeOf(attribute).icon
})), [ shown ]);
const store = useCallback((next: PromotedAttribute[]) => {
@@ -201,6 +208,12 @@ export default function PromotedAttributesCard({
{item.icon && <Icon icon={item.icon} />}
<span className="promoted-attribute-name">{item.caption}</span>
+ <Badge
+ className="promoted-attribute-type"
+ text={typeOf(attribute).title}
+ outline
+ />
+
<ActionButton
className="promoted-attribute-edit"
icon="bx bx-edit"
@@ -259,3 +272,12 @@ export default function PromotedAttributesCard({
</>
);
}
+
+/** What an attribute holds, as the editor lists the kinds. */
+function typeOf(attribute: PromotedAttribute) {
+ const kind = attribute.type === "relation"
+ ? RELATION_DEFINITION_TYPE
+ : attribute.labelType ?? "text";
+
+ return TYPES.get(kind) ?? DEFAULT_TYPE;
+}