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 link target in read-only and shared notes
Elian Doran529f9abf486e
diff --git a/apps/client/src/services/block_reference.spec.ts b/apps/client/src/services/block_reference.spec.tsindex 4cdac7557e..2936bf9857 100644--- a/apps/client/src/services/block_reference.spec.ts+++ b/apps/client/src/services/block_reference.spec.ts@@ -1,4 +1,5 @@import type { CKTextEditor } from "@triliumnext/ckeditor5";+import { applyTabs } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type { CommandNames } from "../components/app_context.js";@@ -68,6 +69,20 @@ describe("consumeBlockReference", () => {expect(block?.classList.contains("block-reference-flash")).toBe(false);});+ it("shows the tab that holds the block", () => {+ const container = buildContainer(+ `<div class="trilium-tabs">` ++ `<section class="trilium-tab"><p class="trilium-tab-title">A</p><div class="trilium-tab-panel"></div></section>` ++ `<section class="trilium-tab"><p class="trilium-tab-title">B</p><div class="trilium-tab-panel"><p data-trilium-block-id="a">A</p></div></section>` ++ `</div>`+ );+ applyTabs(container, { placeholder: "" });++ consumeBlockReference(container, { block: "a" });++ expect(container.querySelector(".trilium-tab--active > .trilium-tab-title")?.textContent).toBe("B");+ });+it("flashes the outermost elements of a range", () => {const container = buildContainer("<p>before</p><p data-trilium-block-id=\"a\">A</p><blockquote><p>quote</p></blockquote>"diff --git a/apps/client/src/services/block_reference.ts b/apps/client/src/services/block_reference.tsindex e1f2e55c00..b9a4044bf9 100644--- a/apps/client/src/services/block_reference.ts+++ b/apps/client/src/services/block_reference.ts@@ -9,7 +9,7 @@ import type { ClipboardAccess } from "../menus/table_context_menu.js";import { getTextEditorContaining } from "../menus/text_editor_context_menu.js";import { getBlockExcerpt } from "./block_excerpts.js";import { copyHtmlWithToast } from "./clipboard_ext.js";-import { expandAncestorDetails } from "./collapsible.js";+import { revealElement } from "./collapsible.js";import froca from "./froca.js";import { t } from "./i18n.js";import { calculateHash, parseNavigationStateFromUrl, type ViewScope } from "./link.js";@@ -137,7 +137,7 @@ export function consumeBlockReference(const first = start ?? end;if (first) {- expandAncestorDetails(first);+ revealElement(first);first.scrollIntoView({ behavior: "smooth", block: "center" });flashBlocks(start && end ? getBlockRangeElements(start, end) : [ first ]);}@@ -147,9 +147,9 @@ export function consumeBlockReference(}/**- * Highlights the blocks that `value`, a `block` link parameter, points at in `container`, and opens- * the collapsed blocks around its first and last block. Of a broken range, the block found is- * highlighted.+ * Highlights the blocks that `value`, a `block` link parameter, points at in `container`, and+ * reveals its first and last block inside collapsed blocks and inactive tabs. Of a broken range,+ * the block found is highlighted.*/export function highlightBlockReference(container: HTMLElement, value: string) {const { start, end } = resolveBlockReference<HTMLElement>(container, value);@@ -160,7 +160,7 @@ export function highlightBlockReference(container: HTMLElement, value: string) {for (const block of [ start, end ]) {if (block) {- expandAncestorDetails(block);+ revealElement(block);}}const elements = start && end ? getBlockRangeElements(start, end) : [ first ];diff --git a/apps/client/src/services/bookmark_jump.spec.ts b/apps/client/src/services/bookmark_jump.spec.tsindex caf9df732d..3152bd852e 100644--- a/apps/client/src/services/bookmark_jump.spec.ts+++ b/apps/client/src/services/bookmark_jump.spec.ts@@ -1,3 +1,4 @@+import { applyTabs } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";import { beforeEach, describe, expect, it, vi } from "vitest";import { consumeBookmark } from "./bookmark_jump.js";@@ -34,6 +35,20 @@ describe("consumeBookmark", () => {expect(viewScope.bookmark).toBeUndefined();});+ it("shows the tab that holds the target", () => {+ const container = buildContainer(+ `<div class="trilium-tabs">` ++ `<section class="trilium-tab"><p class="trilium-tab-title">A</p><div class="trilium-tab-panel"></div></section>` ++ `<section class="trilium-tab"><p class="trilium-tab-title">B</p><div class="trilium-tab-panel"><p><a id="in-tab"></a>target</p></div></section>` ++ `</div>`+ );+ applyTabs(container, { placeholder: "" });++ consumeBookmark(container, { bookmark: "in-tab" });++ expect(container.querySelector(".trilium-tab--active > .trilium-tab-title")?.textContent).toBe("B");+ });+it("leaves the bookmark unconsumed when the content container is not rendered yet", () => {const viewScope: ViewScope = { bookmark: "deep" };diff --git a/apps/client/src/services/bookmark_jump.ts b/apps/client/src/services/bookmark_jump.tsindex 92a43377af..fc8a1aeb75 100644--- a/apps/client/src/services/bookmark_jump.ts+++ b/apps/client/src/services/bookmark_jump.ts@@ -1,9 +1,9 @@-import { expandAncestorDetails } from "./collapsible.js";+import { revealElement } from "./collapsible.js";import type { ViewScope } from "./link.js";/*** Consumes `viewScope.bookmark` (the `?bookmark=` link parameter) against a rendered note- * content container: expands any closed collapsible ancestor of the target, scrolls to it,+ * content container: reveals the target inside collapsed blocks and inactive tabs, scrolls to it,* and clears the bookmark so it fires only once (mirrors `consumeSearchTerms`).** A null `container` leaves the bookmark untouched, because the content is not rendered yet and@@ -20,7 +20,7 @@ export function consumeBookmark(container: ParentNode | null | undefined, viewScconst bookmark = viewScope.bookmark;const el = [...container.querySelectorAll("[id]")].find((candidate) => candidate.id === bookmark);if (el) {- expandAncestorDetails(el);+ revealElement(el);el.scrollIntoView({ behavior: "smooth", block: "center" });}viewScope.bookmark = undefined;diff --git a/apps/client/src/services/collapsible.spec.ts b/apps/client/src/services/collapsible.spec.tsindex 58f0729d3d..b8abaf1a99 100644--- a/apps/client/src/services/collapsible.spec.ts+++ b/apps/client/src/services/collapsible.spec.ts@@ -1,5 +1,7 @@+import { applyTabs } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";import { describe, expect, it } from "vitest";-import { expandAncestorDetails } from "./collapsible.js";++import { expandAncestorDetails, revealElement } from "./collapsible.js";/** Build a DOM tree from an HTML string and return the element matching `selector`. */function mount(html: string, selector: string): HTMLElement {@@ -65,3 +67,24 @@ describe("expandAncestorDetails", () => {expect(outer instanceof HTMLDetailsElement && outer.open).toBe(true);});});++describe("revealElement", () => {+ it("opens the collapsed blocks and shows the tabs around an element", () => {+ const container = document.createElement("div");+ container.innerHTML =+ `<div class="trilium-tabs">` ++ `<section class="trilium-tab"><p class="trilium-tab-title">A</p><div class="trilium-tab-panel"></div></section>` ++ `<section class="trilium-tab"><p class="trilium-tab-title">B</p><div class="trilium-tab-panel">` ++ `<details><summary>s</summary><span class="m">hit</span></details>` ++ `</div></section>` ++ `</div>`;+ applyTabs(container, { placeholder: "" });+ const match = container.querySelector(".m");+ expect(match).not.toBeNull();++ revealElement(match as Element);++ expect(container.querySelector("details")?.open).toBe(true);+ expect(container.querySelector(".trilium-tab--active > .trilium-tab-title")?.textContent).toBe("B");+ });+});diff --git a/apps/client/src/services/collapsible.ts b/apps/client/src/services/collapsible.tsindex 7efbfeb2c1..a7e0f16cab 100644--- a/apps/client/src/services/collapsible.ts+++ b/apps/client/src/services/collapsible.ts@@ -1,3 +1,5 @@+import { revealTab } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";+/** Open every collapsed `<details>` ancestor of `el` so content inside it becomes visible. */export function expandAncestorDetails(el: Element) {let details = el.closest("details");@@ -8,3 +10,9 @@ export function expandAncestorDetails(el: Element) {details = details.parentElement?.closest("details") ?? null;}}++/** Shows `el` wherever it is hidden: opens the `<details>` and shows the tabs that enclose it. */+export function revealElement(el: Element) {+ expandAncestorDetails(el);+ revealTab(el);+}diff --git a/apps/client/src/widgets/find_in_html.ts b/apps/client/src/widgets/find_in_html.tsindex a5ce66bf98..67b5aac2cf 100644--- a/apps/client/src/widgets/find_in_html.ts+++ b/apps/client/src/widgets/find_in_html.ts@@ -1,10 +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 { revealElement } from "../services/collapsible.js";import utils from "../services/utils.js";import type FindWidget from "./find.js";import type { FindResult } from "./find.js";@@ -123,8 +122,7 @@ export default class FindInHtml {this.$results.removeClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);// 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]);+ revealElement($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 b5a6c1457a..9e568786b0 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, revealTab } from "./tabs_read_only.js";+import { applyTabs, revealFragment, revealTab } from "./tabs_read_only.js";const PLACEHOLDER = "Tab title";@@ -151,4 +151,19 @@ describe("applyTabs", () => {revealTab(container.querySelector("#outside") as Element);expect(activeTitles(container)).toEqual(["Outer 1", "Inner 2"]);});++ it("reveals the element a URL fragment names", () => {+ const container = renderTabs(+ `<div class="trilium-tabs">${tab("A", "")}${tab("B", "<h2 id=\"step two\">x</h2>")}</div>`+ );+ document.body.append(container);++ expect(revealFragment("#step%20two")?.textContent).toBe("x");+ expect(activeTitles(container)).toEqual(["B"]);++ expect(revealFragment("")).toBeNull();+ expect(revealFragment("#missing")).toBeNull();+ expect(revealFragment("#%E0%A4%A")).toBeNull();+ container.remove();+ });});diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.tsindex ed37d37608..f090e07a8e 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts@@ -109,6 +109,24 @@ export function revealTab(element: Element) {}}+/**+ * Returns the element that the URL fragment `hash` (`#id`, percent-encoded) names, after showing+ * every tab that encloses it, or `null` when no element has that ID.+ */+export function revealFragment(hash: string): Element | null {+ let id: string;+ try {+ id = decodeURIComponent(hash.slice(1));+ } catch {+ return null;+ }+ const target = id ? document.getElementById(id) : null;+ if (target) {+ revealTab(target);+ }+ return target;+}+/** 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;diff --git a/packages/share-theme/src/scripts/index.ts b/packages/share-theme/src/scripts/index.tsindex 2bdcfb577c..4518728423 100644--- a/packages/share-theme/src/scripts/index.ts+++ b/packages/share-theme/src/scripts/index.ts@@ -13,7 +13,7 @@ import "highlight.js/styles/default.css";import "@triliumnext/ckeditor5/src/theme/ck-content.css";import "@triliumnext/ckeditor5/src/theme/tabs.css";-import { applyTabs } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";+import { applyTabs, revealFragment } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";function $try<T extends (...a: unknown[]) => unknown>(func: T, ...args: Parameters<T>) {try {@@ -72,6 +72,11 @@ function setupTabs() {if (content) {applyTabs(content, { placeholder: content.dataset.tabTitlePlaceholder ?? "" });}++ // The browser does not scroll to a fragment inside a hidden panel.+ const showFragment = () => revealFragment(location.hash)?.scrollIntoView();+ showFragment();+ window.addEventListener("hashchange", showFragment);}function determineNoteType() {diff --git a/packages/share-theme/src/scripts/modules/toc.ts b/packages/share-theme/src/scripts/modules/toc.tsindex e83ea0bbab..eb119a861f 100644--- a/packages/share-theme/src/scripts/modules/toc.ts+++ b/packages/share-theme/src/scripts/modules/toc.ts@@ -1,3 +1,5 @@+import { revealTab } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";+import { closeMobileMenus } from "./mobile.js";/**@@ -27,6 +29,7 @@ export default function setupToC() {e.preventDefault();e.stopPropagation();+ revealTab(target);target.scrollIntoView({behavior: "smooth"});closeMobileMenus();});