Skip to content

Commit 3cc4893

Browse files
committed
feat: 添加 Umami 统计组件及其翻译,优化导航和样式
1 parent f23e027 commit 3cc4893

7 files changed

Lines changed: 42 additions & 23 deletions

File tree

.vitepress/data/i18n.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export const contributorsTranslations = {
2020
"zh-TW": zh_tw.contributors,
2121
};
2222

23+
// Umami 统计组件翻译
24+
export const umamiStatsTranslations = {
25+
"zh-CN": zh.umamiStats,
26+
en: en.umamiStats,
27+
"zh-TW": zh_tw.umamiStats,
28+
};
29+
2330
// 下载组件翻译
2431
export const downloadTranslations = {
2532
"zh-CN": zh.download,

.vitepress/data/lang/en.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export const nav: DefaultTheme.NavItem[] = [
3030
},
3131
];
3232

33+
// Umami stats component translations
34+
export const umamiStats = {
35+
views: "Views",
36+
visits: "Visits",
37+
loading: "Loading...",
38+
};
39+
3340
// Contributors component translations
3441
export const contributors = {
3542
author: "Author",

.vitepress/data/lang/zh.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export const nav: DefaultTheme.NavItem[] = [
3030
},
3131
];
3232

33+
// Umami 统计组件翻译
34+
export const umamiStats = {
35+
views: "浏览量",
36+
visits: "访问次数",
37+
loading: "加载中...",
38+
};
39+
3340
// 贡献者组件翻译
3441
export const contributors = {
3542
author: "作者",

.vitepress/data/lang/zh_tw.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export const nav: DefaultTheme.NavItem[] = [
3030
},
3131
];
3232

33+
// Umami 統計組件翻譯
34+
export const umamiStats = {
35+
views: "瀏覽量",
36+
visits: "訪問次數",
37+
loading: "載入中...",
38+
};
39+
3340
// 貢獻者組件翻譯
3441
export const contributors = {
3542
author: "作者",

.vitepress/theme/components/UmamiStats/UmamiStats.vue

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<script setup lang="ts">
2-
import { onMounted, ref } from "vue";
2+
import { computed, onMounted, ref } from "vue";
33
import { useData } from "vitepress";
4+
import { umamiStatsTranslations } from "../../../data/i18n";
45
56
const { lang } = useData();
67
const stats = ref<{ pageviews: number; visits: number } | null>(null);
78
const displayStats = ref({ pageviews: 0, visits: 0 });
89
9-
const t = {
10-
"zh-CN": { views: "浏览量", visits: "访问次数", loading: "加载中..." },
11-
en: { views: "Views", visits: "Visits", loading: "Loading..." },
12-
"zh-TW": { views: "瀏覽量", visits: "訪問次數", loading: "載入中..." },
13-
}[lang.value] ?? { views: "浏览量", visits: "访问次数", loading: "加载中..." };
10+
const t = computed(
11+
() =>
12+
umamiStatsTranslations[lang.value as keyof typeof umamiStatsTranslations] ??
13+
umamiStatsTranslations["zh-CN"],
14+
);
1415
1516
const CACHE_KEY = "umami-stats-cache";
1617
const formatNum = (n: number) =>
@@ -91,7 +92,7 @@ onMounted(async () => {
9192
<div class="umami-stats">
9293
<span v-if="!stats">{{ t.loading }}</span>
9394
<span v-else>
94-
<strong>{{ formatNum(displayStats.pageviews) }}</strong> {{ t.views }} ·
95+
<strong>{{ formatNum(displayStats.pageviews) }}</strong> {{ t.views }}
9596
<strong>{{ formatNum(displayStats.visits) }}</strong> {{ t.visits }}
9697
</span>
9798
</div>
@@ -102,8 +103,7 @@ onMounted(async () => {
102103
display: inline-flex;
103104
align-items: center;
104105
gap: 6px;
105-
padding: 4px 12px;
106-
font-size: 0.75rem;
106+
font-size: 0.8rem;
107107
color: var(--vp-c-text-2);
108108
font-variant-numeric: tabular-nums;
109109
}
@@ -114,9 +114,4 @@ onMounted(async () => {
114114
display: inline-block;
115115
text-align: right;
116116
}
117-
@media (max-width: 960px) {
118-
.umami-stats {
119-
display: none;
120-
}
121-
}
122117
</style>

.vitepress/theme/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ export default {
4040
skipText[lang.value] || skipText["zh-CN"],
4141
);
4242
},
43+
"nav-bar-content-after": () => {
44+
return h("div", { class: "nav-stats-center" }, h(UmamiStats));
45+
},
4346
"layout-bottom": () => {
4447
// 从 VitePress 获取当前语言
4548
const { lang } = useData();
4649
const currentLang = (lang.value || "zh-CN") as Lang;
4750
const footerData = getFooterData(currentLang);
48-
return h("div", [
49-
h("div", { class: "footer-stats" }, h(UmamiStats)),
50-
h(Footer, { Footer_Data: footerData }),
51-
]);
51+
return h(Footer, { Footer_Data: footerData });
5252
},
5353
});
5454
},

.vitepress/theme/style.css

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ iframe {
7575
left: 50%;
7676
top: 50%;
7777
transform: translate(-50%, -50%);
78-
}
79-
80-
.VPNavBar:not(.home.top) .nav-stats-center {
81-
opacity: 0;
82-
pointer-events: none;
78+
z-index: 1;
8379
}
8480

8581
:focus-visible {

0 commit comments

Comments
 (0)