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(text): show the tab holding a find result in read-only notes
Elian Doranc8bc84df8253
diff --git a/apps/client/src/widgets/find_in_html.spec.ts b/apps/client/src/widgets/find_in_html.spec.tsindex e3aa2da308..6e7d1f1f25 100644--- a/apps/client/src/widgets/find_in_html.spec.ts+++ b/apps/client/src/widgets/find_in_html.spec.ts@@ -1,5 +1,7 @@import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { applyTabs } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";+import FindInHtml from "./find_in_html.js";/**@@ -89,4 +91,23 @@ describe("FindInHtml", () => {expect(secondary).toContain("a.c");expect(secondary).toContain("a+c");});++ it("shows the tab that holds the current match", async () => {+ const { finder, container } = setup(+ "<div class=\"trilium-tabs\">" ++ "<section class=\"trilium-tab\"><p class=\"trilium-tab-title\">A</p>" ++ "<div class=\"trilium-tab-panel\"><p>first</p></div></section>" ++ "<section class=\"trilium-tab\"><p class=\"trilium-tab-title\">B</p>" ++ "<div class=\"trilium-tab-panel\"><p>needle</p></div></section>" ++ "</div>"+ );+ applyTabs(container, { placeholder: "" });+ const activeTitle = () => container.querySelector(".trilium-tab--active > .trilium-tab-title")?.textContent;+ expect(activeTitle()).toBe("A");++ const result = await finder.performFind("needle", false, false);++ expect(result.totalFound).toBe(1);+ expect(activeTitle()).toBe("B");+ });});diff --git a/apps/client/src/widgets/find_in_html.ts b/apps/client/src/widgets/find_in_html.tsindex fd4163ab0a..a5ce66bf98 100644--- a/apps/client/src/widgets/find_in_html.ts+++ b/apps/client/src/widgets/find_in_html.ts@@ -1,7 +1,9 @@// ck-find-result and ck-find-result_selected are the styles ck-editor// uses for highlighting matches, use the same one on CodeMirror// for consistency+import { revealTab } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";import type Mark from "mark.js";+import { expandAncestorDetails } from "../services/collapsible.js";import utils from "../services/utils.js";import type FindWidget from "./find.js";@@ -119,9 +121,10 @@ 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.+ // Reveal matches inside collapsed <details> and inactive tabs, like native find-in-page.// Must precede scrollIntoView so the target is laid out first (#10616).expandAncestorDetails($current[0]);+ revealTab($current[0]);$current[0].scrollIntoView({ block: 'center', inline: 'center'});$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);}diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.tsindex 6aeae640af..b5a6c1457a 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { applyTabs } from "./tabs_read_only.js";+import { applyTabs, revealTab } from "./tabs_read_only.js";const PLACEHOLDER = "Tab title";@@ -133,4 +133,22 @@ describe("applyTabs", () => {press(titleOf(container, "Inner 2"), "ArrowRight");expect(activeTitles(container)).toEqual(["Outer 1", "Inner 1"]);});++ it("reveals every tab that encloses an element", () => {+ const inner = `<div class="trilium-tabs">${tab("Inner 1", "")}${tab("Inner 2", "<p id=\"target\">x</p>")}</div>`;+ const container = renderTabs(+ `<div class="trilium-tabs">${tab("Outer 1", "")}${tab("Outer 2", inner)}</div><p id="outside">y</p>`+ );+ expect(activeTitles(container)).toEqual(["Outer 1", "Inner 1"]);++ const target = container.querySelector("#target");+ expect(target).not.toBeNull();+ revealTab(target as Element);+ expect(activeTitles(container)).toEqual(["Outer 2", "Inner 2"]);+ expect(titleOf(container, "Outer 1").getAttribute("aria-expanded")).toBe("false");++ titleOf(container, "Outer 1").click();+ revealTab(container.querySelector("#outside") as Element);+ expect(activeTitles(container)).toEqual(["Outer 1", "Inner 2"]);+ });});diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.tsindex 605e086b27..ed37d37608 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts@@ -15,10 +15,9 @@ export interface ApplyTabsOptions {*/export function applyTabs(container: ParentNode, { placeholder }: ApplyTabsOptions) {for (const block of container.querySelectorAll<HTMLElement>(`.${CLASSES.tabs}`)) {- if (appliedTabs.has(block)) {+ if (activators.has(block)) {continue;}- appliedTabs.add(block);const titles: HTMLElement[] = [];for (const tab of block.querySelectorAll<HTMLElement>(`:scope > .${CLASSES.tab}`)) {@@ -56,6 +55,7 @@ export function applyTabs(container: ParentNode, { placeholder }: ApplyTabsOptiotitle.setAttribute("aria-expanded", String(isActive));}};+ activators.set(block, activate);const focusAndActivate = (index: number) => {const title = titles[(index + titles.length) % titles.length];title.focus();@@ -93,5 +93,22 @@ export function applyTabs(container: ParentNode, { placeholder }: ApplyTabsOptio}}-const appliedTabs = new WeakSet<HTMLElement>();+/**+ * Shows every tab that encloses `element`, such as the one holding a find result, in blocks that+ * {@link applyTabs} set up.+ */+export function revealTab(element: Element) {+ let tab = element.closest(`.${CLASSES.tab}`);+ while (tab) {+ const block = tab.parentElement;+ const title = tab.querySelector<HTMLElement>(`:scope > .${CLASSES.tabTitle}`);+ if (block && title) {+ activators.get(block)?.(title);+ }+ tab = block?.closest(`.${CLASSES.tab}`) ?? null;+ }+}++/** Shows the tab of the given title, keyed by the tabs block that {@link applyTabs} set up. */+const activators = new WeakMap<HTMLElement, (title: HTMLElement) => void>();let lastTabId = 0;