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 tabs blocks as tabs in read-only notes
Elian Doran3c04bec1b93a
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsxindex 6a811d384d..82a3ed7b38 100644--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx@@ -25,7 +25,7 @@ import {} from "../../react/hooks";import { RawHtmlBlock } from "../../react/RawHtml";import { TypeWidgetProps } from "../type_widget";-import { applyReferenceLinks } from "./read_only_helper";+import { applyReferenceLinks, applyTabs } from "./read_only_helper";import {loadEmbeddedAttachment,loadEmbeddedNote,@@ -104,7 +104,7 @@ interface ReadOnlyTextContentProps {/*** Renders arbitrary CKEditor-style HTML with the same pipeline as {@link ReadOnlyText}:* mermaid rewriting, inline mermaid, embed expansion, KaTeX math, reference-link- * titles, code-block syntax highlighting, and image click handling. Transforms re-run+ * titles, tabs, code-block syntax highlighting, and image click handling. Transforms re-run* whenever `html` changes.*/export function ReadOnlyTextContent({@@ -144,6 +144,7 @@ export function ReadOnlyTextContent({]));applyMath(container);+ applyTabs(container);setupImageOpening(container, true);}, [ html, noteId, ntxId, contentRef ]);diff --git a/apps/client/src/widgets/type_widgets/text/read_only_helper.spec.ts b/apps/client/src/widgets/type_widgets/text/read_only_helper.spec.tsnew file mode 100644index 0000000000..66db4b2a62--- /dev/null+++ b/apps/client/src/widgets/type_widgets/text/read_only_helper.spec.ts@@ -0,0 +1,117 @@+import { describe, expect, it } from "vitest";++import { applyTabs } from "./read_only_helper";++function renderTabs(html: string) {+ const container = document.createElement("div");+ container.innerHTML = html;+ applyTabs(container);+ return container;+}++function tab(title: string, body: string) {+ return `<section class="trilium-tab"><p class="trilium-tab-title">${title}</p>` ++ `<div class="trilium-tab-panel">${body}</div></section>`;+}++function titleOf(container: HTMLElement, text: string) {+ const title = [...container.querySelectorAll<HTMLElement>(".trilium-tab-title")]+ .find(element => element.textContent === text);+ expect(title).toBeDefined();+ return title as HTMLElement;+}++function activeTitles(container: HTMLElement) {+ return [...container.querySelectorAll(".trilium-tab--active > .trilium-tab-title")]+ .map(title => title.textContent);+}++function press(element: HTMLElement, key: string) {+ const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true });+ element.dispatchEvent(event);+ return event;+}++describe("applyTabs", () => {+ it("shows the first tab and links each title to its panel", () => {+ const container = renderTabs(`<div class="trilium-tabs">${tab("A", "<p>a</p>")}${tab("B", "<p>b</p>")}</div>`);++ expect(activeTitles(container)).toEqual(["A"]);+ const titles = [...container.querySelectorAll<HTMLElement>(".trilium-tab-title")];+ const panels = [...container.querySelectorAll<HTMLElement>(".trilium-tab-panel")];+ expect(titles.map(title => title.getAttribute("aria-expanded"))).toEqual(["true", "false"]);+ for (const [index, title] of titles.entries()) {+ const panel = panels[index];+ expect(title.getAttribute("role")).toBe("button");+ expect(title.tabIndex).toBe(0);+ expect(panel.id).not.toBe("");+ expect(title.getAttribute("aria-controls")).toBe(panel.id);+ expect(panel.getAttribute("role")).toBe("region");+ expect(panel.getAttribute("aria-labelledby")).toBe(title.id);+ }+ expect(new Set([...titles, ...panels].map(element => element.id)).size).toBe(4);+ });++ it("switches tabs on click, Enter and Space", () => {+ const container = renderTabs(+ `<div class="trilium-tabs">${tab("A", "")}${tab("B", "")}${tab("C", "")}</div>`+ );++ titleOf(container, "B").click();+ expect(activeTitles(container)).toEqual(["B"]);+ expect(titleOf(container, "A").getAttribute("aria-expanded")).toBe("false");+ expect(titleOf(container, "B").getAttribute("aria-expanded")).toBe("true");++ press(titleOf(container, "C"), "Enter");+ expect(activeTitles(container)).toEqual(["C"]);++ expect(press(titleOf(container, "A"), " ").defaultPrevented).toBe(true);+ expect(activeTitles(container)).toEqual(["A"]);+ });++ it("moves between titles with the arrow, Home and End keys", () => {+ const container = renderTabs(+ `<div class="trilium-tabs">${tab("A", "")}${tab("B", "")}${tab("C", "")}</div>`+ );+ document.body.appendChild(container);++ press(titleOf(container, "A"), "ArrowRight");+ expect(activeTitles(container)).toEqual(["B"]);+ expect(document.activeElement).toBe(titleOf(container, "B"));++ press(titleOf(container, "B"), "End");+ expect(activeTitles(container)).toEqual(["C"]);++ press(titleOf(container, "C"), "ArrowRight");+ expect(activeTitles(container)).toEqual(["A"]);++ press(titleOf(container, "A"), "ArrowLeft");+ expect(activeTitles(container)).toEqual(["C"]);++ press(titleOf(container, "C"), "Home");+ expect(activeTitles(container)).toEqual(["A"]);+ expect(document.activeElement).toBe(titleOf(container, "A"));++ titleOf(container, "B").click();+ applyTabs(container);+ expect(activeTitles(container)).toEqual(["B"]);+ press(titleOf(container, "B"), "ArrowRight");+ expect(activeTitles(container)).toEqual(["C"]);++ container.remove();+ });++ it("keeps nested tabs blocks independent", () => {+ const inner = `<div class="trilium-tabs">${tab("Inner 1", "")}${tab("Inner 2", "")}</div>`;+ const container = renderTabs(+ `<div class="trilium-tabs">${tab("Outer 1", inner)}${tab("Outer 2", "")}</div>`+ );+ expect(activeTitles(container)).toEqual(["Outer 1", "Inner 1"]);++ titleOf(container, "Inner 2").click();+ expect(activeTitles(container)).toEqual(["Outer 1", "Inner 2"]);++ press(titleOf(container, "Inner 2"), "ArrowRight");+ expect(activeTitles(container)).toEqual(["Outer 1", "Inner 1"]);+ });+});diff --git a/apps/client/src/widgets/type_widgets/text/read_only_helper.ts b/apps/client/src/widgets/type_widgets/text/read_only_helper.tsindex b83f261021..02e810bf17 100644--- a/apps/client/src/widgets/type_widgets/text/read_only_helper.ts+++ b/apps/client/src/widgets/type_widgets/text/read_only_helper.ts@@ -1,4 +1,5 @@import link from "../../../services/link";+import { randomString } from "../../../services/utils";/** Fills the reference links in `container`, which shows content of `hostNoteId` if given. */export async function applyReferenceLinks(@@ -15,3 +16,83 @@ export async function applyReferenceLinks(referenceLink.replaceChildren(spanEl);}}++/**+ * Turns the tabs blocks in `container` into working tabs: the first tab of each block shows, and+ * a click on a title, Enter or Space shows that tab. The arrow keys, Home and End move between+ * the titles of a block. A block that is already set up keeps its listeners and its active tab.+ */+export function applyTabs(container: HTMLElement) {+ for (const block of container.querySelectorAll<HTMLElement>(".trilium-tabs")) {+ if (appliedTabs.has(block)) {+ continue;+ }+ appliedTabs.add(block);++ const titles: HTMLElement[] = [];+ for (const tab of block.querySelectorAll<HTMLElement>(":scope > .trilium-tab")) {+ const title = tab.querySelector<HTMLElement>(":scope > .trilium-tab-title");+ const panel = tab.querySelector<HTMLElement>(":scope > .trilium-tab-panel");+ if (!title || !panel) {+ continue;+ }++ const id = `trilium-tab-${randomString(8)}`;+ title.id = `${id}-title`;+ title.tabIndex = 0;+ title.setAttribute("role", "button");+ title.setAttribute("aria-controls", `${id}-panel`);+ panel.id = `${id}-panel`;+ panel.setAttribute("role", "region");+ panel.setAttribute("aria-labelledby", title.id);+ titles.push(title);+ }+ if (!titles.length) {+ continue;+ }++ const activate = (activeTitle: HTMLElement) => {+ for (const title of titles) {+ const isActive = title === activeTitle;+ title.parentElement?.classList.toggle("trilium-tab--active", isActive);+ title.setAttribute("aria-expanded", String(isActive));+ }+ };+ const focusAndActivate = (index: number) => {+ const title = titles[(index + titles.length) % titles.length];+ title.focus();+ activate(title);+ };++ for (const [index, title] of titles.entries()) {+ title.addEventListener("click", () => activate(title));+ title.addEventListener("keydown", (e) => {+ const step = getComputedStyle(block).direction === "rtl" ? -1 : 1;+ switch (e.key) {+ case "Enter":+ case " ":+ activate(title);+ break;+ case "ArrowRight":+ focusAndActivate(index + step);+ break;+ case "ArrowLeft":+ focusAndActivate(index - step);+ break;+ case "Home":+ focusAndActivate(0);+ break;+ case "End":+ focusAndActivate(titles.length - 1);+ break;+ default:+ return;+ }+ e.preventDefault();+ });+ }+ activate(titles[0]);+ }+}++const appliedTabs = new WeakSet<HTMLElement>();