perf: optimize TOC positioning logic
This commit is contained in:
+27
-27
@@ -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');
|
||||
if (hasEnoughSpace()) {
|
||||
toc.style.left = `${contentRect.right + gap}px`;
|
||||
toc.style.right = 'auto';
|
||||
} else {
|
||||
toc.classList.remove('toc-overflow-right');
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
||||
+37
-82
@@ -1,36 +1,55 @@
|
||||
.toc {
|
||||
margin-left: calc(var(--size-theme-font) * 0.75);
|
||||
position: fixed;
|
||||
display: block;
|
||||
z-index: 1000;
|
||||
top: 20vh;
|
||||
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;
|
||||
|
||||
&.toc-visible .toc-content {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.toc-content::-webkit-scrollbar {
|
||||
&::-webkit-scrollbar {
|
||||
width: calc(var(--size-theme-font) * 0.375);
|
||||
}
|
||||
|
||||
.toc-content::-webkit-scrollbar-track {
|
||||
&::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.toc-content::-webkit-scrollbar-thumb {
|
||||
&::-webkit-scrollbar-thumb {
|
||||
background: var(--color-theme-foreground);
|
||||
|
||||
&:hover {
|
||||
background: transparent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&.toc-visible .toc-content {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.toc-list {
|
||||
list-style: none;
|
||||
@@ -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);
|
||||
.toc.toc-visible {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
|
||||
Reference in New Issue
Block a user