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/content manager: add highlight for filtering
Adorian Dorand589a4275f73
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsxindex e22f496171..fcf1156a61 100644--- a/apps/client/src/widgets/react/hooks.spec.tsx+++ b/apps/client/src/widgets/react/hooks.spec.tsx@@ -6,6 +6,35 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { type DelayedVisibilityPhase, useDelayedVisibility, useImperativeSearchHighlighlighting, useStaticTooltip } from "./hooks";+/**+ * mark.js delegating to the real implementation, so marking still works, while recording the calls+ * `unmark()` receives — its DOM effect cannot be observed under happy-dom, where it removes nothing.+ */+const markSpies = vi.hoisted(() => ({ unmark: vi.fn() }));++vi.mock("mark.js", async (importOriginal) => {+ const actual = await importOriginal<{ default: new (ctx: unknown) => Record<string, (...args: unknown[]) => unknown> }>();++ return {+ default: class {+ private inner: Record<string, (...args: unknown[]) => unknown>;++ constructor(ctx: unknown) {+ this.inner = new actual.default(ctx);+ }++ markRegExp(...args: unknown[]) {+ return this.inner.markRegExp(...args);+ }++ unmark(...args: unknown[]) {+ markSpies.unmark(...args);+ return this.inner.unmark(...args);+ }+ }+ };+});+let currentPhase: DelayedVisibilityPhase | undefined;function Probe({ active }: { active: boolean }) {@@ -169,6 +198,38 @@ describe("useImperativeSearchHighlighlighting", () => {target.remove();});+ it("clears previous highlights once the tokens are cleared", async () => {+ // Callers that keep the same element and merely drop the tokens — emptying a filter box —+ // rely on this, or the old highlights stay in the DOM for good.+ //+ // The removal itself is asserted through mark.js rather than the DOM: its `unmark()` is a+ // no-op under happy-dom (it removes nothing even from a fresh instance), so only the call+ // can be observed here.+ await mount([ "needle" ]);+ const target = content("<p>a needle here</p>");+ highlight?.(target);+ expect(target.querySelectorAll(".ck-find-result").length).toBeGreaterThan(0);+ markSpies.unmark.mockClear();++ await mount(null);+ highlight?.(target);++ expect(markSpies.unmark).toHaveBeenCalled();+ target.remove();+ });++ it("does not touch an element that was never highlighted", async () => {+ await mount(null);+ const target = content("<p>a needle here</p>");+ markSpies.unmark.mockClear();++ highlight?.(target);++ expect(markSpies.unmark).not.toHaveBeenCalled();+ expect(target.innerHTML).toBe("<p>a needle here</p>");+ target.remove();+ });+it("leaves a collapsed block closed when it holds no match", async () => {await mount([ "needle" ]);const target = content("<details><summary>t</summary><p>nothing relevant</p></details>");diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 406443ac20..ab163db788 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -1251,13 +1251,22 @@ export function useImperativeSearchHighlighlighting(highlightedTokens: string[]}, [ highlightedTokens ]);return (el: HTMLElement | null | undefined) => {- if (!el || !highlightRegex) return;+ if (!el) return;++ // Nothing has ever been highlighted here, so there is also nothing to clear.+ if (!mark.current && !highlightRegex) return;if (!mark.current) {mark.current = new Mark(el);}+ // Clearing comes first and happens even with no tokens left: callers that keep the same+ // element and merely drop the tokens (e.g. emptying a filter box) rely on this to remove the+ // previous highlights, which would otherwise stay in the DOM for good.mark.current.unmark();++ if (!highlightRegex) return;+mark.current.markRegExp(highlightRegex, {element: "span",className: "ck-find-result",diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.tsx b/apps/client/src/widgets/type_widgets/options/content_manager.tsxindex 4630ce6b82..d7f3687a74 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager.tsx@@ -150,6 +150,13 @@ export default function ContentManagerSettings({ note }: TypeWidgetProps) {const applyFilter = useMemo(() => debounce(setAppliedFilter, FILTER_DEBOUNCE_MS), []);useEffect(() => () => applyFilter.clear(), [ applyFilter ]);+ // A single token, not one per word: the query matches the typed text as one substring, so+ // splitting it would highlight words that never took part in the match.+ const highlightedTokens = useMemo(() => {+ const wanted = appliedFilter.trim();+ return wanted ? [ wanted ] : null;+ }, [ appliedFilter ]);+const clearFilter = useCallback(() => {// Drop any pending debounced call, otherwise the text just cleared would be re-applied.applyFilter.clear();@@ -187,17 +194,20 @@ export default function ContentManagerSettings({ note }: TypeWidgetProps) {} /><div className="content-manager-list">- <CategoryList pageNote={note} categories={categories} isFiltered={!!appliedFilter.trim()} />+ <CategoryList pageNote={note} categories={categories} highlightedTokens={highlightedTokens} /></div></>);}-function CategoryList({ pageNote, categories, isFiltered }: {+function CategoryList({ pageNote, categories, highlightedTokens }: {pageNote: FNote,categories: CategoryNotes[] | null,- isFiltered: boolean+ /** The filter text, so matches stand out in each row's title and path. `null` when not filtering. */+ highlightedTokens: string[] | null}) {+ const isFiltered = !!highlightedTokens;+if (!categories) {return <p className="content-manager-loading">{t("content_manager.loading")}</p>;}@@ -224,7 +234,7 @@ function CategoryList({ pageNote, categories, isFiltered }: {note={pageNote}notePath={pageNote.noteId}noteIds={notes.map(({ noteId }) => noteId)}- highlightedTokens={null}+ highlightedTokens={highlightedTokens}viewConfig={undefined}saveConfig={NOOP}media="screen"