11<!-- Adapted from https://github.com/MakieOrg/Makie.jl/blob/master/docs/src/.vitepress/theme/VersionPicker.vue -->
22
33<script setup lang="ts">
4- import { computed , ref , onMounted } from ' vue'
5- import { useRoute } from ' vitepress'
4+ import { ref , onMounted , computed } from ' vue'
5+ import { useData } from ' vitepress'
66import VPNavBarMenuGroup from ' vitepress/dist/client/theme-default/components/VPNavBarMenuGroup.vue'
77import VPNavScreenMenuGroup from ' vitepress/dist/client/theme-default/components/VPNavScreenMenuGroup.vue'
88
9+ // Extend the global Window interface to include DOC_VERSIONS and DOCUMENTER_CURRENT_VERSION
10+ declare global {
11+ interface Window {
12+ DOC_VERSIONS? : string [];
13+ DOCUMENTER_CURRENT_VERSION? : string ;
14+ }
15+ }
16+
917const props = defineProps <{
1018 screenMenu? : boolean
1119}>()
1220
13- const route = useRoute ()
14-
15- const versions = ref ([]);
21+ const versions = ref <Array <{ text: string , link: string }>>([]);
1622const currentVersion = ref (' Versions' );
23+ const isClient = ref (false );
24+ const { site } = useData ()
1725
18- const waitForGlobalDocumenterVars = () => {
19- return new Promise ((resolve ) => {
26+ const isLocalBuild = () => {
27+ return typeof window !== ' undefined' && (window .location .hostname === ' localhost' || window .location .hostname === ' 127.0.0.1' );
28+ }
29+
30+ const getBaseRepository = () => {
31+ if (typeof window === ' undefined' ) return ' ' ; // Handle server-side rendering (SSR)
32+ const { origin, pathname } = window .location ;
33+ // Check if it's a GitHub Pages (or similar) setup
34+ if (origin .includes (' github.io' )) {
35+ // Extract the first part of the path as the repository name
36+ const pathParts = pathname .split (' /' ).filter (Boolean );
37+ const baseRepo = pathParts .length > 0 ? ` /${pathParts [0 ]}/ ` : ' /' ;
38+ return ` ${origin }${baseRepo } ` ;
39+ } else {
40+ // For custom domains, use just the origin (e.g., https://docs.makie.org)
41+ return origin ;
42+ }
43+ };
44+
45+ const waitForScriptsToLoad = () => {
46+ return new Promise <boolean >((resolve ) => {
47+ if (isLocalBuild ()) {
48+ resolve (false );
49+ return ;
50+ }
2051 const checkInterval = setInterval (() => {
21- if (window .DOC_VERSIONS && window .DOCUMENTER_CURRENT_VERSION ) {
52+ if (window .DOC_VERSIONS && window .DOCUMENTER_CURRENT_VERSION ) {
2253 clearInterval (checkInterval );
23- resolve ({
24- versions: window .DOC_VERSIONS ,
25- currentVersion: window .DOCUMENTER_CURRENT_VERSION
26- });
27- }
28- }, 100 ); // Check every 100ms
29- });
54+ resolve (true );
55+ }
56+ }, 100 );
57+ // Timeout after 5 seconds
58+ setTimeout (() => {
59+ clearInterval (checkInterval );
60+ resolve (false );
61+ }, 5000 );
62+ });
3063};
3164
32- onMounted (async () => {
33- const globalvars = await waitForGlobalDocumenterVars ();
34- versions .value = globalvars .versions .map ((v ) => {
35- return {text: v , link: ` ${window .location .origin }/${v }/ ` }
36- });
37- currentVersion .value = globalvars .currentVersion ;
38- });
65+ const loadVersions = async () => {
66+ if (typeof window === ' undefined' ) return ; // Guard for SSR
67+
68+ try {
69+ if (isLocalBuild ()) {
70+ // Handle the local build scenario directly
71+ const fallbackVersions = [' dev' ];
72+ versions .value = fallbackVersions .map (v => ({
73+ text: v ,
74+ link: ' /'
75+ }));
76+ currentVersion .value = ' dev' ;
77+ } else {
78+ // For non-local builds, wait for scripts to load
79+ const scriptsLoaded = await waitForScriptsToLoad ();
80+ const getBaseRepositoryPath = computed (() => {
81+ return getBaseRepository ();
82+ });
3983
84+ if (scriptsLoaded && window .DOC_VERSIONS && window .DOCUMENTER_CURRENT_VERSION ) {
85+ versions .value = window .DOC_VERSIONS .map ((v : string ) => ({
86+ text: v ,
87+ link: ` ${getBaseRepositoryPath .value }/${v }/ `
88+ }));
89+ currentVersion .value = window .DOCUMENTER_CURRENT_VERSION ;
90+ } else {
91+ // Fallback logic if scripts fail to load or are not available
92+ const fallbackVersions = [' dev' ];
93+ versions .value = fallbackVersions .map (v => ({
94+ text: v ,
95+ link: ` ${getBaseRepositoryPath .value }/${v }/ `
96+ }));
97+ currentVersion .value = ' dev' ;
98+ }
99+ }
100+ } catch (error ) {
101+ console .warn (' Error loading versions:' , error );
102+ // Use fallback logic in case of an error
103+ const fallbackVersions = [' dev' ];
104+ const getBaseRepositoryPath = computed (() => {
105+ return getBaseRepository ();
106+ });
107+ versions .value = fallbackVersions .map (v => ({
108+ text: v ,
109+ link: ` ${getBaseRepositoryPath .value }/${v }/ `
110+ }));
111+ currentVersion .value = ' dev' ;
112+ }
113+ isClient .value = true ;
114+ };
115+
116+ onMounted (loadVersions );
40117 </script >
41118
42119<template >
43- <VPNavBarMenuGroup
44- v-if =" !screenMenu"
45- :item =" { text: currentVersion, items: versions }"
46- class =" VPVersionPicker"
47- />
48- <VPNavScreenMenuGroup
49- v-else
50- :text =" currentVersion"
51- :items =" versions"
52- class =" VPVersionPicker"
53- />
120+ <template v-if =" isClient " >
121+ <VPNavBarMenuGroup
122+ v-if =" !screenMenu && versions.length > 0"
123+ :item =" { text: currentVersion, items: versions }"
124+ class =" VPVersionPicker"
125+ />
126+ <VPNavScreenMenuGroup
127+ v-else-if =" screenMenu && versions.length > 0"
128+ :text =" currentVersion"
129+ :items =" versions"
130+ class =" VPVersionPicker"
131+ />
132+ </template >
54133</template >
55134
56135<style scoped>
57136.VPVersionPicker :deep(button .text ) {
58137 color : var (--vp-c-text-1 ) !important ;
59138}
60-
61139.VPVersionPicker :hover :deep(button .text ) {
62140 color : var (--vp-c-text-2 ) !important ;
63141}
64- </style >
142+ </style >
0 commit comments