@@ -7,7 +7,10 @@ import { emptyPost } from "./post/helper";
7
7
8
8
export const postsSymbol : InjectionKey < Post > = Symbol ( "posts" ) ;
9
9
10
- function createConfigProvider ( Layout : Component ) {
10
+ /**
11
+ * 创建 Layout 组件
12
+ */
13
+ const createConfigProvider = ( Layout : Component ) => {
11
14
return defineComponent ( {
12
15
name : "ConfigProvider" ,
13
16
setup ( _ , { slots } ) {
@@ -23,17 +26,23 @@ function createConfigProvider(Layout: Component) {
23
26
return ( ) => h ( Layout , null , slots ) ;
24
27
} ,
25
28
} ) ;
26
- }
29
+ } ;
27
30
28
31
export const configProvider = ( Layout : Component ) => {
29
32
return createConfigProvider ( Layout ) ;
30
33
} ;
31
34
35
+ /**
36
+ * 返回非响应式的 useDate() 对象
37
+ */
32
38
export const useUnrefData = ( ) => {
33
39
const { theme, frontmatter, site, page } = useData ( ) ;
34
40
return { theme : unref ( theme ) , frontmatter : unref ( frontmatter ) , site : unref ( site ) , page : unref ( page ) } ;
35
41
} ;
36
42
43
+ /**
44
+ * 返回全部 Posts 数据
45
+ */
37
46
export const useAllPosts = ( ) : Post => {
38
47
const { theme } = useData ( ) ;
39
48
const posts = unref ( theme ) . posts ;
@@ -43,41 +52,59 @@ export const useAllPosts = (): Post => {
43
52
return posts ;
44
53
} ;
45
54
55
+ /**
56
+ * 返回 Posts 数据,当处于多语言功能时,返回对应语言的 Posts 数据,否则返回全部 Posts 数据
57
+ */
46
58
export const usePosts = ( ) : Post => {
47
- const { theme, localeIndex } = useData ( ) ;
48
- const posts = unref ( theme ) . posts ;
49
-
50
- if ( ! posts ) return emptyPost ;
59
+ const { localeIndex } = useData ( ) ;
60
+ const posts = useAllPosts ( ) ;
51
61
52
62
// 兼容多语言功能,先从多语言下获取 posts 数据,获取不到说明没有使用多语言功能,则获取所有 posts 数据。因为多语言可以随时切换,因此使用 computed
53
63
return computed ( ( ) => posts . locales ?. [ unref ( localeIndex ) ] || posts ) ;
54
64
} ;
55
65
66
+ /**
67
+ * 是否为首页
68
+ */
56
69
export const isHomePage = ( ) => {
57
70
const { frontmatter } = useData ( ) ;
58
71
return ! isCategoriesPage ( ) && ! isTagsPage ( ) && unref ( frontmatter ) . layout === "home" ;
59
72
} ;
60
73
74
+ /**
75
+ * 是否为分类页
76
+ */
61
77
export const isCategoriesPage = ( ) => {
62
78
const { frontmatter } = useData ( ) ;
63
79
return unref ( frontmatter ) . categoriesPage ;
64
80
} ;
65
-
81
+ /**
82
+ * 是否为标签页
83
+ */
66
84
export const isTagsPage = ( ) => {
67
85
const { frontmatter } = useData ( ) ;
68
86
return unref ( frontmatter ) . tagsPage ;
69
87
} ;
70
88
89
+ /**
90
+ * 是否为归档页
91
+ */
71
92
export const isArchivesPage = ( ) => {
72
93
const { frontmatter } = useData ( ) ;
73
94
return unref ( frontmatter ) . archivesPage ;
74
95
} ;
75
96
97
+ /**
98
+ * 是否为目录页
99
+ */
76
100
export const isCataloguePage = ( ) => {
77
101
const { frontmatter } = useData ( ) ;
78
102
return unref ( frontmatter ) . catalogue ;
79
103
} ;
80
104
105
+ /**
106
+ * 获取默认背景色
107
+ */
81
108
export const getBgColor = ( ) => {
82
109
const { theme } = useData ( ) ;
83
110
return (
0 commit comments