Skip to content

Commit d39a468

Browse files
committed
feat: 完善沉浸式阅读功能
- 沉浸模式下浏览器标题显示为"JavaGuide -沉浸式阅读中" - 修复页面切换后退出沉浸模式时标题未正确恢复的问题 - 修复小屏幕下侧边栏切换按钮仍然显示的问题 - 隐藏侧边栏遮罩层 - 使用 usePageData 替代 setTimeout 获取页面标题,优化性能
1 parent c6d432e commit d39a468

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

docs/.vuepress/components/LayoutToggle.vue

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,60 @@
3030
</template>
3131

3232
<script setup lang="ts">
33-
import { ref, onMounted, watch } from "vue";
33+
import { ref, onMounted, watch, computed } from "vue";
34+
import { usePageData } from "vuepress/client";
3435
3536
const isHidden = ref(false);
37+
const pageData = usePageData();
3638
3739
const STORAGE_KEY = "javaguide-layout-hidden";
40+
const IMMERSIVE_TITLE = "JavaGuide - 沉浸式阅读中";
41+
42+
// 计算当前页面的原始标题
43+
const originalTitle = computed(() => {
44+
const title = pageData.value.title;
45+
const siteTitle = "JavaGuide";
46+
return title ? `${title} | ${siteTitle}` : siteTitle;
47+
});
3848
3949
const toggleLayout = () => {
4050
isHidden.value = !isHidden.value;
4151
};
4252
53+
// 更新浏览器标题
54+
const updateBrowserTitle = (hidden: boolean) => {
55+
if (typeof document === "undefined") return;
56+
document.title = hidden ? IMMERSIVE_TITLE : originalTitle.value;
57+
};
58+
4359
// 应用隐藏状态
4460
const applyHiddenState = (hidden: boolean) => {
45-
if (typeof document !== "undefined") {
46-
if (hidden) {
47-
document.documentElement.classList.add("layout-hidden");
48-
} else {
49-
document.documentElement.classList.remove("layout-hidden");
50-
}
51-
}
61+
if (typeof document === "undefined") return;
62+
document.documentElement.classList.toggle("layout-hidden", hidden);
63+
updateBrowserTitle(hidden);
5264
};
5365
54-
// 监听状态变化
66+
// 监听沉浸模式状态变化
5567
watch(isHidden, (newVal) => {
5668
applyHiddenState(newVal);
57-
// 保存到 localStorage
58-
if (typeof localStorage !== "undefined") {
59-
localStorage.setItem(STORAGE_KEY, String(newVal));
60-
}
69+
localStorage?.setItem(STORAGE_KEY, String(newVal));
6170
});
6271
63-
onMounted(() => {
64-
// 从 localStorage 读取状态
65-
if (typeof localStorage !== "undefined") {
66-
const saved = localStorage.getItem(STORAGE_KEY);
67-
if (saved === "true") {
68-
isHidden.value = true;
69-
applyHiddenState(true);
72+
// 监听页面切换,更新标题
73+
watch(
74+
() => pageData.value.path,
75+
() => {
76+
if (isHidden.value) {
77+
updateBrowserTitle(true);
7078
}
79+
},
80+
);
81+
82+
onMounted(() => {
83+
const saved = localStorage?.getItem(STORAGE_KEY);
84+
if (saved === "true") {
85+
isHidden.value = true;
86+
applyHiddenState(true);
7187
}
7288
});
7389
</script>

docs/.vuepress/styles/index.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ html.layout-hidden {
3838
width: 0 !important;
3939
}
4040

41+
// 隐藏侧边栏切换按钮(小屏幕下的展开按钮)
42+
.toggle-sidebar-wrapper {
43+
display: none !important;
44+
opacity: 0 !important;
45+
pointer-events: none !important;
46+
}
47+
48+
// 隐藏侧边栏遮罩层
49+
.vp-sidebar-mask {
50+
display: none !important;
51+
}
52+
4153
// 侧边栏包装器
4254
.vp-sidebar-wrapper,
4355
.sidebar-wrapper {

0 commit comments

Comments
 (0)