-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLayout.vue
More file actions
214 lines (187 loc) · 5.88 KB
/
Layout.vue
File metadata and controls
214 lines (187 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<template>
<div class="opentiny-next-layout">
<!-- 自定义顶部导航栏 -->
<CustomHeader />
<!-- 主内容区域 -->
<div class="main-content">
<ThemeProvider :color-mode="colorMode">
<DefaultLayout>
<template #doc-before>
<div class="doc-title">
{{docTitle}}
</div>
</template>
</DefaultLayout>
</ThemeProvider>
</div>
</div>
</template>
<script setup lang="ts">
import DefaultTheme from 'vitepress/theme'
import { ThemeProvider } from '@opentiny/tiny-robot'
import { onMounted, onUnmounted, ref,computed,watch } from 'vue'
import { colorModeSubject } from './color-mode'
import CustomHeader from './components/CustomHeader.vue'
import { useData, useRoute, useRouter } from 'vitepress'
// 创建响应式的 colorMode 值
const colorMode = ref<'light' | 'dark' | 'auto'>('auto')
const docTitle = ref('指南')
// 获取 VitePress 数据
const { site,theme } = useData()
const route = useRoute()
const themeConfig = computed(() => theme.value)
const router = useRouter();
// 在客户端环境下同步 ColorModeSubject
if (typeof window !== 'undefined') {
let unsubscribe: (() => void) | undefined = undefined
onMounted(() => {
// 订阅 ColorModeSubject 的变化
unsubscribe = colorModeSubject.subscribe((mode) => {
colorMode.value = mode
})
})
// 组件卸载时取消订阅
onUnmounted(() => {
unsubscribe?.()
})
}
// 获取 VitePress 默认布局组件
const DefaultLayout = DefaultTheme.Layout
// 定义重定向映射
const redirectMap = [
{
patterns: ['/tiny-engine.html', '/tiny-engine/', '/tiny-engine/guide.html', '/tiny-engine/guide/'],
target: '/tiny-engine/guide/introduction'
},
{
patterns: ['/tiny-engine/dev/', '/tiny-engine/dev.html'],
target: '/tiny-engine/dev/dev-intro'
},
{
patterns: ['/tiny-engine/portal/', '/tiny-engine/portal.html'],
target: '/tiny-engine/portal/ecosystem-intro'
},
{
patterns: ['/tiny-vue.html', '/tiny-vue/', '/tiny-vue/guide.html', '/tiny-vue/guide/'],
target: '/tiny-vue/guide/introduce'
},
{
patterns: ['/tiny-robot.html', '/tiny-robot/', '/tiny-robot/guide.html', '/tiny-robot/guide/'],
target: '/tiny-robot/guide/quick-start'
},
{
patterns: ['/tiny-robot/examples/', '/tiny-robot/examples.html'],
target: '/tiny-robot/examples/assistant'
},
{
patterns: ['/next-sdk.html', '/next-sdk/'],
target: '/next-sdk/guide'
},
{
patterns: ['/genui-sdk.html', '/genui-sdk/', '/genui-sdk/guide.html', '/genui-sdk/guide/'],
target: '/genui-sdk/guide/installation'
}
];
// 将文档标题更新逻辑提取为独立函数,便于维护和测试
const updateDocTitle = () => {
const base = site.value?.base || '/'
const path = route.path.replace(new RegExp(`^${base}`), '/')
const cfg = themeConfig.value || {}
for (const { patterns, target } of redirectMap) {
if (patterns.includes(path)) {
router.go(target);
break;
}
}
// next-sdk / tiny-vue: 从对应 sidebar 的 guide 中寻找匹配项
if (path.includes('/next-sdk/') || path.includes('/tiny-vue/')) {
let sidebarConfig: any[] = []
if (path.includes('/next-sdk/')) {
sidebarConfig = cfg.sidebar?.['/next-sdk/guide/'] || []
} else if (path.includes('/tiny-vue/')) {
sidebarConfig = cfg.sidebar?.['/tiny-vue/guide/'] || []
}
if (!sidebarConfig || !sidebarConfig.length) {
docTitle.value = '指南'
return
}
const pathIndex = sidebarConfig.findIndex((child: any) =>
child.items?.some((item: any) => item?.link && path.includes(item.link))
)
docTitle.value = sidebarConfig[pathIndex >= 0 ? pathIndex : 0]?.text || '指南'
return
}
// tiny-robot: 从 nav 中匹配 activeMatch
if (path.includes('/tiny-robot/')) {
const navConfig = cfg.nav || []
if (!navConfig.length) {
docTitle.value = ''
return
}
const match = navConfig.find((item: any) => item?.activeMatch && path.includes(item.activeMatch))
docTitle.value = match?.text || ''
return
}
// tiny-engine: 需要先找到对应的 engineNav,然后查 sidebar 的二级或三级项
if (path.includes('/tiny-engine/')) {
const engineNavConfig = cfg.engineNav || []
if (!engineNavConfig.length) {
docTitle.value = ''
return
}
const activeNav = engineNavConfig.find((item: any) => item?.activeMatch && path.includes(item.activeMatch))
const engineSidebarConfig = cfg.sidebar?.[`/tiny-engine${activeNav?.activeMatch}`] || []
if (!engineSidebarConfig.length) {
docTitle.value = ''
return
}
let enginePathkey = 0
let engineDeepPathkey: number | null = null
engineSidebarConfig.forEach((child: any, key: number) => {
child.items?.forEach((item: any, deepKey: number) => {
if (item?.items?.length) {
const foundDeep = item.items.find((deepItem: any) => deepItem?.link && path.includes(deepItem.link))
if (foundDeep) {
enginePathkey = key
engineDeepPathkey = deepKey
}
} else if (item?.link && path.includes(item.link)) {
enginePathkey = key
}
})
})
if (engineDeepPathkey !== null) {
docTitle.value = engineSidebarConfig[enginePathkey].items?.[engineDeepPathkey]?.text || ''
} else {
docTitle.value = engineSidebarConfig[enginePathkey]?.text || ''
}
return
}
// 默认情况:清空标题
docTitle.value = ''
}
// 监听路由变更,初始化并同步标题
watch(
() => route.path,
updateDocTitle,
{ deep: true, immediate: true }
)
</script>
<style scoped>
.opentiny-next-layout {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.main-content {
flex: 1;
}
.doc-title{
color: #1476ff;
margin-top: 0.125rem;
margin-bottom: 10px;
font-weight: 600;
font-size: 0.875rem;
line-height: 1.25rem;
}
</style>