Skip to content

Commit f276a2e

Browse files
committed
Table of contents update
1 parent fe2cd54 commit f276a2e

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

src/routes/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
{@render children()}
6666
</article>
6767
<footer>
68-
2025
68+
2025-2026
6969

7070
<a
7171
class="link"

src/routes/posts/+layout.svelte

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,51 @@
88
function recalcActiveHeader(): void {
99
const headers = Array.from(
1010
document.querySelectorAll<HTMLElement>(
11-
['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].map(it => `.article__layout > ${it}`).join(', ')
11+
[/* 'h1', */'h2', 'h3', 'h4', 'h5', 'h6'].map(it => `.article__layout > ${it}`).join(', ')
1212
)
1313
);
1414
const navHeaders = Array.from(
1515
document.querySelectorAll<HTMLElement>('.toc .toc-link')
1616
);
1717
const scrollTop = window.scrollY;
18-
const offsets = headers.map(it =>
19-
it.getBoundingClientRect().top -
18+
const scrollHeight = window.innerHeight;
19+
const offsets = headers.map(it => {
20+
const bbox = it.getBoundingClientRect();
21+
return bbox.top -
2022
(parseFloat(getComputedStyle(it).marginTop) || 0) +
21-
window.scrollY
22-
);
23-
let activeIndex = 0;
24-
for (let i = 1; i < offsets.length; ++i) {
25-
if (scrollTop >= offsets[i] - 40 - 2) {
26-
activeIndex = i;
23+
window.scrollY;
24+
});
25+
const heights = offsets.map((it, i) => {
26+
if (i === offsets.length - 1) {
27+
return document.body.scrollHeight - it;
28+
}
29+
return offsets[i + 1] - it;
30+
});
31+
let firstActiveIndex = -1;
32+
let lastActiveIndex = -1;
33+
for (let i = 0; i < offsets.length; ++i) {
34+
const intersection = Math.min(scrollTop + scrollHeight, offsets[i] + heights[i]) -
35+
Math.max(scrollTop, offsets[i]);
36+
console.log(i, intersection, offsets[i], scrollTop, scrollHeight)
37+
if (intersection > 0) {
38+
if (firstActiveIndex < 0) {
39+
firstActiveIndex = i;
40+
}
41+
lastActiveIndex = i;
2742
}
2843
}
29-
const oldItem = document.querySelector<HTMLElement>('.toc-link_active');
30-
const newItem = navHeaders[activeIndex];
31-
if (newItem && newItem !== oldItem) {
32-
oldItem?.classList.remove('toc-link_active');
33-
newItem.classList.add('toc-link_active');
44+
console.log({firstActiveIndex, lastActiveIndex})
45+
firstActiveIndex = Math.max(firstActiveIndex, 0);
46+
lastActiveIndex = Math.max(lastActiveIndex, 0);
47+
const oldItems = new Set(document.querySelectorAll<HTMLElement>('.toc-link_active'));
48+
const newItems = new Set(navHeaders.slice(firstActiveIndex, lastActiveIndex + 1));
49+
if (!(oldItems.isSubsetOf(newItems) && newItems.isSubsetOf(oldItems)) ) {
50+
for (const it of oldItems) {
51+
it.classList.remove('toc-link_active');
52+
}
53+
for (const it of newItems) {
54+
it.classList.add('toc-link_active');
55+
}
3456
}
3557
}
3658

src/routes/posts/media-fragments/Video.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
preload="auto"
55
muted
66
playsinline
7-
webkitplaysinline
7+
{...{webkitplaysinline: ''}}
88
></video>
99

1010
<style>

0 commit comments

Comments
 (0)