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(search): draw a matched attribute as an icon, a name and a value
Elian Doran717e45530b37
diff --git a/apps/client/src/widgets/quick_search.spec.tsx b/apps/client/src/widgets/quick_search.spec.tsxindex 254f5abc66..94557a087a 100644--- a/apps/client/src/widgets/quick_search.spec.tsx+++ b/apps/client/src/widgets/quick_search.spec.tsx@@ -75,7 +75,8 @@ describe("QuickSearch", () => {expect(first.querySelector(".quick-search-item-icon")?.classList.contains("bx-note")).toBe(true);expect(first.querySelector(".search-result-title")?.innerHTML).toBe("<b>Note</b> 0");const badges = first.querySelectorAll(".search-result-attributes > .ext-badge.outline");- expect([ ...badges ].map((badge) => badge.textContent)).toEqual([ "#year=1954", "#author=tolkien" ]);+ const values = [ ...badges ].map((badge) => badge.querySelector(".attribute-badge-value")?.textContent);+ expect(values).toEqual([ "1954", "tolkien" ]);expect(first.querySelector(".search-result-content")?.innerHTML).toBe("about <b>hello</b>");});@@ -552,7 +553,7 @@ function response(count: number, highlightedTokens: string[]): QuickSearchResponnoteTitle: `Note ${index}`,notePathTitle: `Note ${index}`,highlightedNotePathTitle: `<b>Note</b> ${index}`,- highlightedAttributeSnippet: "#year=1954<br>#author=tolkien",+ highlightedAttributeSnippet: "#year="1954"<br>#author="tolkien"",highlightedContentSnippet: "about <b>hello</b>",icon: "bx bx-note"}));diff --git a/apps/client/src/widgets/react/Badge.css b/apps/client/src/widgets/react/Badge.cssindex 5d785153ce..d1b55c6243 100644--- a/apps/client/src/widgets/react/Badge.css+++ b/apps/client/src/widgets/react/Badge.css@@ -55,6 +55,61 @@}}+/*+ * An attribute a search matched: its kind's icon and its name, then its value in a tinted segment+ * reaching the badge's end. Muted unless the host sets `--color`.+ */+.ext-badge.attribute-badge {+ overflow: hidden;+ border-color: var(--color, var(--muted-text-color));+ color: var(--color, var(--muted-text-color));++ &.has-value {+ padding-inline-end: 0;+ }++ /* Centered by the badge, so it takes none of the Next theme's `.dropdown-item .tn-icon` offset. */+ .tn-icon {+ translate: none;+ }++ > span,+ .text {+ align-self: stretch;+ }++ .text {+ display: flex;+ align-items: center;+ min-width: 0;+ }++ .attribute-badge-name {+ flex-shrink: 0;+ max-width: 50%;+ overflow: hidden;+ text-overflow: ellipsis;+ }++ .attribute-badge-value {+ display: flex;+ align-items: center;+ align-self: stretch;+ min-width: 0;+ margin-inline-start: 6px;+ padding-inline: 6px 8px;+ border-inline-start: 1px solid var(--color, var(--muted-text-color));+ background: color-mix(in srgb, var(--color, var(--muted-text-color)) 15%, transparent);+ color: var(--main-text-color);++ > span {+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+ }+}+.dropdown-badge {min-width: 0;overflow: hidden;diff --git a/apps/client/src/widgets/react/Badge.spec.tsx b/apps/client/src/widgets/react/Badge.spec.tsxnew file mode 100644index 0000000000..ba15dca750--- /dev/null+++ b/apps/client/src/widgets/react/Badge.spec.tsx@@ -0,0 +1,42 @@+import { describe, expect, it } from "vitest";++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>" });+ expect(parseAttributeSnippetLine("#status="<b class=\"search-fuzzy-match\">open</b>""))+ .toEqual({ type: "label", name: "status", value: "<b class=\"search-fuzzy-match\">open</b>" });+ expect(parseAttributeSnippetLine("~author="J. R. R. "Tolkien"""))+ .toEqual({ type: "relation", name: "author", value: "J. R. R. "Tolkien"" });+ });++ it("gives up on a line it cannot read, such as one the server cut short", () => {+ expect(parseAttributeSnippetLine("#status="a long val...")).toBeNull();+ expect(parseAttributeSnippetLine("status")).toBeNull();+ });+});++describe("AttributeSnippetBadges", () => {+ it("draws each attribute as its kind's icon, its name and its value, or the line as it came", () => {+ const container = renderInto(<AttributeSnippetBadges+ className="attrs"+ snippet={"#<b>tag</b><br>~template="Book"<br>#status="cut sho..."}+ />);+ 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(".attribute-badge-name")?.innerHTML).toBe("<b>tag</b>");+ expect(tag.querySelector(".attribute-badge-value")).toBeNull();++ expect(template.querySelector(".tn-icon")?.className).toBe("bx bx-transfer tn-icon");+ expect(template.querySelector(".attribute-badge-name")?.textContent).toBe("template");+ expect(template.querySelector(".attribute-badge-value")?.textContent).toBe("Book");+ expect(template.classList.contains("has-value")).toBe(true);++ expect(cut.classList.contains("attribute-badge")).toBe(false);+ expect(cut.textContent).toBe("#status=\"cut sho...");+ });+});diff --git a/apps/client/src/widgets/react/Badge.tsx b/apps/client/src/widgets/react/Badge.tsxindex aceffc4280..e390abc333 100644--- a/apps/client/src/widgets/react/Badge.tsx+++ b/apps/client/src/widgets/react/Badge.tsx@@ -57,7 +57,8 @@ export function Badge({ icon, className, text, tooltip, href, outline, ...contai/*** A search result's `highlightedAttributeSnippet`, which the server joins with `<br>`, as one outline- * badge per attribute, its search highlights kept.+ * badge per attribute, its search highlights kept: the icon of its kind, its name, and its value in a+ * segment of its own. A line that does not parse is shown as it came.*/export function AttributeSnippetBadges({ snippet, className }: { snippet: string | undefined; className: string }) {const lines = snippet?.split(/<br\s*\/?>/i).map((line) => line.trim()).filter(Boolean);@@ -65,13 +66,53 @@ export function AttributeSnippetBadges({ snippet, className }: { snippet: stringreturn (<div className={className}>- {lines.map((line, index) => (- <Badge key={index} outline text={<RawHtml html={line} />} />- ))}+ {lines.map((line, index) => {+ const attribute = parseAttributeSnippetLine(line);+ if (!attribute) {+ return <Badge key={index} outline text={<RawHtml html={line} />} />;+ }++ return (+ <Badge+ key={index}+ outline+ className={clsx("attribute-badge", { "has-value": !!attribute.value })}+ icon={attribute.type === "label" ? "bx bx-purchase-tag" : "bx bx-transfer"}+ text={<>+ <RawHtml className="attribute-badge-name" html={attribute.name} />+ {attribute.value && (+ // The segment spans the badge's height; the text in it can end in an+ // ellipsis, which a flex container's own text cannot.+ <span className="attribute-badge-value"><RawHtml html={attribute.value} /></span>+ )}+ </>}+ />+ );+ })}</div>);}+/** One attribute of a search result's snippet, its name and value as highlighted HTML. */+export interface AttributeSnippetLine {+ type: "label" | "relation";+ 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 `"`. 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 = /^([#~])(.+?)(?:="(.*)")?$/s.exec(line);+ if (!match || match[2].includes("="")) return null;++ const [ , prefix, name, value ] = match;+ return { type: prefix === "#" ? "label" : "relation", name, ...(value ? { value } : {}) };+}+export function BadgeWithDropdown({ text, children, tooltip, className, dropdownProps, ...props }: BadgeProps & {children: ComponentChildren,dropdownProps?: Partial<DropdownProps>diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsxindex f72709c99c..798a2dc015 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -123,7 +123,7 @@ describe("NoteAutocomplete's suggestion list", () => {const notes: Suggestion[] = [{ notePath: "root/a", noteTitle: "Alpha", notePathTitle: "Alpha", highlightedNotePathTitle: "<b>Al</b>pha", icon: "bx bx-file" },{ notePath: "root/x/b", noteTitle: "Beta", notePathTitle: "X / Beta", highlightedNotePathTitle: "X / Beta",- highlightedAttributeSnippet: "#tag<br>#status=\"<b>al</b>\"" }+ highlightedAttributeSnippet: "#tag<br>#status="<b>al</b>"" }];const commands: Suggestion[] = [{ action: "command", commandId: "cmd1", noteTitle: "Cmd One", highlightedNotePathTitle: "Cmd One",@@ -205,8 +205,9 @@ describe("NoteAutocomplete's suggestion list", () => {expect(beta.querySelector(".tn-icon")?.className).toBe("bx bx-note tn-icon");// One outline badge per attribute, its highlight kept.const badges = beta.querySelectorAll(".note-suggestion-attributes > .ext-badge.outline");- expect([ ...badges ].map((badge) => badge.textContent)).toEqual([ "#tag", "#status=\"al\"" ]);- expect(badges[1].querySelector("b")?.textContent).toBe("al");+ const names = [ ...badges ].map((badge) => badge.querySelector(".attribute-badge-name")?.textContent);+ expect(names).toEqual([ "tag", "status" ]);+ expect(badges[1].querySelector(".attribute-badge-value b")?.textContent).toBe("al");expect(input.getAttribute("aria-expanded")).toBe("true");expect(input.getAttribute("aria-activedescendant")).toBe(alpha.id);});