Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): scroll shared pages at the page level

Elian Doranba561342fcf5
diff --git a/packages/share-theme/src/content/content.css b/packages/share-theme/src/content/content.css
index 2e73be9c01..7aa8616079 100644
--- a/packages/share-theme/src/content/content.css
+++ b/packages/share-theme/src/content/content.css
@@ -113,6 +113,10 @@ body:not(.math-loaded) .math-tex {
}
body.type-webView {
+ #right-pane {
+ height: 100vh;
+ }
+
#main {
padding: 0;
height: 100%;
@@ -133,6 +137,12 @@ body.type-webView {
border: 0;
}
}
+
+ @media (max-width: 48em) {
+ #right-pane {
+ height: calc(100dvh - var(--header-height));
+ }
+ }
}
/* table styles */
diff --git a/packages/share-theme/src/page/header.css b/packages/share-theme/src/page/header.css
index bffa8ed336..fa29f4240f 100644
--- a/packages/share-theme/src/page/header.css
+++ b/packages/share-theme/src/page/header.css
@@ -1,10 +1,18 @@
+html {
+ --header-height: 3.5rem;
+}
+
#header {
background: var(--background-secondary);
display: none;
justify-content: space-between;
align-items: center;
- padding: 6px 12px;
+ height: var(--header-height);
+ padding: 0 12px;
border-bottom: 1px solid var(--background-highlight);
+ position: sticky;
+ top: 0;
+ z-index: 1;
}
#header-logo,
diff --git a/packages/share-theme/src/page/layout.css b/packages/share-theme/src/page/layout.css
index 8443824326..6bf6ff20a6 100644
--- a/packages/share-theme/src/page/layout.css
+++ b/packages/share-theme/src/page/layout.css
@@ -1,20 +1,8 @@
-html,
-body {
- width: 100vw;
- height: 100vh;
- overflow: hidden;
- display: flex;
- flex-direction: column;
-}
-
#split-pane {
display: flex;
flex-direction: row;
- width: 100vw;
- flex: 1;
- overflow-x: hidden;
- overflow-y: auto;
- padding-inline: max(0px, (100% - 90rem) / 2);
+ max-width: 90rem;
+ margin-inline: auto;
}
#left-pane {
@@ -22,7 +10,7 @@ body {
flex-direction: column;
min-width: 0;
width: min(18rem, 25vw);
- height: 100%;
+ height: 100vh;
justify-content: flex-start;
position: sticky;
top: 0;
@@ -34,7 +22,6 @@ body {
flex: 1;
min-width: 0;
position: relative;
- min-height: 100%;
display: flex;
flex-direction: row;
align-items: flex-start;
@@ -67,9 +54,12 @@ body {
}
@media (max-width: 48em) {
- html,
- body {
- height: 100%;
+ html {
+ scroll-padding-top: var(--header-height);
+ }
+
+ body:is(.menu-open, .toc-open) {
+ overflow: hidden;
}
#right-pane, #main {
@@ -88,6 +78,7 @@ body {
left: 0;
width: 80%;
max-width: 300px;
+ height: auto;
background: var(--background-secondary);
transform: translateX(-100%);
transition: transform 200ms ease;
diff --git a/packages/share-theme/src/page/toc.spec.ts b/packages/share-theme/src/page/toc.spec.ts
index 112a3f23c2..96a084ecb0 100644
--- a/packages/share-theme/src/page/toc.spec.ts
+++ b/packages/share-theme/src/page/toc.spec.ts
@@ -12,16 +12,14 @@ describe("setupToC", () => {
});
it("marks the entry of the last heading scrolled past, whatever the heading levels", () => {
- const container = renderPage([ [ 1, "intro", 100 ], [ 2, "details", 500 ], [ 3, "more", 900 ] ]);
+ const scrollTo = renderPage([ [ 1, "intro", 100 ], [ 2, "details", 500 ], [ 3, "more", 900 ] ]);
expect(activeEntry()).toBe("intro");
- container.scrollTop = 520;
- container.dispatchEvent(new Event("scroll"));
+ scrollTo(520);
expect(activeEntry()).toBe("details");
- container.scrollTop = 900;
- container.dispatchEvent(new Event("scroll"));
+ scrollTo(900);
expect(activeEntry()).toBe("more");
});
@@ -43,7 +41,7 @@ describe("setupToC", () => {
expect(() => setupToC()).not.toThrow();
document.body.innerHTML = `
- <div id="split-pane">
+ <div>
<ul id="toc"><li><a href="#missing">Missing</a></li><li><a>No link</a></li></ul>
</div>
`;
@@ -143,11 +141,12 @@ describe("setupToC", () => {
/**
* Renders the content and the table of contents of a page with headings given as level, slug and
- * distance from the top, sets the table of contents up and returns the scrolling container.
+ * 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 ][]) {
document.body.innerHTML = `
- <div id="split-pane">
+ <div>
<div id="content">
${headings.map(([ level, slug ]) => `<h${level} id="${slug}">${slug}${anchor(slug)}`
+ `</h${level}>`).join("")}
@@ -157,19 +156,19 @@ function renderPage(headings: [ level: number, slug: string, offsetTop: number ]
</ul>
</div>
`;
+ let scrolled = 0;
for (const [ , slug, offsetTop ] of headings) {
const heading = document.getElementById(slug);
if (heading) {
- Object.defineProperty(heading, "offsetTop", { value: offsetTop });
+ heading.getBoundingClientRect = () => DOMRect.fromRect({ y: offsetTop - scrolled });
}
}
setupToC();
- const container = document.getElementById("split-pane");
- if (!container) {
- throw new Error("The container is missing.");
- }
- return container;
+ return (y: number) => {
+ scrolled = y;
+ window.dispatchEvent(new Event("scroll"));
+ };
}
function activeEntry() {
diff --git a/packages/share-theme/src/page/toc.ts b/packages/share-theme/src/page/toc.ts
index fb744b31f0..2414855894 100644
--- a/packages/share-theme/src/page/toc.ts
+++ b/packages/share-theme/src/page/toc.ts
@@ -16,9 +16,8 @@ import "./toc.css";
export default function setupToC() {
setupHeadingLinks();
- const container = document.getElementById("split-pane");
const toc = document.getElementById("toc");
- if (!toc || !container) return;
+ if (!toc) return;
const sections = [ ...document.querySelectorAll("#content .toc-anchor") ]
.map((anchor) => anchor.parentElement)
@@ -41,7 +40,7 @@ export default function setupToC() {
// 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 && container.scrollTop + 50 < sections[index].offsetTop) {
+ while (--index > 0 && sections[index].getBoundingClientRect().top > 50) {
// Walk back to the last section scrolled past.
}
@@ -51,7 +50,7 @@ export default function setupToC() {
};
changeLinkState();
- container.addEventListener("scroll", changeLinkState);
+ window.addEventListener("scroll", changeLinkState, { passive: true });
}
/** How long a heading link shows its check mark after copying, in milliseconds. */