Repository navigation
Expand file tree
/
Copy pathanchor-landing.html
More file actions
114 lines (108 loc) · 5.18 KB
/
Copy pathanchor-landing.html
File metadata and controls
114 lines (108 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<script>
// Typesetting equations and loading images change the height of everything
// above the reader and push the text they were reading out of view. Keep the
// reader's place: until they scroll, that is the link target (#id or a text
// fragment, #:~:text=...); once they scroll, it is the block of text at about
// a third of the way down the window. Each time the page height changes, put
// that place back where it was in the window, until the height has held still
// for 1.5 s after MathJax finishes (or 30 s have passed). The CSS
// scroll-margin-top in styles.css places a link target in the upper-middle.
// A text fragment is not in location.hash, so it is read from the navigation
// entry (a browser that strips it leaves only the id behaviour).
// Following another in-page link ends this: the browser's jump is the new place.
(function () {
var hash = "", text = null;
try { hash = decodeURIComponent(location.hash.slice(1).replace(/:~:.*$/, "")); } catch (e) {}
try {
var nav = performance.getEntriesByType("navigation")[0];
var m = nav && /#.*?:~:.*?\btext=([^&]*)/.exec(nav.name);
// Split on the raw commas before decoding: an encoded comma (%2C) is text.
// The form is [prefix-,]start[,end][,-suffix]: drop a prefix or suffix
// only when there are other parts, so a lone term may start or end in "-".
var parts = m ? m[1].split(",") : [];
if (parts.length > 1 && /-$/.test(parts[0])) parts.shift();
if (parts.length > 1 && /^-/.test(parts[parts.length - 1])) parts.pop();
if (parts[0]) text = decodeURIComponent(parts[0]);
} catch (e) {}
function findText() {
var needle = text.replace(/\s+/g, " ").toLowerCase();
var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
acceptNode: function (n) {
var p = n.parentElement;
return p && !p.closest("script, style, mjx-container, nav")
? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
}
});
for (var n = walker.nextNode(); n; n = walker.nextNode()) {
if (n.data.replace(/\s+/g, " ").toLowerCase().indexOf(needle) >= 0) return n.parentElement;
}
return null;
}
function target() {
var el = hash && document.getElementById(hash);
return el || (text && findText());
}
// The reader's place: null means the link target; otherwise a block of text
// and where its top sat in the window when the reader last scrolled.
var place = null, expectedY = null, engaged = false, queued = false, stopped = false;
var BLOCK = "p, li, h1, h2, h3, h4, h5, h6, figcaption, td, th, pre, blockquote";
function land() {
if (place) {
// A block the reader has since collapsed or removed has no position to
// keep: leave the page alone until their next scroll picks a new one.
if (!place.el.isConnected || !place.el.getClientRects().length) return;
var d = place.el.getBoundingClientRect().top - place.top;
if (Math.abs(d) >= 1) window.scrollBy({ top: d, behavior: "instant" });
} else {
var el = target();
if (el) el.scrollIntoView({ block: text && !hash ? "center" : "start", behavior: "instant" });
}
expectedY = window.scrollY;
}
function track() {
queued = false;
if (stopped || Math.abs(window.scrollY - expectedY) < 1) return; // our own landing
expectedY = window.scrollY;
var line = window.innerHeight / 3;
var at = document.elementFromPoint(window.innerWidth / 2, line);
var el = at && at.closest(BLOCK);
if (el && el.closest("mjx-container, nav")) el = null;
if (!el) { // the point fell in a margin: take the first block below the line
var all = document.querySelectorAll(BLOCK);
for (var i = 0; i < all.length && !el; i++) {
if (all[i].getBoundingClientRect().bottom > line && !all[i].closest("mjx-container, nav")) el = all[i];
}
}
if (el) place = { el: el, top: el.getBoundingClientRect().top };
}
// Only the reader's own input makes a scroll theirs: the browser's jump to
// #id on load is not.
function engage() { engaged = true; }
["wheel", "touchstart", "pointerdown"].forEach(function (ev) {
window.addEventListener(ev, engage, { passive: true });
});
window.addEventListener("keydown", function (e) {
if (/^(Arrow|Page|Home$|End$| $)/.test(e.key)) engage();
}, { passive: true });
window.addEventListener("scroll", function () {
if (engaged && !queued) { queued = true; requestAnimationFrame(track); }
}, { passive: true });
var ro, timer, ready = false;
function finish() { stopped = true; if (ro) ro.disconnect(); }
function quiet() { clearTimeout(timer); if (ready) timer = setTimeout(finish, 1500); }
window.addEventListener("hashchange", finish, { once: true });
setTimeout(finish, 30000);
if (window.ResizeObserver) {
ro = new ResizeObserver(function () { if (!stopped) { land(); quiet(); } });
ro.observe(document.body);
}
function afterMath() {
var mj = window.MathJax;
var p = mj && mj.startup && mj.startup.promise;
function done() { ready = true; if (!stopped) { land(); quiet(); } }
(p || Promise.resolve()).then(done, done);
}
if (document.readyState === "complete") afterMath();
else window.addEventListener("load", afterMath);
})();
</script>