Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/filtering: add support for extra keywords

Adorian Doran7f321dcfa187
diff --git a/apps/client/src/widgets/react/Card.spec.tsx b/apps/client/src/widgets/react/Card.spec.tsx
index c8390dc4f7..49ec3ff47c 100644
--- a/apps/client/src/widgets/react/Card.spec.tsx
+++ b/apps/client/src/widgets/react/Card.spec.tsx
@@ -186,6 +186,30 @@ describe("under a filter", () => {
expect(container.querySelector(".picture")).not.toBeNull();
});
+ it("finds a card and a setting by words that are not written on either", () => {
+ const card = renderInto(
+ <FilterProvider query="rest api">
+ <Card filterExtraKeywords="ETAPI is a REST API">
+ <OptionCardSection className="token" label="my token" />
+ </Card>
+ </FilterProvider>
+ );
+
+ // The card matched, so what it holds comes with it, and the words are never written out.
+ expect(classesOf(card, ".tn-card")).toContain("tn-filter-match");
+ expect(card.querySelector(".token")).not.toBeNull();
+ expect(card.textContent).not.toContain("REST");
+
+ const setting = renderInto(
+ <FilterProvider query="passphrase">
+ <OptionCardSection className="secret" label="Password" filterExtraKeywords="passphrase" />
+ </FilterProvider>
+ );
+
+ expect(setting.querySelector(".secret")).not.toBeNull();
+ expect(setting.textContent).toBe("Password");
+ });
+
it("keeps a section that accompanies the settings instead of being one", () => {
const card = (query: string) => renderInto(
<FilterProvider query={query}>
diff --git a/apps/client/src/widgets/react/Card.tsx b/apps/client/src/widgets/react/Card.tsx
index 8f024b44a8..dedf8eca81 100644
--- a/apps/client/src/widgets/react/Card.tsx
+++ b/apps/client/src/widgets/react/Card.tsx
@@ -4,7 +4,9 @@ import { JSX, HTMLAttributes } from "preact";
import { useContext } from "preact/hooks";
import clsx from "clsx";
-import { type FilterRole, FilterPassthrough, filterRoleClass, useFilterMatch, useIsFiltering } from "./filter";
+import {
+ type FilterRole, FilterPassthrough, filterRoleClass, useFilterMatch, useIsFiltering
+} from "./filter";
import { useUniqueName } from "./hooks";
// #region Card Frame
@@ -43,6 +45,12 @@ export interface CardProps {
* a help mark would reach for.
*/
actions?: ComponentChildren;
+ /**
+ * Words the card is found by that are not written on it, for a card with no name of its own or
+ * one whose name says less than what it holds. Reuse a sentence the page already has rather
+ * than inventing words for it: every word in it becomes one the card can be found by.
+ */
+ filterExtraKeywords?: string;
}
/**
@@ -54,7 +62,7 @@ export interface CardProps {
* nothing is torn down and rebuilt as the query changes).
*/
export function Card(props: {children: ComponentChildren} & CardProps) {
- const matched = useFilterMatch(props.heading, props.description);
+ const matched = useFilterMatch(props.heading, props.description, props.filterExtraKeywords);
const filtering = useIsFiltering();
return <div className={clsx("tn-card", props.className,
@@ -163,6 +171,11 @@ export interface OptionCardSectionProps extends CardSectionProps {
stacked?: boolean;
/** The controls the option is operated with, placed on the trailing edge. */
children?: ComponentChildren;
+ /**
+ * Words the setting is found by that are not written on it.
+ * See {@link CardProps.filterExtraKeywords}.
+ */
+ filterExtraKeywords?: string;
}
/**
@@ -176,8 +189,11 @@ export interface OptionCardSectionProps extends CardSectionProps {
* That last part is settled in CSS, from whether any sub-section is still there.
*/
export function OptionCardSection(props: OptionCardSectionProps) {
- const {label, description, name, stacked, children, className, subSections, ...rest} = props;
- const matched = useFilterMatch(label, description);
+ const {
+ label, description, name, stacked, children, className, subSections,
+ filterExtraKeywords, ...rest
+ } = props;
+ const matched = useFilterMatch(label, description, filterExtraKeywords);
const filtering = useIsFiltering();
const id = useUniqueName(name);
const bound = !!name && isValidElement(children);
@@ -187,7 +203,7 @@ export function OptionCardSection(props: OptionCardSectionProps) {
return <CardSection className={clsx("tn-card-option", className, {
"tn-card-option-stacked": stacked
})}
- // Kept for its details rather than for itself, which is what a companion is.
+ // Kept for its details rather than itself, which is what a companion is.
filterRole={filtering ? (matched ? "match" : "companion") : undefined}
subSections={subSections && (
<FilterPassthrough when={matched}>{subSections}</FilterPassthrough>
diff --git a/apps/client/src/widgets/react/filter.css b/apps/client/src/widgets/react/filter.css
index 37297303f5..898e7ed985 100644
--- a/apps/client/src/widgets/react/filter.css
+++ b/apps/client/src/widgets/react/filter.css
@@ -6,6 +6,9 @@
* the marks alone, which is why only one rule is needed here: what a group considers its own chrome
* is the group's business, and stays with the component that knows its shape.
*/
+/* Said over whatever layout the group is given: a group's own `display` carries the same weight as
+ this rule, which would leave an empty group standing or not by nothing more than the order the
+ stylesheets happen to land in. */
.tn-filter-scope:not(:has(.tn-filter-match)) {
- display: none;
+ display: none !important;
}