Commit 2c3587509d
Unsigned
Layout: unified · split
docs/.vitepress/theme/Layout.vue +3 −104
| @@ -7,12 +7,10 @@ | ||
| 7 | 7 | --> |
| 8 | 8 | <script setup lang="ts"> |
| 9 | 9 | import { |
| 10 | customStorageEventName, | |
| 11 | 10 | useEventListener, |
| 12 | 11 | useStorage, |
| 13 | 12 | useThrottleFn |
| 14 | 13 | } from '@vueuse/core' |
| 15 | import { usePreferredReducedMotion } from '@vueuse/core' | |
| 16 | 14 | import { getScrollOffset, useData, useRoute } from 'vitepress' |
| 17 | 15 | import type { DefaultTheme as Theme } from 'vitepress' |
| 18 | 16 | import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue' |
| @@ -35,9 +33,9 @@ import NotFoundComponent from './components/NotFound.vue' | ||
| 35 | 33 | import { NolebaseEnhancedReadabilitiesScreenMenu } from './components/settings' |
| 36 | 34 | import NavActions from './components/NavActions.vue' |
| 37 | 35 | import SidebarCard from './components/SidebarCard.vue' |
| 38 | import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey, TakodachiStorageKey } from './constants' | |
| 36 | import TakodachiChaser from './components/TakodachiChaser.vue' | |
| 37 | import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants' | |
| 39 | 38 | import { useEffects } from './composables/useEffects' |
| 40 | import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from './math' | |
| 41 | 39 | |
| 42 | 40 | const route = useRoute() |
| 43 | 41 | const { frontmatter, isDark, site, theme } = useData() |
| @@ -794,8 +792,6 @@ const breadcrumbs = computed(() => { | ||
| 794 | 792 | return buildBreadcrumbs(sidebar, currentPath) |
| 795 | 793 | }) |
| 796 | 794 | |
| 797 | // Respect user's reduced motion preferences | |
| 798 | const prefs = usePreferredReducedMotion() | |
| 799 | 795 | const { effectsEnabled } = useEffects() |
| 800 | 796 | |
| 801 | 797 | watchEffect(() => { |
| @@ -984,80 +980,6 @@ watch( | ||
| 984 | 980 | { immediate: true } |
| 985 | 981 | ) |
| 986 | 982 | |
| 987 | const speed = 2 | |
| 988 | const snapThreshold = 5 // px | |
| 989 | ||
| 990 | const takodachiRef = ref<HTMLImageElement | null>(null) | |
| 991 | let mousePos: Vec2D | null = null | |
| 992 | let target: Vec2D | null = null | |
| 993 | let offset: Vec2D = [0, 0] | |
| 994 | ||
| 995 | const position = ref({ | |
| 996 | x: 0, | |
| 997 | y: 0 | |
| 998 | }) | |
| 999 | ||
| 1000 | const 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 | ||
| 1008 | const takodachiDisable = ref<(() => void) | null>(null) | |
| 1009 | const takodachiToggledOn = useStorage(TakodachiStorageKey, false) | |
| 1010 | ||
| 1011 | const 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 | ||
| 1061 | 983 | function initTitleOnly() { |
| 1062 | 984 | document.querySelectorAll('.custom-block').forEach(block => { |
| 1063 | 985 | const children = [...block.children] |
| @@ -1368,18 +1290,6 @@ onMounted(() => { | ||
| 1368 | 1290 | ) return |
| 1369 | 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 | |
| 1385 | 1295 | onUnmounted(() => { |
| @@ -1388,7 +1298,6 @@ onUnmounted(() => { | ||
| 1388 | 1298 | tocMutationObserver?.disconnect() |
| 1389 | 1299 | cancelAnimationFrame(outlineFollowRaf) |
| 1390 | 1300 | cancelAnimationFrame(tocRaf) |
| 1391 | takodachiDisable.value && takodachiDisable.value() | |
| 1392 | 1301 | window.removeEventListener(BOOKMARK_CHANGE_EVENT, syncTocBookmarkStates) |
| 1393 | 1302 | }) |
| 1394 | 1303 | </script> |
| @@ -1396,17 +1305,7 @@ onUnmounted(() => { | ||
| 1396 | 1305 | <template> |
| 1397 | 1306 | <Layout> |
| 1398 | 1307 | <template #layout-top> |
| 1399 | <img | |
| 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 | /> | |
| 1308 | <TakodachiChaser /> | |
| 1410 | 1309 | <!-- Home sidebar menu --> |
| 1411 | 1310 | <template v-if="isHome"> |
| 1412 | 1311 | <Transition name="home-fade"> |
docs/.vitepress/theme/components/TakodachiChaser.vue added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | <script setup lang="ts"> | |
| 2 | import { useStorage } from '@vueuse/core' | |
| 3 | import { onMounted, onUnmounted, ref, watch } from 'vue' | |
| 4 | ||
| 5 | import { TakodachiStorageKey } from '../constants' | |
| 6 | import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from '../math' | |
| 7 | ||
| 8 | const speed = 2 | |
| 9 | const snapThreshold = 5 // px | |
| 10 | ||
| 11 | const takodachiRef = ref<HTMLImageElement | null>(null) | |
| 12 | let mousePos: Vec2D | null = null | |
| 13 | let target: Vec2D | null = null | |
| 14 | let offset: Vec2D = [0, 0] | |
| 15 | ||
| 16 | const position = ref({ x: 0, y: 0 }) | |
| 17 | ||
| 18 | const 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 | ||
| 26 | const takodachiToggledOn = useStorage(TakodachiStorageKey, false) | |
| 27 | let disable: (() => void) | null = null | |
| 28 | ||
| 29 | const 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 | ||
| 77 | watch(takodachiToggledOn, reload) | |
| 78 | ||
| 79 | onMounted(reload) | |
| 80 | onUnmounted(() => 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> | |