-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.js
More file actions
312 lines (271 loc) Β· 10.8 KB
/
script.js
File metadata and controls
312 lines (271 loc) Β· 10.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Global variables
let currentI18n = null;
let currentLogger = null;
// Initialize i18n and logger when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
// Wait for i18n.js to load
if (typeof i18n !== 'undefined' && typeof logger !== 'undefined') {
currentI18n = i18n;
currentLogger = logger;
// Initialize i18n
currentI18n.updateDOM();
currentLogger.info('Website initialized with i18n support');
// Log page load
currentLogger.info('Page loaded', {
url: window.location.href,
language: currentI18n.getCurrentLanguage(),
timestamp: new Date().toISOString()
});
}
});
// Language toggle function
function toggleLanguage() {
if (!currentI18n) return;
const currentLang = currentI18n.getCurrentLanguage();
const newLang = currentLang === 'zh' ? 'en' : 'zh';
currentI18n.setLanguage(newLang);
currentLogger.info(`Language switched from ${currentLang} to ${newLang}`);
}
// Smooth scrolling for navigation links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
const headerOffset = 80;
const elementPosition = target.getBoundingClientRect().top;
const offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
// Log navigation
if (currentLogger) {
currentLogger.info('Navigation clicked', {
target: this.getAttribute('href'),
text: this.textContent
});
}
}
});
});
// Header background opacity on scroll
window.addEventListener('scroll', () => {
const header = document.querySelector('.header');
if (window.scrollY > 50) {
header.style.background = 'rgba(255, 255, 255, 0.98)';
} else {
header.style.background = 'rgba(255, 255, 255, 0.95)';
}
});
// Intersection Observer for animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
}, observerOptions);
// Observe feature cards and doc cards for fade-in animation
document.addEventListener('DOMContentLoaded', () => {
const animatedElements = document.querySelectorAll('.feature-card, .doc-card');
animatedElements.forEach(el => {
el.style.opacity = '0';
el.style.transform = 'translateY(20px)';
el.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
observer.observe(el);
});
});
// Helper function to extract clean code text from highlighted HTML
function getCleanCodeText(codeElement) {
// Create a temporary element to process the code
const temp = document.createElement('div');
temp.innerHTML = codeElement.innerHTML;
// Remove all syntax highlighting spans but keep their text content
const spans = temp.querySelectorAll('span');
spans.forEach(span => {
const textNode = document.createTextNode(span.textContent);
span.parentNode.replaceChild(textNode, span);
});
return temp.textContent || temp.innerText || '';
}
// Copy code functionality
document.addEventListener('DOMContentLoaded', () => {
const codeBlocks = document.querySelectorAll('.code-block');
codeBlocks.forEach(block => {
const copyButton = document.createElement('button');
copyButton.innerHTML = 'π';
copyButton.className = 'copy-button';
copyButton.style.cssText = `
position: absolute;
top: 12px;
right: 12px;
background: rgba(255, 255, 255, 0.1);
border: none;
color: #e2e8f0;
padding: 8px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background 0.2s;
`;
copyButton.addEventListener('mouseenter', () => {
copyButton.style.background = 'rgba(255, 255, 255, 0.2)';
});
copyButton.addEventListener('mouseleave', () => {
copyButton.style.background = 'rgba(255, 255, 255, 0.1)';
});
copyButton.addEventListener('click', () => {
// Get clean text content without syntax highlighting spans
const code = getCleanCodeText(block.querySelector('code'));
navigator.clipboard.writeText(code).then(() => {
copyButton.innerHTML = 'β';
copyButton.style.background = 'rgba(80, 250, 123, 0.2)';
setTimeout(() => {
copyButton.innerHTML = 'π';
copyButton.style.background = 'rgba(255, 255, 255, 0.1)';
}, 2000);
// Log copy action
if (currentLogger) {
currentLogger.info('Code copied to clipboard', {
codeLength: code.length,
codePreview: code.substring(0, 50) + '...',
hasHighlighting: block.querySelector('.keyword') !== null
});
}
}).catch(err => {
copyButton.innerHTML = 'β';
copyButton.style.background = 'rgba(255, 121, 198, 0.2)';
setTimeout(() => {
copyButton.innerHTML = 'π';
copyButton.style.background = 'rgba(255, 255, 255, 0.1)';
}, 2000);
if (currentLogger) {
currentLogger.error('Failed to copy code', err);
}
});
});
block.style.position = 'relative';
block.appendChild(copyButton);
});
});
// Mobile menu toggle function
function toggleMobileMenu() {
const navLinks = document.getElementById('navLinks');
const toggleButton = document.querySelector('.mobile-menu-toggle');
navLinks.classList.toggle('active');
toggleButton.classList.toggle('active');
// Log mobile menu action
if (currentLogger) {
currentLogger.info('Mobile menu toggled', {
isOpen: navLinks.classList.contains('active')
});
}
}
// Close mobile menu when clicking on a nav link
document.addEventListener('DOMContentLoaded', () => {
const navLinks = document.querySelectorAll('.nav-link');
const navMenu = document.getElementById('navLinks');
const toggleButton = document.querySelector('.mobile-menu-toggle');
navLinks.forEach(link => {
link.addEventListener('click', () => {
if (navMenu.classList.contains('active')) {
navMenu.classList.remove('active');
if (toggleButton) {
toggleButton.classList.remove('active');
}
if (currentLogger) {
currentLogger.info('Mobile menu closed via navigation link');
}
}
});
});
// Close mobile menu when clicking outside
document.addEventListener('click', (e) => {
const isClickInsideNav = e.target.closest('.nav-container');
if (!isClickInsideNav && navMenu.classList.contains('active')) {
navMenu.classList.remove('active');
if (toggleButton) {
toggleButton.classList.remove('active');
}
if (currentLogger) {
currentLogger.info('Mobile menu closed via outside click');
}
}
});
});
// Performance optimization: Debounced resize handler
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
// Close mobile menu if window is resized to desktop size
if (window.innerWidth > 768) {
const navMenu = document.getElementById('navLinks');
const toggleButton = document.querySelector('.mobile-menu-toggle');
if (navMenu && navMenu.classList.contains('active')) {
navMenu.classList.remove('active');
if (toggleButton) {
toggleButton.classList.remove('active');
}
}
}
}, 250);
});
// Add some interactive effects to buttons
document.querySelectorAll('.btn').forEach(btn => {
btn.addEventListener('mouseenter', () => {
btn.style.transform = 'translateY(-2px)';
});
btn.addEventListener('mouseleave', () => {
btn.style.transform = 'translateY(0)';
});
btn.addEventListener('click', () => {
// Log button clicks
if (currentLogger) {
currentLogger.info('Button clicked', {
buttonText: btn.textContent,
buttonHref: btn.href || 'no-href',
buttonClass: btn.className
});
}
});
});
// Add debug console functions
if (typeof window !== 'undefined') {
window.webmagicDebug = {
getLogs: () => currentLogger ? currentLogger.getLogs() : [],
exportLogs: () => currentLogger ? currentLogger.exportLogs() : console.log('Logger not available'),
setLogLevel: (level) => currentLogger ? currentLogger.setLevel(level) : console.log('Logger not available'),
getCurrentLanguage: () => currentI18n ? currentI18n.getCurrentLanguage() : 'Unknown',
switchLanguage: (lang) => currentI18n ? currentI18n.setLanguage(lang) : console.log('i18n not available')
};
console.log('WebMagic Debug Functions Available:');
console.log('- webmagicDebug.getLogs()');
console.log('- webmagicDebug.exportLogs()');
console.log('- webmagicDebug.setLogLevel("DEBUG"|"INFO"|"WARN"|"ERROR")');
console.log('- webmagicDebug.getCurrentLanguage()');
console.log('- webmagicDebug.switchLanguage("zh"|"en")');
}
// Lazy loading for better performance
if ('IntersectionObserver' in window) {
const lazyElements = document.querySelectorAll('[data-lazy]');
const lazyObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const element = entry.target;
element.src = element.dataset.lazy;
element.removeAttribute('data-lazy');
lazyObserver.unobserve(element);
}
});
});
lazyElements.forEach(element => {
lazyObserver.observe(element);
});
}