valaxy-theme-yun 0.14.56 → 0.14.58

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.
@@ -1,8 +1,10 @@
1
1
  <script lang="ts" setup>
2
+ import { computed, nextTick } from 'vue'
2
3
  import type { PageData, Post } from 'valaxy'
3
- import { usePostTitle, useSiteConfig } from 'valaxy'
4
+ import { onContentUpdated, usePostTitle, useSiteConfig } from 'valaxy'
4
5
  import type { StyleValue } from 'vue'
5
- import { computed } from 'vue'
6
+ import { useRoute, useRouter } from 'vue-router'
7
+ import { scrollTo } from '../utils'
6
8
  import { usePostProperty } from '../composables'
7
9
 
8
10
  const props = defineProps<{
@@ -16,6 +18,59 @@ const { styles, icon, color } = usePostProperty(props.frontmatter.type)
16
18
  const title = usePostTitle(computed(() => props.frontmatter))
17
19
 
18
20
  const aside = computed(() => props.frontmatter.aside !== false)
21
+
22
+ const route = useRoute()
23
+ const router = useRouter()
24
+
25
+ nextTick(() => {
26
+ if (route.hash) {
27
+ setTimeout(() => {
28
+ scrollTo(document.body, route.hash, true)
29
+ }, 0)
30
+ }
31
+ })
32
+
33
+ onContentUpdated(() => {
34
+ // to extract
35
+ // click title scroll
36
+ window.addEventListener(
37
+ 'click',
38
+ async (e) => {
39
+ const link = (e.target as Element).closest('a')
40
+ if (link) {
41
+ const { protocol, hostname, pathname, hash, target } = link
42
+ const currentUrl = window.location
43
+ const extMatch = pathname.match(/\.\w+$/)
44
+ // only intercept inbound links
45
+ if (
46
+ !e.ctrlKey
47
+ && !e.shiftKey
48
+ && !e.altKey
49
+ && !e.metaKey
50
+ && target !== '_blank'
51
+ && protocol === currentUrl.protocol
52
+ && hostname === currentUrl.hostname
53
+ && !(extMatch && extMatch[0] !== '.html')
54
+ ) {
55
+ if (pathname === currentUrl.pathname) {
56
+ e.preventDefault()
57
+ // scroll between hash anchors in the same page
58
+ if (hash && hash !== currentUrl.hash) {
59
+ await router.push({ hash })
60
+ history.replaceState({ ...history.state }, '')
61
+
62
+ // still emit the event so we can listen to it in themes
63
+ window.dispatchEvent(new Event('hashchange'))
64
+ // use smooth scroll when clicking on header anchor links
65
+ scrollTo(link, hash, link.classList.contains('header-anchor'))
66
+ }
67
+ }
68
+ }
69
+ }
70
+ },
71
+ { capture: true },
72
+ )
73
+ })
19
74
  </script>
20
75
 
21
76
  <template>
@@ -2,8 +2,9 @@
2
2
  import { useThemeConfig } from 'valaxy'
3
3
  import { onMounted } from 'vue'
4
4
  import { createFireworks } from '@explosions/fireworks'
5
+ import type { YunTheme } from '../types'
5
6
 
6
- const themeConfig = useThemeConfig()
7
+ const themeConfig = useThemeConfig<YunTheme.Config>()
7
8
 
8
9
  onMounted(() => {
9
10
  createFireworks({
@@ -14,7 +14,7 @@ const { t } = useI18n()
14
14
  </script>
15
15
 
16
16
  <template>
17
- <div v-if="addonWaline && addonWaline.options" flex="~" text="sm" py="1">
17
+ <div v-if="addonWaline && addonWaline.options" flex="~" text="sm" my="1" h="5">
18
18
  <div v-if="addonWaline.options.pageview" inline-flex justify="center" items="center" mx="2" :title="t('post.pageview_count')">
19
19
  <div inline-flex i-ri-eye-line />
20
20
  <span ml-1 inline-flex class="waline-pageview-count" :data-path="route.path" />
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valaxy-theme-yun",
3
- "version": "0.14.56",
3
+ "version": "0.14.58",
4
4
  "author": {
5
5
  "email": "me@yunyoujun.cn",
6
6
  "name": "YunYouJun",
@@ -19,12 +19,12 @@
19
19
  "dependencies": {
20
20
  "@explosions/fireworks": "^0.0.2",
21
21
  "@iconify-json/ant-design": "^1.1.10",
22
- "@iconify-json/simple-icons": "^1.1.68",
22
+ "@iconify-json/simple-icons": "^1.1.69",
23
23
  "animejs": "^3.2.1"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/animejs": "^3.1.7",
27
- "valaxy": "0.14.56",
27
+ "valaxy": "0.14.58",
28
28
  "valaxy-addon-waline": "0.1.0"
29
29
  }
30
30
  }
package/utils/index.ts CHANGED
@@ -9,3 +9,35 @@ export function onImgError(e: Event, defaultImg = anonymousImage) {
9
9
  targetEl.setAttribute('data-src', targetEl.src)
10
10
  targetEl.src = defaultImg
11
11
  }
12
+
13
+ export function scrollTo(el: HTMLElement, hash: string, smooth = false) {
14
+ let target: Element | null = null
15
+ try {
16
+ target = el.classList.contains('header-anchor')
17
+ ? el
18
+ : ((decodeURIComponent(hash) && document.querySelector(decodeURIComponent(hash))) || null)
19
+ }
20
+ catch (e) {
21
+ console.warn(e)
22
+ }
23
+
24
+ if (target) {
25
+ const targetPadding = -64
26
+ const targetTop
27
+ = window.scrollY
28
+ + (target as HTMLElement).getBoundingClientRect().top
29
+ + targetPadding
30
+
31
+ // only smooth scroll if distance is smaller than screen height.
32
+ if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight) {
33
+ window.scrollTo(0, targetTop)
34
+ }
35
+ else {
36
+ window.scrollTo({
37
+ // left: 0,
38
+ top: targetTop,
39
+ behavior: 'smooth',
40
+ })
41
+ }
42
+ }
43
+ }