perf: optimize TOC positioning logic
This commit is contained in:
+27
-27
@@ -14,7 +14,6 @@ function toggleToc() {
|
|||||||
|
|
||||||
if (isVisible) {
|
if (isVisible) {
|
||||||
toc.classList.remove('toc-visible');
|
toc.classList.remove('toc-visible');
|
||||||
toc.classList.remove('toc-overflow-right');
|
|
||||||
toc_toggle.innerHTML = toc_icon;
|
toc_toggle.innerHTML = toc_icon;
|
||||||
toc_toggle.setAttribute('aria-label', 'Show Table of Contents');
|
toc_toggle.setAttribute('aria-label', 'Show Table of Contents');
|
||||||
|
|
||||||
@@ -82,39 +81,40 @@ function setupToggleButton() {
|
|||||||
toc_toggle.addEventListener('click', toggleToc);
|
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() {
|
async function locateToc() {
|
||||||
const toc = document.getElementById("toc");
|
const toc = document.getElementById("toc");
|
||||||
if (!toc) return;
|
if (!toc) return;
|
||||||
const toc_toggle = document.getElementById("toc-toggle");
|
|
||||||
|
|
||||||
const content = document.getElementById("content");
|
const content = document.getElementById("content");
|
||||||
|
if (!content) return;
|
||||||
|
|
||||||
const contentRect = content.getBoundingClientRect();
|
const contentRect = content.getBoundingClientRect();
|
||||||
const contentRight = contentRect.right;
|
const gap = 12;
|
||||||
|
|
||||||
if (contentRight) {
|
if (hasEnoughSpace()) {
|
||||||
let leftPosition = contentRight + 12;
|
toc.style.left = `${contentRect.right + gap}px`;
|
||||||
|
toc.style.right = 'auto';
|
||||||
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 {
|
} else {
|
||||||
toc.classList.remove('toc-overflow-right');
|
toc.style.left = 'auto';
|
||||||
}
|
toc.style.right = `${gap}px`;
|
||||||
}
|
toc.style.top = 'auto';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleResize() {
|
function handleResize() {
|
||||||
@@ -154,7 +154,7 @@ async function init_toc() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function register_toc() {
|
function registerToc() {
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
void init_toc();
|
void init_toc();
|
||||||
});
|
});
|
||||||
@@ -162,4 +162,4 @@ function register_toc() {
|
|||||||
|
|
||||||
|
|
||||||
terminal.registerRefresh(generateToc);
|
terminal.registerRefresh(generateToc);
|
||||||
terminal.registerInitFunc(register_toc);
|
terminal.registerInitFunc(registerToc);
|
||||||
|
|||||||
+37
-82
@@ -1,36 +1,55 @@
|
|||||||
.toc {
|
.toc {
|
||||||
margin-left: calc(var(--size-theme-font) * 0.75);
|
|
||||||
position: fixed;
|
position: fixed;
|
||||||
display: block;
|
|
||||||
z-index: 1000;
|
|
||||||
top: 20vh;
|
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);
|
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 {
|
.toc-content {
|
||||||
overflow: hidden;
|
|
||||||
display: none;
|
display: none;
|
||||||
max-height: calc(100vh - 20vh - var(--size-theme-font) * 5);
|
overflow-y: auto;
|
||||||
}
|
|
||||||
|
|
||||||
&.toc-visible .toc-content {
|
&::-webkit-scrollbar {
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
|
|
||||||
.toc-content::-webkit-scrollbar {
|
|
||||||
width: calc(var(--size-theme-font) * 0.375);
|
width: calc(var(--size-theme-font) * 0.375);
|
||||||
}
|
}
|
||||||
|
|
||||||
.toc-content::-webkit-scrollbar-track {
|
&::-webkit-scrollbar-track {
|
||||||
background: transparent;
|
background: transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
.toc-content::-webkit-scrollbar-thumb {
|
&::-webkit-scrollbar-thumb {
|
||||||
background: var(--color-theme-foreground);
|
background: var(--color-theme-foreground);
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: transparent;
|
background: transparent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
&.toc-visible .toc-content {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
.toc-list {
|
.toc-list {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
@@ -65,33 +84,14 @@
|
|||||||
background: transparent;
|
background: transparent;
|
||||||
color: var(--color-theme-accent);
|
color: var(--color-theme-accent);
|
||||||
font-weight: bold;
|
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 {
|
.toc-toggle {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: calc(var(--size-theme-font) * -0.0625);
|
top: 0;
|
||||||
left: calc(var(--size-theme-font) * -0.0625);
|
left: 0;
|
||||||
width: calc(var(--size-theme-font) * 2);
|
width: calc(var(--size-theme-font) * 2);
|
||||||
height: calc(var(--size-theme-font) * 2);
|
height: calc(var(--size-theme-font) * 2);
|
||||||
padding: 0;
|
padding: 0;
|
||||||
@@ -105,6 +105,7 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
font-size: var(--size-theme-font);
|
font-size: var(--size-theme-font);
|
||||||
z-index: 1002;
|
z-index: 1002;
|
||||||
|
-webkit-tap-highlight-color: transparent;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: transparent;
|
background: transparent;
|
||||||
@@ -113,63 +114,17 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 1400px) {
|
@media (max-width: 1400px) {
|
||||||
.toc {
|
.toc.toc-visible {
|
||||||
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);
|
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) {
|
@media (max-width: 480px) {
|
||||||
.toc.toc-visible {
|
.toc.toc-visible {
|
||||||
max-width: max(calc(100vw - (var(--size-theme-font) * 12.5)), 50vw);
|
|
||||||
max-height: 60vh;
|
max-height: 60vh;
|
||||||
|
|
||||||
.toc-content {
|
.toc-content {
|
||||||
max-height: calc(60vh - var(--size-theme-font) * 3.75);
|
max-height: calc(60vh - var(--size-theme-font) * 4);
|
||||||
overflow-y: auto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.toc-list {
|
.toc-list {
|
||||||
|
|||||||
Reference in New Issue
Block a user