docs/.vitepress/theme/Layout.vue
161 lines · 4430 bytes
1<!--
2 Copyright (c) 2024 taskylizard
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15-->
16<script setup lang="ts">
17import DefaultTheme from 'vitepress/theme'
18import { useEventListener, customStorageEventName } from '@vueuse/core'
19import SidebarCard from './components/SidebarCard.vue'
20import AnnouncementPill from './components/AnnouncementPill.vue'
21import NotFoundComponent from './components/NotFound.vue'
22import {
23 NolebaseEnhancedReadabilitiesMenu,
24 NolebaseEnhancedReadabilitiesScreenMenu
25} from '@nolebase/vitepress-plugin-enhanced-readabilities/client'
26import { usePreferredReducedMotion } from '@vueuse/core'
27import { ref, onMounted, onUnmounted } from 'vue'
28import { type Vec2D, v2add, v2mag, v2norm, v2smul, v2sub } from './math'
29
30// Respect user's reduced motion preferences
31const prefs = usePreferredReducedMotion()
32
33const speed = 2
34const snapThreshold = 5 // px
35
36const takodachiRef = ref<HTMLImageElement | null>(null)
37let mousePos: Vec2D | null = null
38let target: Vec2D | null = null
39let offset: Vec2D = [0, 0]
40
41const position = ref({
42 x: 0,
43 y: 0
44})
45
46const updateMousePos = (e: MouseEvent | TouchEvent) => {
47 if (e instanceof MouseEvent) {
48 mousePos = [e.clientX, e.clientY]
49 } else {
50 mousePos = [e.touches[0].clientX, e.touches[0].clientY]
51 }
52}
53
54const takodachiDisable = ref<(() => void) | null>(null)
55
56const reloadTakodachi = () => {
57 takodachiDisable.value && takodachiDisable.value()
58
59 if (prefs.value === 'reduce') return
60 const saved = localStorage.getItem('preference-takodachi-enable-disable')
61 if (saved === 'Disable') return
62
63 window.addEventListener('mousemove', updateMousePos)
64 window.addEventListener('touchstart', updateMousePos)
65
66 const chaser = takodachiRef!.value
67 if (chaser) {
68 offset = v2smul([chaser.clientWidth, chaser.clientHeight], 0.5)
69 setTimeout(() => {
70 chaser.classList.remove('opacity-0')
71 }, 1000)
72 }
73
74 const intervalId = setInterval(() => {
75 if (!mousePos) return
76
77 const currentPos: Vec2D = [
78 position.value.x + offset[0],
79 position.value.y + offset[1]
80 ]
81
82 target = target ?? currentPos
83
84 const diff = v2sub(mousePos, currentPos)
85
86 target =
87 v2mag(diff) < snapThreshold
88 ? mousePos
89 : v2add(target, v2smul(v2norm(diff), speed))
90
91 const finalVec = v2sub(target, offset)
92
93 position.value = { x: finalVec[0], y: finalVec[1] }
94 }, 10)
95
96 takodachiDisable.value = () => {
97 chaser?.classList.add('opacity-0')
98
99 window.removeEventListener('mousemove', updateMousePos)
100 window.removeEventListener('touchstart', updateMousePos)
101 clearInterval(intervalId)
102
103 takodachiDisable.value = null
104 }
105}
106
107onMounted(() => {
108 if (import.meta.env.SSR) return
109
110 // Storage changed in other documents.
111 useEventListener(window, 'storage', () => {
112 reloadTakodachi()
113 })
114
115 // Storage changed in the same document.
116 useEventListener(window, customStorageEventName, () => {
117 reloadTakodachi()
118 })
119
120 reloadTakodachi()
121})
122
123onUnmounted(() => {
124 takodachiDisable.value && takodachiDisable.value()
125})
126
127const { Layout } = DefaultTheme
128</script>
129
130<template>
131 <Layout>
132 <template #layout-top>
133 <img
134 id="takodachi"
135 ref="takodachiRef"
136 src="/takodachi.webp"
137 alt="Takodachi"
138 class="pointer-events-none fixed absolute z-[9999] h-10 w-10 opacity-0 transition-opacity duration-500"
139 :style="{
140 left: `${position.x}px`,
141 top: `${position.y}px`
142 }"
143 />
144 </template>
145 <template #not-found>
146 <NotFoundComponent />
147 </template>
148 <template #sidebar-nav-after>
149 <SidebarCard />
150 </template>
151 <template #home-hero-info-before>
152 <AnnouncementPill />
153 </template>
154 <template #nav-bar-content-after>
155 <NolebaseEnhancedReadabilitiesMenu />
156 </template>
157 <template #nav-bar-screen-content-after>
158 <NolebaseEnhancedReadabilitiesScreenMenu />
159 </template>
160 </Layout>
161</template>