Skip to content

Build your personal knowledge base with Trilium Notes

client/read-only notes: add support for "Search in note" to expand collapsible blocks when needed

Adorian Dorand13bc3544af3
diff --git a/apps/client/src/widgets/find_in_html.spec.ts b/apps/client/src/widgets/find_in_html.spec.ts
new file mode 100644
index 0000000000..eb43295273
--- /dev/null
+++ b/apps/client/src/widgets/find_in_html.spec.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from "vitest";
+import { expandAncestorDetails } from "./find_in_html.js";
+
+/** Build a DOM tree from an HTML string and return the element matching `selector`. */
+function mount(html: string, selector: string): HTMLElement {
+ const container = document.createElement("div");
+ container.innerHTML = html;
+ const el = container.querySelector<HTMLElement>(selector);
+ if (!el) {
+ throw new Error(`No element matched ${selector}`);
+ }
+ return el;
+}
+
+describe("expandAncestorDetails", () => {
+ it("opens a single collapsed ancestor", () => {
+ const match = mount(`<details><summary>t</summary><span class="m">hit</span></details>`, ".m");
+ expandAncestorDetails(match);
+ expect(match.closest("details")?.open).toBe(true);
+ });
+
+ it("opens every level of nested collapsed ancestors", () => {
+ const match = mount(`
+ <details class="outer"><summary>o</summary>
+ <details class="inner"><summary>i</summary><span class="m">hit</span></details>
+ </details>`, ".m");
+ expandAncestorDetails(match);
+ const outer = match.closest("details.outer");
+ const inner = match.closest("details.inner");
+ expect(outer instanceof HTMLDetailsElement && outer.open).toBe(true);
+ expect(inner instanceof HTMLDetailsElement && inner.open).toBe(true);
+ });
+
+ it("leaves an already-open ancestor open", () => {
+ const match = mount(`<details open><summary>t</summary><span class="m">hit</span></details>`, ".m");
+ expandAncestorDetails(match);
+ expect(match.closest("details")?.open).toBe(true);
+ });
+
+ it("is a no-op when there is no <details> ancestor", () => {
+ const match = mount(`<p><span class="m">hit</span></p>`, ".m");
+ expect(() => expandAncestorDetails(match)).not.toThrow();
+ });
+});
diff --git a/apps/client/src/widgets/find_in_html.ts b/apps/client/src/widgets/find_in_html.ts
index 0bf2bd376d..45cc21fd35 100644
--- a/apps/client/src/widgets/find_in_html.ts
+++ b/apps/client/src/widgets/find_in_html.ts
@@ -85,8 +85,22 @@ export default class FindInHtml {
if (this.$results?.length) {
const $current = this.$results.eq(this.currentIndex);
this.$results.removeClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
+ // Reveal matches inside collapsed <details>, like native find-in-page.
+ // Must precede scrollIntoView so the target is laid out first.
+ expandAncestorDetails($current[0]);
$current[0].scrollIntoView({ block: 'center', inline: 'center'});
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
}
}
}
+
+/** Open every collapsed `<details>` ancestor of `el` so a match inside it becomes visible. */
+export function expandAncestorDetails(el: HTMLElement) {
+ let details = el.closest("details");
+ while (details) {
+ if (!details.open) {
+ details.open = true;
+ }
+ details = details.parentElement?.closest("details") ?? null;
+ }
+}