1
1
<script setup lang="ts" name="HomePostList">
2
- import { computed , inject , reactive , unref } from " vue" ;
2
+ import { computed , inject , reactive , unref , ref , watch } from " vue" ;
3
3
import HomePostItem from " ./HomePostItem.vue" ;
4
4
import { postsSymbol } from " ../configProvider" ;
5
5
import Pagination from " ./Pagination.vue" ;
6
- import { useData } from " vitepress" ;
6
+ import { useData , useRoute } from " vitepress" ;
7
7
import { useDesign } from " ../hooks" ;
8
+ import { isHomePages , isCategoriesPages , isTagsPages } from " ../configProvider.ts" ;
8
9
9
10
const { getPrefixClass } = useDesign ();
10
11
const prefixClass = getPrefixClass (" post-list" );
@@ -16,15 +17,60 @@ const pageInfo = reactive({
16
17
pageNum: 1 ,
17
18
pageSizes: [10 , 20 , 50 , 100 , 200 ],
18
19
pageSize: unref (frontmatter ).tk ?.page ?.pageSize || 10 ,
19
- total: posts . sortPostsByDateAndSticky ?. length || 0 ,
20
+ total: 0 ,
20
21
});
21
22
22
- const pageOptions = computed (() => ({ size: " small" , ... unref (frontmatter ).tk ?.page }));
23
+ const pageOptions = { size: " small" , ... unref (frontmatter ).tk ?.page };
24
+
25
+ const route = useRoute ();
26
+ const category = ref (" " );
27
+ const tag = ref (" " );
28
+
29
+ watch (
30
+ route ,
31
+ () => {
32
+ const { searchParams } = new URL (window .location .href );
33
+
34
+ const pageNum = searchParams .get (" pageNum" ) || 1 ;
35
+ if (pageNum !== pageInfo .pageNum ) pageInfo .pageNum = pageNum ;
36
+
37
+ const {
38
+ data : { frontmatter },
39
+ } = route ;
40
+
41
+ if (frontmatter .categoriesPage || frontmatter .layout === " home" ) {
42
+ const c = searchParams .get (" category" ) || " " ;
43
+ if (c !== unref (category )) category .value = c ;
44
+ }
45
+
46
+ if (frontmatter .tagsPages || frontmatter .layout === " home" ) {
47
+ const t = searchParams .get (" tag" ) || " " ;
48
+ if (t !== unref (tag )) tag .value = t ;
49
+ }
50
+ },
51
+ { immediate: true }
52
+ );
23
53
24
54
const currentPosts = computed (() => {
25
55
const { pageNum, pageSize } = pageInfo ;
26
- return posts .sortPostsByDateAndSticky .slice ((pageNum - 1 ) * pageSize , pageNum * pageSize );
56
+
57
+ let post = posts .sortPostsByDateAndSticky ;
58
+ if (unref (category )) post = posts .groupPosts .categories [unref (category )];
59
+ else if (unref (tag )) post = posts .groupPosts .tags [unref (tag )];
60
+
61
+ pageInfo .total = post .length ;
62
+
63
+ return post .slice ((pageNum - 1 ) * pageSize , pageNum * pageSize );
27
64
});
65
+
66
+ const pageNumKey = " pageNum" ;
67
+ const handlePagination = () => {
68
+ const { searchParams } = new URL (window .location .href ! );
69
+ searchParams .delete (pageNumKey );
70
+ searchParams .append (pageNumKey , String (pageInfo .pageNum ));
71
+
72
+ window .history .pushState ({}, " " , ` ${window .location .pathname }?${searchParams .toString ()} ` );
73
+ };
28
74
</script >
29
75
30
76
<template >
@@ -40,6 +86,7 @@ const currentPosts = computed(() => {
40
86
v-if =" posts.sortPostsByDateAndSticky?.length >= pageInfo.pageSize"
41
87
v-model =" pageInfo"
42
88
v-bind =" pageOptions"
89
+ @pagination =" handlePagination"
43
90
/>
44
91
</div >
45
92
</ClientOnly >
0 commit comments