diff --git a/docs/.vitepress/theme/Layout.vue b/docs/.vitepress/theme/Layout.vue index 0fa5be8..45b7ed0 100644 --- a/docs/.vitepress/theme/Layout.vue +++ b/docs/.vitepress/theme/Layout.vue @@ -3,7 +3,7 @@ import DefaultTheme from 'vitepress/theme' import { useData } from 'vitepress' import TextType from './components/TextType.vue' import GitHubStars from './components/GitHubStars.vue' -import { onMounted, ref, watch } from 'vue' +import { onMounted, ref, watch, computed } from 'vue' import ReadingProgress from './components/ReadingProgress.vue' import { Setting } from '@element-plus/icons-vue' @@ -75,6 +75,18 @@ const resetLineHeight = () => { lineHeight.value = DEFAULT_LINE_HEIGHT } +// ============================================ +// 目录栏(左侧 VPSidebar)收起/展开功能 +// ============================================ +const SIDEBAR_COLLAPSED_KEY = 'ev-sidebar-collapsed' +const sidebarCollapsed = ref(false) + +const toggleSidebar = () => { + sidebarCollapsed.value = !sidebarCollapsed.value +} + +const isHomePage = computed(() => frontmatter.value.layout === 'home') + onMounted(() => { const saved = clampFontSize(localStorage.getItem(FONT_SIZE_STORAGE_KEY)) const savedLineHeight = clampLineHeight( @@ -86,6 +98,13 @@ onMounted(() => { applyLineHeight(savedLineHeight) isHydrated.value = true + // 恢复目录栏收起状态 + const savedCollapsed = localStorage.getItem(SIDEBAR_COLLAPSED_KEY) + if (savedCollapsed === 'true') { + sidebarCollapsed.value = true + document.body.classList.add('ev-sidebar-collapsed') + } + initOutlineAutoScroll() }) @@ -263,10 +282,30 @@ watch(lineHeight, (next) => { applyLineHeight(normalized) localStorage.setItem(LINE_HEIGHT_STORAGE_KEY, String(normalized)) }) + +watch(sidebarCollapsed, (collapsed) => { + if (typeof document === 'undefined') return + document.body.classList.toggle('ev-sidebar-collapsed', collapsed) + localStorage.setItem(SIDEBAR_COLLAPSED_KEY, String(collapsed)) +})