Commit 2c3587509d

2c3587509dbc7fd230908e733414a827c6cc9562

parent: d412924605

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-07-09 04:39 UTC

tako

Layout: unified · split

docs/.vitepress/theme/Layout.vue +3 −104
@@ -7,12 +7,10 @@
7--> 7-->
8<script setup lang="ts"> 8<script setup lang="ts">
9import { 9import {
10 customStorageEventName,
11 useEventListener, 10 useEventListener,
12 useStorage, 11 useStorage,
13 useThrottleFn 12 useThrottleFn
14} from '@vueuse/core' 13} from '@vueuse/core'
15import { usePreferredReducedMotion } from '@vueuse/core'
16import { getScrollOffset, useData, useRoute } from 'vitepress' 14import { getScrollOffset, useData, useRoute } from 'vitepress'
17import type { DefaultTheme as Theme } from 'vitepress' 15import type { DefaultTheme as Theme } from 'vitepress'
18import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue' 16import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue'
@@ -35,9 +33,9 @@ import NotFoundComponent from './components/NotFound.vue'
35import { NolebaseEnhancedReadabilitiesScreenMenu } from './components/settings' 33import { NolebaseEnhancedReadabilitiesScreenMenu } from './components/settings'
36import NavActions from './components/NavActions.vue' 34import NavActions from './components/NavActions.vue'
37import SidebarCard from './components/SidebarCard.vue' 35import SidebarCard from './components/SidebarCard.vue'
38import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey, TakodachiStorageKey } from './constants' 36import TakodachiChaser from './components/TakodachiChaser.vue'
37import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants'
39import { useEffects } from './composables/useEffects' 38import { useEffects } from './composables/useEffects'
40import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from './math'
41 39
42const route = useRoute() 40const route = useRoute()
43const { frontmatter, isDark, site, theme } = useData() 41const { frontmatter, isDark, site, theme } = useData()
@@ -794,8 +792,6 @@ const breadcrumbs = computed(() => {
794 return buildBreadcrumbs(sidebar, currentPath) 792 return buildBreadcrumbs(sidebar, currentPath)
795}) 793})
796 794
797// Respect user's reduced motion preferences
798const prefs = usePreferredReducedMotion()
799const { effectsEnabled } = useEffects() 795const { effectsEnabled } = useEffects()
800 796
801watchEffect(() => { 797watchEffect(() => {
@@ -984,80 +980,6 @@ watch(
984 { immediate: true } 980 { immediate: true }
985) 981)
986 982
987const speed = 2
988const snapThreshold = 5 // px
989
990const takodachiRef = ref<HTMLImageElement | null>(null)
991let mousePos: Vec2D | null = null
992let target: Vec2D | null = null
993let offset: Vec2D = [0, 0]
994
995const position = ref({
996 x: 0,
997 y: 0
998})
999
1000const updateMousePos = (e: MouseEvent | TouchEvent) => {
1001 if (e instanceof MouseEvent) {
1002 mousePos = [e.clientX, e.clientY]
1003 } else {
1004 mousePos = [e.touches[0].clientX, e.touches[0].clientY]
1005 }
1006}
1007
1008const takodachiDisable = ref<(() => void) | null>(null)
1009const takodachiToggledOn = useStorage(TakodachiStorageKey, false)
1010
1011const reloadTakodachi = () => {
1012 takodachiDisable.value?.()
1013
1014 if (prefs.value === 'reduce') return
1015 const saved = takodachiToggledOn
1016 if (saved.value === false) return
1017
1018 window.addEventListener('mousemove', updateMousePos)
1019 window.addEventListener('touchstart', updateMousePos)
1020
1021 const chaser = takodachiRef!.value
1022 if (chaser) {
1023 offset = v2smul([chaser.clientWidth, chaser.clientHeight], 0.5)
1024 setTimeout(() => {
1025 chaser.classList.remove('opacity-0')
1026 }, 1000)
1027 }
1028
1029 const intervalId = setInterval(() => {
1030 if (!mousePos) return
1031
1032 const currentPos: Vec2D = [
1033 position.value.x + offset[0],
1034 position.value.y + offset[1]
1035 ]
1036
1037 target = target ?? currentPos
1038
1039 const diff = v2sub(mousePos, currentPos)
1040
1041 target = v2mag(diff) < snapThreshold
1042 ? mousePos
1043 : v2add(target, v2smul(v2norm(diff), speed))
1044
1045 const finalVec = v2sub(target, offset)
1046
1047 position.value = { x: finalVec[0], y: finalVec[1] }
1048 }, 10)
1049
1050 takodachiDisable.value = () => {
1051 chaser?.classList.add('opacity-0')
1052
1053 window.removeEventListener('mousemove', updateMousePos)
1054 window.removeEventListener('touchstart', updateMousePos)
1055 clearInterval(intervalId)
1056
1057 takodachiDisable.value = null
1058 }
1059}
1060
1061function initTitleOnly() { 983function initTitleOnly() {
1062 document.querySelectorAll('.custom-block').forEach(block => { 984 document.querySelectorAll('.custom-block').forEach(block => {
1063 const children = [...block.children] 985 const children = [...block.children]
@@ -1368,18 +1290,6 @@ onMounted(() => {
1368 ) return 1290 ) return
1369 requestAnimationFrame(() => (hamburger as HTMLElement)?.click()) 1291 requestAnimationFrame(() => (hamburger as HTMLElement)?.click())
1370 }) 1292 })
1371
1372 // Storage changed in other documents.
1373 useEventListener(window, 'storage', () => {
1374 reloadTakodachi()
1375 })
1376
1377 // Storage changed in the same document.
1378 useEventListener(window, customStorageEventName, () => {
1379 reloadTakodachi()
1380 })
1381
1382 reloadTakodachi()
1383}) 1293})
1384 1294
1385onUnmounted(() => { 1295onUnmounted(() => {
@@ -1388,7 +1298,6 @@ onUnmounted(() => {
1388 tocMutationObserver?.disconnect() 1298 tocMutationObserver?.disconnect()
1389 cancelAnimationFrame(outlineFollowRaf) 1299 cancelAnimationFrame(outlineFollowRaf)
1390 cancelAnimationFrame(tocRaf) 1300 cancelAnimationFrame(tocRaf)
1391 takodachiDisable.value && takodachiDisable.value()
1392 window.removeEventListener(BOOKMARK_CHANGE_EVENT, syncTocBookmarkStates) 1301 window.removeEventListener(BOOKMARK_CHANGE_EVENT, syncTocBookmarkStates)
1393}) 1302})
1394</script> 1303</script>
@@ -1396,17 +1305,7 @@ onUnmounted(() => {
1396<template> 1305<template>
1397 <Layout> 1306 <Layout>
1398 <template #layout-top> 1307 <template #layout-top>
1399 <img 1308 <TakodachiChaser />
1400 id="takodachi"
1401 ref="takodachiRef"
1402 src="/takodachi.webp"
1403 alt="Takodachi"
1404 class="pointer-events-none fixed absolute z-[9999] h-10 w-10 opacity-0 transition-opacity duration-500"
1405 :style="{
1406 left: `${position.x}px`,
1407 top: `${position.y}px`
1408 }"
1409 />
1410 <!-- Home sidebar menu --> 1309 <!-- Home sidebar menu -->
1411 <template v-if="isHome"> 1310 <template v-if="isHome">
1412 <Transition name="home-fade"> 1311 <Transition name="home-fade">
docs/.vitepress/theme/components/TakodachiChaser.vue added +95
@@ -0,0 +1,95 @@
1<script setup lang="ts">
2import { useStorage } from '@vueuse/core'
3import { onMounted, onUnmounted, ref, watch } from 'vue'
4
5import { TakodachiStorageKey } from '../constants'
6import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from '../math'
7
8const speed = 2
9const snapThreshold = 5 // px
10
11const takodachiRef = ref<HTMLImageElement | null>(null)
12let mousePos: Vec2D | null = null
13let target: Vec2D | null = null
14let offset: Vec2D = [0, 0]
15
16const position = ref({ x: 0, y: 0 })
17
18const updateMousePos = (e: MouseEvent | TouchEvent) => {
19 if (e instanceof MouseEvent) {
20 mousePos = [e.clientX, e.clientY]
21 } else {
22 mousePos = [e.touches[0].clientX, e.touches[0].clientY]
23 }
24}
25
26const takodachiToggledOn = useStorage(TakodachiStorageKey, false)
27let disable: (() => void) | null = null
28
29const reload = () => {
30 disable?.()
31
32 if (!takodachiToggledOn.value) return
33
34 window.addEventListener('mousemove', updateMousePos)
35 window.addEventListener('touchstart', updateMousePos)
36
37 const chaser = takodachiRef.value
38 if (chaser) {
39 offset = v2smul([chaser.clientWidth, chaser.clientHeight], 0.5)
40 setTimeout(() => {
41 chaser.classList.remove('opacity-0')
42 }, 1000)
43 }
44
45 const intervalId = setInterval(() => {
46 if (!mousePos) return
47
48 const currentPos: Vec2D = [
49 position.value.x + offset[0],
50 position.value.y + offset[1]
51 ]
52
53 target = target ?? currentPos
54
55 const diff = v2sub(mousePos, currentPos)
56
57 target = v2mag(diff) < snapThreshold
58 ? mousePos
59 : v2add(target, v2smul(v2norm(diff), speed))
60
61 const finalVec = v2sub(target, offset)
62
63 position.value = { x: finalVec[0], y: finalVec[1] }
64 }, 10)
65
66 disable = () => {
67 chaser?.classList.add('opacity-0')
68
69 window.removeEventListener('mousemove', updateMousePos)
70 window.removeEventListener('touchstart', updateMousePos)
71 clearInterval(intervalId)
72
73 disable = null
74 }
75}
76
77watch(takodachiToggledOn, reload)
78
79onMounted(reload)
80onUnmounted(() => disable?.())
81</script>
82
83<template>
84 <img
85 id="takodachi"
86 ref="takodachiRef"
87 src="/takodachi.webp"
88 alt="Takodachi"
89 class="pointer-events-none fixed absolute z-[9999] h-10 w-10 opacity-0 transition-opacity duration-500"
90 :style="{
91 left: `${position.x}px`,
92 top: `${position.y}px`
93 }"
94 />
95</template>