feat: support responsive TOC
This commit is contained in:
+113
-30
@@ -1,21 +1,48 @@
|
||||
import tocbot from "tocbot";
|
||||
|
||||
async function generateToc() {
|
||||
const toc = document.querySelector(".toc");
|
||||
let toc_icon = "☰"
|
||||
let toc_close_icon = "✕"
|
||||
|
||||
function toggleToc() {
|
||||
const toc = document.getElementById("toc");
|
||||
if (!toc) return;
|
||||
const toc_toggle = document.getElementById("toc-toggle");
|
||||
|
||||
if (!toc_toggle) return;
|
||||
|
||||
const isVisible = toc.classList.contains('toc-visible');
|
||||
|
||||
if (isVisible) {
|
||||
toc.classList.remove('toc-visible');
|
||||
toc.classList.remove('toc-overflow-right');
|
||||
toc_toggle.innerHTML = toc_icon;
|
||||
toc_toggle.setAttribute('aria-label', 'Show Table of Contents');
|
||||
|
||||
} else {
|
||||
toc.classList.add('toc-visible');
|
||||
toc_toggle.innerHTML = toc_close_icon;
|
||||
toc_toggle.setAttribute('aria-label', 'Hide Table of Contents');
|
||||
}
|
||||
void locateToc();
|
||||
}
|
||||
|
||||
async function generateToc() {
|
||||
const toc = document.getElementById("toc");
|
||||
if (!toc) return;
|
||||
|
||||
const content = document.getElementById("content");
|
||||
const titles = content.querySelectorAll("h1, h2, h3, h4");
|
||||
|
||||
if (!titles || titles.length === 0) {
|
||||
toc.style.display = "none";
|
||||
toc.remove();
|
||||
return;
|
||||
} else {
|
||||
toc.style.display = "block";
|
||||
}
|
||||
|
||||
if (typeof tocbot !== 'undefined') {
|
||||
tocbot.destroy();
|
||||
tocbot.init({
|
||||
tocSelector: ".toc",
|
||||
tocSelector: ".toc-content",
|
||||
contentSelector: "#content",
|
||||
headingSelector: "h1, h2, h3, h4",
|
||||
extraListClasses: "toc-list",
|
||||
@@ -25,21 +52,94 @@ async function generateToc() {
|
||||
scrollSmooth: true,
|
||||
});
|
||||
}
|
||||
|
||||
setupTocVisibility();
|
||||
setupToggleButton();
|
||||
void locateToc();
|
||||
}
|
||||
|
||||
async function locateToc() {
|
||||
const toc = document.querySelector(".toc")
|
||||
function setupTocVisibility() {
|
||||
const toc = document.querySelector(".toc");
|
||||
if (!toc) return;
|
||||
const content = document.getElementById("content");
|
||||
const contentRight = content.getBoundingClientRect().right;
|
||||
if (toc && contentRight) {
|
||||
toc.style.left = `${contentRight}px`;
|
||||
|
||||
toc.style.display = "block";
|
||||
|
||||
if (window.innerWidth <= 1400) {
|
||||
toc.classList.remove('toc-visible');
|
||||
} else {
|
||||
toc.classList.add('toc-visible');
|
||||
}
|
||||
}
|
||||
|
||||
function setupToggleButton() {
|
||||
const toc = document.getElementById("toc");
|
||||
const toc_toggle = document.getElementById("toc-toggle");
|
||||
|
||||
const isVisible = toc.classList.contains('toc-visible');
|
||||
toc_toggle.innerHTML = isVisible ? toc_close_icon : toc_icon;
|
||||
toc_toggle.setAttribute('aria-label', isVisible ? 'Hide Table of Contents' : 'Show Table of Contents');
|
||||
|
||||
toc_toggle.addEventListener('click', toggleToc);
|
||||
}
|
||||
|
||||
async function locateToc() {
|
||||
const toc = document.getElementById("toc");
|
||||
if (!toc) return;
|
||||
const toc_toggle = document.getElementById("toc-toggle");
|
||||
|
||||
const content = document.getElementById("content");
|
||||
const contentRect = content.getBoundingClientRect();
|
||||
const contentRight = contentRect.right;
|
||||
|
||||
if (contentRight) {
|
||||
let leftPosition = contentRight + 12;
|
||||
|
||||
const screenWidth = window.innerWidth;
|
||||
const buttonWidth = toc_toggle.offsetWidth;
|
||||
const tocWidth = toc.classList.contains('toc-visible') ? toc.offsetWidth : buttonWidth;
|
||||
|
||||
const maxLeft = screenWidth - tocWidth - 30;
|
||||
leftPosition = Math.min(leftPosition, maxLeft);
|
||||
leftPosition = Math.max(leftPosition, 30);
|
||||
|
||||
|
||||
toc.style.left = `${leftPosition}px`;
|
||||
|
||||
if (toc.classList.contains('toc-visible')) {
|
||||
const rightEdge = leftPosition + tocWidth;
|
||||
|
||||
if (rightEdge > window.innerWidth) {
|
||||
toc.classList.add('toc-overflow-right');
|
||||
} else {
|
||||
toc.classList.remove('toc-overflow-right');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleResize() {
|
||||
const toc = document.getElementById("toc");
|
||||
if (!toc) return;
|
||||
|
||||
setupTocVisibility();
|
||||
setupToggleButton();
|
||||
void locateToc();
|
||||
}
|
||||
|
||||
function debounce(func, wait) {
|
||||
let timeout;
|
||||
return function executedFunction(...args) {
|
||||
const later = () => {
|
||||
clearTimeout(timeout);
|
||||
func(...args);
|
||||
};
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
};
|
||||
}
|
||||
|
||||
function destroyToc() {
|
||||
const toc = document.querySelector(".toc")
|
||||
const toc = document.getElementById("toc")
|
||||
if (!toc) return;
|
||||
toc.style.display = "none";
|
||||
toc.remove();
|
||||
@@ -50,25 +150,8 @@ function destroyToc() {
|
||||
|
||||
async function init_toc() {
|
||||
void generateToc();
|
||||
void locateToc();
|
||||
window.addEventListener('resize', debounce(handleResize, 250));
|
||||
|
||||
let resizeTimer;
|
||||
let isVisible = true;
|
||||
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
isVisible = !document.hidden;
|
||||
});
|
||||
|
||||
window.addEventListener('resize', () => {
|
||||
if (!isVisible) return;
|
||||
|
||||
clearTimeout(resizeTimer);
|
||||
resizeTimer = setTimeout(() => {
|
||||
requestAnimationFrame(() => {
|
||||
locateToc();
|
||||
});
|
||||
}, 200);
|
||||
});
|
||||
}
|
||||
|
||||
function register_toc() {
|
||||
|
||||
+111
-17
@@ -1,12 +1,24 @@
|
||||
$toc-bg: color-mix(in srgb, var(--color-theme-background) 70%, transparent);
|
||||
$toc-border: 1px solid var(--color-theme-foreground);
|
||||
|
||||
.toc {
|
||||
margin-left: 12px;
|
||||
max-height: 60vh;
|
||||
overflow-y: auto;
|
||||
background: var(--color-theme-background);
|
||||
border: 1px solid var(--color-theme-foreground);
|
||||
padding: 12px;
|
||||
background: $toc-bg;
|
||||
border: $toc-border;
|
||||
position: fixed;
|
||||
display: none;
|
||||
display: block;
|
||||
z-index: 1000;
|
||||
top: 20vh;
|
||||
padding: 40px 12px 12px;
|
||||
|
||||
.toc-content {
|
||||
display: none;
|
||||
}
|
||||
|
||||
&.toc-visible .toc-content {
|
||||
display: block;
|
||||
}
|
||||
|
||||
&::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
@@ -18,7 +30,6 @@
|
||||
|
||||
&::-webkit-scrollbar-thumb {
|
||||
background: var(--color-theme-foreground);
|
||||
border-radius: 3px;
|
||||
|
||||
&:hover {
|
||||
background: var(--color-theme-background);
|
||||
@@ -36,7 +47,7 @@
|
||||
|
||||
.toc-list {
|
||||
margin-left: 16px;
|
||||
border-left: 1px solid var(--color-theme-foreground);
|
||||
border-left: $toc-border;
|
||||
padding-left: 8px;
|
||||
}
|
||||
}
|
||||
@@ -47,27 +58,110 @@
|
||||
text-decoration: none;
|
||||
color: var(--color-theme-foreground);
|
||||
font-size: 0.875rem;
|
||||
border-radius: 3px;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: var(--color-theme-background);
|
||||
background: transparent;
|
||||
color: var(--color-theme-accent);
|
||||
}
|
||||
|
||||
&.is-active-link {
|
||||
background: var(--color-theme-background);
|
||||
&.is-active-link,
|
||||
.is-active-li > & {
|
||||
background: transparent;
|
||||
color: var(--color-theme-accent);
|
||||
font-weight: bold;
|
||||
position: relative;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.is-active-li {
|
||||
> .toc-link {
|
||||
background: var(--color-theme-background);
|
||||
color: var(--color-theme-accent);
|
||||
font-weight: bold;
|
||||
@media (min-width: 1401px) {
|
||||
.toc:not(.toc-visible) {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
border: none;
|
||||
|
||||
.toc-content {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.toc-toggle {
|
||||
position: absolute;
|
||||
top: -1px;
|
||||
left: -1px;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
outline: none;
|
||||
background: inherit;
|
||||
border: $toc-border;
|
||||
color: var(--color-theme-foreground);
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
z-index: 1002;
|
||||
|
||||
&:hover {
|
||||
background: inherit;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 1400px) {
|
||||
.toc {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
background: $toc-bg;
|
||||
border: none;
|
||||
max-height: none;
|
||||
overflow: visible;
|
||||
left: calc(100vw - 52px);
|
||||
|
||||
.toc-content {
|
||||
display: none;
|
||||
}
|
||||
|
||||
&.toc-visible {
|
||||
width: 280px;
|
||||
max-width: calc(100vw - 20px);
|
||||
height: auto;
|
||||
max-height: calc(100vh - 40px);
|
||||
padding: 40px 12px 12px;
|
||||
background: $toc-bg;
|
||||
border: $toc-border;
|
||||
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
|
||||
overflow-y: auto;
|
||||
z-index: 1001;
|
||||
|
||||
&.toc-overflow-right {
|
||||
left: auto !important;
|
||||
right: 10px;
|
||||
}
|
||||
|
||||
.toc-content {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
.toc-link {
|
||||
font-size: 0.85rem;
|
||||
padding: 5px 8px;
|
||||
}
|
||||
|
||||
.toc-list-item {
|
||||
margin: 3px 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.toc.toc-visible {
|
||||
width: calc(100vw - 200px);
|
||||
max-height: 60vh;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -27,7 +27,10 @@
|
||||
<div th:utext="${post.content.content}" id="content">Post Content</div>
|
||||
</section>
|
||||
</article>
|
||||
<section class="toc" th:if="${theme.config.basic.show_toc}"></section>
|
||||
<nav id="toc" th:if="${theme.config.basic.show_toc}" class="toc">
|
||||
<button id="toc-toggle" class="toc-toggle" aria-label="Hide Table of Contents"></button>
|
||||
<div id="toc-content" class="toc-content"></div>
|
||||
</nav>
|
||||
<section class="pagination">
|
||||
<div class="pagination__title">
|
||||
<span class="pagination__title-h">Read other posts</span>
|
||||
|
||||
Reference in New Issue
Block a user