Skip to content

Commit

Permalink
feat(page): support url hash for pagination
Browse files Browse the repository at this point in the history
close #19
  • Loading branch information
Octobug committed Feb 10, 2024
1 parent b2484aa commit 5a0249e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .vitepress/theme/components/HomeFeed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import type { ContentData } from "vitepress";
import { onContentUpdated, useData } from "vitepress";
import { ref } from "vue";
import { toDashedHash } from "../utils";
import { data as allPosts } from "../posts.data";
import PostList from "./PostList.vue";
Expand All @@ -49,6 +50,8 @@ const postList = ref<ContentData[]>([]);
postList.value = allPosts.slice(0, page.size);
function turnTo(n: number) {
window.location.hash = `#${toDashedHash(n.toString())}`;
n = Math.min(n, page.total);
page.cursor.value = n;
const start = (n - 1) * page.size;
postList.value = allPosts.slice(start, start + page.size);
Expand All @@ -60,8 +63,15 @@ function setPostListMinHeight() {
(<HTMLElement>root).style.setProperty("--post-list-min-height", `${h}rem`);
}
function loadPage() {
const defaultPage = parseInt(window.location.hash?.slice(1));
page.cursor.value = defaultPage || 1;
turnTo(page.cursor.value);
}
onContentUpdated(() => {
setPostListMinHeight();
loadPage();
});
</script>

Expand Down

0 comments on commit 5a0249e

Please sign in to comment.