-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (148 loc) · 4.57 KB
/
Copy pathscript.js
File metadata and controls
165 lines (148 loc) · 4.57 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const themeToggle = document.getElementById("themeToggle");
const htmlElement = document.documentElement;
function applyTheme(theme) {
if (theme === "dark") {
htmlElement.classList.add("dark");
themeToggle.setAttribute("title", "Passa a Light Mode");
themeToggle.setAttribute("aria-label", "Switch to light mode");
} else {
htmlElement.classList.remove("dark");
themeToggle.setAttribute("title", "Passa a Dark Mode");
themeToggle.setAttribute("aria-label", "Switch to dark mode");
}
}
function toggleTheme() {
const currentIsDark = htmlElement.classList.contains("dark");
const newTheme = currentIsDark ? "light" : "dark";
applyTheme(newTheme);
localStorage.setItem("themePreference", newTheme);
}
// --- Copy to Clipboard Functionality ---
document.querySelectorAll(".copy-btn").forEach((button) => {
button.addEventListener("click", () => {
// Find the previous sibling element which is the <pre>
const codeBlock = button.previousElementSibling;
// Inside <pre>, there is a <code>
const textToCopy = codeBlock.innerText;
navigator.clipboard
.writeText(textToCopy)
.then(() => {
const originalText = button.textContent;
button.textContent = "Copiato!";
button.classList.add("text-green-600", "dark:text-green-400");
button.classList.remove(
"text-slate-600",
"dark:text-slate-300",
"hover:text-blue-600",
"dark:hover:text-blue-400",
);
setTimeout(() => {
button.textContent = originalText;
button.classList.remove(
"text-green-600",
"dark:text-green-400",
);
button.classList.add(
"text-slate-600",
"dark:text-slate-300",
"hover:text-blue-600",
"dark:hover:text-blue-400",
);
}, 2000);
})
.catch((err) => {
console.error("Failed to copy text: ", err);
button.textContent = "Errore";
});
});
});
const savedTheme = localStorage.getItem("themePreference");
const systemPrefersDark = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
if (savedTheme) {
applyTheme(savedTheme);
} else {
applyTheme(systemPrefersDark ? "dark" : "light");
}
themeToggle.addEventListener("click", toggleTheme);
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
if (!localStorage.getItem("themePreference")) {
applyTheme(e.matches ? "dark" : "light");
}
});
// --- Scroll Spy with Sliding Marker ---
const sections = document.querySelectorAll("section[id]");
const navLinks = document.querySelectorAll(".nav-link");
const navMarker = document.getElementById("navMarker");
function highlightNavigation() {
let scrollY = window.scrollY;
let activeLink = null;
// Check if we've reached the bottom of the page
if (
window.innerHeight + Math.ceil(scrollY) >=
document.body.offsetHeight - 10
) {
const lastSectionId =
sections[sections.length - 1].getAttribute("id");
navLinks.forEach((link) => {
if (link.getAttribute("href").includes(lastSectionId)) {
activeLink = link;
}
});
} else {
// Greedy selection: Find the last section that has been scrolled past
sections.forEach((current) => {
const sectionTop = current.offsetTop - 150; // Offset for header
const sectionId = current.getAttribute("id");
if (scrollY >= sectionTop) {
navLinks.forEach((link) => {
if (link.getAttribute("href").includes(sectionId)) {
activeLink = link;
}
});
}
});
}
if (activeLink) {
// Update Text Styles
navLinks.forEach((link) => {
link.classList.remove(
"text-blue-600",
"dark:text-blue-400",
"font-medium",
);
link.classList.add("text-slate-500", "dark:text-slate-400");
});
activeLink.classList.remove("text-slate-500", "dark:text-slate-400");
activeLink.classList.add(
"text-blue-600",
"dark:text-blue-400",
"font-medium",
);
// Update Sliding Marker Position
// The marker is absolute relative to the 'relative' div wrapper.
// The ul has no top margin/padding, so link.parentElement.offsetTop works best.
navMarker.style.height = `${activeLink.offsetHeight}px`;
navMarker.style.top = `${activeLink.offsetTop}px`;
navMarker.classList.remove("opacity-0");
} else {
// No active section (e.g., at the top of the page)
navMarker.classList.add("opacity-0");
navLinks.forEach((link) => {
link.classList.remove(
"text-blue-600",
"dark:text-blue-400",
"font-medium",
);
link.classList.add("text-slate-500", "dark:text-slate-400");
});
}
}
// Trigger once on load to set initial position
setTimeout(highlightNavigation, 100);
window.addEventListener("scroll", highlightNavigation);
// Initialize Lucide Icons
lucide.createIcons();