feat: support responsive TOC
This commit is contained in:
+113
-30
@@ -1,21 +1,48 @@
|
|||||||
import tocbot from "tocbot";
|
import tocbot from "tocbot";
|
||||||
|
|
||||||
async function generateToc() {
|
let toc_icon = "☰"
|
||||||
const toc = document.querySelector(".toc");
|
let toc_close_icon = "✕"
|
||||||
|
|
||||||
|
function toggleToc() {
|
||||||
|
const toc = document.getElementById("toc");
|
||||||
if (!toc) return;
|
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 content = document.getElementById("content");
|
||||||
const titles = content.querySelectorAll("h1, h2, h3, h4");
|
const titles = content.querySelectorAll("h1, h2, h3, h4");
|
||||||
|
|
||||||
if (!titles || titles.length === 0) {
|
if (!titles || titles.length === 0) {
|
||||||
toc.style.display = "none";
|
toc.style.display = "none";
|
||||||
toc.remove();
|
toc.remove();
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
toc.style.display = "block";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof tocbot !== 'undefined') {
|
if (typeof tocbot !== 'undefined') {
|
||||||
tocbot.destroy();
|
tocbot.destroy();
|
||||||
tocbot.init({
|
tocbot.init({
|
||||||
tocSelector: ".toc",
|
tocSelector: ".toc-content",
|
||||||
contentSelector: "#content",
|
contentSelector: "#content",
|
||||||
headingSelector: "h1, h2, h3, h4",
|
headingSelector: "h1, h2, h3, h4",
|
||||||
extraListClasses: "toc-list",
|
extraListClasses: "toc-list",
|
||||||
@@ -25,21 +52,94 @@ async function generateToc() {
|
|||||||
scrollSmooth: true,
|
scrollSmooth: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setupTocVisibility();
|
||||||
|
setupToggleButton();
|
||||||
void locateToc();
|
void locateToc();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function locateToc() {
|
function setupTocVisibility() {
|
||||||
const toc = document.querySelector(".toc")
|
const toc = document.querySelector(".toc");
|
||||||
if (!toc) return;
|
if (!toc) return;
|
||||||
const content = document.getElementById("content");
|
|
||||||
const contentRight = content.getBoundingClientRect().right;
|
toc.style.display = "block";
|
||||||
if (toc && contentRight) {
|
|
||||||
toc.style.left = `${contentRight}px`;
|
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() {
|
function destroyToc() {
|
||||||
const toc = document.querySelector(".toc")
|
const toc = document.getElementById("toc")
|
||||||
if (!toc) return;
|
if (!toc) return;
|
||||||
toc.style.display = "none";
|
toc.style.display = "none";
|
||||||
toc.remove();
|
toc.remove();
|
||||||
@@ -50,25 +150,8 @@ function destroyToc() {
|
|||||||
|
|
||||||
async function init_toc() {
|
async function init_toc() {
|
||||||
void generateToc();
|
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() {
|
function register_toc() {
|
||||||
|
|||||||
+112
-18
@@ -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 {
|
.toc {
|
||||||
margin-left: 12px;
|
margin-left: 12px;
|
||||||
max-height: 60vh;
|
max-height: 60vh;
|
||||||
overflow-y: auto;
|
background: $toc-bg;
|
||||||
background: var(--color-theme-background);
|
border: $toc-border;
|
||||||
border: 1px solid var(--color-theme-foreground);
|
|
||||||
padding: 12px;
|
|
||||||
position: fixed;
|
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 {
|
&::-webkit-scrollbar {
|
||||||
width: 6px;
|
width: 6px;
|
||||||
@@ -18,7 +30,6 @@
|
|||||||
|
|
||||||
&::-webkit-scrollbar-thumb {
|
&::-webkit-scrollbar-thumb {
|
||||||
background: var(--color-theme-foreground);
|
background: var(--color-theme-foreground);
|
||||||
border-radius: 3px;
|
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: var(--color-theme-background);
|
background: var(--color-theme-background);
|
||||||
@@ -36,7 +47,7 @@
|
|||||||
|
|
||||||
.toc-list {
|
.toc-list {
|
||||||
margin-left: 16px;
|
margin-left: 16px;
|
||||||
border-left: 1px solid var(--color-theme-foreground);
|
border-left: $toc-border;
|
||||||
padding-left: 8px;
|
padding-left: 8px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,27 +58,110 @@
|
|||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
color: var(--color-theme-foreground);
|
color: var(--color-theme-foreground);
|
||||||
font-size: 0.875rem;
|
font-size: 0.875rem;
|
||||||
border-radius: 3px;
|
|
||||||
transition: all 0.2s ease;
|
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: var(--color-theme-background);
|
background: transparent;
|
||||||
color: var(--color-theme-accent);
|
color: var(--color-theme-accent);
|
||||||
}
|
}
|
||||||
|
|
||||||
&.is-active-link {
|
&.is-active-link,
|
||||||
background: var(--color-theme-background);
|
.is-active-li > & {
|
||||||
|
background: transparent;
|
||||||
color: var(--color-theme-accent);
|
color: var(--color-theme-accent);
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.is-active-li {
|
@media (min-width: 1401px) {
|
||||||
> .toc-link {
|
.toc:not(.toc-visible) {
|
||||||
background: var(--color-theme-background);
|
width: 32px;
|
||||||
color: var(--color-theme-accent);
|
height: 32px;
|
||||||
font-weight: bold;
|
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>
|
<div th:utext="${post.content.content}" id="content">Post Content</div>
|
||||||
</section>
|
</section>
|
||||||
</article>
|
</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">
|
<section class="pagination">
|
||||||
<div class="pagination__title">
|
<div class="pagination__title">
|
||||||
<span class="pagination__title-h">Read other posts</span>
|
<span class="pagination__title-h">Read other posts</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user