Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): keep the marked ToC entry in view

Elian Doran28e4a4725e94
diff --git a/packages/share-theme/src/page/toc.spec.ts b/packages/share-theme/src/page/toc.spec.ts
index 2c07a9c7cf..c1f7e494ef 100644
--- a/packages/share-theme/src/page/toc.spec.ts
+++ b/packages/share-theme/src/page/toc.spec.ts
@@ -24,6 +24,38 @@ describe("setupToC", () => {
expect(activeEntry()).toBe("more");
});
+ it("scrolls the ToC pane to center the marked entry once it leaves the pane", () => {
+ // A pane 100px tall whose entries are 20px tall and 40px apart.
+ const headings = Array.from({ length: 10 }, (_, i): [ number, string, number ] => [ 2, `h${i}`, 100 + i * 400 ]);
+ const scrollTo = renderPage(headings, (pane) => {
+ Object.defineProperty(pane, "clientHeight", { value: 100 });
+ pane.getBoundingClientRect = () => DOMRect.fromRect({ y: 0, height: 100 });
+ for (const [ index, entry ] of [ ...pane.querySelectorAll("a") ].entries()) {
+ entry.getBoundingClientRect = () => DOMRect.fromRect({ y: index * 40 - pane.scrollTop, height: 20 });
+ }
+ });
+ const tocPane = document.getElementById("toc-pane");
+ if (!tocPane) {
+ throw new Error("The ToC pane is missing.");
+ }
+
+ // h1 and h2 are inside the pane, so it does not move.
+ scrollTo(520);
+ scrollTo(920);
+ expect(activeEntry()).toBe("h2");
+ expect(tocPane.scrollTop).toBe(0);
+
+ // h7 is at 280px, below the pane: it moves to the middle, 40px from the top.
+ scrollTo(2920);
+ expect(activeEntry()).toBe("h7");
+ expect(tocPane.scrollTop).toBe(240);
+
+ // Scrolling back up to h1, at 40px - 240px, brings the pane back with it.
+ scrollTo(520);
+ expect(activeEntry()).toBe("h1");
+ expect(tocPane.scrollTop).toBe(0);
+ });
+
it("keeps the copy in the navigation pane in step, and closes the pane on a click", () => {
const scrollTo = renderPage([ [ 2, "intro", 100 ], [ 2, "details", 500 ] ]);
const treeEntry = (selector: string) =>
@@ -186,7 +218,10 @@ describe("setupToC", () => {
* distance from the top of the page, sets the table of contents up and returns a function that
* scrolls the page.
*/
-function renderPage(headings: [ level: number, slug: string, offsetTop: number ][]) {
+function renderPage(
+ headings: [ level: number, slug: string, offsetTop: number ][],
+ prepareTocPane?: (pane: HTMLElement) => void
+) {
const link = (slug: string) => `<a href="#${encodeURIComponent(slug)}">${slug}</a>`;
document.body.innerHTML = `
<div>
@@ -194,9 +229,11 @@ function renderPage(headings: [ level: number, slug: string, offsetTop: number ]
${headings.map(([ level, slug ]) => `<h${level} id="${slug}">${slug}${anchor(slug)}`
+ `</h${level}>`).join("")}
</div>
- <ul id="toc">
- ${headings.map(([ , slug ]) => `<li>${link(slug)}</li>`).join("")}
- </ul>
+ <div id="toc-pane">
+ <ul id="toc">
+ ${headings.map(([ , slug ]) => `<li>${link(slug)}</li>`).join("")}
+ </ul>
+ </div>
<nav class="tree-toc">
${headings.map(([ , slug ]) => `<div>${link(slug)}</div>`).join("")}
</nav>
@@ -210,6 +247,11 @@ function renderPage(headings: [ level: number, slug: string, offsetTop: number ]
}
}
+ const tocPane = document.getElementById("toc-pane");
+ if (prepareTocPane && tocPane) {
+ prepareTocPane(tocPane);
+ }
+
setupToC();
return (y: number) => {
scrolled = y;
diff --git a/packages/share-theme/src/page/toc.ts b/packages/share-theme/src/page/toc.ts
index 590e8200ff..5093ad6d71 100644
--- a/packages/share-theme/src/page/toc.ts
+++ b/packages/share-theme/src/page/toc.ts
@@ -38,24 +38,46 @@ export default function setupToC() {
});
}
+ const tocPane = document.getElementById("toc-pane");
+ let activeIndex = -1;
+
// Marks the entry of the last section scrolled past, or the first entry above every section.
const changeLinkState = () => {
let index = sections.length;
while (--index > 0 && sections[index].getBoundingClientRect().top > 50) {
// Walk back to the last section scrolled past.
}
+ if (index === activeIndex) return;
+ activeIndex = index;
for (const links of linkLists) {
for (const [ linkIndex, link ] of links.entries()) {
link.classList.toggle("active", linkIndex === index);
}
}
+
+ const activeLink = tocPane?.querySelectorAll("a")[index];
+ if (tocPane && activeLink) {
+ keepInView(tocPane, activeLink);
+ }
};
changeLinkState();
window.addEventListener("scroll", changeLinkState, { passive: true });
}
+/**
+ * Scrolls `pane` to put `entry` in its middle when the entry is not entirely visible. Only the pane
+ * scrolls: `scrollIntoView()` would also move the page, which is scrolling already.
+ */
+function keepInView(pane: HTMLElement, entry: HTMLElement) {
+ const paneRect = pane.getBoundingClientRect();
+ const entryRect = entry.getBoundingClientRect();
+ if (entryRect.top >= paneRect.top && entryRect.bottom <= paneRect.bottom) return;
+
+ pane.scrollTop += entryRect.top - paneRect.top - (pane.clientHeight - entryRect.height) / 2;
+}
+
/** How long a heading link shows its check mark after copying, in milliseconds. */
const COPIED_DURATION = 1500;