Skip to content

Commit e2a4e05

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

File tree

2 files changed

+82
-79
lines changed

2 files changed

+82
-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: 81 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,71 @@
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+
base = window.location.origin + '/' + lvAnchor;
52+
after = currentPath.substring(idx + lvAnchor.length);
53+
} else {
54+
// Everything up to (and including) "book/"
55+
// e.g. "/C:/Users/.../guide/book/"
56+
base = currentPath.substring(0, idx + bookAnchor.length);
57+
58+
// The rest "after book/" part
59+
// e.g. "index.html" or "zh-TW/getting_started/foo.html"
60+
after = currentPath.substring(idx + bookAnchor.length);
5061
}
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-
62+
6063
// Split into segments and remove any leading lang if it’s known
6164
// e.g. ["index.html"] or ["en", "getting_started", "foo.html"]
6265
let segments = after.split('/').filter(s => s.length > 0);
63-
66+
6467
if (segments.length > 0 && supportedLangs.includes(segments[0])) {
6568
// remove the first segment if it’s a supported lang
6669
// e.g. now ["getting_started", "foo.html"]
67-
segments.shift();
70+
segments.shift();
6871
}
69-
72+
7073
// Insert the chosen language as the first path segment
7174
// Also, English has no prefix
7275
let newPath;
@@ -77,10 +80,10 @@
7780
// e.g. /C:/Users/.../guide/book/zh-TW/getting_started/foo.html
7881
newPath = base + chosenLang + '/' + segments.join('/');
7982
}
80-
83+
8184
window.location.href = newPath;
8285
}
83-
86+
8487
if (
8588
langPopup.style.display === 'block' &&
8689
!langToggleButton.contains(e.target) &&
@@ -91,48 +94,48 @@
9194
});
9295

9396
// 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-
}
97+
langPopup.addEventListener('focusout', function (e) {
98+
// e.relatedTarget can be null in some browsers
99+
if (!!e.relatedTarget &&
100+
!langToggleButton.contains(e.relatedTarget) &&
101+
!langPopup.contains(e.relatedTarget)) {
102+
hideLangs();
103+
}
101104
});
102105

103106
// 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-
}
107+
document.addEventListener('keydown', function (e) {
108+
if (langPopup.style.display !== 'block') return;
109+
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
110+
111+
let li;
112+
switch (e.key) {
113+
case 'Escape':
114+
e.preventDefault();
115+
hideLangs();
116+
break;
117+
case 'ArrowUp':
118+
e.preventDefault();
119+
li = document.activeElement.parentElement;
120+
if (li && li.previousElementSibling) {
121+
li.previousElementSibling.querySelector('a, button').focus();
122+
}
123+
break;
124+
case 'ArrowDown':
125+
e.preventDefault();
126+
li = document.activeElement.parentElement;
127+
if (li && li.nextElementSibling) {
128+
li.nextElementSibling.querySelector('a, button').focus();
129+
}
130+
break;
131+
case 'Home':
132+
e.preventDefault();
133+
langPopup.querySelector('li:first-child a, li:first-child button').focus();
134+
break;
135+
case 'End':
136+
e.preventDefault();
137+
langPopup.querySelector('li:last-child a, li:last-child button').focus();
138+
break;
139+
}
137140
});
138141
})();

0 commit comments

Comments
 (0)