| 1 |
--- |
| 2 |
// Determine the base URL - no base path for docs.jj-vcs.dev |
| 3 |
const siteBase = '/'; |
| 4 |
--- |
| 5 |
|
| 6 |
<div class="version-switcher" id="version-switcher-container" style="display: none;"> |
| 7 |
<label for="version-select" class="sl-hidden">Select documentation version</label> |
| 8 |
<select id="version-select" class="version-select"> |
| 9 |
<option>Loading versions...</option> |
| 10 |
</select> |
| 11 |
</div> |
| 12 |
|
| 13 |
<script define:vars={{ siteBase }}> |
| 14 |
// Fetch versions client-side and populate the dropdown |
| 15 |
async function loadVersions() { |
| 16 |
const container = document.getElementById('version-switcher-container'); |
| 17 |
const select = document.getElementById('version-select'); |
| 18 |
|
| 19 |
if (!container || !select) return; |
| 20 |
|
| 21 |
try { |
| 22 |
const response = await fetch(`${siteBase}versions.json`); |
| 23 |
if (!response.ok) { |
| 24 |
// No versions.json available (e.g., in development) |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
const versions = await response.json(); |
| 29 |
if (!versions || versions.length === 0) return; |
| 30 |
|
| 31 |
// Determine current version from URL path |
| 32 |
const pathMatch = window.location.pathname.match(/^\/([^\/]+)/); |
| 33 |
let currentVersion = 'unknown'; |
| 34 |
|
| 35 |
if (pathMatch) { |
| 36 |
const pathVersion = pathMatch[1]; |
| 37 |
// Check if this is a direct version match |
| 38 |
let versionObj = versions.find(v => v.version === pathVersion); |
| 39 |
// Check if this matches an alias |
| 40 |
if (!versionObj) { |
| 41 |
versionObj = versions.find(v => v.aliases && v.aliases.includes(pathVersion)); |
| 42 |
} |
| 43 |
currentVersion = versionObj?.version || pathVersion; |
| 44 |
} |
| 45 |
|
| 46 |
// Clear and populate the select |
| 47 |
select.innerHTML = ''; |
| 48 |
versions.forEach(v => { |
| 49 |
const option = document.createElement('option'); |
| 50 |
option.value = v.version; |
| 51 |
|
| 52 |
// Display title with alias if it has one |
| 53 |
const displayTitle = v.aliases && v.aliases.length > 0 |
| 54 |
? `${v.title} (${v.aliases.join(', ')})` |
| 55 |
: v.title; |
| 56 |
option.textContent = displayTitle; |
| 57 |
|
| 58 |
if (v.version === currentVersion) { |
| 59 |
option.selected = true; |
| 60 |
} |
| 61 |
|
| 62 |
select.appendChild(option); |
| 63 |
}); |
| 64 |
|
| 65 |
// Show the container |
| 66 |
container.style.display = 'flex'; |
| 67 |
|
| 68 |
// Add change handler |
| 69 |
select.addEventListener('change', (e) => { |
| 70 |
const selectedVersion = e.target.value; |
| 71 |
const currentPath = window.location.pathname; |
| 72 |
const versionRegex = /^\/[^\/]+/; |
| 73 |
const pagePath = currentPath.replace(versionRegex, '').replace(/^\//, ''); |
| 74 |
window.location.href = `${siteBase}${selectedVersion}/${pagePath}`; |
| 75 |
}); |
| 76 |
} catch (error) { |
| 77 |
console.debug('Could not load versions.json:', error); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Load versions when DOM is ready |
| 82 |
if (document.readyState === 'loading') { |
| 83 |
document.addEventListener('DOMContentLoaded', loadVersions); |
| 84 |
} else { |
| 85 |
loadVersions(); |
| 86 |
} |
| 87 |
</script> |
| 88 |
|
| 89 |
<style> |
| 90 |
.version-switcher { |
| 91 |
display: flex; |
| 92 |
align-items: center; |
| 93 |
gap: 0.5rem; |
| 94 |
} |
| 95 |
|
| 96 |
.version-select { |
| 97 |
padding: 0.4rem 2rem 0.4rem 0.75rem; |
| 98 |
border: 1px solid var(--sl-color-gray-5); |
| 99 |
border-radius: 0.375rem; |
| 100 |
background-color: var(--sl-color-black); |
| 101 |
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%23999' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e"); |
| 102 |
background-position: right 0.5rem center; |
| 103 |
background-repeat: no-repeat; |
| 104 |
background-size: 1.25rem; |
| 105 |
color: var(--sl-color-white); |
| 106 |
font-size: 0.875rem; |
| 107 |
cursor: pointer; |
| 108 |
appearance: none; |
| 109 |
transition: border-color 0.2s; |
| 110 |
} |
| 111 |
|
| 112 |
.version-select:hover { |
| 113 |
border-color: var(--sl-color-gray-2); |
| 114 |
} |
| 115 |
|
| 116 |
.version-select:focus { |
| 117 |
outline: 2px solid var(--sl-color-accent); |
| 118 |
outline-offset: 2px; |
| 119 |
} |
| 120 |
|
| 121 |
.sl-hidden { |
| 122 |
position: absolute; |
| 123 |
width: 1px; |
| 124 |
height: 1px; |
| 125 |
padding: 0; |
| 126 |
margin: -1px; |
| 127 |
overflow: hidden; |
| 128 |
clip: rect(0, 0, 0, 0); |
| 129 |
white-space: nowrap; |
| 130 |
border-width: 0; |
| 131 |
} |
| 132 |
</style> |