first commit
This commit is contained in:
129
static/js/api.js
Normal file
129
static/js/api.js
Normal file
@@ -0,0 +1,129 @@
|
||||
// Copy code to clipboard
|
||||
function copyCode(btn) {
|
||||
const pre = btn.closest('pre');
|
||||
const code = pre.querySelector('code').textContent;
|
||||
|
||||
navigator.clipboard.writeText(code).then(() => {
|
||||
const originalHTML = btn.innerHTML;
|
||||
btn.innerHTML = '<i class="fas fa-check"></i> Copied!';
|
||||
btn.classList.add('btn-success');
|
||||
btn.classList.remove('btn-secondary');
|
||||
|
||||
setTimeout(() => {
|
||||
btn.innerHTML = originalHTML;
|
||||
btn.classList.remove('btn-success');
|
||||
btn.classList.add('btn-secondary');
|
||||
}, 2000);
|
||||
}).catch(err => {
|
||||
console.error('Failed to copy:', err);
|
||||
alert('Failed to copy to clipboard');
|
||||
});
|
||||
}
|
||||
|
||||
// Theme toggle
|
||||
function toggleTheme() {
|
||||
const html = document.documentElement;
|
||||
const currentTheme = html.getAttribute('data-bs-theme');
|
||||
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
||||
html.setAttribute('data-bs-theme', newTheme);
|
||||
localStorage.setItem('theme', newTheme);
|
||||
|
||||
const icon = document.getElementById('themeIcon');
|
||||
if (icon) {
|
||||
icon.className = newTheme === 'dark' ? 'fas fa-moon' : 'fas fa-sun';
|
||||
}
|
||||
}
|
||||
|
||||
// Load saved theme on page load
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
const savedTheme = localStorage.getItem('theme') || 'dark';
|
||||
document.documentElement.setAttribute('data-bs-theme', savedTheme);
|
||||
const icon = document.getElementById('themeIcon');
|
||||
if (icon) {
|
||||
icon.className = savedTheme === 'dark' ? 'fas fa-moon' : 'fas fa-sun';
|
||||
}
|
||||
|
||||
// Initialize syntax highlighting
|
||||
if (typeof hljs !== 'undefined') {
|
||||
hljs.highlightAll();
|
||||
}
|
||||
});
|
||||
|
||||
// Smooth scroll for navigation links
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
||||
anchor.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
const target = document.querySelector(this.getAttribute('href'));
|
||||
if (target) {
|
||||
target.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
block: 'start',
|
||||
inline: 'nearest'
|
||||
});
|
||||
|
||||
// Highlight target card briefly
|
||||
target.classList.add('border-primary');
|
||||
setTimeout(() => {
|
||||
target.classList.remove('border-primary');
|
||||
}, 2000);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Active navigation highlighting on scroll
|
||||
window.addEventListener('scroll', () => {
|
||||
const sections = document.querySelectorAll('.endpoint-card');
|
||||
const navLinks = document.querySelectorAll('.list-group-item');
|
||||
|
||||
let current = '';
|
||||
|
||||
sections.forEach(section => {
|
||||
const sectionTop = section.offsetTop;
|
||||
const sectionHeight = section.clientHeight;
|
||||
|
||||
// Check if section is in viewport (with offset for navbar)
|
||||
if (window.pageYOffset >= sectionTop - 120) {
|
||||
current = section.getAttribute('id');
|
||||
}
|
||||
});
|
||||
|
||||
// Update active link
|
||||
navLinks.forEach(link => {
|
||||
link.classList.remove('active');
|
||||
if (link.getAttribute('href') === '#' + current) {
|
||||
link.classList.add('active');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Test API endpoint directly from documentation (optional utility)
|
||||
async function testEndpoint(endpoint, method = 'POST', body = {}) {
|
||||
try {
|
||||
const response = await fetch(endpoint, {
|
||||
method: method,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
const data = await response.text();
|
||||
console.log('API Response:', data);
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('API Error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Quick copy all code snippets (Ctrl+Shift+C)
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.ctrlKey && e.shiftKey && e.key === 'C') {
|
||||
const allCode = Array.from(document.querySelectorAll('pre code'))
|
||||
.map(code => code.textContent)
|
||||
.join('\n\n' + '='.repeat(50) + '\n\n');
|
||||
|
||||
navigator.clipboard.writeText(allCode).then(() => {
|
||||
alert('All code snippets copied to clipboard!');
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user