Skip to content

Commit 7889907

Browse files
committed
🐛 Fix lang-toggle.js for live deploy
1 parent 603e8a0 commit 7889907

File tree

2 files changed

+81
-79
lines changed

2 files changed

+81
-79
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ indent_style = tab
66
# Optional: git will commit as lf, this will only affect local files
77
end_of_line = lf
88

9-
[*.{py,md,yml,sh,cmake,json}]
9+
[*.{py,md,yml,sh,cmake,json,js}]
1010
indent_style = space
1111
indent_size = 2
1212

guide/theme/lang_toggle.js

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,70 @@
55
const langPopup = document.getElementById('lang-list');
66

77
if (!langToggleButton || !langPopup) {
8-
return; // Safety check: if they're missing from the page, do nothing
8+
return; // Safety check: if they're missing from the page, do nothing
99
}
1010

1111
function showLangs() {
12-
langPopup.style.display = 'block';
13-
langToggleButton.setAttribute('aria-expanded', 'true');
12+
langPopup.style.display = 'block';
13+
langToggleButton.setAttribute('aria-expanded', 'true');
1414
}
1515

1616
function hideLangs() {
17-
langPopup.style.display = 'none';
18-
langToggleButton.setAttribute('aria-expanded', 'false');
19-
langToggleButton.focus();
17+
langPopup.style.display = 'none';
18+
langToggleButton.setAttribute('aria-expanded', 'false');
19+
langToggleButton.focus();
2020
}
2121

22-
langToggleButton.addEventListener('click', function() {
23-
if (langPopup.style.display === 'block') {
24-
hideLangs();
25-
} else {
26-
showLangs();
27-
}
22+
langToggleButton.addEventListener('click', function () {
23+
if (langPopup.style.display === 'block') {
24+
hideLangs();
25+
} else {
26+
showLangs();
27+
}
2828
});
2929

3030
document.addEventListener('click', function (e) {
3131
if (e.target && e.target.matches('button[data-lang]')) {
3232
const chosenLang = e.target.getAttribute('data-lang');
3333
const supportedLangs = ['en', 'ko-KR']; // Add translated languages here
34-
34+
3535
let currentPath = window.location.pathname;
36-
36+
3737
// Find "book/" in the path
3838
const bookAnchor = 'book/';
39-
const idx = currentPath.indexOf(bookAnchor);
40-
39+
let idx = currentPath.indexOf(bookAnchor);
40+
let base = '';
41+
let after = '';
4142
if (idx === -1) {
42-
// Fallback: If "book/" isn’t in the path
43-
// Just go to the top-level file in the chosen language.
44-
if (chosenLang === 'en')
45-
window.location.href = 'index.html';
46-
else
47-
window.location.href = chosenLang + '/index.html';
48-
49-
return;
43+
// assume live
44+
const lvAnchor = 'learn-vulkan/';
45+
// eg "/learn-vulkan/ko-KR/initialization/index.html"
46+
idx = currentPath.indexOf(lvAnchor);
47+
if (idx == -1) {
48+
// error
49+
return;
50+
}
51+
after = currentPath.substring(idx + lvAnchor.length);
52+
} else {
53+
// Everything up to (and including) "book/"
54+
// e.g. "/C:/Users/.../guide/book/"
55+
base = currentPath.substring(0, idx + bookAnchor.length);
56+
57+
// The rest "after book/" part
58+
// e.g. "index.html" or "zh-TW/getting_started/foo.html"
59+
after = currentPath.substring(idx + bookAnchor.length);
5060
}
51-
52-
// Everything up to (and including) "book/"
53-
// e.g. "/C:/Users/.../guide/book/"
54-
const base = currentPath.substring(0, idx + bookAnchor.length);
55-
56-
// The rest "after book/" part
57-
// e.g. "index.html" or "zh-TW/getting_started/foo.html"
58-
let after = currentPath.substring(idx + bookAnchor.length);
59-
61+
6062
// Split into segments and remove any leading lang if it’s known
6163
// e.g. ["index.html"] or ["en", "getting_started", "foo.html"]
6264
let segments = after.split('/').filter(s => s.length > 0);
63-
65+
6466
if (segments.length > 0 && supportedLangs.includes(segments[0])) {
6567
// remove the first segment if it’s a supported lang
6668
// e.g. now ["getting_started", "foo.html"]
67-
segments.shift();
69+
segments.shift();
6870
}
69-
71+
7072
// Insert the chosen language as the first path segment
7173
// Also, English has no prefix
7274
let newPath;
@@ -77,10 +79,10 @@
7779
// e.g. /C:/Users/.../guide/book/zh-TW/getting_started/foo.html
7880
newPath = base + chosenLang + '/' + segments.join('/');
7981
}
80-
82+
8183
window.location.href = newPath;
8284
}
83-
85+
8486
if (
8587
langPopup.style.display === 'block' &&
8688
!langToggleButton.contains(e.target) &&
@@ -91,48 +93,48 @@
9193
});
9294

9395
// Also hide if focus goes elsewhere
94-
langPopup.addEventListener('focusout', function(e) {
95-
// e.relatedTarget can be null in some browsers
96-
if (!!e.relatedTarget &&
97-
!langToggleButton.contains(e.relatedTarget) &&
98-
!langPopup.contains(e.relatedTarget)) {
99-
hideLangs();
100-
}
96+
langPopup.addEventListener('focusout', function (e) {
97+
// e.relatedTarget can be null in some browsers
98+
if (!!e.relatedTarget &&
99+
!langToggleButton.contains(e.relatedTarget) &&
100+
!langPopup.contains(e.relatedTarget)) {
101+
hideLangs();
102+
}
101103
});
102104

103105
// Optional: Add keyboard navigation (like theme popup)
104-
document.addEventListener('keydown', function(e) {
105-
if (langPopup.style.display !== 'block') return;
106-
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
107-
108-
let li;
109-
switch (e.key) {
110-
case 'Escape':
111-
e.preventDefault();
112-
hideLangs();
113-
break;
114-
case 'ArrowUp':
115-
e.preventDefault();
116-
li = document.activeElement.parentElement;
117-
if (li && li.previousElementSibling) {
118-
li.previousElementSibling.querySelector('a, button').focus();
119-
}
120-
break;
121-
case 'ArrowDown':
122-
e.preventDefault();
123-
li = document.activeElement.parentElement;
124-
if (li && li.nextElementSibling) {
125-
li.nextElementSibling.querySelector('a, button').focus();
126-
}
127-
break;
128-
case 'Home':
129-
e.preventDefault();
130-
langPopup.querySelector('li:first-child a, li:first-child button').focus();
131-
break;
132-
case 'End':
133-
e.preventDefault();
134-
langPopup.querySelector('li:last-child a, li:last-child button').focus();
135-
break;
136-
}
106+
document.addEventListener('keydown', function (e) {
107+
if (langPopup.style.display !== 'block') return;
108+
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
109+
110+
let li;
111+
switch (e.key) {
112+
case 'Escape':
113+
e.preventDefault();
114+
hideLangs();
115+
break;
116+
case 'ArrowUp':
117+
e.preventDefault();
118+
li = document.activeElement.parentElement;
119+
if (li && li.previousElementSibling) {
120+
li.previousElementSibling.querySelector('a, button').focus();
121+
}
122+
break;
123+
case 'ArrowDown':
124+
e.preventDefault();
125+
li = document.activeElement.parentElement;
126+
if (li && li.nextElementSibling) {
127+
li.nextElementSibling.querySelector('a, button').focus();
128+
}
129+
break;
130+
case 'Home':
131+
e.preventDefault();
132+
langPopup.querySelector('li:first-child a, li:first-child button').focus();
133+
break;
134+
case 'End':
135+
e.preventDefault();
136+
langPopup.querySelector('li:last-child a, li:last-child button').focus();
137+
break;
138+
}
137139
});
138140
})();

0 commit comments

Comments
 (0)