From 304863c99fa5306beac8911d571fee63806a1281 Mon Sep 17 00:00:00 2001 From: Erzbir Date: Fri, 27 Mar 2026 23:03:19 +0800 Subject: [PATCH] perf: optimize TOC positioning logic --- src/module/post.js | 56 +++++++++--------- src/styles/toc.scss | 141 +++++++++++++++----------------------------- 2 files changed, 76 insertions(+), 121 deletions(-) diff --git a/src/module/post.js b/src/module/post.js index 7ee2e64..be6c1de 100644 --- a/src/module/post.js +++ b/src/module/post.js @@ -14,7 +14,6 @@ function toggleToc() { 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'); @@ -82,39 +81,40 @@ function setupToggleButton() { toc_toggle.addEventListener('click', toggleToc); } +function hasEnoughSpace() { + const content = document.getElementById("content"); + const toc = document.getElementById("toc"); + if (!content || !toc) return false; + + const contentRect = content.getBoundingClientRect(); + const gap = 12; + const tocExpandedWidth = toc.offsetWidth > 0 + ? toc.offsetWidth + : parseInt(getComputedStyle(document.documentElement).fontSize) * 17.5; + + const spaceRight = window.innerWidth - contentRect.right - gap; + return spaceRight >= tocExpandedWidth; +} + async function locateToc() { const toc = document.getElementById("toc"); if (!toc) return; - const toc_toggle = document.getElementById("toc-toggle"); const content = document.getElementById("content"); + if (!content) return; + const contentRect = content.getBoundingClientRect(); - const contentRight = contentRect.right; + const gap = 12; - 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 - content.offsetLeft - 20; - 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'); - } - } + if (hasEnoughSpace()) { + toc.style.left = `${contentRect.right + gap}px`; + toc.style.right = 'auto'; + } else { + toc.style.left = 'auto'; + toc.style.right = `${gap}px`; + toc.style.top = 'auto'; } + } function handleResize() { @@ -154,7 +154,7 @@ async function init_toc() { } -function register_toc() { +function registerToc() { document.addEventListener('DOMContentLoaded', () => { void init_toc(); }); @@ -162,4 +162,4 @@ function register_toc() { terminal.registerRefresh(generateToc); -terminal.registerInitFunc(register_toc); +terminal.registerInitFunc(registerToc); diff --git a/src/styles/toc.scss b/src/styles/toc.scss index ef816fe..b8c950d 100644 --- a/src/styles/toc.scss +++ b/src/styles/toc.scss @@ -1,37 +1,56 @@ .toc { - margin-left: calc(var(--size-theme-font) * 0.75); position: fixed; - display: block; - z-index: 1000; top: 20vh; - padding: calc(var(--size-theme-font) * 2.5) calc(var(--size-theme-font) * 0.75) calc(var(--size-theme-font) * 0.75); + z-index: 1000; + display: none; + padding-top: calc(var(--size-theme-font) * 2.5); + + &:not(.toc-visible) { + width: calc(var(--size-theme-font) * 2); + height: calc(var(--size-theme-font) * 2); + padding: 0; + background: transparent; + border: none; + } + + &.toc-visible { + width: calc(var(--size-theme-font) * 17.5); + max-width: calc(100vw - var(--size-theme-font) * 2.5); + height: auto; + max-height: calc(80vh - var(--size-theme-font) * 2); + padding: calc(var(--size-theme-font) * 2.5) calc(var(--size-theme-font) * 0.75) calc(var(--size-theme-font) * 0.75); + background: var(--color-theme-background); + + .toc-content { + max-height: calc(80vh - var(--size-theme-font) * 7); + } + } .toc-content { - overflow: hidden; display: none; - max-height: calc(100vh - 20vh - var(--size-theme-font) * 5); + overflow-y: auto; + + &::-webkit-scrollbar { + width: calc(var(--size-theme-font) * 0.375); + } + + &::-webkit-scrollbar-track { + background: transparent; + } + + &::-webkit-scrollbar-thumb { + background: var(--color-theme-foreground); + + &:hover { + background: transparent; + } + } } &.toc-visible .toc-content { display: block; } - .toc-content::-webkit-scrollbar { - width: calc(var(--size-theme-font) * 0.375); - } - - .toc-content::-webkit-scrollbar-track { - background: transparent; - } - - .toc-content::-webkit-scrollbar-thumb { - background: var(--color-theme-foreground); - - &:hover { - background: transparent; - } - } - .toc-list { list-style: none; padding: 0; @@ -43,7 +62,7 @@ .toc-list { margin-left: var(--size-theme-font); - border-left: calc(var(--size-theme-font) * 0.0625) solid var(--color-theme-foreground); + border-left: calc(var(--size-theme-font) * 0.0625) solid var(--color-theme-foreground); padding-left: calc(var(--size-theme-font) * 0.5); } } @@ -65,33 +84,14 @@ background: transparent; color: var(--color-theme-accent); font-weight: bold; - position: relative; } } } -@media (min-width: 1401px) { - .toc:not(.toc-visible) { - width: calc(var(--size-theme-font) * 2); - height: calc(var(--size-theme-font) * 2); - padding: 0; - background: transparent; - border: none; - - .toc-content { - display: none; - } - } - - .toc.toc-visible { - max-width: 25vw - } -} - .toc-toggle { position: absolute; - top: calc(var(--size-theme-font) * -0.0625); - left: calc(var(--size-theme-font) * -0.0625); + top: 0; + left: 0; width: calc(var(--size-theme-font) * 2); height: calc(var(--size-theme-font) * 2); padding: 0; @@ -105,6 +105,7 @@ cursor: pointer; font-size: var(--size-theme-font); z-index: 1002; + -webkit-tap-highlight-color: transparent; &:hover { background: transparent; @@ -113,63 +114,17 @@ } @media (max-width: 1400px) { - .toc { - width: calc(var(--size-theme-font) * 2); - height: calc(var(--size-theme-font) * 2); - padding: 0; - border: none; - overflow: visible; - left: calc(100vw - var(--size-theme-font) * 3.25); - - .toc-content { - display: none; - } - - &.toc-visible { - width: calc(var(--size-theme-font) * 17.5); - max-width: calc(100vw - var(--size-theme-font) * 1.25); - height: auto; - max-height: calc(100vh - var(--size-theme-font) * 5); - padding: calc(var(--size-theme-font) * 2.5) calc(var(--size-theme-font) * 0.75) calc(var(--size-theme-font) * 0.75); - background: var(--color-theme-background); - border: calc(var(--size-theme-font) * 0.0625) solid var(--color-theme-foreground); - z-index: 1001; - - &.toc-overflow-right { - left: auto !important; - right: calc(var(--size-theme-font) * 0.625); - } - - .toc-content { - display: block; - overflow-y: auto; - max-height: calc(100vh - var(--size-theme-font) * 8.5); - } - - .toc-list { - - } - } - - .toc-link { - font-size: calc(var(--size-theme-font) * 0.85); - padding: calc(var(--size-theme-font) * 0.3125) calc(var(--size-theme-font) * 0.5); - } - - .toc-list-item { - margin: calc(var(--size-theme-font) * 0.1875) 0; - } + .toc.toc-visible { + border: calc(var(--size-theme-font) * 0.0625) solid var(--color-theme-foreground); } } @media (max-width: 480px) { .toc.toc-visible { - max-width: max(calc(100vw - (var(--size-theme-font) * 12.5)), 50vw); max-height: 60vh; .toc-content { - max-height: calc(60vh - var(--size-theme-font) * 3.75); - overflow-y: auto; + max-height: calc(60vh - var(--size-theme-font) * 4); } .toc-list {