generated from mintlify/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage-switch.js
More file actions
123 lines (106 loc) · 2.97 KB
/
language-switch.js
File metadata and controls
123 lines (106 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(() => {
const STORAGE_KEY = 'languageSwitch_scrollPosition';
function getPathWithoutLang(url) {
const path = url.pathname || url;
return path.replace(/^\/(en|pt-br|es|fr)\//, '/');
}
function storeScrollPosition() {
const scrollY = window.scrollY || window.pageYOffset;
const path = getPathWithoutLang(window.location);
const lang =
window.location.pathname.match(/^\/(en|pt-br|es|fr)\//)?.[1] || 'en';
sessionStorage.setItem(
STORAGE_KEY,
JSON.stringify({
path: path,
scrollY: scrollY,
sourceLang: lang,
timestamp: Date.now(),
}),
);
}
function restoreScrollPosition() {
const stored = sessionStorage.getItem(STORAGE_KEY);
if (!stored) return;
try {
const data = JSON.parse(stored);
const currentPath = getPathWithoutLang(window.location);
if (data.path === currentPath && typeof data.scrollY === 'number') {
setTimeout(() => {
window.scrollTo(0, data.scrollY);
sessionStorage.removeItem(STORAGE_KEY);
}, 150);
} else {
sessionStorage.removeItem(STORAGE_KEY);
}
} catch (e) {
console.error('Failed to restore scroll position:', e);
sessionStorage.removeItem(STORAGE_KEY);
}
}
function isLanguageSelector(element) {
if (!element) return false;
const className = element.className || '';
const tagName = element.tagName || '';
return (
className.toLowerCase().includes('language') ||
className.toLowerCase().includes('locale') ||
element.getAttribute?.('data-lang') ||
element.getAttribute?.('role') === 'combobox' ||
(tagName === 'BUTTON' &&
element.textContent
?.toLowerCase()
.match(
/^(en|pt-br|es|fr|portugu|english|espanol|español|spanish|francais|français|french)/,
))
);
}
function interceptLanguageSwitch() {
let lastClickTime = 0;
const CLICK_THRESHOLD = 300;
document.addEventListener('click', (e) => {
const target = e.target;
const currentTime = Date.now();
if (currentTime - lastClickTime < CLICK_THRESHOLD) return;
lastClickTime = currentTime;
let element = target;
let foundLanguageSelector = false;
for (let i = 0; i < 5 && element; i++) {
if (isLanguageSelector(element)) {
foundLanguageSelector = true;
break;
}
element = element.parentElement;
}
if (foundLanguageSelector) {
setTimeout(storeScrollPosition, 10);
}
});
document.addEventListener('beforeunload', () => {
const scrollY = window.scrollY || window.pageYOffset;
const path = getPathWithoutLang(window.location);
sessionStorage.setItem(
STORAGE_KEY,
JSON.stringify({
path: path,
scrollY: scrollY,
timestamp: Date.now(),
}),
);
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
interceptLanguageSwitch();
restoreScrollPosition();
});
} else {
interceptLanguageSwitch();
restoreScrollPosition();
}
window.addEventListener('pageshow', (event) => {
if (event.persisted) {
restoreScrollPosition();
}
});
})();