Repository navigation
feat(site): tree and "On this page" behaviour, theme switch #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Full text on hover for tree and "On this page" items that one line cuts short ("…"). | ||
| // Checked when the pointer enters an item, so it stays right as sections open or the window | ||
| // resizes; items that fit get no tooltip. | ||
| (function () { | ||
| document.addEventListener("mouseover", function (e) { | ||
| var el = e.target && e.target.closest ? e.target.closest(".md-nav .md-ellipsis") : null; | ||
| if (!el) return; | ||
| if (el.scrollWidth > el.clientWidth) { | ||
| el.title = el.textContent.replace(/\s+/g, " ").trim(); | ||
| } else { | ||
| el.removeAttribute("title"); | ||
| } | ||
| }); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // The main site's theme switch, driving Zensical's color scheme as well. | ||
| (function () { | ||
| function sync() { | ||
| var toggle = document.getElementById("theme-switch"); | ||
| if (toggle) toggle.checked = document.documentElement.classList.contains("light-mode"); | ||
| } | ||
|
|
||
| document.addEventListener("change", function (e) { | ||
| if (!e.target || e.target.id !== "theme-switch") return; | ||
| var light = e.target.checked; | ||
| document.documentElement.classList.toggle("light-mode", light); | ||
| document.body.setAttribute("data-md-color-scheme", light ? "default" : "slate"); | ||
| try { localStorage.setItem("theme", light ? "light" : "dark"); } catch (err) {} | ||
| }); | ||
|
|
||
| sync(); | ||
| document.addEventListener("DOMContentLoaded", sync); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // "On this page" marks the section the reader is at: the last one whose heading is at or above the | ||
| // line where a jump to a heading puts it (the theme's scroll margin, 3.6rem below the window top). | ||
| // | ||
| // The theme marks a heading as reached at the header's height plus 0.9 times the gap above the | ||
| // main grid. That gap is zero here: it also sets when the sidebars stick, and a gap makes them | ||
| // slide as the page scrolls. So a heading reached by a click landed below the theme's line, and | ||
| // the entry before it stayed marked. This sets the marks from the jump's line instead; the theme | ||
| // keeps setting its own, so they are set again whenever it changes them (a mutation callback | ||
| // runs before the browser paints). | ||
| // | ||
| // The last sections of a page cannot scroll up to the line. A clicked entry stays marked until | ||
| // the reader scrolls (wheel, touch or keys), so clicking one of them marks it, not the last one. | ||
| (function () { | ||
| var NAV = ".md-sidebar--secondary .md-nav--secondary"; | ||
| var ACTIVE = "md-nav__link--active"; | ||
| var PASSED = "md-nav__link--passed"; | ||
| var SLACK_PX = 4; | ||
| var nav = null; | ||
| var entries = []; | ||
| var clicked = null; // the clicked entry's index, until the reader scrolls | ||
| var watcher = new MutationObserver(apply); | ||
|
|
||
| function line() { | ||
| var rem = parseFloat(getComputedStyle(document.documentElement).fontSize); | ||
| return 3.6 * rem + SLACK_PX; | ||
| } | ||
|
|
||
| function current() { | ||
| if (clicked !== null) return clicked; | ||
| var root = document.documentElement; | ||
| if (window.scrollY > 0 && window.innerHeight + window.scrollY >= root.scrollHeight - 1) { | ||
| return entries.length - 1; | ||
| } | ||
| var at = line(); | ||
| var index = -1; | ||
| entries.forEach(function (entry, i) { | ||
| if (entry.heading.getBoundingClientRect().top <= at) index = i; | ||
| }); | ||
| return index; | ||
| } | ||
|
|
||
| function apply() { | ||
| if (!entries.length) return; | ||
| var index = current(); | ||
| entries.forEach(function (entry, i) { | ||
| var link = entry.link; | ||
| if (link.classList.contains(ACTIVE) !== (i === index)) link.classList.toggle(ACTIVE, i === index); | ||
| if (link.classList.contains(PASSED) !== (i <= index)) link.classList.toggle(PASSED, i <= index); | ||
| }); | ||
| } | ||
|
|
||
| // A new page's "On this page": its entries and their headings. | ||
| function attach() { | ||
| var found = document.querySelector(NAV); | ||
| if (found === nav) return; | ||
| nav = found; | ||
| clicked = null; | ||
| watcher.disconnect(); | ||
| entries = []; | ||
| if (!nav) return; | ||
| nav.querySelectorAll("a.md-nav__link[href*='#']").forEach(function (link) { | ||
| var id; | ||
| try { | ||
| id = decodeURIComponent(link.hash.slice(1)); | ||
| } catch (e) { | ||
| return; // a malformed %-escape: skip the entry, keep the others | ||
| } | ||
| var heading = id && document.getElementById(id); | ||
| if (heading) entries.push({ link: link, heading: heading }); | ||
| }); | ||
| watcher.observe(nav, { attributes: true, attributeFilter: ["class"], subtree: true }); | ||
| apply(); | ||
| } | ||
|
|
||
| document.addEventListener("click", function (e) { | ||
| var link = e.target && e.target.closest ? e.target.closest(NAV + " a.md-nav__link") : null; | ||
| if (!link) return; | ||
| var index = entries.findIndex(function (entry) { return entry.link === link; }); | ||
| clicked = index >= 0 ? index : null; | ||
| apply(); | ||
| }, true); | ||
|
|
||
| function release() { | ||
| if (clicked === null) return; | ||
| clicked = null; | ||
| apply(); | ||
| } | ||
|
|
||
| ["wheel", "touchstart", "keydown"].forEach(function (type) { | ||
| window.addEventListener(type, release, { passive: true }); | ||
| }); | ||
| window.addEventListener("scroll", apply, { passive: true }); | ||
| window.addEventListener("resize", apply); | ||
| attach(); | ||
| new MutationObserver(attach).observe(document.documentElement, { childList: true, subtree: true }); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Tree sections stay open until the reader closes them. Each page comes with its own tree, in | ||
| // which only the path to that page is open, so opening one section and then moving to another | ||
| // page closed every other section. Remember the open sections for the tab, by their toggle's id | ||
| // (the same on every page, since every page has the same tree), and reopen them in each new | ||
| // page's tree. | ||
| // | ||
| // A section opens or closes on its own page, by its toggle. The sections a page opens because it | ||
| // lies inside them count as opened too. A click on a section's row that opens its page and | ||
| // flips it (tree-toggle.js) asks for that state with window.lwTreeOpen(id, open, href): the | ||
| // tree of the page at `href` gets it, even where that page would open the section itself (it is | ||
| // the section's own page), however long the page takes to arrive. | ||
| // | ||
| // The new tree is reopened in a mutation callback, before the browser paints it. The theme may | ||
| // already have computed its styles by then (with those sections closed), so reopening would play | ||
| // the open animation: the reopened sections get .lw-tree-restore, which turns transitions off, | ||
| // until two frames later. This must run before tree-scroll.js, which measures the new tree to keep | ||
| // the clicked item in place: load it first. | ||
| (function () { | ||
| var KEY = "lw-tree-open"; | ||
| var TOGGLE = ".md-nav--primary .md-nav__toggle[id]"; | ||
| var open = load(); | ||
| var tree = null; | ||
| var request = null; // {id, open, path}: the state the tree of page `path` gives section `id` | ||
|
|
||
| function load() { | ||
| try { | ||
| return new Set(JSON.parse(sessionStorage.getItem(KEY) || "[]")); | ||
| } catch (e) { | ||
| return new Set(); | ||
| } | ||
| } | ||
|
|
||
| function save() { | ||
| try { | ||
| sessionStorage.setItem(KEY, JSON.stringify(Array.from(open))); | ||
| } catch (e) { | ||
| // Without storage the sections stay open while the tab does not reload. | ||
| } | ||
| } | ||
|
|
||
| // Give a new tree the remembered sections and the requested state; remember the sections it | ||
| // opened itself. | ||
| function sync(nav) { | ||
| // A request is for the page its click opens. Any other page's tree drops it: another click | ||
| // came first, or the click opened no page. | ||
| var asked = request && request.path === location.pathname ? request : null; | ||
| request = null; | ||
| var changed = []; | ||
| nav.querySelectorAll(".md-nav__toggle[id]").forEach(function (toggle) { | ||
| var want = asked && toggle.id === asked.id ? asked.open : open.has(toggle.id) || toggle.checked; | ||
| if (toggle.checked !== want) { | ||
| toggle.parentElement.classList.add("lw-tree-restore"); | ||
| changed.push(toggle.parentElement); | ||
| toggle.checked = want; | ||
| } | ||
| if (want) open.add(toggle.id); | ||
| }); | ||
| save(); | ||
| if (changed.length) { | ||
| requestAnimationFrame(function () { | ||
| requestAnimationFrame(function () { | ||
| changed.forEach(function (item) { item.classList.remove("lw-tree-restore"); }); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| window.lwTreeOpen = function (id, isOpen, href) { | ||
| if (isOpen) open.add(id); | ||
| else open.delete(id); | ||
| save(); | ||
| request = { id: id, open: isOpen, path: new URL(href, location.href).pathname }; | ||
| }; | ||
|
|
||
| function check() { | ||
| var nav = document.querySelector(".md-nav--primary"); | ||
| if (nav && nav !== tree) { | ||
| tree = nav; | ||
| sync(nav); | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener("change", function (e) { | ||
| var toggle = e.target; | ||
| if (!toggle.matches || !toggle.matches(TOGGLE)) return; | ||
| if (toggle.checked) open.add(toggle.id); | ||
| else open.delete(toggle.id); | ||
| save(); | ||
| }); | ||
|
|
||
| check(); | ||
| new MutationObserver(check).observe(document.documentElement, { childList: true, subtree: true }); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // The page read before this one is marked in the tree (.lw-tree-prev, a lighter blue than the | ||
| // current page), so after a quick look elsewhere the way back is in sight. Going back marks the | ||
| // page just left: two pages can be compared by going back and forth. | ||
| // | ||
| // Pages are told apart by path; an anchor within the page is the same page, and a reload keeps | ||
| // the mark. Kept for the tab. Marked in a mutation callback, before the new tree is painted. | ||
| (function () { | ||
| var KEY = "lw-tree-trail"; | ||
| var MARK = "lw-tree-prev"; | ||
| var tree = null; | ||
|
|
||
| function load() { | ||
| try { | ||
| return JSON.parse(sessionStorage.getItem(KEY) || "{}"); | ||
| } catch (e) { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| function save(trail) { | ||
| try { | ||
| sessionStorage.setItem(KEY, JSON.stringify(trail)); | ||
| } catch (e) { | ||
| // Without storage the mark stays until the tab reloads. | ||
| } | ||
| } | ||
|
|
||
| var trail = load(); // {current, previous}: page paths | ||
|
|
||
| function mark(nav) { | ||
| var path = location.pathname; | ||
| if (trail.current !== path) { | ||
| trail = { current: path, previous: trail.current || null }; | ||
| save(trail); | ||
| } | ||
| nav.querySelectorAll("a.md-nav__link[href]").forEach(function (link) { | ||
| var prev = trail.previous !== null && link.pathname === trail.previous && !link.hash; | ||
| if (link.classList.contains(MARK) !== prev) link.classList.toggle(MARK, prev); | ||
| }); | ||
| } | ||
|
|
||
| function check() { | ||
| var nav = document.querySelector(".md-nav--primary"); | ||
| if (nav && nav !== tree) { | ||
| tree = nav; | ||
| mark(nav); | ||
| } | ||
| } | ||
|
|
||
| check(); | ||
| new MutationObserver(check).observe(document.documentElement, { childList: true, subtree: true }); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Keep the tree still when the reader clicks an item in it: the clicked item stays exactly where | ||
| // it was on screen. | ||
| // | ||
| // On a page change (instant navigation), the new page's tree comes in scrolled to the top, and | ||
| // the theme then scrolls it so the active item sits in the middle of the pane. Both positions get | ||
| // painted, so the tree flickers or jumps. Instead, on the click, remember how far below the | ||
| // pane's top the item is; when the new tree is inserted (a mutation callback runs before the | ||
| // browser paints) scroll it so its active item (the clicked one) is at that distance again, and | ||
| // do the same in place of the theme's centering. Sections may have opened or closed above the | ||
| // item, so it is the item's place that is kept, not the scroll offset. | ||
| // | ||
| // The new pane only becomes a scroll container when the theme sizes it, a frame after it is | ||
| // inserted; it gets the old pane's height (same layout) at once, so the position takes before | ||
| // first paint. | ||
| (function () { | ||
| var PANE = ".md-sidebar--primary .md-sidebar__scrollwrap"; | ||
| var HOLD_MS = 1500; | ||
| var hold = null; // {href, offset, height} while a click's page change is in progress | ||
| var timer = null; | ||
|
|
||
| function isPane(el) { | ||
| return hold !== null && el && el.matches && el.matches(PANE); | ||
| } | ||
|
|
||
| // The pane's active item, once it is the clicked one (not the page being left). | ||
| function clicked(pane) { | ||
| var active = pane.querySelector(".md-nav__link--active[href]"); | ||
| return active && active.href === hold.href ? active : null; | ||
| } | ||
|
|
||
| function release() { | ||
| hold = null; | ||
| if (timer) clearTimeout(timer); | ||
| timer = null; | ||
| } | ||
|
|
||
| // Scroll `pane` so the clicked item sits `hold.offset` below the pane's top again. | ||
| function place(pane, item) { | ||
| var delta = item.getBoundingClientRect().top - pane.getBoundingClientRect().top - hold.offset; | ||
| if (Math.abs(delta) >= 1) pane.scrollTop += delta; | ||
| } | ||
|
|
||
| document.addEventListener("click", function (e) { | ||
| var pane = document.querySelector(PANE); | ||
| var link = e.target && e.target.closest ? e.target.closest("a[href]") : null; | ||
| if (!pane || !link || !pane.contains(link)) return; | ||
| hold = { | ||
| href: link.href, | ||
| offset: link.getBoundingClientRect().top - pane.getBoundingClientRect().top, | ||
| height: pane.style.height, | ||
| }; | ||
| // A click that does not navigate, or a slow page, must not hold forever. | ||
| if (timer) clearTimeout(timer); | ||
| timer = setTimeout(release, HOLD_MS); | ||
| }, true); | ||
|
|
||
| // The new page's tree: size it as the old one was and place it, before first paint. | ||
| new MutationObserver(function () { | ||
| var pane = document.querySelector(PANE); | ||
| var item = isPane(pane) ? clicked(pane) : null; | ||
| if (!item) return; | ||
| if (!pane.style.height && hold.height) pane.style.height = hold.height; | ||
| place(pane, item); | ||
| }).observe(document.documentElement, { childList: true, subtree: true }); | ||
|
|
||
| // The theme's centering of the active item: place it instead, and the page change is done. | ||
| var nativeScrollTo = Element.prototype.scrollTo; | ||
| Element.prototype.scrollTo = function () { | ||
| var item = isPane(this) ? clicked(this) : null; | ||
| if (item) { | ||
| place(this, item); | ||
| release(); | ||
| return; | ||
| } | ||
| return nativeScrollTo.apply(this, arguments); | ||
| }; | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // A click on a tree section's row, its title or its arrow, always opens the section's page (every | ||
| // section has one) and flips the section: open becomes closed, closed becomes open. | ||
| // - its page already shown: flip it in place. | ||
| // - another page shown: ask tree-open.js to give the next page's tree the flipped state, and | ||
| // let the title's link open the page. A click on the arrow follows the title's link too, | ||
| // instead of only flipping the section as the theme's arrow does. | ||
| (function () { | ||
| var ROW = ".md-nav--primary .md-nav__container"; | ||
|
|
||
| document.addEventListener("click", function (e) { | ||
| // Clicks with a modifier key (new tab or window) or another button stay the browser's | ||
| if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return; | ||
| var row = e.target && e.target.closest ? e.target.closest(ROW) : null; | ||
| if (!row) return; | ||
| var link = row.querySelector(":scope > a.md-nav__link[href]"); | ||
| var toggle = row.parentElement.querySelector(":scope > .md-nav__toggle"); | ||
| if (!link || !toggle) return; | ||
| var isOpen = !toggle.checked; | ||
| if (link.href.split("#")[0] === location.href.split("#")[0]) { | ||
| // Before the theme's instant navigation or the arrow's label sees the click. | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| toggle.checked = isOpen; | ||
| toggle.dispatchEvent(new Event("change", { bubbles: true })); | ||
| return; | ||
| } | ||
| if (window.lwTreeOpen) window.lwTreeOpen(toggle.id, isOpen, link.href); | ||
| if (e.target.closest("a") !== link) { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| link.click(); | ||
| } | ||
| }, true); | ||
| })(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.