@sugarat/theme 0.1.25 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sugarat/theme",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "description": "简约风的 Vitepress 博客主题,sugarat vitepress blog theme",
5
5
  "main": "src/index.ts",
6
6
  "exports": {
@@ -33,7 +33,11 @@ import { computed, watch } from 'vue'
33
33
  import { ElTag } from 'element-plus'
34
34
  import { useBrowserLocation, useDark } from '@vueuse/core'
35
35
  import { useRouter } from 'vitepress'
36
- import { useActiveTag, useArticles } from '../composables/config/blog'
36
+ import {
37
+ useActiveTag,
38
+ useArticles,
39
+ useCurrentPageNum
40
+ } from '../composables/config/blog'
37
41
 
38
42
  const docs = useArticles()
39
43
 
@@ -50,14 +54,18 @@ const isDark = useDark({
50
54
  const colorMode = computed(() => (isDark.value ? 'light' : 'dark'))
51
55
 
52
56
  const tagType: any = ['', 'info', 'success', 'warning', 'danger']
57
+ const currentPage = useCurrentPageNum()
53
58
 
54
59
  const handleCloseTag = () => {
55
60
  activeTag.value.label = ''
56
61
  activeTag.value.type = ''
62
+ currentPage.value = 1
57
63
  router.go(`${window.location.origin}${router.route.path}`)
58
64
  }
59
65
 
60
66
  const router = useRouter()
67
+ const location = useBrowserLocation()
68
+
61
69
  const handleTagClick = (tag: string, type: string) => {
62
70
  if (tag === activeTag.value.label) {
63
71
  handleCloseTag()
@@ -65,14 +73,14 @@ const handleTagClick = (tag: string, type: string) => {
65
73
  }
66
74
  activeTag.value.type = type
67
75
  activeTag.value.label = tag
68
-
76
+ currentPage.value = 1
69
77
  router.go(
70
- `${window.location.origin}${router.route.path}?tag=${tag}&type=${type}`
78
+ `${location.value.origin}${router.route.path}?tag=${tag}&type=${type}`
71
79
  )
72
80
  }
73
- const location = useBrowserLocation()
81
+
74
82
  watch(
75
- () => location.value,
83
+ location,
76
84
  () => {
77
85
  if (location.value.href) {
78
86
  const url = new URL(location.value.href!)
@@ -17,21 +17,24 @@
17
17
  v-if="wikiList.length >= pageSize"
18
18
  small
19
19
  background
20
- v-model:current-page="currentPage"
20
+ :current-page="currentPage"
21
+ @update:current-page="handleUpdatePageNum"
21
22
  :page-size="pageSize"
22
23
  :total="filterData.length"
23
24
  layout="prev, pager, next, jumper"
24
25
  />
25
26
  </template>
26
27
  <script setup lang="ts">
27
- import { computed, ref } from 'vue'
28
+ import { computed, watch } from 'vue'
28
29
  import { ElPagination } from 'element-plus'
29
- import { useData } from 'vitepress'
30
+ import { useData, useRouter } from 'vitepress'
31
+ import { useBrowserLocation } from '@vueuse/core'
30
32
  import BlogItem from './BlogItem.vue'
31
33
  import {
32
34
  useArticles,
33
35
  useActiveTag,
34
- useBlogConfig
36
+ useBlogConfig,
37
+ useCurrentPageNum
35
38
  } from '../composables/config/blog'
36
39
  import { Theme } from '../composables/config'
37
40
 
@@ -68,11 +71,43 @@ const { home } = useBlogConfig()
68
71
  const pageSize = computed(
69
72
  () => frontmatter.value.blog?.pageSize || home?.pageSize || 6
70
73
  )
71
- const currentPage = ref(1)
72
-
74
+ const currentPage = useCurrentPageNum()
73
75
  const currentWikiData = computed(() => {
74
76
  const startIdx = (currentPage.value - 1) * pageSize.value
75
77
  const endIdx = startIdx + pageSize.value
76
78
  return filterData.value.slice(startIdx, endIdx)
77
79
  })
80
+
81
+ const router = useRouter()
82
+ const location = useBrowserLocation()
83
+ const queryPageNumKey = 'pageNum'
84
+ const handleUpdatePageNum = (current: number) => {
85
+ if (currentPage.value === current) {
86
+ return
87
+ }
88
+ currentPage.value = current
89
+ const { searchParams } = new URL(window.location.href!)
90
+ searchParams.delete(queryPageNumKey)
91
+ searchParams.append(queryPageNumKey, String(current))
92
+ router.go(
93
+ `${location.value.origin}${router.route.path}?${searchParams.toString()}`
94
+ )
95
+ }
96
+
97
+ watch(
98
+ location,
99
+ () => {
100
+ if (location.value.href) {
101
+ const { searchParams } = new URL(location.value.href)
102
+ if (searchParams.has(queryPageNumKey)) {
103
+ currentPage.value = Number(searchParams.get(queryPageNumKey))
104
+ } else {
105
+ currentPage.value = 1
106
+ }
107
+ }
108
+ },
109
+ {
110
+ immediate: true
111
+ }
112
+ )
78
113
  </script>
@@ -16,6 +16,7 @@ const configSymbol: InjectionKey<Ref<Theme.Config>> = Symbol('theme-config')
16
16
 
17
17
  const activeTagSymbol: InjectionKey<Ref<Theme.activeTag>> = Symbol('active-tag')
18
18
 
19
+ const currentPageNum: InjectionKey<Ref<number>> = Symbol('home-page-num')
19
20
  const homeConfigSymbol: InjectionKey<Theme.HomeConfig> = Symbol('home-config')
20
21
 
21
22
  export function withConfigProvider(App: Component) {
@@ -39,6 +40,10 @@ export function withConfigProvider(App: Component) {
39
40
  type: ''
40
41
  })
41
42
  provide(activeTagSymbol, activeTag)
43
+
44
+ const pageNum = ref(1)
45
+ provide(currentPageNum, pageNum)
46
+
42
47
  return () => h(App, null, slots)
43
48
  }
44
49
  })
@@ -75,6 +80,9 @@ export function useArticles() {
75
80
  export function useActiveTag() {
76
81
  return inject(activeTagSymbol)!
77
82
  }
83
+ export function useCurrentPageNum() {
84
+ return inject(currentPageNum)!
85
+ }
78
86
 
79
87
  export function useCurrentArticle() {
80
88
  const blogConfig = useConfig()
@@ -1,63 +0,0 @@
1
- <script lang="ts" setup>
2
- import { useData, useRoute } from 'vitepress'
3
- import { computed, onMounted, ref, watch } from 'vue'
4
- import { useBlogConfig } from '../composables/config/blog'
5
-
6
- const routeData = useRoute()
7
- const { frontmatter } = useData()
8
- const { article } = useBlogConfig()
9
- const openLazy = computed(() => {
10
- return (frontmatter.value.lazy || article?.lazy) ?? true
11
- })
12
- const imageList = ref<HTMLImageElement[]>([])
13
- const currentObserver = ref<IntersectionObserver>()
14
- const refresh = () => {
15
- if (openLazy.value) {
16
- const docDomContainer = window.document.querySelector('#VPContent')
17
- const imgs = docDomContainer?.querySelectorAll<HTMLImageElement>(
18
- '.content-container .main img'
19
- )
20
- // 销毁旧的
21
- imageList.value.forEach((v) => currentObserver.value?.unobserve(v))
22
-
23
- // 存新的
24
- imageList.value = Array.from(imgs as unknown as HTMLImageElement[])
25
-
26
- // 初始化
27
- imageList.value.forEach((img) => {
28
- const src = img.getAttribute('src')
29
- if (src) {
30
- img.removeAttribute('src')
31
- img.setAttribute('data-src', src)
32
- }
33
- })
34
-
35
- const observer = new IntersectionObserver((entries) => {
36
- entries.forEach((entry) => {
37
- if (entry.isIntersecting) {
38
- const img = entry.target
39
- const src = img.getAttribute('data-src')
40
- if (src) {
41
- img.setAttribute('src', src)
42
- }
43
- observer.unobserve(img)
44
-
45
- console.log('render', src)
46
- }
47
- })
48
- })
49
-
50
- // 监听
51
- imageList.value.forEach((img) => {
52
- observer.observe(img)
53
- })
54
- }
55
- }
56
- watch(routeData, () => {
57
- refresh()
58
- })
59
-
60
- onMounted(() => {
61
- refresh()
62
- })
63
- </script>