docs/.vitepress/theme/Layout.vue
219 lines · 6200 bytes
1<!--
2 All Rights Reserved
3
4 Copyright (c) 2025 taskylizard
5
6 All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
7-->
8<script setup lang="ts">
9import {
10 customStorageEventName,
11 useEventListener,
12 useStorage
13} from '@vueuse/core'
14import { usePreferredReducedMotion } from '@vueuse/core'
15import { useData, useRoute } from 'vitepress'
16import type { DefaultTheme as Theme } from 'vitepress'
17import DefaultTheme from 'vitepress/theme'
18import { computed, onMounted, onUnmounted, ref } from 'vue'
19import { sidebar } from '../configs/constants'
20import AnnouncementPill from './components/AnnouncementPill.vue'
21import NotFoundComponent from './components/NotFound.vue'
22import {
23 NolebaseEnhancedReadabilitiesMenu,
24 NolebaseEnhancedReadabilitiesScreenMenu
25} from './components/settings'
26import SidebarCard from './components/SidebarCard.vue'
27import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from './math'
28import { TakodachiStorageKey } from './constants'
29
30const route = useRoute()
31const { Layout } = DefaultTheme
32
33interface BreadcrumbItem {
34 text: string
35 link?: string
36}
37
38const buildBreadcrumbs = (
39 items: Theme.SidebarItem[],
40 currentPath: string
41): BreadcrumbItem[] => {
42 for (const item of items) {
43 // Check if this item matches the current path
44 if (item.link === currentPath) {
45 return [
46 {
47 text: item.text!.replace('mr-2 block w-4 h-4 bg-cover', '').trim(),
48 link: item.link
49 }
50 ]
51 }
52
53 // Check nested items
54 if (item.items) {
55 const nestedBreadcrumbs = buildBreadcrumbs(item.items, currentPath)
56 if (nestedBreadcrumbs.length > 0) {
57 return [
58 {
59 text: item.text!.replace('mr-2 block w-4 h-4 bg-cover', '').trim(),
60 link: item.link
61 },
62 ...nestedBreadcrumbs
63 ]
64 }
65 }
66 }
67 return []
68}
69
70const breadcrumbs = computed(() => {
71 const currentPath = route.path
72 // @ts-expect-error
73 return buildBreadcrumbs(sidebar, currentPath)
74})
75
76// Respect user's reduced motion preferences
77const prefs = usePreferredReducedMotion()
78
79const speed = 2
80const snapThreshold = 5 // px
81
82const takodachiRef = ref<HTMLImageElement | null>(null)
83let mousePos: Vec2D | null = null
84let target: Vec2D | null = null
85let offset: Vec2D = [0, 0]
86
87const position = ref({
88 x: 0,
89 y: 0
90})
91
92const updateMousePos = (e: MouseEvent | TouchEvent) => {
93 if (e instanceof MouseEvent) {
94 mousePos = [e.clientX, e.clientY]
95 } else {
96 mousePos = [e.touches[0].clientX, e.touches[0].clientY]
97 }
98}
99
100const takodachiDisable = ref<(() => void) | null>(null)
101const takodachiToggledOn = useStorage(TakodachiStorageKey, false)
102
103const reloadTakodachi = () => {
104 takodachiDisable.value?.()
105
106 if (prefs.value === 'reduce') return
107 const saved = takodachiToggledOn
108 if (saved.value === false) return
109
110 window.addEventListener('mousemove', updateMousePos)
111 window.addEventListener('touchstart', updateMousePos)
112
113 const chaser = takodachiRef!.value
114 if (chaser) {
115 offset = v2smul([chaser.clientWidth, chaser.clientHeight], 0.5)
116 setTimeout(() => {
117 chaser.classList.remove('opacity-0')
118 }, 1000)
119 }
120
121 const intervalId = setInterval(() => {
122 if (!mousePos) return
123
124 const currentPos: Vec2D = [
125 position.value.x + offset[0],
126 position.value.y + offset[1]
127 ]
128
129 target = target ?? currentPos
130
131 const diff = v2sub(mousePos, currentPos)
132
133 target =
134 v2mag(diff) < snapThreshold
135 ? mousePos
136 : v2add(target, v2smul(v2norm(diff), speed))
137
138 const finalVec = v2sub(target, offset)
139
140 position.value = { x: finalVec[0], y: finalVec[1] }
141 }, 10)
142
143 takodachiDisable.value = () => {
144 chaser?.classList.add('opacity-0')
145
146 window.removeEventListener('mousemove', updateMousePos)
147 window.removeEventListener('touchstart', updateMousePos)
148 clearInterval(intervalId)
149
150 takodachiDisable.value = null
151 }
152}
153
154onMounted(() => {
155 if (import.meta.env.SSR) return
156
157 // Storage changed in other documents.
158 useEventListener(window, 'storage', () => {
159 reloadTakodachi()
160 })
161
162 // Storage changed in the same document.
163 useEventListener(window, customStorageEventName, () => {
164 reloadTakodachi()
165 })
166
167 reloadTakodachi()
168})
169
170onUnmounted(() => {
171 takodachiDisable.value && takodachiDisable.value()
172})
173</script>
174
175<template>
176 <Layout>
177 <template #layout-top>
178 <img id="takodachi" ref="takodachiRef" src="/takodachi.webp" alt="Takodachi"
179 class="pointer-events-none fixed absolute z-[9999] h-10 w-10 opacity-0 transition-opacity duration-500" :style="{
180 left: `${position.x}px`,
181 top: `${position.y}px`
182 }" />
183 </template>
184 <template #doc-before>
185 <!-- Breadcrumb Navigation -->
186 <nav v-if="breadcrumbs.length > 0" class="breadcrumb mb-6 text-sm text-text-2" aria-label="Breadcrumb">
187 <ol class="flex flex-wrap items-center gap-2">
188 <li>
189 <a href="/" class="hover:text-primary transition-colors">
190 <span class="i-lucide:home"></span>
191 </a>
192 </li>
193 <li v-for="(crumb, index) in breadcrumbs" :key="crumb.link || index" class="flex items-center gap-2">
194 <span class="i-lucide:chevron-right text-text-3 flex-shrink-0"></span>
195 <span v-if="index === breadcrumbs.length - 1" v-html="crumb.text"
196 class="font-medium text-text-1 break-words"></span>
197 <a v-else :href="crumb.link" v-html="crumb.text"
198 class="hover:text-primary transition-colors break-words"></a>
199 </li>
200 </ol>
201 </nav>
202 </template>
203 <template #not-found>
204 <NotFoundComponent />
205 </template>
206 <template #sidebar-nav-after>
207 <SidebarCard />
208 </template>
209 <template #home-hero-info-before>
210 <AnnouncementPill />
211 </template>
212 <template #nav-bar-content-after>
213 <NolebaseEnhancedReadabilitiesMenu />
214 </template>
215 <template #nav-screen-content-after>
216 <NolebaseEnhancedReadabilitiesScreenMenu />
217 </template>
218 </Layout>
219</template>