From db9a246e7bc65c06cc78db31d2214d82652877c4 Mon Sep 17 00:00:00 2001 From: Erzbir Date: Sun, 3 Aug 2025 14:09:04 +0800 Subject: [PATCH 1/2] chore: 1.0.1 --- theme.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theme.yaml b/theme.yaml index 3a067bb..8ff39fc 100644 --- a/theme.yaml +++ b/theme.yaml @@ -13,7 +13,7 @@ spec: repo: https://github.com/Erzbir/halo-theme-terminal.git settingName: "theme-terminal-ez-setting" configMapName: "theme-terminal-ez-configMap" - version: 1.0.0 + version: 1.0.1 require: ">=2.8.0" license: - name: "MIT" From b796ea9e46d72ec65b835703d2123488dc6af773 Mon Sep 17 00:00:00 2001 From: Erzbir Date: Sun, 3 Aug 2025 18:42:03 +0800 Subject: [PATCH 2/2] feat: support responsive TOC --- src/module/post.js | 143 ++++++++++++++++++++++++++++++++++---------- src/styles/toc.scss | 130 ++++++++++++++++++++++++++++++++++------ templates/post.html | 5 +- 3 files changed, 229 insertions(+), 49 deletions(-) diff --git a/src/module/post.js b/src/module/post.js index 9b3a237..cfddb0d 100644 --- a/src/module/post.js +++ b/src/module/post.js @@ -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() { diff --git a/src/styles/toc.scss b/src/styles/toc.scss index 7bc951c..6e703a8 100644 --- a/src/styles/toc.scss +++ b/src/styles/toc.scss @@ -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; } } -} \ No newline at end of file +} + +.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; + } +} diff --git a/templates/post.html b/templates/post.html index 25ca51b..b7442ff 100644 --- a/templates/post.html +++ b/templates/post.html @@ -27,7 +27,10 @@
Post Content
-
+
Read other posts