Commit 15ed083ef1
Unsigned
Layout: unified · split
docs/.vitepress/plugins/urlSearchPlugin.ts +44
| @@ -29,6 +29,11 @@ export interface TabTarget { | ||
| 29 | 29 | tabs: string[] |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | export interface RandomLinkPage { | |
| 33 | pageId: string | |
| 34 | links: string[] | |
| 35 | } | |
| 36 | ||
| 32 | 37 | interface PageSearchMetadata { |
| 33 | 38 | links: PageLink[] |
| 34 | 39 | tabTargets: TabTarget[] |
| @@ -532,12 +537,43 @@ export function collectPageLinks(src: string, page: string) { | ||
| 532 | 537 | collectedByPage.set(pageId, extractSearchMetadataFromMarkdown(src, pageId)) |
| 533 | 538 | } |
| 534 | 539 | |
| 540 | function getRandomLinks(links: PageLink[]): RandomLinkPage[] { | |
| 541 | const byPage = new Map<string, Set<string>>() | |
| 542 | ||
| 543 | for (const link of links) { | |
| 544 | if (!/^https?:\/\//i.test(link.href)) continue | |
| 545 | const pageId = normalizePageId(link.pageId) | |
| 546 | let hrefs = byPage.get(pageId) | |
| 547 | if (!hrefs) { | |
| 548 | hrefs = new Set() | |
| 549 | byPage.set(pageId, hrefs) | |
| 550 | } | |
| 551 | hrefs.add(link.href) | |
| 552 | } | |
| 553 | ||
| 554 | return [...byPage.entries()] | |
| 555 | .sort(([a], [b]) => a.localeCompare(b)) | |
| 556 | .map(([pageId, hrefs]) => ({ pageId, links: [...hrefs] })) | |
| 557 | } | |
| 558 | ||
| 559 | function normalizePageId(pageId: string) { | |
| 560 | const trimmed = pageId.replace(/\/+$/, '') | |
| 561 | return trimmed || '/' | |
| 562 | } | |
| 563 | ||
| 535 | 564 | export function writeUrlSearchIndex(outDir: string) { |
| 536 | 565 | const links = getDedupedLinks() |
| 537 | 566 | const linksPath = join(outDir, 'url-search-index.json') |
| 538 | 567 | writeFileSync(linksPath, JSON.stringify(links), 'utf-8') |
| 539 | 568 | console.log(`[url-search] wrote ${links.length} links -> ${linksPath}`) |
| 540 | 569 | |
| 570 | const randomLinks = getRandomLinks(links) | |
| 571 | const randomLinksPath = join(outDir, 'random-links.json') | |
| 572 | writeFileSync(randomLinksPath, JSON.stringify(randomLinks), 'utf-8') | |
| 573 | console.log( | |
| 574 | `[url-search] wrote ${randomLinks.length} random pages -> ${randomLinksPath}` | |
| 575 | ) | |
| 576 | ||
| 541 | 577 | const tabTargets = getDedupedTabTargets() |
| 542 | 578 | const tabTargetsPath = join(outDir, 'tab-search-index.json') |
| 543 | 579 | writeFileSync(tabTargetsPath, JSON.stringify(tabTargets), 'utf-8') |
| @@ -558,6 +594,14 @@ export function urlSearchDevPlugin() { | ||
| 558 | 594 | res.end(JSON.stringify(deduped)) |
| 559 | 595 | } |
| 560 | 596 | ) |
| 597 | server.middlewares.use( | |
| 598 | '/random-links.json', | |
| 599 | (_req: any, res: any) => { | |
| 600 | const links = getRandomLinks(getDedupedLinks()) | |
| 601 | res.setHeader('Content-Type', 'application/json') | |
| 602 | res.end(JSON.stringify(links)) | |
| 603 | } | |
| 604 | ) | |
| 561 | 605 | server.middlewares.use( |
| 562 | 606 | '/tab-search-index.json', |
| 563 | 607 | (_req: any, res: any) => { |
docs/.vitepress/theme/components/NavActions.vue +116 −67
| @@ -3,14 +3,16 @@ import { Moon, Search, Settings, Sun } from 'lucide-vue-next' | ||
| 3 | 3 | import { useData } from 'vitepress' |
| 4 | 4 | // @ts-ignore |
| 5 | 5 | import VPSocialLink from 'vitepress/dist/client/theme-default/components/VPSocialLink.vue' |
| 6 | import { inject } from 'vue' | |
| 6 | import { computed, inject } from 'vue' | |
| 7 | 7 | |
| 8 | 8 | import { navActions } from '../navActions' |
| 9 | import { ensureNavItemsLoaded, isNavItemEnabled } from '../navItemsState' | |
| 9 | 10 | import { openSearch } from '../searchState' |
| 10 | 11 | import { openSettings } from '../settingsState' |
| 11 | import NolebaseEnhancedReadabilitiesMenu from './settings/Menu.vue' | |
| 12 | import BookmarksPanel from './BookmarksPanel.vue' | |
| 13 | 12 | import AnnouncementsPanel from './AnnouncementsPanel.vue' |
| 13 | import BookmarksPanel from './BookmarksPanel.vue' | |
| 14 | import NavRandomLink from './NavRandomLink.vue' | |
| 15 | import NolebaseEnhancedReadabilitiesMenu from './settings/Menu.vue' | |
| 14 | 16 | import SettingsPanel from './SettingsPanel.vue' |
| 15 | 17 | |
| 16 | 18 | const ICON_SIZE = 18 |
| @@ -20,60 +22,93 @@ const { isDark } = useData() | ||
| 20 | 22 | const toggleAppearance = inject<() => void>('toggle-appearance', () => { |
| 21 | 23 | isDark.value = !isDark.value |
| 22 | 24 | }) |
| 25 | ||
| 26 | ensureNavItemsLoaded() | |
| 27 | ||
| 28 | function isActionEnabled(action: (typeof navActions)[number]) { | |
| 29 | switch (action.type) { | |
| 30 | case 'announcement': | |
| 31 | return isNavItemEnabled('announcements') | |
| 32 | case 'bookmarks': | |
| 33 | return isNavItemEnabled('bookmarks') | |
| 34 | case 'random': | |
| 35 | return isNavItemEnabled('random') | |
| 36 | case 'link': | |
| 37 | return isNavItemEnabled('social') | |
| 38 | default: | |
| 39 | return true | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | const visibleNavActions = computed(() => navActions.filter(isActionEnabled)) | |
| 23 | 44 | </script> |
| 24 | 45 | |
| 25 | 46 | <template> |
| 26 | 47 | <div class="nav-actions-container"> |
| 27 | <div class="nav-actions" role="group" aria-label="Site actions"> | |
| 28 | <template v-for="(action, i) in navActions" :key="i"> | |
| 29 | <div class="nav-action" :class="`nav-action--${action.type}`"> | |
| 30 | <button | |
| 31 | v-if="action.type === 'search'" | |
| 32 | type="button" | |
| 33 | class="nav-action-btn" | |
| 34 | aria-label="Search" | |
| 35 | aria-keyshortcuts="/ control+k meta+k" | |
| 36 | @click="openSearch()" | |
| 37 | > | |
| 38 | <Search :size="ICON_SIZE" :stroke-width="ICON_STROKE" /> | |
| 39 | </button> | |
| 40 | ||
| 41 | <button | |
| 42 | v-else-if="action.type === 'appearance'" | |
| 43 | type="button" | |
| 44 | class="nav-action-btn" | |
| 45 | :aria-label="isDark ? 'Switch to light theme' : 'Switch to dark theme'" | |
| 46 | @click="toggleAppearance()" | |
| 47 | > | |
| 48 | <Moon :size="ICON_SIZE" :stroke-width="ICON_STROKE" class="nav-appearance-moon" /> | |
| 49 | <Sun :size="ICON_SIZE" :stroke-width="ICON_STROKE" class="nav-appearance-sun" /> | |
| 50 | </button> | |
| 51 | ||
| 52 | <VPSocialLink | |
| 53 | v-else-if="action.type === 'link'" | |
| 54 | :icon="action.icon" | |
| 55 | :link="action.link" | |
| 56 | :aria-label="action.label" | |
| 57 | :me="false" | |
| 58 | /> | |
| 59 | ||
| 60 | <template v-else-if="action.type === 'settings'"> | |
| 61 | <NolebaseEnhancedReadabilitiesMenu /> | |
| 48 | <div class="nav-actions" role="group" aria-label="Site actions"> | |
| 49 | <template | |
| 50 | v-for="action in visibleNavActions" | |
| 51 | :key="action.type === 'link' ? action.link : action.type" | |
| 52 | > | |
| 53 | <div class="nav-action" :class="`nav-action--${action.type}`"> | |
| 62 | 54 | <button |
| 55 | v-if="action.type === 'search'" | |
| 63 | 56 | type="button" |
| 64 | class="nav-action-btn nav-settings-mobile" | |
| 65 | aria-label="Settings" | |
| 66 | @click="openSettings()" | |
| 57 | class="nav-action-btn" | |
| 58 | aria-label="Search" | |
| 59 | aria-keyshortcuts="/ control+k meta+k" | |
| 60 | @click="openSearch()" | |
| 67 | 61 | > |
| 68 | <Settings :size="ICON_SIZE" :stroke-width="ICON_STROKE" /> | |
| 62 | <Search :size="ICON_SIZE" :stroke-width="ICON_STROKE" /> | |
| 69 | 63 | </button> |
| 70 | </template> | |
| 71 | <BookmarksPanel v-else-if="action.type === 'bookmarks'" /> | |
| 72 | <AnnouncementsPanel v-else-if="action.type === 'announcement'" /> | |
| 73 | </div> | |
| 74 | </template> | |
| 75 | </div> | |
| 76 | <SettingsPanel /> | |
| 64 | ||
| 65 | <button | |
| 66 | v-else-if="action.type === 'appearance'" | |
| 67 | type="button" | |
| 68 | class="nav-action-btn" | |
| 69 | :aria-label="isDark | |
| 70 | ? 'Switch to light theme' | |
| 71 | : 'Switch to dark theme'" | |
| 72 | @click="toggleAppearance()" | |
| 73 | > | |
| 74 | <Moon | |
| 75 | :size="ICON_SIZE" | |
| 76 | :stroke-width="ICON_STROKE" | |
| 77 | class="nav-appearance-moon" | |
| 78 | /> | |
| 79 | <Sun | |
| 80 | :size="ICON_SIZE" | |
| 81 | :stroke-width="ICON_STROKE" | |
| 82 | class="nav-appearance-sun" | |
| 83 | /> | |
| 84 | </button> | |
| 85 | ||
| 86 | <VPSocialLink | |
| 87 | v-else-if="action.type === 'link'" | |
| 88 | :icon="action.icon" | |
| 89 | :link="action.link" | |
| 90 | :aria-label="action.label" | |
| 91 | :me="false" | |
| 92 | /> | |
| 93 | ||
| 94 | <template v-else-if="action.type === 'settings'"> | |
| 95 | <NolebaseEnhancedReadabilitiesMenu /> | |
| 96 | <button | |
| 97 | type="button" | |
| 98 | class="nav-action-btn nav-settings-mobile" | |
| 99 | aria-label="Settings" | |
| 100 | @click="openSettings()" | |
| 101 | > | |
| 102 | <Settings :size="ICON_SIZE" :stroke-width="ICON_STROKE" /> | |
| 103 | </button> | |
| 104 | </template> | |
| 105 | <BookmarksPanel v-else-if="action.type === 'bookmarks'" /> | |
| 106 | <NavRandomLink v-else-if="action.type === 'random'" /> | |
| 107 | <AnnouncementsPanel v-else-if="action.type === 'announcement'" /> | |
| 108 | </div> | |
| 109 | </template> | |
| 110 | </div> | |
| 111 | <SettingsPanel /> | |
| 77 | 112 | </div> |
| 78 | 113 | </template> |
| 79 | 114 | |
| @@ -106,16 +141,18 @@ const toggleAppearance = inject<() => void>('toggle-appearance', () => { | ||
| 106 | 141 | justify-content: center; |
| 107 | 142 | } |
| 108 | 143 | |
| 109 | /* collapse the slot when announcements are disabled */ | |
| 110 | .nav-action--announcement:empty { | |
| 111 | display: none; | |
| 112 | } | |
| 113 | ||
| 114 | 144 | /* Social links move to the full-screen menu on phones. */ |
| 115 | 145 | .nav-action--link { |
| 116 | 146 | display: none; |
| 117 | 147 | } |
| 118 | 148 | |
| 149 | @media (max-width: 359.9px) { | |
| 150 | .nav-actions { | |
| 151 | --nav-pill-item-size: 1.625rem; | |
| 152 | --nav-pill-gap: 0; | |
| 153 | } | |
| 154 | } | |
| 155 | ||
| 119 | 156 | @media (min-width: 768px) { |
| 120 | 157 | .nav-action--link { |
| 121 | 158 | display: flex; |
| @@ -137,13 +174,15 @@ const toggleAppearance = inject<() => void>('toggle-appearance', () => { | ||
| 137 | 174 | height: 100%; |
| 138 | 175 | color: var(--vp-c-text-1); |
| 139 | 176 | opacity: 0.55; |
| 140 | transition: | |
| 141 | opacity 0.25s, | |
| 142 | transform 0.2s cubic-bezier(0.16, 1, 0.3, 1); | |
| 177 | transition: opacity 0.25s, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1); | |
| 143 | 178 | } |
| 144 | 179 | |
| 145 | .nav-action-btn:hover { opacity: 1; } | |
| 146 | .nav-action-btn:active { transform: scale(0.94); } | |
| 180 | .nav-action-btn:hover { | |
| 181 | opacity: 1; | |
| 182 | } | |
| 183 | .nav-action-btn:active { | |
| 184 | transform: scale(0.94); | |
| 185 | } | |
| 147 | 186 | |
| 148 | 187 | .nav-actions :deep(.VPSocialLink) { |
| 149 | 188 | width: 100% !important; |
| @@ -158,7 +197,9 @@ const toggleAppearance = inject<() => void>('toggle-appearance', () => { | ||
| 158 | 197 | color: var(--vp-c-text-1) !important; |
| 159 | 198 | opacity: 1; |
| 160 | 199 | } |
| 161 | .nav-actions :deep(.VPSocialLink:active) { transform: scale(0.94); } | |
| 200 | .nav-actions :deep(.VPSocialLink:active) { | |
| 201 | transform: scale(0.94); | |
| 202 | } | |
| 162 | 203 | .nav-actions :deep(.VPSocialLink > span) { |
| 163 | 204 | width: var(--nav-pill-icon-size) !important; |
| 164 | 205 | height: var(--nav-pill-icon-size) !important; |
| @@ -183,7 +224,8 @@ const toggleAppearance = inject<() => void>('toggle-appearance', () => { | ||
| 183 | 224 | } |
| 184 | 225 | } |
| 185 | 226 | |
| 186 | .nav-action--settings :deep(.VPNolebaseEnhancedReadabilitiesMenuFlyout .button) { | |
| 227 | .nav-action--settings | |
| 228 | :deep(.VPNolebaseEnhancedReadabilitiesMenuFlyout .button) { | |
| 187 | 229 | width: 100% !important; |
| 188 | 230 | height: 100% !important; |
| 189 | 231 | padding: 0 !important; |
| @@ -196,10 +238,12 @@ const toggleAppearance = inject<() => void>('toggle-appearance', () => { | ||
| 196 | 238 | opacity 0.25s, |
| 197 | 239 | transform 0.2s cubic-bezier(0.16, 1, 0.3, 1) !important; |
| 198 | 240 | } |
| 199 | .nav-action--settings :deep(.VPNolebaseEnhancedReadabilitiesMenuFlyout:hover .button) { | |
| 241 | .nav-action--settings | |
| 242 | :deep(.VPNolebaseEnhancedReadabilitiesMenuFlyout:hover .button) { | |
| 200 | 243 | opacity: 1; |
| 201 | 244 | } |
| 202 | .nav-action--settings :deep(.VPNolebaseEnhancedReadabilitiesMenuFlyout .button:active) { | |
| 245 | .nav-action--settings | |
| 246 | :deep(.VPNolebaseEnhancedReadabilitiesMenuFlyout .button:active) { | |
| 203 | 247 | transform: scale(0.94); |
| 204 | 248 | } |
| 205 | 249 | |
| @@ -212,11 +256,16 @@ const toggleAppearance = inject<() => void>('toggle-appearance', () => { | ||
| 212 | 256 | height: var(--nav-pill-icon-size); |
| 213 | 257 | } |
| 214 | 258 | |
| 215 | .nav-action--settings :deep(.vpi-chevron-down) { display: none; } | |
| 216 | ||
| 259 | .nav-action--settings :deep(.vpi-chevron-down) { | |
| 260 | display: none; | |
| 261 | } | |
| 217 | 262 | </style> |
| 218 | 263 | |
| 219 | 264 | <style> |
| 220 | html.dark .nav-appearance-sun { display: none; } | |
| 221 | html:not(.dark) .nav-appearance-moon { display: none; } | |
| 265 | html.dark .nav-appearance-sun { | |
| 266 | display: none; | |
| 267 | } | |
| 268 | html:not(.dark) .nav-appearance-moon { | |
| 269 | display: none; | |
| 270 | } | |
| 222 | 271 | </style> |
docs/.vitepress/theme/components/NavRandomLink.vue added +123
| @@ -0,0 +1,123 @@ | ||
| 1 | <script setup lang="ts"> | |
| 2 | import { Dice5 } from 'lucide-vue-next' | |
| 3 | import { onBeforeUnmount, onMounted } from 'vue' | |
| 4 | import type { RandomLinkPage } from '../../plugins/urlSearchPlugin' | |
| 5 | import { | |
| 6 | disabledPages, | |
| 7 | ensureDisabledPagesLoaded, | |
| 8 | normalizePageId | |
| 9 | } from '../randomLinksState' | |
| 10 | ||
| 11 | const ICON_SIZE = 18 | |
| 12 | const ICON_STROKE = 2 | |
| 13 | const RANDOM_LINKS_URL = '/random-links.json' | |
| 14 | ||
| 15 | let pages: RandomLinkPage[] | null = null | |
| 16 | let pagesPromise: Promise<RandomLinkPage[]> | null = null | |
| 17 | let idleHandle: number | undefined | |
| 18 | ||
| 19 | function loadPages() { | |
| 20 | if (pages) return Promise.resolve(pages) | |
| 21 | if (pagesPromise) return pagesPromise | |
| 22 | ||
| 23 | pagesPromise = fetch(RANDOM_LINKS_URL) | |
| 24 | .then((res) => { | |
| 25 | if (!res.ok) throw new Error(`HTTP ${res.status}`) | |
| 26 | return res.json() | |
| 27 | }) | |
| 28 | .then((data: RandomLinkPage[]) => { | |
| 29 | pages = data.filter( | |
| 30 | (page) => | |
| 31 | !!page && | |
| 32 | typeof page.pageId === 'string' && | |
| 33 | Array.isArray(page.links) | |
| 34 | ) | |
| 35 | return pages | |
| 36 | }) | |
| 37 | .catch(() => { | |
| 38 | pages = [] | |
| 39 | return pages | |
| 40 | }) | |
| 41 | .finally(() => { | |
| 42 | pagesPromise = null | |
| 43 | }) | |
| 44 | ||
| 45 | return pagesPromise | |
| 46 | } | |
| 47 | ||
| 48 | function pick(list: string[]) { | |
| 49 | return list[Math.floor(Math.random() * list.length)] | |
| 50 | } | |
| 51 | ||
| 52 | function enabledLinks() { | |
| 53 | const disabled = disabledPages.value | |
| 54 | const hrefs = new Set<string>() | |
| 55 | ||
| 56 | for (const page of pages ?? []) { | |
| 57 | if (disabled.has(normalizePageId(page.pageId))) continue | |
| 58 | for (const href of page.links) { | |
| 59 | if (typeof href === 'string' && /^https?:\/\//i.test(href)) { | |
| 60 | hrefs.add(href) | |
| 61 | } | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | return [...hrefs] | |
| 66 | } | |
| 67 | ||
| 68 | function openRandom() { | |
| 69 | const tab = window.open('about:blank', '_blank') | |
| 70 | if (!tab) return | |
| 71 | tab.opener = null | |
| 72 | ||
| 73 | const links = enabledLinks() | |
| 74 | if (links.length) { | |
| 75 | tab.location.replace(pick(links)) | |
| 76 | return | |
| 77 | } | |
| 78 | ||
| 79 | loadPages().then(() => { | |
| 80 | const available = enabledLinks() | |
| 81 | if (available.length) { | |
| 82 | tab.location.replace(pick(available)) | |
| 83 | } else { | |
| 84 | tab.close() | |
| 85 | } | |
| 86 | }) | |
| 87 | } | |
| 88 | ||
| 89 | onMounted(() => { | |
| 90 | ensureDisabledPagesLoaded() | |
| 91 | ||
| 92 | const preload = () => { | |
| 93 | ensureDisabledPagesLoaded() | |
| 94 | void loadPages() | |
| 95 | } | |
| 96 | ||
| 97 | if ('requestIdleCallback' in window) { | |
| 98 | idleHandle = window.requestIdleCallback(preload, { timeout: 5000 }) | |
| 99 | } else { | |
| 100 | idleHandle = window.setTimeout(preload, 2000) | |
| 101 | } | |
| 102 | }) | |
| 103 | ||
| 104 | onBeforeUnmount(() => { | |
| 105 | if (idleHandle === undefined) return | |
| 106 | if ('cancelIdleCallback' in window) window.cancelIdleCallback(idleHandle) | |
| 107 | else clearTimeout(idleHandle) | |
| 108 | }) | |
| 109 | </script> | |
| 110 | ||
| 111 | <template> | |
| 112 | <button | |
| 113 | type="button" | |
| 114 | class="nav-action-btn" | |
| 115 | aria-label="Open a random link in a new tab" | |
| 116 | title="Random links" | |
| 117 | @click="openRandom" | |
| 118 | @pointerenter="loadPages" | |
| 119 | @focus="loadPages" | |
| 120 | > | |
| 121 | <Dice5 :size="ICON_SIZE" :stroke-width="ICON_STROKE" aria-hidden="true" /> | |
| 122 | </button> | |
| 123 | </template> | |
docs/.vitepress/theme/components/settings/Announcements.vue deleted −61
| @@ -1,61 +0,0 @@ | ||
| 1 | <script setup lang="ts"> | |
| 2 | import { NuInputHighlight, NuInputHorizontalRadioGroup } from '@nolebase/ui' | |
| 3 | import { useStorage } from '@vueuse/core' | |
| 4 | import { computed, ref } from 'vue' | |
| 5 | ||
| 6 | import MenuHelp from './MenuHelp.vue' | |
| 7 | import MenuTitle from './MenuTitle.vue' | |
| 8 | ||
| 9 | import { AnnouncementsEnabledKey } from '../../constants' | |
| 10 | ||
| 11 | const menuTitleElementRef = ref<HTMLDivElement>() | |
| 12 | const isMenuHelpPoppedUp = ref(false) | |
| 13 | ||
| 14 | const announcements = useStorage(AnnouncementsEnabledKey, 'on') | |
| 15 | ||
| 16 | const fieldOptions = computed(() => [ | |
| 17 | { | |
| 18 | value: 'on', | |
| 19 | title: 'On', | |
| 20 | ariaLabel: 'On', | |
| 21 | text: 'ON', | |
| 22 | name: 'Announcements' | |
| 23 | }, | |
| 24 | { | |
| 25 | value: 'off', | |
| 26 | title: 'Off', | |
| 27 | ariaLabel: 'Off', | |
| 28 | text: 'OFF', | |
| 29 | name: 'Announcements' | |
| 30 | } | |
| 31 | ]) | |
| 32 | </script> | |
| 33 | ||
| 34 | <template> | |
| 35 | <div space-y-2 role="radiogroup"> | |
| 36 | <div ref="menuTitleElementRef" relative flex items-center> | |
| 37 | <MenuTitle title="Announcements" aria-label="Announcements" flex="1" pr-4> | |
| 38 | <template #icon> | |
| 39 | <span i-lucide:megaphone mr-1 aria-hidden="true" /> | |
| 40 | </template> | |
| 41 | </MenuTitle> | |
| 42 | <MenuHelp | |
| 43 | v-model:is-popped-up="isMenuHelpPoppedUp" | |
| 44 | :menu-title-element-ref="menuTitleElementRef" | |
| 45 | > | |
| 46 | <h4 text-md mb-1 font-semibold>Announcements</h4> | |
| 47 | <p text="sm" mb-2 max-w-100> | |
| 48 | Shows the megaphone in the navbar with news about new sections, components, and improvements. | |
| 49 | </p> | |
| 50 | </MenuHelp> | |
| 51 | </div> | |
| 52 | <NuInputHighlight :active="isMenuHelpPoppedUp" class="rounded-md"> | |
| 53 | <NuInputHorizontalRadioGroup | |
| 54 | v-model="announcements" | |
| 55 | bg="$vp-nolebase-enhanced-readabilities-menu-background-color" | |
| 56 | text="sm $vp-nolebase-enhanced-readabilities-menu-text-color" | |
| 57 | :options="fieldOptions" | |
| 58 | /> | |
| 59 | </NuInputHighlight> | |
| 60 | </div> | |
| 61 | </template> | |
docs/.vitepress/theme/components/settings/Menu.vue +10 −4
| @@ -3,9 +3,10 @@ import { useMounted } from '@vueuse/core' | ||
| 3 | 3 | |
| 4 | 4 | import AccentBackground from './AccentBackground.vue' |
| 5 | 5 | import AccentColor from './AccentColor.vue' |
| 6 | import Announcements from './Announcements.vue' | |
| 7 | 6 | import Effects from './Effects.vue' |
| 8 | 7 | import Favicons from './Favicons.vue' |
| 8 | import NavItems from './NavItems.vue' | |
| 9 | import RandomLinks from './RandomLinks.vue' | |
| 9 | 10 | import SettingsFlyout from './SettingsFlyout.vue' |
| 10 | 11 | |
| 11 | 12 | const mounted = useMounted() |
| @@ -24,19 +25,24 @@ const mounted = useMounted() | ||
| 24 | 25 | <AccentBackground /> |
| 25 | 26 | <Effects /> |
| 26 | 27 | <Favicons /> |
| 27 | <Announcements /> | |
| 28 | <RandomLinks /> | |
| 29 | <NavItems /> | |
| 28 | 30 | </div> |
| 29 | 31 | </SettingsFlyout> |
| 30 | 32 | </template> |
| 31 | 33 | |
| 32 | 34 | <style> |
| 33 | 35 | .VPNolebaseEnhancedReadabilitiesMenu { |
| 34 | --vp-nolebase-enhanced-readabilities-menu-background-color: var(--wk-c-menu-bg); | |
| 36 | --vp-nolebase-enhanced-readabilities-menu-background-color: var( | |
| 37 | --wk-c-menu-bg | |
| 38 | ); | |
| 35 | 39 | --vp-nolebase-enhanced-readabilities-menu-text-color: var(--vp-c-text-1); |
| 36 | 40 | } |
| 37 | 41 | |
| 38 | 42 | .dark .VPNolebaseEnhancedReadabilitiesMenu { |
| 39 | --vp-nolebase-enhanced-readabilities-menu-background-color: var(--wk-c-menu-bg); | |
| 43 | --vp-nolebase-enhanced-readabilities-menu-background-color: var( | |
| 44 | --wk-c-menu-bg | |
| 45 | ); | |
| 40 | 46 | --vp-nolebase-enhanced-readabilities-menu-text-color: var(--vp-c-text-1); |
| 41 | 47 | } |
| 42 | 48 | |
docs/.vitepress/theme/components/settings/NavItems.vue added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | <script setup lang="ts"> | |
| 2 | import { computed, ref } from 'vue' | |
| 3 | ||
| 4 | import { | |
| 5 | ensureNavItemsLoaded, | |
| 6 | isNavItemEnabled, | |
| 7 | toggleNavItem | |
| 8 | } from '../../navItemsState' | |
| 9 | import type { NavItemKey } from '../../navItemsState' | |
| 10 | import MenuHelp from './MenuHelp.vue' | |
| 11 | import MenuTitle from './MenuTitle.vue' | |
| 12 | import SettingsSelect from './SettingsSelect.vue' | |
| 13 | ||
| 14 | const items: Array<{ key: NavItemKey; title: string; icon: string }> = [ | |
| 15 | { key: 'bookmarks', title: 'Bookmarks', icon: 'i-lucide:bookmark' }, | |
| 16 | { key: 'random', title: 'Random links', icon: 'i-lucide:dice-5' }, | |
| 17 | { key: 'announcements', title: 'Announcements', icon: 'i-lucide:megaphone' }, | |
| 18 | { key: 'social', title: 'External links', icon: 'i-lucide:link' } | |
| 19 | ] | |
| 20 | ||
| 21 | const menuTitleElementRef = ref<HTMLDivElement>() | |
| 22 | const isMenuHelpPoppedUp = ref(false) | |
| 23 | ||
| 24 | ensureNavItemsLoaded() | |
| 25 | ||
| 26 | const rows = computed(() => | |
| 27 | items.map((item) => ({ ...item, enabled: isNavItemEnabled(item.key) })) | |
| 28 | ) | |
| 29 | </script> | |
| 30 | ||
| 31 | <template> | |
| 32 | <div class="nav-items" space-y-2> | |
| 33 | <div ref="menuTitleElementRef" relative flex items-center> | |
| 34 | <MenuTitle title="Menu items" aria-label="Menu items" flex="1" pr-4> | |
| 35 | <template #icon> | |
| 36 | <span i-lucide:panel-top mr-1 aria-hidden="true" /> | |
| 37 | </template> | |
| 38 | </MenuTitle> | |
| 39 | <MenuHelp | |
| 40 | v-model:is-popped-up="isMenuHelpPoppedUp" | |
| 41 | :menu-title-element-ref="menuTitleElementRef" | |
| 42 | > | |
| 43 | <h4 text-md mb-1 font-semibold>Menu items</h4> | |
| 44 | <p text="sm" mb-2 max-w-100> | |
| 45 | Choose which actions appear in the navbar. | |
| 46 | </p> | |
| 47 | </MenuHelp> | |
| 48 | </div> | |
| 49 | ||
| 50 | <SettingsSelect | |
| 51 | label="Select items" | |
| 52 | aria-label="Navbar items" | |
| 53 | :active="isMenuHelpPoppedUp" | |
| 54 | > | |
| 55 | <button | |
| 56 | v-for="item in rows" | |
| 57 | :key="item.key" | |
| 58 | type="button" | |
| 59 | class="settings-select-row" | |
| 60 | :class="{ 'is-disabled': !item.enabled }" | |
| 61 | :aria-pressed="item.enabled" | |
| 62 | :title="`${item.enabled ? 'Disable' : 'Enable'} ${item.title}`" | |
| 63 | @click="toggleNavItem(item.key)" | |
| 64 | > | |
| 65 | <span | |
| 66 | :class="item.icon" | |
| 67 | class="settings-select-row-icon" | |
| 68 | aria-hidden="true" | |
| 69 | /> | |
| 70 | <span class="settings-select-row-title">{{ item.title }}</span> | |
| 71 | <span | |
| 72 | class="settings-select-row-eye" | |
| 73 | :class="item.enabled ? 'i-lucide:eye' : 'i-lucide:eye-off'" | |
| 74 | aria-hidden="true" | |
| 75 | /> | |
| 76 | </button> | |
| 77 | </SettingsSelect> | |
| 78 | </div> | |
| 79 | </template> | |
docs/.vitepress/theme/components/settings/RandomLinks.vue added +85
| @@ -0,0 +1,85 @@ | ||
| 1 | <script setup lang="ts"> | |
| 2 | import { computed, ref } from 'vue' | |
| 3 | ||
| 4 | import { | |
| 5 | ensureDisabledPagesLoaded, | |
| 6 | isGroupDisabled, | |
| 7 | randomPageGroups, | |
| 8 | toggleGroup | |
| 9 | } from '../../randomLinksState' | |
| 10 | import MenuHelp from './MenuHelp.vue' | |
| 11 | import MenuTitle from './MenuTitle.vue' | |
| 12 | import SettingsSelect from './SettingsSelect.vue' | |
| 13 | ||
| 14 | const ICON_PREFIXES = | |
| 15 | /^(lucide|mdi|ic|mingcute|material-symbols|simple-icons|uil|octicon|logos|akar-icons|map|bi|streamline-logos|streamline-ultimate|streamline-sharp|iconoir)-/ | |
| 16 | ||
| 17 | const menuTitleElementRef = ref<HTMLDivElement>() | |
| 18 | const isMenuHelpPoppedUp = ref(false) | |
| 19 | ||
| 20 | ensureDisabledPagesLoaded() | |
| 21 | ||
| 22 | const rows = computed(() => | |
| 23 | randomPageGroups.map((group) => ({ | |
| 24 | group, | |
| 25 | disabled: isGroupDisabled(group), | |
| 26 | iconClass: iconClass(group.icon) | |
| 27 | })) | |
| 28 | ) | |
| 29 | ||
| 30 | function iconClass(icon: string) { | |
| 31 | const name = icon.match(/^:(.+):$/)?.[1] | |
| 32 | if (!name) return 'i-lucide:file-text' | |
| 33 | return ICON_PREFIXES.test(name) ? `i-${name}` : `i-twemoji-${name}` | |
| 34 | } | |
| 35 | </script> | |
| 36 | ||
| 37 | <template> | |
| 38 | <div class="random-links" space-y-2> | |
| 39 | <div ref="menuTitleElementRef" relative flex items-center> | |
| 40 | <MenuTitle title="Random links" aria-label="Random links" flex="1" pr-4> | |
| 41 | <template #icon> | |
| 42 | <span i-lucide:dice-5 mr-1 aria-hidden="true" /> | |
| 43 | </template> | |
| 44 | </MenuTitle> | |
| 45 | <MenuHelp | |
| 46 | v-model:is-popped-up="isMenuHelpPoppedUp" | |
| 47 | :menu-title-element-ref="menuTitleElementRef" | |
| 48 | > | |
| 49 | <h4 text-md mb-1 font-semibold>Random links</h4> | |
| 50 | <p text="sm" mb-2 max-w-100> | |
| 51 | Choose which pages the navbar dice picks links from. | |
| 52 | </p> | |
| 53 | </MenuHelp> | |
| 54 | </div> | |
| 55 | ||
| 56 | <SettingsSelect | |
| 57 | label="Select pages" | |
| 58 | aria-label="Pages included in random links" | |
| 59 | :active="isMenuHelpPoppedUp" | |
| 60 | > | |
| 61 | <button | |
| 62 | v-for="row in rows" | |
| 63 | :key="row.group.key" | |
| 64 | type="button" | |
| 65 | class="settings-select-row" | |
| 66 | :class="{ 'is-disabled': row.disabled }" | |
| 67 | :aria-pressed="!row.disabled" | |
| 68 | :title="`${row.disabled ? 'Enable' : 'Disable'} ${row.group.title}`" | |
| 69 | @click="toggleGroup(row.group)" | |
| 70 | > | |
| 71 | <span | |
| 72 | :class="row.iconClass" | |
| 73 | class="settings-select-row-icon" | |
| 74 | aria-hidden="true" | |
| 75 | /> | |
| 76 | <span class="settings-select-row-title">{{ row.group.title }}</span> | |
| 77 | <span | |
| 78 | class="settings-select-row-eye" | |
| 79 | :class="row.disabled ? 'i-lucide:eye-off' : 'i-lucide:eye'" | |
| 80 | aria-hidden="true" | |
| 81 | /> | |
| 82 | </button> | |
| 83 | </SettingsSelect> | |
| 84 | </div> | |
| 85 | </template> | |
docs/.vitepress/theme/components/settings/ScreenMenu.vue +14 −5
| @@ -3,14 +3,18 @@ import { useMounted } from '@vueuse/core' | ||
| 3 | 3 | import { useData } from 'vitepress' |
| 4 | 4 | import VPSocialLinks from 'vitepress/dist/client/theme-default/components/VPSocialLinks.vue' |
| 5 | 5 | |
| 6 | import { ensureNavItemsLoaded, isNavItemEnabled } from '../../navItemsState' | |
| 6 | 7 | import AccentBackground from './AccentBackground.vue' |
| 7 | 8 | import AccentColor from './AccentColor.vue' |
| 8 | import Announcements from './Announcements.vue' | |
| 9 | 9 | import Effects from './Effects.vue' |
| 10 | 10 | import Favicons from './Favicons.vue' |
| 11 | import NavItems from './NavItems.vue' | |
| 12 | import RandomLinks from './RandomLinks.vue' | |
| 11 | 13 | |
| 12 | 14 | const mounted = useMounted() |
| 13 | 15 | const { theme } = useData() |
| 16 | ||
| 17 | ensureNavItemsLoaded() | |
| 14 | 18 | </script> |
| 15 | 19 | |
| 16 | 20 | <template> |
| @@ -19,9 +23,10 @@ const { theme } = useData() | ||
| 19 | 23 | <AccentBackground /> |
| 20 | 24 | <Effects /> |
| 21 | 25 | <Favicons /> |
| 22 | <Announcements /> | |
| 26 | <RandomLinks /> | |
| 27 | <NavItems /> | |
| 23 | 28 | <VPSocialLinks |
| 24 | v-if="theme.socialLinks" | |
| 29 | v-if="theme.socialLinks && isNavItemEnabled('social')" | |
| 25 | 30 | :links="theme.socialLinks" |
| 26 | 31 | class="screen-menu-social-links" |
| 27 | 32 | /> |
| @@ -30,12 +35,16 @@ const { theme } = useData() | ||
| 30 | 35 | |
| 31 | 36 | <style> |
| 32 | 37 | .VPNolebaseEnhancedReadabilitiesMenu { |
| 33 | --vp-nolebase-enhanced-readabilities-menu-background-color: var(--wk-c-menu-bg); | |
| 38 | --vp-nolebase-enhanced-readabilities-menu-background-color: var( | |
| 39 | --wk-c-menu-bg | |
| 40 | ); | |
| 34 | 41 | --vp-nolebase-enhanced-readabilities-menu-text-color: var(--vp-c-text-1); |
| 35 | 42 | } |
| 36 | 43 | |
| 37 | 44 | .dark .VPNolebaseEnhancedReadabilitiesMenu { |
| 38 | --vp-nolebase-enhanced-readabilities-menu-background-color: var(--wk-c-menu-bg); | |
| 45 | --vp-nolebase-enhanced-readabilities-menu-background-color: var( | |
| 46 | --wk-c-menu-bg | |
| 47 | ); | |
| 39 | 48 | --vp-nolebase-enhanced-readabilities-menu-text-color: var(--vp-c-text-1); |
| 40 | 49 | } |
| 41 | 50 | </style> |
docs/.vitepress/theme/components/settings/SettingsSelect.vue added +181
| @@ -0,0 +1,181 @@ | ||
| 1 | <script setup lang="ts"> | |
| 2 | import { NuInputHighlight, NuVerticalTransition } from '@nolebase/ui' | |
| 3 | import { onMounted, onUnmounted, ref } from 'vue' | |
| 4 | ||
| 5 | defineProps<{ | |
| 6 | label: string | |
| 7 | ariaLabel?: string | |
| 8 | active?: boolean | |
| 9 | }>() | |
| 10 | ||
| 11 | const open = ref(false) | |
| 12 | const rootRef = ref<HTMLDivElement>() | |
| 13 | ||
| 14 | function onDocClick(e: MouseEvent) { | |
| 15 | if (rootRef.value && !rootRef.value.contains(e.target as Node)) { | |
| 16 | open.value = false | |
| 17 | } | |
| 18 | } | |
| 19 | ||
| 20 | onMounted(() => document.addEventListener('click', onDocClick, true)) | |
| 21 | onUnmounted(() => document.removeEventListener('click', onDocClick, true)) | |
| 22 | </script> | |
| 23 | ||
| 24 | <template> | |
| 25 | <div | |
| 26 | ref="rootRef" | |
| 27 | class="settings-select" | |
| 28 | :class="{ 'settings-select--open': open }" | |
| 29 | @keydown.esc="open = false" | |
| 30 | > | |
| 31 | <NuInputHighlight :active="!!active" class="rounded-md"> | |
| 32 | <div> | |
| 33 | <button | |
| 34 | type="button" | |
| 35 | class="settings-select-trigger" | |
| 36 | :aria-expanded="open" | |
| 37 | :aria-label="ariaLabel ?? label" | |
| 38 | @click="open = !open" | |
| 39 | > | |
| 40 | <span class="settings-select-label">{{ label }}</span> | |
| 41 | <span | |
| 42 | class="settings-select-chevron i-lucide:chevron-down" | |
| 43 | :class="{ 'settings-select-chevron--open': open }" | |
| 44 | aria-hidden="true" | |
| 45 | /> | |
| 46 | </button> | |
| 47 | ||
| 48 | <NuVerticalTransition :duration="200"> | |
| 49 | <div | |
| 50 | v-show="open" | |
| 51 | class="settings-select-options" | |
| 52 | role="group" | |
| 53 | :aria-label="ariaLabel ?? label" | |
| 54 | > | |
| 55 | <slot /> | |
| 56 | </div> | |
| 57 | </NuVerticalTransition> | |
| 58 | </div> | |
| 59 | </NuInputHighlight> | |
| 60 | </div> | |
| 61 | </template> | |
| 62 | ||
| 63 | <style scoped> | |
| 64 | .settings-select { | |
| 65 | position: relative; | |
| 66 | } | |
| 67 | ||
| 68 | .settings-select-trigger { | |
| 69 | display: flex; | |
| 70 | align-items: center; | |
| 71 | gap: 8px; | |
| 72 | width: 100%; | |
| 73 | min-height: 44px; | |
| 74 | padding: 7px 10px; | |
| 75 | border-radius: 8px; | |
| 76 | border: 2px solid var(--vp-c-divider); | |
| 77 | background: var(--wk-c-menu-bg); | |
| 78 | color: var(--vp-c-text-1); | |
| 79 | font-size: 13px; | |
| 80 | cursor: pointer; | |
| 81 | transition: border-color 0.15s, background-color 0.15s; | |
| 82 | } | |
| 83 | ||
| 84 | .settings-select-trigger:hover { | |
| 85 | border-color: var(--vp-c-brand-1); | |
| 86 | } | |
| 87 | ||
| 88 | .settings-select--open .settings-select-trigger { | |
| 89 | border-color: var(--vp-c-brand-1); | |
| 90 | } | |
| 91 | ||
| 92 | .settings-select-label { | |
| 93 | flex: 1; | |
| 94 | text-align: left; | |
| 95 | } | |
| 96 | ||
| 97 | .settings-select-chevron { | |
| 98 | flex: none; | |
| 99 | width: 16px; | |
| 100 | height: 16px; | |
| 101 | color: var(--vp-c-text-2); | |
| 102 | transition: transform 0.2s ease; | |
| 103 | } | |
| 104 | ||
| 105 | .settings-select-chevron--open { | |
| 106 | transform: rotate(180deg); | |
| 107 | } | |
| 108 | ||
| 109 | .settings-select-options { | |
| 110 | margin-top: 6px; | |
| 111 | max-height: 240px; | |
| 112 | overflow-y: auto; | |
| 113 | overscroll-behavior: contain; | |
| 114 | display: flex; | |
| 115 | flex-direction: column; | |
| 116 | gap: 2px; | |
| 117 | padding: 4px; | |
| 118 | border-radius: 8px; | |
| 119 | border: 1px solid var(--vp-c-divider); | |
| 120 | background: var(--vp-c-bg-elv); | |
| 121 | box-shadow: var(--vp-shadow-2); | |
| 122 | } | |
| 123 | </style> | |
| 124 | ||
| 125 | <style> | |
| 126 | .settings-select-row { | |
| 127 | display: flex; | |
| 128 | align-items: center; | |
| 129 | gap: 8px; | |
| 130 | width: 100%; | |
| 131 | min-width: 0; | |
| 132 | padding: 5px 8px; | |
| 133 | border: 0; | |
| 134 | border-radius: 8px; | |
| 135 | background: transparent; | |
| 136 | color: var(--vp-c-text-1); | |
| 137 | font-family: inherit; | |
| 138 | font-size: 13px; | |
| 139 | text-align: left; | |
| 140 | cursor: pointer; | |
| 141 | transition: opacity 0.15s, background-color 0.15s; | |
| 142 | } | |
| 143 | ||
| 144 | .settings-select-row:hover { | |
| 145 | background-color: var(--vp-c-bg-soft); | |
| 146 | } | |
| 147 | ||
| 148 | .settings-select-row.is-disabled { | |
| 149 | opacity: 0.45; | |
| 150 | } | |
| 151 | ||
| 152 | .settings-select-row.is-disabled:hover { | |
| 153 | opacity: 0.75; | |
| 154 | } | |
| 155 | ||
| 156 | .settings-select-row-icon { | |
| 157 | flex: none; | |
| 158 | width: 16px; | |
| 159 | height: 16px; | |
| 160 | opacity: 0.85; | |
| 161 | } | |
| 162 | ||
| 163 | .settings-select-row-title { | |
| 164 | flex: 1; | |
| 165 | min-width: 0; | |
| 166 | overflow: hidden; | |
| 167 | text-overflow: ellipsis; | |
| 168 | white-space: nowrap; | |
| 169 | } | |
| 170 | ||
| 171 | .settings-select-row-eye { | |
| 172 | flex: none; | |
| 173 | width: 15px; | |
| 174 | height: 15px; | |
| 175 | opacity: 0.55; | |
| 176 | } | |
| 177 | ||
| 178 | .settings-select-row:hover .settings-select-row-eye { | |
| 179 | opacity: 0.9; | |
| 180 | } | |
| 181 | </style> | |
docs/.vitepress/theme/composables/useAnnouncements.ts +5 −5
| @@ -2,14 +2,15 @@ import { useStorage } from '@vueuse/core' | ||
| 2 | 2 | import { computed } from 'vue' |
| 3 | 3 | |
| 4 | 4 | import { data as announcements } from '../announcements.data' |
| 5 | import { AnnouncementsEnabledKey, AnnouncementsReadKey } from '../constants' | |
| 5 | import { AnnouncementsReadKey } from '../constants' | |
| 6 | import { announcementsEnabled, ensureNavItemsLoaded } from '../navItemsState' | |
| 6 | 7 | |
| 7 | 8 | export type { Announcement, AnnouncementType } from '../announcements.data' |
| 8 | 9 | |
| 9 | 10 | export function useAnnouncements() { |
| 10 | // on by default | |
| 11 | const enabledPref = useStorage(AnnouncementsEnabledKey, 'on') | |
| 12 | const enabled = computed(() => enabledPref.value !== 'off') | |
| 11 | ensureNavItemsLoaded() | |
| 12 | ||
| 13 | const enabled = computed(() => announcementsEnabled.value) | |
| 13 | 14 | |
| 14 | 15 | const readIds = useStorage<string[]>(AnnouncementsReadKey, []) |
| 15 | 16 | const readSet = computed(() => new Set(readIds.value)) |
| @@ -33,7 +34,6 @@ export function useAnnouncements() { | ||
| 33 | 34 | |
| 34 | 35 | return { |
| 35 | 36 | announcements, |
| 36 | enabledPref, | |
| 37 | 37 | enabled, |
| 38 | 38 | isRead, |
| 39 | 39 | markRead, |
docs/.vitepress/theme/navActions.ts +6
| @@ -24,6 +24,10 @@ export interface NavAnnouncementAction extends NavActionBase { | ||
| 24 | 24 | type: 'announcement' |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | export interface NavRandomAction extends NavActionBase { | |
| 28 | type: 'random' | |
| 29 | } | |
| 30 | ||
| 27 | 31 | export interface NavLinkAction extends NavActionBase { |
| 28 | 32 | type: 'link' |
| 29 | 33 | icon: string |
| @@ -37,12 +41,14 @@ export type NavAction = | ||
| 37 | 41 | | NavSettingsAction |
| 38 | 42 | | NavBookmarksAction |
| 39 | 43 | | NavAnnouncementAction |
| 44 | | NavRandomAction | |
| 40 | 45 | | NavLinkAction |
| 41 | 46 | |
| 42 | 47 | export const navActions: NavAction[] = [ |
| 43 | 48 | { type: 'appearance' }, |
| 44 | 49 | { type: 'search' }, |
| 45 | 50 | { type: 'bookmarks' }, |
| 51 | { type: 'random' }, | |
| 46 | 52 | { type: 'announcement' }, |
| 47 | 53 | { type: 'settings' }, |
| 48 | 54 | { |
docs/.vitepress/theme/navItemsState.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | import { ref } from 'vue' | |
| 2 | ||
| 3 | import { AnnouncementsEnabledKey } from './constants' | |
| 4 | ||
| 5 | export type NavItemKey = 'announcements' | 'bookmarks' | 'random' | 'social' | |
| 6 | type HideableNavItemKey = Exclude<NavItemKey, 'announcements'> | |
| 7 | ||
| 8 | const HIDDEN_ITEMS_KEY = 'wotaku-nav-items-hidden' | |
| 9 | const DEFAULT_HIDDEN_ITEMS: HideableNavItemKey[] = ['random'] | |
| 10 | const HIDEABLE_ITEMS = new Set<HideableNavItemKey>([ | |
| 11 | 'bookmarks', | |
| 12 | 'random', | |
| 13 | 'social' | |
| 14 | ]) | |
| 15 | ||
| 16 | export const announcementsEnabled = ref(true) | |
| 17 | const hiddenItems = ref<HideableNavItemKey[]>([...DEFAULT_HIDDEN_ITEMS]) | |
| 18 | let loaded = false | |
| 19 | ||
| 20 | function isHideableKey(value: unknown): value is HideableNavItemKey { | |
| 21 | return typeof value === 'string' && | |
| 22 | HIDEABLE_ITEMS.has(value as HideableNavItemKey) | |
| 23 | } | |
| 24 | ||
| 25 | export function ensureNavItemsLoaded() { | |
| 26 | if (loaded || typeof window === 'undefined') return | |
| 27 | loaded = true | |
| 28 | ||
| 29 | try { | |
| 30 | announcementsEnabled.value = | |
| 31 | localStorage.getItem(AnnouncementsEnabledKey) !== 'off' | |
| 32 | ||
| 33 | const raw = localStorage.getItem(HIDDEN_ITEMS_KEY) | |
| 34 | if (!raw) { | |
| 35 | hiddenItems.value = [...DEFAULT_HIDDEN_ITEMS] | |
| 36 | persistHiddenItems() | |
| 37 | return | |
| 38 | } | |
| 39 | const parsed = JSON.parse(raw) | |
| 40 | if (!Array.isArray(parsed)) return | |
| 41 | hiddenItems.value = [...new Set(parsed.filter(isHideableKey))] | |
| 42 | } catch { | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | function persistHiddenItems() { | |
| 47 | try { | |
| 48 | localStorage.setItem(HIDDEN_ITEMS_KEY, JSON.stringify(hiddenItems.value)) | |
| 49 | } catch { | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | export function isNavItemEnabled(key: NavItemKey) { | |
| 54 | if (key === 'announcements') return announcementsEnabled.value | |
| 55 | return !hiddenItems.value.includes(key) | |
| 56 | } | |
| 57 | ||
| 58 | export function toggleNavItem(key: NavItemKey) { | |
| 59 | ensureNavItemsLoaded() | |
| 60 | ||
| 61 | if (key === 'announcements') { | |
| 62 | announcementsEnabled.value = !announcementsEnabled.value | |
| 63 | try { | |
| 64 | localStorage.setItem( | |
| 65 | AnnouncementsEnabledKey, | |
| 66 | announcementsEnabled.value ? 'on' : 'off' | |
| 67 | ) | |
| 68 | } catch { | |
| 69 | } | |
| 70 | return | |
| 71 | } | |
| 72 | ||
| 73 | const next = new Set(hiddenItems.value) | |
| 74 | if (next.has(key)) next.delete(key) | |
| 75 | else next.add(key) | |
| 76 | ||
| 77 | hiddenItems.value = [...next] | |
| 78 | persistHiddenItems() | |
| 79 | } | |
docs/.vitepress/theme/randomLinksState.ts added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | import { computed, ref } from 'vue' | |
| 2 | ||
| 3 | import { homeCards } from '../configs/constants' | |
| 4 | ||
| 5 | export interface RandomPageRow { | |
| 6 | key: string | |
| 7 | pageId: string | |
| 8 | title: string | |
| 9 | icon: string | |
| 10 | children: RandomPageRow[] | |
| 11 | } | |
| 12 | ||
| 13 | const STORAGE_KEY = 'wotaku-random-links-disabled' | |
| 14 | ||
| 15 | const disabledPageIds = ref<string[]>([]) | |
| 16 | let loaded = false | |
| 17 | ||
| 18 | export const disabledPages = computed( | |
| 19 | () => new Set(disabledPageIds.value.map(normalizePageId)) | |
| 20 | ) | |
| 21 | ||
| 22 | export function normalizePageId(pageId: string) { | |
| 23 | const trimmed = pageId.replace(/\/+$/, '') | |
| 24 | return trimmed || '/' | |
| 25 | } | |
| 26 | ||
| 27 | function toRow(card: (typeof homeCards)[number]): RandomPageRow { | |
| 28 | return { | |
| 29 | key: card.id, | |
| 30 | pageId: normalizePageId(card.link), | |
| 31 | title: card.title, | |
| 32 | icon: card.icon, | |
| 33 | children: [] | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | function buildGroups(): RandomPageRow[] { | |
| 38 | return homeCards | |
| 39 | .filter((card) => !card.section) | |
| 40 | .map((card) => { | |
| 41 | const row = toRow(card) | |
| 42 | row.children = homeCards | |
| 43 | .filter((child) => child.section === card.title) | |
| 44 | .map(toRow) | |
| 45 | return row | |
| 46 | }) | |
| 47 | } | |
| 48 | ||
| 49 | export const randomPageGroups = buildGroups() | |
| 50 | ||
| 51 | const DEFAULT_ENABLED_KEYS = new Set([ | |
| 52 | 'websites', | |
| 53 | 'software', | |
| 54 | 'misc', | |
| 55 | 'tools', | |
| 56 | 'art', | |
| 57 | 'merch', | |
| 58 | 'music', | |
| 59 | 'comms', | |
| 60 | 'vtuber', | |
| 61 | 'games' | |
| 62 | ]) | |
| 63 | ||
| 64 | function defaultDisabledPageIds() { | |
| 65 | return randomPageGroups | |
| 66 | .filter((group) => !DEFAULT_ENABLED_KEYS.has(group.key)) | |
| 67 | .flatMap(groupPageIds) | |
| 68 | } | |
| 69 | ||
| 70 | export function ensureDisabledPagesLoaded() { | |
| 71 | if (loaded || typeof window === 'undefined') return | |
| 72 | loaded = true | |
| 73 | ||
| 74 | let raw: string | null = null | |
| 75 | try { | |
| 76 | raw = localStorage.getItem(STORAGE_KEY) | |
| 77 | } catch { | |
| 78 | disabledPageIds.value = defaultDisabledPageIds() | |
| 79 | return | |
| 80 | } | |
| 81 | ||
| 82 | if (!raw) { | |
| 83 | disabledPageIds.value = defaultDisabledPageIds() | |
| 84 | persist() | |
| 85 | return | |
| 86 | } | |
| 87 | ||
| 88 | try { | |
| 89 | const parsed = JSON.parse(raw) | |
| 90 | if (!Array.isArray(parsed)) { | |
| 91 | disabledPageIds.value = defaultDisabledPageIds() | |
| 92 | persist() | |
| 93 | return | |
| 94 | } | |
| 95 | disabledPageIds.value = [ | |
| 96 | ...new Set( | |
| 97 | parsed | |
| 98 | .filter((pageId): pageId is string => typeof pageId === 'string') | |
| 99 | .map(normalizePageId) | |
| 100 | ) | |
| 101 | ] | |
| 102 | } catch { | |
| 103 | disabledPageIds.value = defaultDisabledPageIds() | |
| 104 | persist() | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | function persist() { | |
| 109 | try { | |
| 110 | localStorage.setItem(STORAGE_KEY, JSON.stringify(disabledPageIds.value)) | |
| 111 | } catch { | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | function setPagesDisabled(pageIds: string[], disabled: boolean) { | |
| 116 | const next = new Set(disabledPageIds.value) | |
| 117 | ||
| 118 | for (const pageId of pageIds) { | |
| 119 | const normalized = normalizePageId(pageId) | |
| 120 | if (disabled) next.add(normalized) | |
| 121 | else next.delete(normalized) | |
| 122 | } | |
| 123 | ||
| 124 | disabledPageIds.value = [...next] | |
| 125 | persist() | |
| 126 | } | |
| 127 | ||
| 128 | export function groupPageIds(group: RandomPageRow) { | |
| 129 | return [group.pageId, ...group.children.map((child) => child.pageId)] | |
| 130 | } | |
| 131 | ||
| 132 | export function isGroupDisabled(group: RandomPageRow) { | |
| 133 | return groupPageIds(group).every((pageId) => disabledPages.value.has(pageId)) | |
| 134 | } | |
| 135 | ||
| 136 | export function toggleGroup(group: RandomPageRow) { | |
| 137 | ensureDisabledPagesLoaded() | |
| 138 | setPagesDisabled(groupPageIds(group), !isGroupDisabled(group)) | |
| 139 | } | |