docs/.vitepress/theme/Layout.vue

2e8fcec47e8029fe82d7098ac9dc5c01d4da2bfc
wotaku/docs/.vitepress/theme/Layout.vue history · blame · raw

241 lines · 6437 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 { TakodachiStorageKey } from './constants'
 28import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from './math'
 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 = v2mag(diff) < snapThreshold
134      ? mousePos
135      : v2add(target, v2smul(v2norm(diff), speed))
136
137    const finalVec = v2sub(target, offset)
138
139    position.value = { x: finalVec[0], y: finalVec[1] }
140  }, 10)
141
142  takodachiDisable.value = () => {
143    chaser?.classList.add('opacity-0')
144
145    window.removeEventListener('mousemove', updateMousePos)
146    window.removeEventListener('touchstart', updateMousePos)
147    clearInterval(intervalId)
148
149    takodachiDisable.value = null
150  }
151}
152
153onMounted(() => {
154  if (import.meta.env.SSR) return
155
156  // Storage changed in other documents.
157  useEventListener(window, 'storage', () => {
158    reloadTakodachi()
159  })
160
161  // Storage changed in the same document.
162  useEventListener(window, customStorageEventName, () => {
163    reloadTakodachi()
164  })
165
166  reloadTakodachi()
167})
168
169onUnmounted(() => {
170  takodachiDisable.value && takodachiDisable.value()
171})
172</script>
173
174<template>
175  <Layout>
176    <template #layout-top>
177      <img
178        id="takodachi"
179        ref="takodachiRef"
180        src="/takodachi.webp"
181        alt="Takodachi"
182        class="pointer-events-none fixed absolute z-[9999] h-10 w-10 opacity-0 transition-opacity duration-500"
183        :style="{
184          left: `${position.x}px`,
185          top: `${position.y}px`
186        }"
187      />
188    </template>
189    <template #doc-before>
190      <!-- Breadcrumb Navigation -->
191      <nav
192        v-if="breadcrumbs.length > 0"
193        class="breadcrumb mb-6 text-sm text-text-2"
194        aria-label="Breadcrumb"
195      >
196        <ol class="flex flex-wrap items-center gap-2">
197          <li>
198            <a href="/" class="hover:text-primary transition-colors">
199              <span class="i-lucide:home"></span>
200            </a>
201          </li>
202          <li
203            v-for="(crumb, index) in breadcrumbs"
204            :key="crumb.link || index"
205            class="flex items-center gap-2"
206          >
207            <span
208              class="i-lucide:chevron-right text-text-3 flex-shrink-0"
209            ></span>
210            <span
211              v-if="index === breadcrumbs.length - 1"
212              v-html="crumb.text"
213              class="font-medium text-text-1 break-words"
214            ></span>
215            <a
216              v-else
217              :href="crumb.link"
218              v-html="crumb.text"
219              class="hover:text-primary transition-colors break-words"
220            ></a>
221          </li>
222        </ol>
223      </nav>
224    </template>
225    <template #not-found>
226      <NotFoundComponent />
227    </template>
228    <template #sidebar-nav-after>
229      <SidebarCard />
230    </template>
231    <template #home-hero-info-before>
232      <AnnouncementPill />
233    </template>
234    <template #nav-bar-content-after>
235      <NolebaseEnhancedReadabilitiesMenu />
236    </template>
237    <template #nav-screen-content-after>
238      <NolebaseEnhancedReadabilitiesScreenMenu />
239    </template>
240  </Layout>
241</template>