|
1 | 1 | <!-- Adapted from https://github.com/MakieOrg/Makie.jl/blob/master/docs/src/.vitepress/theme/VersionPicker.vue --> |
2 | 2 |
|
3 | 3 | <script setup lang="ts"> |
4 | | -import { ref, onMounted, computed } from 'vue' |
5 | | -import { useData } from 'vitepress' |
| 4 | +import { computed, ref, onMounted } from 'vue' |
| 5 | +import { useRoute } from 'vitepress' |
6 | 6 | import VPNavBarMenuGroup from 'vitepress/dist/client/theme-default/components/VPNavBarMenuGroup.vue' |
7 | 7 | import VPNavScreenMenuGroup from 'vitepress/dist/client/theme-default/components/VPNavScreenMenuGroup.vue' |
8 | 8 |
|
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 | | -
|
17 | 9 | const props = defineProps<{ |
18 | 10 | screenMenu?: boolean |
19 | 11 | }>() |
20 | 12 |
|
21 | | -const versions = ref<Array<{ text: string, link: string }>>([]); |
22 | | -const currentVersion = ref('Versions'); |
23 | | -const isClient = ref(false); |
24 | | -const { site } = useData() |
25 | | -
|
26 | | -const isLocalBuild = () => { |
27 | | - return typeof window !== 'undefined' && (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'); |
28 | | -} |
| 13 | +const route = useRoute() |
29 | 14 |
|
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('qutip.org')) || (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 | | -}; |
| 15 | +const versions = ref([]); |
| 16 | +const currentVersion = ref('Versions'); |
44 | 17 |
|
45 | | -const waitForScriptsToLoad = () => { |
46 | | - return new Promise<boolean>((resolve) => { |
47 | | - if (isLocalBuild()) { |
48 | | - resolve(false); |
49 | | - return; |
50 | | - } |
| 18 | +const waitForGlobalDocumenterVars = () => { |
| 19 | + return new Promise((resolve) => { |
51 | 20 | const checkInterval = setInterval(() => { |
52 | | - if (window.DOC_VERSIONS && window.DOCUMENTER_CURRENT_VERSION) { |
| 21 | + if (window.DOC_VERSIONS && window.DOCUMENTER_CURRENT_VERSION) { |
53 | 22 | clearInterval(checkInterval); |
54 | | - resolve(true); |
55 | | - } |
56 | | - }, 100); |
57 | | - // Timeout after 5 seconds |
58 | | - setTimeout(() => { |
59 | | - clearInterval(checkInterval); |
60 | | - resolve(false); |
61 | | - }, 5000); |
62 | | - }); |
| 23 | + resolve({ |
| 24 | + versions: window.DOC_VERSIONS, |
| 25 | + currentVersion: window.DOCUMENTER_CURRENT_VERSION |
| 26 | + }); |
| 27 | + } |
| 28 | + }, 100); // Check every 100ms |
| 29 | + }); |
63 | 30 | }; |
64 | 31 |
|
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 | | - }); |
| 32 | +onMounted(async () => { |
| 33 | + const globalvars = await waitForGlobalDocumenterVars(); |
| 34 | + versions.value = globalvars.versions.map((v) => { |
| 35 | + return {text: v, link: `${window.location.origin}/QuantumToolbox.jl/${v}/`} |
| 36 | + }); |
| 37 | + currentVersion.value = globalvars.currentVersion; |
| 38 | +}); |
83 | 39 |
|
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); |
117 | 40 | </script> |
118 | 41 |
|
119 | 42 | <template> |
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> |
| 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 | + /> |
133 | 54 | </template> |
134 | 55 |
|
135 | 56 | <style scoped> |
136 | 57 | .VPVersionPicker :deep(button .text) { |
137 | 58 | color: var(--vp-c-text-1) !important; |
138 | 59 | } |
| 60 | +
|
139 | 61 | .VPVersionPicker:hover :deep(button .text) { |
140 | 62 | color: var(--vp-c-text-2) !important; |
141 | 63 | } |
|
0 commit comments