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
fix(text): scroll to a linked tab
Elian Dorandb44f90a66ff
diff --git a/apps/client/src/services/block_reference.spec.ts b/apps/client/src/services/block_reference.spec.tsindex d96ac0f342..18c7b7187f 100644--- a/apps/client/src/services/block_reference.spec.ts+++ b/apps/client/src/services/block_reference.spec.ts@@ -83,6 +83,21 @@ describe("consumeBlockReference", () => {expect(container.querySelector(".trilium-tab--active > .trilium-tab-title")?.textContent).toBe("B");});+ it("scrolls to the title of a linked tab, which has no box of its own", () => {+ const container = buildContainer(+ `<div class="trilium-tabs"><section class="trilium-tab" data-trilium-block-id="t">` ++ `<p class="trilium-tab-title">A</p><div class="trilium-tab-panel"><p>a</p></div>` ++ `</section></div>`+ );+ const title = container.querySelector(".trilium-tab-title");+ expect(title).not.toBeNull();++ consumeBlockReference(container, { block: "t" });++ expect(scrollIntoView).toHaveBeenCalledTimes(1);+ expect(scrollIntoView.mock.contexts[0]).toBe(title);+ });+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>"@@ -189,6 +204,26 @@ describe("revealHighlightedBlocks", () => {expect(container.scrollTop).toBe(500);});+ it("measures a highlighted tab from the top of its title to the bottom of its panel", () => {+ const container = buildContainer(+ "<div class=\"trilium-tabs\"><section class=\"trilium-tab block-reference-highlight\">"+ + "<p class=\"trilium-tab-title\">A</p><div class=\"trilium-tab-panel\"><p>a</p></div>"+ + "</section></div>"+ );+ const title = container.querySelector(".trilium-tab-title");+ const panel = container.querySelector(".trilium-tab-panel");+ expect(title).not.toBeNull();+ expect(panel).not.toBeNull();+ Object.defineProperty(container, "clientHeight", { value: 300 });+ stubRect(container, 100, 400);+ stubRect(title as Element, 600, 630);+ stubRect(panel as Element, 630, 700);++ revealHighlightedBlocks(container);++ expect(container.scrollTop).toBe(400);+ });+it("leaves the scroll position alone without highlighted blocks", () => {const container = buildContainer("<p>A</p>");container.scrollTop = 20;diff --git a/apps/client/src/services/block_reference.ts b/apps/client/src/services/block_reference.tsindex 0632bb7c3c..e6582eaf28 100644--- a/apps/client/src/services/block_reference.ts+++ b/apps/client/src/services/block_reference.ts@@ -1,4 +1,5 @@import type { CKTextEditor } from "@triliumnext/ckeditor5";+import { CLASSES as TAB_CLASSES } from "@triliumnext/ckeditor5/src/plugins/tabs/constants.js";import {type BlockRange, formatBlockRange, parseBlockRange, resolveBlockRange, resolveBlockReference} from "@triliumnext/commons";@@ -163,7 +164,7 @@ export function consumeBlockReference(const first = start ?? end;if (first) {revealElement(first);- first.scrollIntoView({ behavior: "smooth", block: "center" });+ getBlockBoxes(first).top.scrollIntoView({ behavior: "smooth", block: "center" });flashBlocks(start && end ? getBlockRangeElements(start, end) : [ first ]);}if (!start || !end) {@@ -203,13 +204,28 @@ export function revealHighlightedBlocks(container: HTMLElement) {return;}- const top = first.getBoundingClientRect().top;- const height = last.getBoundingClientRect().bottom - top;+ const top = getBlockBoxes(first).top.getBoundingClientRect().top;+ const height = getBlockBoxes(last).bottom.getBoundingClientRect().bottom - top;const viewportTop = container.getBoundingClientRect().top + container.clientTop;const margin = Math.max(0, (container.clientHeight - height) / 2);container.scrollTop += top - viewportTop - margin;}+/**+ * Returns the elements that render the top and bottom of `block`. A tab is `display: contents`, so+ * its title and panel render it.+ */+function getBlockBoxes(block: Element): { top: Element; bottom: Element } {+ if (block.classList.contains(TAB_CLASSES.tab)) {+ const title = block.querySelector(`:scope > .${TAB_CLASSES.tabTitle}`);+ const panel = block.querySelector(`:scope > .${TAB_CLASSES.tabPanel}`);+ if (title && panel) {+ return { top: title, bottom: panel };+ }+ }+ return { top: block, bottom: block };+}+/** The outermost elements inside the range from `start` to `end`, both included. */export function getBlockRangeElements(start: Element, end: Element) {const range = start.ownerDocument.createRange();