diff --git a/themes/coo/layout/_partial/toc.ejs b/themes/coo/layout/_partial/toc.ejs new file mode 100644 index 0000000..6383ef6 --- /dev/null +++ b/themes/coo/layout/_partial/toc.ejs @@ -0,0 +1,17 @@ + +<% if (is_post()) { %> +
+ +
+ + +<% } %> diff --git a/themes/coo/layout/layout.ejs b/themes/coo/layout/layout.ejs index ab11006..3f60c48 100644 --- a/themes/coo/layout/layout.ejs +++ b/themes/coo/layout/layout.ejs @@ -4,6 +4,7 @@ <%- partial('_partial/header') %> <%- body %> +<%- partial('_partial/toc') %> <%- partial('_partial/search') %> <%- partial('_partial/footer') %> diff --git a/themes/coo/source/css/components/toc.css b/themes/coo/source/css/components/toc.css new file mode 100644 index 0000000..293dab6 --- /dev/null +++ b/themes/coo/source/css/components/toc.css @@ -0,0 +1,359 @@ +/* stylelint-disable at-rule-no-deprecated */ + +/* Simple Timeline TOC */ +.toc-container { + @apply fixed top-1/2 z-40 -translate-y-1/2; + @apply block; + + width: 20px; + opacity: 1; + + /* Constrain height to viewport */ + max-height: 80vh; + overflow: visible; + + /* Responsive positioning */ + right: 1rem; /* 16px on mobile */ +} + +/* Larger screens */ +@media (width >= 768px) { + .toc-container { + right: 1.5rem; /* 24px on tablet+ */ + } +} + +@media (width >= 1280px) { + .toc-container { + right: 1.5rem; /* 24px on desktop */ + } +} + +.toc-list { + @apply relative flex flex-col items-center space-y-3 py-4; + + height: 100%; +} + +/* Timeline line - background (unread) */ +.toc-list::before { + content: ''; + + @apply absolute bottom-0 left-1/2 top-0 w-0.5; + @apply bg-slate-300 dark:bg-slate-600; + + transform: translateX(-50%); + z-index: 1; +} + +/* Timeline line - progress (read) */ +.toc-list::after { + content: ''; + + @apply absolute left-1/2 top-0 w-0.5; + @apply bg-gradient-to-b from-emerald-400 via-emerald-500 to-emerald-600; + + transform: translateX(-50%); + z-index: 1; + + /* Height controlled by CSS variable */ + height: var(--progress-height, 0%); + transition: height 0.3s ease-out; +} + +/* Timeline dots with larger hover area */ +.toc-link { + @apply relative flex cursor-pointer items-center justify-center; + @apply transition-all duration-200; + + text-decoration: none; + z-index: 2; + + /* Responsive clickable area */ + width: 32px; + height: 32px; + margin: 3px 0; + + /* Background for hover effect */ + border-radius: 50%; +} + +/* Mobile optimizations */ +@media (width <= 767px) { + .toc-link { + width: 36px; + height: 36px; + margin: 2px 0; + } +} + +/* Add subtle background when hovering over the list */ +.toc-list:hover .toc-link { + @apply bg-slate-100 dark:bg-slate-800; + + opacity: 0.7; +} + +/* Highlight the currently hovered link */ +.toc-link:hover { + @apply bg-blue-100 dark:bg-blue-900; + + opacity: 1 !important; + transform: scale(1.1); +} + +/* Actual dot using ::before */ +.toc-link::before { + content: ''; + + @apply rounded-full transition-all duration-200; + + /* Default unread state - responsive size */ + width: 12px; + height: 12px; + + @apply bg-slate-400 dark:bg-slate-500; +} + +/* Larger dots on mobile for better visibility */ +@media (width <= 767px) { + .toc-link::before { + width: 14px; + height: 14px; + } +} + +/* Hover effects for dots */ +.toc-list:hover .toc-link::before { + /* Dim all dots when hovering over the list */ + @apply bg-slate-300 dark:bg-slate-600; + + opacity: 0.6; +} + +.toc-list:hover .toc-link.read::before { + /* Dim read dots */ + @apply bg-emerald-400; + + opacity: 0.6; +} + +.toc-list:hover .toc-link.active::before { + /* Dim active dots */ + @apply bg-emerald-600; + + opacity: 0.6; +} + +/* Highlight the currently hovered dot */ +.toc-link:hover::before { + @apply bg-slate-700 dark:bg-slate-200; + + opacity: 1 !important; + transform: scale(1.2); + box-shadow: 0 0 0 3px rgb(59 130 246 / 30%); +} + +.toc-link.read:hover::before { + @apply bg-emerald-700; + + opacity: 1 !important; + transform: scale(1.2); + box-shadow: 0 0 0 3px rgb(16 185 129 / 30%); +} + +.toc-link.active:hover::before { + @apply bg-emerald-800; + + opacity: 1 !important; + transform: scale(1.2); + box-shadow: 0 0 0 3px rgb(16 185 129 / 40%); +} + +/* Active state */ +.toc-link.active::before { + @apply bg-emerald-700 ring-2 ring-emerald-300; +} + +/* Tooltip - show all when any link is hovered */ +.toc-link::after { + content: attr(data-tooltip); + + @apply absolute top-1/2 -translate-y-1/2; + @apply rounded-lg bg-slate-600 px-3 py-2 text-xs text-white; + @apply whitespace-nowrap shadow-lg; + @apply dark:bg-slate-600 dark:text-slate-300; + + /* Fixed position to the left of the timeline */ + right: 44px; + z-index: 50; + + /* Hidden by default */ + opacity: 0; + pointer-events: none; + transition: opacity 0.2s ease-out; + + /* Prevent tooltip from going off screen */ + max-width: calc(100vw - 80px); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +/* Mobile tooltip adjustments */ +@media (width <= 767px) { + .toc-link::after { + right: 48px; + + @apply px-2 py-1 text-xs; + + max-width: calc(100vw - 100px); + } +} + +/* Show all tooltips when any link is hovered */ +.toc-list:hover .toc-link::after { + opacity: 1; +} + +/* Highlight the currently hovered tooltip */ +.toc-link:hover::after { + @apply bg-slate-800 text-white; + @apply dark:bg-slate-700 dark:text-slate-100; + @apply font-medium; + + transform: translateY(-50%) scale(1.05); + box-shadow: + 0 8px 25px -3px rgb(0 0 0 / 20%), + 0 4px 6px -2px rgb(0 0 0 / 10%); +} + +@keyframes tooltip-fade-in { + from { + opacity: 0; + transform: translateY(-50%) translateX(8px); + } + + to { + opacity: 1; + transform: translateY(-50%) translateX(0); + } +} + +/* Mobile touch optimizations */ +@media (width <= 767px) { + /* Ensure TOC doesn't interfere with content on very small screens */ + .toc-container { + right: 0.5rem; + + /* Semi-transparent by default on mobile */ + opacity: 0.6; + transition: opacity 0.3s ease; + } + + /* Full opacity when interacting */ + .toc-container:active, + .toc-container:hover, + .toc-container:focus-within { + opacity: 1; + } + + /* Semi-transparent timeline and dots */ + .toc-list::before { + @apply bg-slate-300/60 dark:bg-slate-600/60; + } + + .toc-list::after { + @apply bg-gradient-to-b from-emerald-400/70 via-emerald-500/70 to-emerald-600/70; + } + + .toc-link::before { + @apply bg-slate-400/70 dark:bg-slate-500/70; + } + + .toc-link.read::before { + @apply bg-emerald-500/80; + } + + .toc-link.active::before { + @apply bg-emerald-700/90; + } + + /* Full opacity when container is active */ + .toc-container:active .toc-list::before, + .toc-container:hover .toc-list::before { + @apply bg-slate-300 dark:bg-slate-600; + } + + .toc-container:active .toc-list::after, + .toc-container:hover .toc-list::after { + @apply bg-gradient-to-b from-emerald-400 via-emerald-500 to-emerald-600; + } + + .toc-container:active .toc-link::before, + .toc-container:hover .toc-link::before { + @apply bg-slate-400 dark:bg-slate-500; + } + + .toc-container:active .toc-link.read::before, + .toc-container:hover .toc-link.read::before { + @apply bg-emerald-500; + } + + .toc-container:active .toc-link.active::before, + .toc-container:hover .toc-link.active::before { + @apply bg-emerald-700; + } + + /* Better touch targets */ + .toc-link { + -webkit-tap-highlight-color: rgb(59 130 246 / 30%); + /* stylelint-disable-next-line property-no-unknown */ + tap-highlight-color: rgb(59 130 246 / 30%); + } + + /* Show tooltips on touch for mobile */ + .toc-link:active::after { + opacity: 1; + } + + /* Reduce animation intensity on mobile for better performance */ + .toc-link:active { + transform: scale(1.05); + } + + .toc-link:active::before { + transform: scale(1.1); + } + + /* Add subtle swipe indicator */ + .toc-container::after { + content: ''; + position: absolute; + top: -8px; + left: 50%; + transform: translateX(-50%); + width: 2px; + height: 4px; + background: rgb(148 163 184 / 50%); + border-radius: 1px; + animation: swipe-hint 2s ease-in-out infinite; + } + + @keyframes swipe-hint { + 0%, + 100% { + opacity: 0.3; + } + + 50% { + opacity: 0.7; + } + } + + /* Hide hint when interacting */ + .toc-container:active::after, + .toc-container:hover::after { + opacity: 0; + } +} diff --git a/themes/coo/source/css/style.tailwindcss b/themes/coo/source/css/style.tailwindcss index 7070598..b96b91a 100644 --- a/themes/coo/source/css/style.tailwindcss +++ b/themes/coo/source/css/style.tailwindcss @@ -12,6 +12,7 @@ @import "components/custom_tag.css"; @import "components/code.css"; @import "components/search.css"; +@import "components/toc.css"; @import "components/pagination.css"; @import "components/common.css"; diff --git a/themes/coo/source/js/main.js b/themes/coo/source/js/main.js index 27727eb..73937e4 100644 --- a/themes/coo/source/js/main.js +++ b/themes/coo/source/js/main.js @@ -58,6 +58,185 @@ const Utils = (function () { }; })(); +// Simple Timeline TOC - Direct Implementation +(function () { + function initTOC() { + const tocContainer = document.getElementById('toc-container'); + const tocList = document.getElementById('toc-list'); + + if (!tocContainer || !tocList) return; + + const headings = document.querySelectorAll('.h2-wrap h2'); + + if (headings.length === 0) { + tocContainer.style.display = 'none'; + return; + } + + // Generate TOC + tocList.innerHTML = ''; + headings.forEach((heading, index) => { + let text = heading.textContent.trim().replace(/^#+\s*/, ''); + const id = heading.id || `heading-${index}`; + + if (!heading.id) { + heading.id = id; + } + + const tocItem = document.createElement('a'); + tocItem.href = `#${id}`; + tocItem.className = 'toc-link'; + tocItem.setAttribute('data-tooltip', text); + + tocItem.addEventListener('click', (e) => { + e.preventDefault(); + const targetElement = document.getElementById(id); + if (targetElement) { + const elementTop = targetElement.getBoundingClientRect().top + window.pageYOffset; + const viewportHeight = window.innerHeight; + const centerOffset = viewportHeight / 2; + const scrollToPosition = elementTop - centerOffset; + + window.scrollTo({ + top: Math.max(0, scrollToPosition), + behavior: 'smooth' + }); + + window.location.hash = id; + } + }); + + tocList.appendChild(tocItem); + }); + + // Update active heading and progress + function updateActiveHeading() { + const tocLinks = document.querySelectorAll('.toc-link'); + let activeIndex = -1; + const scrollTop = window.pageYOffset + 100; + + for (let i = headings.length - 1; i >= 0; i--) { + if (headings[i].offsetTop <= scrollTop) { + activeIndex = i; + break; + } + } + + tocLinks.forEach((link, index) => { + link.classList.remove('active'); + if (index <= activeIndex) { + link.classList.add('read'); + } else { + link.classList.remove('read'); + } + }); + + if (activeIndex >= 0 && tocLinks[activeIndex]) { + tocLinks[activeIndex].classList.add('active'); + } + + // Update progress + let progressPercent = 0; + if (activeIndex >= 0) { + progressPercent = ((activeIndex + 1) / headings.length) * 100; + } + + const scrollPercent = + (window.pageYOffset / (document.documentElement.scrollHeight - window.innerHeight)) * 100; + const finalProgress = Math.max(progressPercent, scrollPercent || 0); + + tocList.style.setProperty('--progress-height', `${Math.min(finalProgress, 100)}%`); + } + + // Swipe/drag functionality for mobile + let isDragging = false; + + function handleTouchStart() { + isDragging = true; + tocContainer.style.transition = 'none'; + } + + function handleTouchMove(e) { + if (!isDragging) return; + e.preventDefault(); + + const currentY = e.touches[0].clientY; + const containerRect = tocContainer.getBoundingClientRect(); + const containerHeight = containerRect.height; + + // Calculate which heading should be active based on touch position + const relativeY = (currentY - containerRect.top) / containerHeight; + const targetIndex = Math.floor(relativeY * headings.length); + const clampedIndex = Math.max(0, Math.min(targetIndex, headings.length - 1)); + + // Update visual feedback + const tocLinks = document.querySelectorAll('.toc-link'); + tocLinks.forEach((link, index) => { + link.classList.remove('active'); + if (index <= clampedIndex) { + link.classList.add('read'); + } else { + link.classList.remove('read'); + } + }); + + if (tocLinks[clampedIndex]) { + tocLinks[clampedIndex].classList.add('active'); + } + } + + function handleTouchEnd(e) { + if (!isDragging) return; + isDragging = false; + tocContainer.style.transition = ''; + + const currentY = e.changedTouches[0].clientY; + const containerRect = tocContainer.getBoundingClientRect(); + const containerHeight = containerRect.height; + + // Calculate target heading + const relativeY = (currentY - containerRect.top) / containerHeight; + const targetIndex = Math.floor(relativeY * headings.length); + const clampedIndex = Math.max(0, Math.min(targetIndex, headings.length - 1)); + + // Navigate to the target heading + if (headings[clampedIndex]) { + const targetHeading = headings[clampedIndex]; + const elementTop = targetHeading.getBoundingClientRect().top + window.pageYOffset; + const viewportHeight = window.innerHeight; + const centerOffset = viewportHeight / 2; + const scrollToPosition = elementTop - centerOffset; + + window.scrollTo({ + top: Math.max(0, scrollToPosition), + behavior: 'smooth' + }); + + window.location.hash = targetHeading.id; + } + } + + // Add touch event listeners + tocContainer.addEventListener('touchstart', handleTouchStart, { passive: false }); + tocContainer.addEventListener('touchmove', handleTouchMove, { passive: false }); + tocContainer.addEventListener('touchend', handleTouchEnd, { passive: false }); + + // Event listeners + window.addEventListener('scroll', updateActiveHeading, { passive: true }); + window.addEventListener('hashchange', updateActiveHeading); + + // Initial update + updateActiveHeading(); + } + + // Run immediately if DOM is ready, otherwise wait + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', initTOC); + } else { + initTOC(); + } +})(); + window.addEventListener('load', () => { const isDarkMode = document.documentElement.classList.contains('dark'); if (!isDarkMode) { @@ -211,7 +390,7 @@ window.addEventListener('load', () => { _this.closeModal(); e.preventDefault(); e.stopPropagation(); - } else if ((e.ctrlKey || e.metaKey) && e.key === 'k') { + } else if ((e.ctrlKey || e.metaKey) && e.key === 'k' && !e.shiftKey) { _this.toggleModal(); e.preventDefault(); e.stopPropagation();