Skip to content

Commit 816913b

Browse files
authored
Merge pull request #2892 from ehuss/heading-nav-debug-update
Improve the heading nav debug
2 parents 4a07076 + 1620858 commit 816913b

File tree

1 file changed

+80
-38
lines changed

1 file changed

+80
-38
lines changed

crates/mdbook-html/front-end/templates/toc.js.hbs

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox)
7878
// ---------------------------------------------------------------------------
7979
// Support for dynamically adding headers to the sidebar.
8080
81-
// This is a debugging tool for the threshold which you can enable in the console.
82-
// eslint-disable-next-line prefer-const
83-
let mdbookThresholdDebug = false;
84-
8581
(function() {
8682
// This is used to detect which direction the page has scrolled since the
8783
// last scroll event.
@@ -108,47 +104,27 @@ let mdbookThresholdDebug = false;
108104
// I'm not sure why eslint seems to have a false positive here.
109105
// eslint-disable-next-line prefer-const
110106
let headerToggles = [];
111-
112-
function drawDebugLine() {
113-
if (!document.body) {
114-
return;
115-
}
116-
const id = 'mdbook-threshold-debug-line';
117-
const existingLine = document.getElementById(id);
118-
if (existingLine) {
119-
existingLine.remove();
120-
}
121-
const line = document.createElement('div');
122-
line.id = id;
123-
line.style.cssText = `
124-
position: fixed;
125-
top: ${threshold}px;
126-
left: 0;
127-
width: 100vw;
128-
height: 2px;
129-
background-color: red;
130-
z-index: 9999;
131-
pointer-events: none;
132-
`;
133-
document.body.appendChild(line);
134-
}
107+
// This is a debugging tool for the threshold which you can enable in the console.
108+
let thresholdDebug = false;
135109
136110
// Updates the threshold based on the scroll position.
137111
function updateThreshold() {
138112
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
139113
const windowHeight = window.innerHeight;
140114
const documentHeight = document.documentElement.scrollHeight;
115+
141116
// The number of pixels below the viewport, at most documentHeight.
142117
// This is used to push the threshold down to the bottom of the page
143118
// as the user scrolls towards the bottom.
144119
const pixelsBelow = Math.max(0, documentHeight - (scrollTop + windowHeight));
145-
// The number of pixels above the viewport, at most defaultDownThreshold.
120+
// The number of pixels above the viewport, at least defaultDownThreshold.
146121
// Similar to pixelsBelow, this is used to push the threshold back towards
147122
// the top when reaching the top of the page.
148123
const pixelsAbove = Math.max(0, defaultDownThreshold - scrollTop);
149124
// How much the threshold should be offset once it gets close to the
150125
// bottom of the page.
151-
let bottomAdd = Math.max(0, windowHeight - pixelsBelow - defaultDownThreshold);
126+
const bottomAdd = Math.max(0, windowHeight - pixelsBelow - defaultDownThreshold);
127+
let adjustedBottomAdd = bottomAdd;
152128
153129
// Adjusts bottomAdd for a small document. The calculation above
154130
// assumes the document is at least twice the windowheight in size. If
@@ -158,7 +134,7 @@ let mdbookThresholdDebug = false;
158134
const maxPixelsBelow = documentHeight - windowHeight;
159135
const t = 1 - pixelsBelow / maxPixelsBelow;
160136
const clamp = Math.max(0, Math.min(1, t));
161-
bottomAdd *= clamp;
137+
adjustedBottomAdd *= clamp;
162138
}
163139
164140
let scrollingDown = true;
@@ -169,10 +145,10 @@ let mdbookThresholdDebug = false;
169145
if (scrollingDown) {
170146
// When scrolling down, move the threshold up towards the default
171147
// downwards threshold position. If near the bottom of the page,
172-
// bottomAdd will offset the threshold towards the bottom of the
173-
// page.
148+
// adjustedBottomAdd will offset the threshold towards the bottom
149+
// of the page.
174150
const amountScrolledDown = scrollTop - lastKnownScrollPosition;
175-
const adjustedDefault = defaultDownThreshold + bottomAdd;
151+
const adjustedDefault = defaultDownThreshold + adjustedBottomAdd;
176152
threshold = Math.max(adjustedDefault, threshold - amountScrolledDown);
177153
} else {
178154
// When scrolling up, move the threshold down towards the default
@@ -181,12 +157,81 @@ let mdbookThresholdDebug = false;
181157
// belongs.
182158
const amountScrolledUp = lastKnownScrollPosition - scrollTop;
183159
const adjustedDefault = defaultUpThreshold - pixelsAbove
184-
+ Math.max(0, bottomAdd - defaultDownThreshold);
160+
+ Math.max(0, adjustedBottomAdd - defaultDownThreshold);
185161
threshold = Math.min(adjustedDefault, threshold + amountScrolledUp);
186162
}
163+
164+
if (documentHeight <= windowHeight) {
165+
threshold = 0;
166+
}
167+
168+
if (thresholdDebug) {
169+
const id = 'mdbook-threshold-debug-data';
170+
let data = document.getElementById(id);
171+
if (data === null) {
172+
data = document.createElement('div');
173+
data.id = id;
174+
data.style.cssText = `
175+
position: fixed;
176+
top: 50px;
177+
right: 10px;
178+
background-color: 0xeeeeee;
179+
z-index: 9999;
180+
pointer-events: none;
181+
`;
182+
document.body.appendChild(data);
183+
}
184+
data.innerHTML = `
185+
<table>
186+
<tr><td>documentHeight</td><td>${documentHeight.toFixed(1)}</td></tr>
187+
<tr><td>windowHeight</td><td>${windowHeight.toFixed(1)}</td></tr>
188+
<tr><td>scrollTop</td><td>${scrollTop.toFixed(1)}</td></tr>
189+
<tr><td>pixelsAbove</td><td>${pixelsAbove.toFixed(1)}</td></tr>
190+
<tr><td>pixelsBelow</td><td>${pixelsBelow.toFixed(1)}</td></tr>
191+
<tr><td>bottomAdd</td><td>${bottomAdd.toFixed(1)}</td></tr>
192+
<tr><td>adjustedBottomAdd</td><td>${adjustedBottomAdd.toFixed(1)}</td></tr>
193+
<tr><td>scrollingDown</td><td>${scrollingDown}</td></tr>
194+
<tr><td>threshold</td><td>${threshold.toFixed(1)}</td></tr>
195+
</table>
196+
`;
197+
drawDebugLine();
198+
}
199+
187200
lastKnownScrollPosition = scrollTop;
188201
}
189202
203+
function drawDebugLine() {
204+
if (!document.body) {
205+
return;
206+
}
207+
const id = 'mdbook-threshold-debug-line';
208+
const existingLine = document.getElementById(id);
209+
if (existingLine) {
210+
existingLine.remove();
211+
}
212+
const line = document.createElement('div');
213+
line.id = id;
214+
line.style.cssText = `
215+
position: fixed;
216+
top: ${threshold}px;
217+
left: 0;
218+
width: 100vw;
219+
height: 2px;
220+
background-color: red;
221+
z-index: 9999;
222+
pointer-events: none;
223+
`;
224+
document.body.appendChild(line);
225+
}
226+
227+
function mdbookEnableThresholdDebug() {
228+
thresholdDebug = true;
229+
updateThreshold();
230+
drawDebugLine();
231+
}
232+
233+
window.mdbookEnableThresholdDebug = mdbookEnableThresholdDebug;
234+
190235
// Updates which headers in the sidebar should be expanded. If the current
191236
// header is inside a collapsed group, then it, and all its parents should
192237
// be expanded.
@@ -210,9 +255,6 @@ let mdbookThresholdDebug = false;
210255
// This is done with a virtual Y threshold, where headers at or below
211256
// that line will be considered the current one.
212257
function updateCurrentHeader() {
213-
if (mdbookThresholdDebug) {
214-
drawDebugLine();
215-
}
216258
if (!headers || !headers.length) {
217259
return;
218260
}

0 commit comments

Comments
 (0)