Skip to content

Commit eb20286

Browse files
author
石石
committed
feat: 修复布局异常
1 parent 561d379 commit eb20286

7 files changed

Lines changed: 93 additions & 12 deletions

File tree

packages/docs/.vitepress/theme/components/TryIt/Content.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<div class="lg:pl-[var(--vp-sidebar-width)] lg:pt-[var(--vp-nav-height)]">
3-
<div class="px-6 pb-6 rounded-2xl lg:mx-12 lg:mt-4">
2+
<div>
3+
<div class="pb-6 rounded-2xl mx-8 lg:mt-4">
44
<!-- Header -->
55
<div class="py-6">
66
<div class="flex items-center justify-between">

packages/docs/.vitepress/theme/composables/useBreadcrumb.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { computed } from 'vue'
22
import { useRoute } from 'vitepress'
33
import { useLayout } from 'vitepress/theme'
4+
import { useI18n } from 'vue-i18n'
5+
import { localePath } from '../utils/i18n'
46
import type { DefaultTheme } from 'vitepress'
57

68
export interface BreadcrumbItem {
@@ -11,6 +13,15 @@ export interface BreadcrumbItem {
1113
export function useBreadcrumb() {
1214
const route = useRoute()
1315
const { sidebar } = useLayout()
16+
const { t } = useI18n()
17+
18+
// 获取主页链接
19+
const getHomeItem = (): BreadcrumbItem => {
20+
return {
21+
text: t('breadcrumb.home'),
22+
link: localePath('/'),
23+
}
24+
}
1425

1526
// 递归查找匹配的面包屑路径
1627
const findBreadcrumbPath = (
@@ -59,11 +70,22 @@ export function useBreadcrumb() {
5970

6071
// 计算面包屑导航
6172
const breadcrumbItems = computed(() => {
73+
// 首先获取主页项
74+
const homeItem = getHomeItem()
75+
76+
// 如果当前就是主页,只显示主页
77+
const currentPath = route.path
78+
const normalizedCurrentPath = normalizePath(currentPath)
79+
const normalizedHomePath = normalizePath(homeItem.link || '/')
80+
81+
if (normalizedCurrentPath === normalizedHomePath || currentPath === homeItem.link) {
82+
return [homeItem]
83+
}
84+
6285
if (!sidebar.value || !route.path) {
63-
return []
86+
return [homeItem]
6487
}
6588

66-
const currentPath = route.path
6789
let sidebarItems: DefaultTheme.SidebarItem[] = []
6890

6991
// 处理不同的 sidebar 格式
@@ -87,11 +109,17 @@ export function useBreadcrumb() {
87109
}
88110

89111
if (sidebarItems.length === 0) {
90-
return []
112+
return [homeItem]
91113
}
92114

93115
const breadcrumbPath = findBreadcrumbPath(sidebarItems, currentPath)
94-
return breadcrumbPath || []
116+
117+
// 始终在面包屑路径前添加主页链接
118+
if (breadcrumbPath && breadcrumbPath.length > 0) {
119+
return [homeItem, ...breadcrumbPath]
120+
}
121+
122+
return [homeItem]
95123
})
96124

97125
return {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<script lang="ts" setup>
2+
import { useLayout } from 'vitepress/theme'
3+
4+
const { isHome, hasSidebar } = useLayout()
5+
</script>
6+
7+
<template>
8+
<div class="VPContent" :class="{ 'has-sidebar': hasSidebar, 'is-home': isHome }">
9+
<slot />
10+
</div>
11+
</template>
12+
13+
<style scoped>
14+
.VPContent {
15+
flex-grow: 1;
16+
flex-shrink: 0;
17+
margin: var(--vp-layout-top-height, 0px) auto 0;
18+
width: 100%;
19+
}
20+
21+
.VPContent.is-home {
22+
width: 100%;
23+
max-width: 100%;
24+
}
25+
26+
.VPContent.has-sidebar {
27+
margin: 0;
28+
}
29+
30+
@media (min-width: 960px) {
31+
.VPContent {
32+
padding-top: var(--vp-nav-height);
33+
}
34+
35+
.VPContent.has-sidebar {
36+
margin: var(--vp-layout-top-height, 0px) 0 0;
37+
padding-left: var(--vp-sidebar-width);
38+
}
39+
}
40+
41+
@media (min-width: 1440px) {
42+
.VPContent.has-sidebar {
43+
padding-right: calc((100vw - var(--vp-layout-max-width)) / 2);
44+
padding-left: calc((100vw - var(--vp-layout-max-width)) / 2 + var(--vp-sidebar-width));
45+
}
46+
}
47+
</style>

packages/docs/.vitepress/theme/layouts/LayoutInner.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { registerWatchers } from 'vitepress/dist/client/theme-default/composable
1313
import { useSidebarControl } from 'vitepress/dist/client/theme-default/composables/sidebar.js'
1414
import { useTryItMode } from '../composables'
1515
import TryItContent from '../components/TryIt/Content.vue'
16+
import Content from './Content.vue'
1617
import 'vitepress/theme'
1718
1819
const { isOpen: isSidebarOpen, open: openSidebar, close: closeSidebar } = useSidebarControl()
@@ -78,9 +79,11 @@ provide('hero-image-slot-exists', heroImageSlotExists)
7879
<template #aside-ads-before><slot name="aside-ads-before" /></template>
7980
<template #aside-ads-after><slot name="aside-ads-after" /></template>
8081
</VPContent>
81-
<ClientOnly v-else>
82-
<TryItContent />
83-
</ClientOnly>
82+
<Content v-else>
83+
<ClientOnly>
84+
<TryItContent />
85+
</ClientOnly>
86+
</Content>
8487

8588
<VPFooter />
8689
<slot name="layout-bottom" />

packages/docs/.vitepress/theme/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
"gymqj97uGGjyXSp99hh-o": "Product",
3535
"u7PCsvYbglQ4mhmnQhK3N": "Trade",
3636
"pABA78lHD--FSi39tpzwf": "Quotes",
37-
"tryIt.backToDoc": "Back to Docs"
37+
"tryIt.backToDoc": "Back to Docs",
38+
"breadcrumb.home": "Home"
3839
}

packages/docs/.vitepress/theme/locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
"gymqj97uGGjyXSp99hh-o": "品类",
3535
"u7PCsvYbglQ4mhmnQhK3N": "交易",
3636
"pABA78lHD--FSi39tpzwf": "行情",
37-
"tryIt.backToDoc": "返回文档"
37+
"tryIt.backToDoc": "返回文档",
38+
"breadcrumb.home": "主页"
3839
}

packages/docs/.vitepress/theme/locales/zh-HK.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
"gymqj97uGGjyXSp99hh-o": "品類",
3535
"u7PCsvYbglQ4mhmnQhK3N": "交易",
3636
"pABA78lHD--FSi39tpzwf": "行情",
37-
"tryIt.backToDoc": "返回文檔"
37+
"tryIt.backToDoc": "返回文檔",
38+
"breadcrumb.home": "主頁"
3839
}

0 commit comments

Comments
 (0)