-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
111 lines (94 loc) · 3.73 KB
/
Copy pathmain.js
File metadata and controls
111 lines (94 loc) · 3.73 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
(function () {
const STORAGE_KEY = "mgt-theme";
const toggle = document.getElementById("themeToggle");
const icon = document.getElementById("themeIcon");
const html = document.documentElement;
const copyStatus = document.getElementById("copyStatus");
const safeStorage = {
get(key) { try { return localStorage.getItem(key); } catch { return null; } },
set(key, val) { try { localStorage.setItem(key, val); } catch { /* private browsing */ } }
};
const isDark = () => html.classList.contains("dark");
const applyTheme = (theme) => {
html.classList.toggle("dark", theme === "dark");
icon.textContent = theme === "dark" ? "☀" : "☽";
};
icon.textContent = isDark() ? "☀" : "☽";
toggle.addEventListener("click", () => {
const next = isDark() ? "light" : "dark";
safeStorage.set(STORAGE_KEY, next);
applyTheme(next);
});
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
if (!safeStorage.get(STORAGE_KEY)) {
applyTheme(e.matches ? "dark" : "light");
}
});
document.querySelectorAll(".tabs").forEach((tabGroup) => {
const activateTab = (tab) => {
const tabId = tab.getAttribute("data-tab");
const parent = tabGroup.parentElement;
tabGroup.querySelectorAll(".tab").forEach((t) => {
t.classList.remove("active");
t.setAttribute("aria-selected", "false");
t.setAttribute("tabindex", "-1");
});
tab.classList.add("active");
tab.setAttribute("aria-selected", "true");
tab.setAttribute("tabindex", "0");
tab.focus();
parent.querySelectorAll(".tab-content").forEach((c) => {
c.classList.remove("active");
});
const target = parent.querySelector("#tab-" + tabId);
if (target) target.classList.add("active");
};
tabGroup.addEventListener("click", (e) => {
const tab = e.target.closest(".tab");
if (tab) activateTab(tab);
});
tabGroup.addEventListener("keydown", (e) => {
const tabs = Array.from(tabGroup.querySelectorAll(".tab"));
const current = tabs.indexOf(e.target);
if (current === -1) return;
let next;
if (e.key === "ArrowRight") {
next = tabs[(current + 1) % tabs.length];
} else if (e.key === "ArrowLeft") {
next = tabs[(current - 1 + tabs.length) % tabs.length];
} else if (e.key === "Home") {
next = tabs[0];
} else if (e.key === "End") {
next = tabs[tabs.length - 1];
}
if (next) {
e.preventDefault();
activateTab(next);
}
});
});
let copyTimeout = null;
document.addEventListener("click", (e) => {
const btn = e.target.closest(".copy-btn");
if (!btn) return;
const text = btn.getAttribute("data-copy");
const original = btn.textContent;
if (copyTimeout) {
clearTimeout(copyTimeout);
copyTimeout = null;
}
navigator.clipboard.writeText(text).then(() => {
btn.textContent = "Copied!";
copyStatus.textContent = "Copied to clipboard";
}).catch(() => {
btn.textContent = "Failed";
copyStatus.textContent = "Copy failed";
}).finally(() => {
copyTimeout = setTimeout(() => {
btn.textContent = original;
copyStatus.textContent = "";
copyTimeout = null;
}, 1500);
});
});
})();