Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): give the attribute badges the attributes panel's icons

Elian Doranc9bf4a2031a8
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_types.ts b/apps/client/src/widgets/attribute_widgets/attribute_types.ts
index 946102b066..8af879239c 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_types.ts
+++ b/apps/client/src/widgets/attribute_widgets/attribute_types.ts
@@ -1,3 +1,6 @@
+import { promotedAttributeDefinitionParser } from "@triliumnext/commons";
+
+import { isDefinitionName } from "../../entities/fattribute";
import type { PromotedAttribute } from "../collections/promoted_attributes";
/** The kind a definition takes when it points at a note instead of holding a value. */
@@ -26,6 +29,25 @@ export const DEFINITION_TYPE_ICONS: Record<string, string> = {
[RELATION_DEFINITION_TYPE]: "bx bx-transfer"
};
+/** The icon of a label, which a definition naming a field the editor does not know keeps. */
+const LABEL_ICON = "bx bx-hash";
+
+/**
+ * What an attribute is, as the attributes panel draws it: the icon of a label or of a relation, or for
+ * a definition (`label:foo`, `relation:foo`) the icon of the field it sets up.
+ */
+export function attributeKindIcon(type: string, name: string, value: string) {
+ if (type === "relation" || (isDefinitionName(name) && name.startsWith("relation:"))) {
+ return DEFINITION_TYPE_ICONS[RELATION_DEFINITION_TYPE];
+ }
+ if (!isDefinitionName(name)) {
+ return LABEL_ICON;
+ }
+
+ const labelType = promotedAttributeDefinitionParser.parse(value).labelType ?? "text";
+ return DEFINITION_TYPE_ICONS[labelType] ?? LABEL_ICON;
+}
+
/** The icon for an attribute's kind: its `labelType`, or the relation icon for a relation. */
export function promotedAttributeIcon(attribute: PromotedAttribute) {
const kind = attribute.type === "relation"
diff --git a/apps/client/src/widgets/react/Badge.spec.tsx b/apps/client/src/widgets/react/Badge.spec.tsx
index ba15dca750..1206f50169 100644
--- a/apps/client/src/widgets/react/Badge.spec.tsx
+++ b/apps/client/src/widgets/react/Badge.spec.tsx
@@ -4,12 +4,22 @@ import { renderInto } from "../../test/render";
import { AttributeSnippetBadges, parseAttributeSnippetLine } from "./Badge";
describe("parseAttributeSnippetLine", () => {
- it("splits a label or a relation into its kind, name and value, highlights kept", () => {
- expect(parseAttributeSnippetLine("#<b>tag</b>")).toEqual({ type: "label", name: "<b>tag</b>" });
+ it("splits a label or a relation into its kind's icon, its name and its value, highlights kept", () => {
+ expect(parseAttributeSnippetLine("#<b>tag</b>")).toEqual({ icon: "bx bx-hash", name: "<b>tag</b>" });
expect(parseAttributeSnippetLine("#status=&quot;<b class=\"search-fuzzy-match\">open</b>&quot;"))
- .toEqual({ type: "label", name: "status", value: "<b class=\"search-fuzzy-match\">open</b>" });
+ .toEqual({ icon: "bx bx-hash", name: "status", value: "<b class=\"search-fuzzy-match\">open</b>" });
expect(parseAttributeSnippetLine("~author=&quot;J. R. R. &quot;Tolkien&quot;&quot;"))
- .toEqual({ type: "relation", name: "author", value: "J. R. R. &quot;Tolkien&quot;" });
+ .toEqual({ icon: "bx bx-transfer", name: "author", value: "J. R. R. &quot;Tolkien&quot;" });
+ });
+
+ it("gives a definition its field's icon and its bare name, its options being no value", () => {
+ expect(parseAttributeSnippetLine("#label:<b>due</b>=&quot;promoted,single,date&quot;"))
+ .toEqual({ icon: "bx bx-calendar", name: "<b>due</b>" });
+ expect(parseAttributeSnippetLine("#relation:author=&quot;promoted&quot;"))
+ .toEqual({ icon: "bx bx-transfer", name: "author" });
+ // A highlight across the prefix leaves the name as text.
+ expect(parseAttributeSnippetLine("#<b>label:due</b>=&quot;promoted&quot;"))
+ .toEqual({ icon: "bx bx-text", name: "due" });
});
it("gives up on a line it cannot read, such as one the server cut short", () => {
@@ -27,7 +37,7 @@ describe("AttributeSnippetBadges", () => {
const [ tag, template, cut ] = container.querySelectorAll<HTMLElement>(".attrs > .ext-badge.outline");
expect(tag.classList.contains("attribute-badge")).toBe(true);
- expect(tag.querySelector(".tn-icon")?.className).toBe("bx bx-purchase-tag tn-icon");
+ expect(tag.querySelector(".tn-icon")?.className).toBe("bx bx-hash tn-icon");
expect(tag.querySelector(".attribute-badge-name")?.innerHTML).toBe("<b>tag</b>");
expect(tag.querySelector(".attribute-badge-value")).toBeNull();
@@ -39,4 +49,20 @@ describe("AttributeSnippetBadges", () => {
expect(cut.classList.contains("attribute-badge")).toBe(false);
expect(cut.textContent).toBe("#status=\"cut sho...");
});
+
+ it("draws a definition as the attributes panel does: its field's icon and its bare name", () => {
+ const container = renderInto(<AttributeSnippetBadges
+ className="attrs"
+ snippet={"#label:<b>due</b>=&quot;promoted,single,date&quot;<br>#relation:author=&quot;promoted&quot;"}
+ />);
+ const [ due, author ] = container.querySelectorAll<HTMLElement>(".attrs > .ext-badge");
+
+ expect(due.querySelector(".tn-icon")?.className).toBe("bx bx-calendar tn-icon");
+ expect(due.querySelector(".attribute-badge-name")?.innerHTML).toBe("<b>due</b>");
+ // What the definition sets up is the icon; its options are no value of the note's.
+ expect(due.querySelector(".attribute-badge-value")).toBeNull();
+
+ expect(author.querySelector(".tn-icon")?.className).toBe("bx bx-transfer tn-icon");
+ expect(author.querySelector(".attribute-badge-name")?.textContent).toBe("author");
+ });
});
diff --git a/apps/client/src/widgets/react/Badge.tsx b/apps/client/src/widgets/react/Badge.tsx
index e390abc333..5dfb22537f 100644
--- a/apps/client/src/widgets/react/Badge.tsx
+++ b/apps/client/src/widgets/react/Badge.tsx
@@ -4,6 +4,9 @@ import clsx from "clsx";
import { ComponentChildren, HTMLAttributes } from "preact";
import { useRef } from "preact/hooks";
+import { isDefinitionName } from "../../entities/fattribute";
+import { escapeHtml } from "../../services/utils";
+import { attributeKindIcon } from "../attribute_widgets/attribute_types";
import Dropdown, { DropdownProps } from "./Dropdown";
import { useStaticTooltip } from "./hooks";
import Icon from "./Icon";
@@ -77,7 +80,7 @@ export function AttributeSnippetBadges({ snippet, className }: { snippet: string
key={index}
outline
className={clsx("attribute-badge", { "has-value": !!attribute.value })}
- icon={attribute.type === "label" ? "bx bx-purchase-tag" : "bx bx-transfer"}
+ icon={attribute.icon}
text={<>
<RawHtml className="attribute-badge-name" html={attribute.name} />
{attribute.value && (
@@ -93,24 +96,48 @@ export function AttributeSnippetBadges({ snippet, className }: { snippet: string
);
}
-/** One attribute of a search result's snippet, its name and value as highlighted HTML. */
+/**
+ * One attribute of a search result's snippet: its kind's icon, and its name and value as highlighted
+ * HTML.
+ */
export interface AttributeSnippetLine {
- type: "label" | "relation";
+ icon: string;
name: string;
value?: string;
}
/**
* Reads a line of `highlightedAttributeSnippet` as `extractAttributeSnippet()` writes it, `#name`,
- * `#name="value"` or `~name="target title"`, escaped, so its quotes arrive as `&quot;`. Returns
- * `null` for a line in no such shape, such as one the server cut short.
+ * `#name="value"` or `~name="target title"`, escaped, so its quotes arrive as `&quot;`. The icon is
+ * the attributes panel's; a definition (`#label:name="promoted,…"`) is shown by its bare name, the
+ * icon standing for the field it sets up. Returns `null` for a line in no such shape, such as one the
+ * server cut short.
*/
export function parseAttributeSnippetLine(line: string): AttributeSnippetLine | null {
const match = /^([#~])(.+?)(?:=&quot;(.*)&quot;)?$/s.exec(line);
if (!match || match[2].includes("=&quot;")) return null;
const [ , prefix, name, value ] = match;
- return { type: prefix === "#" ? "label" : "relation", name, ...(value ? { value } : {}) };
+ const type = prefix === "#" ? "label" : "relation";
+ const plainName = htmlToText(name);
+ const icon = attributeKindIcon(type, plainName, htmlToText(value ?? ""));
+
+ if (type === "label" && isDefinitionName(plainName)) {
+ const definitionPrefix = plainName.substring(0, plainName.indexOf(":") + 1);
+ // A highlight across the prefix leaves no bare name to keep it in, so the name is shown as text.
+ const bareName = name.startsWith(definitionPrefix)
+ ? name.substring(definitionPrefix.length)
+ : escapeHtml(plainName.substring(definitionPrefix.length));
+ return { icon, name: bareName };
+ }
+
+ return { icon, name, ...(value ? { value } : {}) };
+}
+
+function htmlToText(html: string) {
+ const template = document.createElement("template");
+ template.innerHTML = html;
+ return template.content.textContent ?? "";
}
export function BadgeWithDropdown({ text, children, tooltip, className, dropdownProps, ...props }: BadgeProps & {
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index 959a5c15bb..606bcdd081 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -16,7 +16,7 @@ import { t } from "../../services/i18n";
import server from "../../services/server";
import { isMobile } from "../../services/utils";
import { AttributeDetail, AttributeDetailOpts, AttributeForm, AttrType, DEFINITION_TYPES, getAttrType } from "../attribute_widgets/attribute_detail";
-import { RELATION_DEFINITION_TYPE } from "../attribute_widgets/attribute_types";
+import { attributeKindIcon, RELATION_DEFINITION_TYPE } from "../attribute_widgets/attribute_types";
import { ColorChip, renderLabelValue } from "../attribute_widgets/label_value_display";
import ActionButton from "../react/ActionButton";
import { FormListItem } from "../react/FormList";
@@ -624,7 +624,7 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, showOwne
const rowRef = useRef<HTMLLIElement>(null);
const attrType = getAttributeKind(attribute);
const markerClass = getKindMarkerClass(attribute, attrType, isSystem);
- const kindIcon = getKindIcon(attribute, attrType);
+ const kindIcon = attributeKindIcon(attribute.type, attribute.name, attribute.value ?? "");
const kindTooltip = getKindTooltip(attribute, attrType, isSystem);
const rowClass = clsx("attribute-row", active && "active", valueEditor && "editing");
@@ -730,21 +730,6 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, showOwne
*/
const IS_MOBILE = isMobile();
-/**
- * What the attribute is, as an icon. A definition takes the icon of the field it sets up, the same one
- * the popup offers that field under — it needs no marker of its own, being only ever listed in a card
- * of definitions. Everything else is the icon of a label or of a relation.
- */
-function getKindIcon(attribute: Attribute, attrType: AttributeKind) {
- if (isDefinition(attrType)) {
- // A definition written by hand can name a field the popup knows nothing of, leaving the icon of
- // the label it defines to stand for it.
- return getDefinitionType(attribute, attrType)?.icon ?? "bx bx-hash";
- }
-
- return attrType === "relation" ? "bx bx-transfer" : "bx bx-hash";
-}
-
/**
* The badge the kind icon carries on its corner, where there is one to carry: a cog for the names
* Trilium reads for itself, and a chevron for a definition whose field is promoted — lifted, that is,