document.addEventListener('DOMContentLoaded', function() { // Sidebar toggle const sidebarToggle = document.getElementById('sidebar-toggle'); const sidebar = document.querySelector('.sidebar'); if (sidebarToggle && sidebar) { sidebarToggle.addEventListener('click', function() { sidebar.classList.toggle('open'); }); } // Load category tree loadCategoryTree(); // Initialize keyboard shortcuts initKeyboardShortcuts(); }); // Load and render category tree in the sidebar function loadCategoryTree() { const categoryTree = document.getElementById('category-tree'); if (!categoryTree) return; fetch('/api/categories') .then(response => response.json()) .then(categories => { renderCategoryTree(categories, categoryTree); }) .catch(error => { console.error('Error loading categories:', error); }); } // Recursively render category tree function renderCategoryTree(categories, container) { if (!categories || categories.length === 0) return; categories.forEach(category => { const li = document.createElement('li'); const link = document.createElement('a'); link.href = `/category/${category.id}`; link.innerHTML = ` ${category.name} `; li.appendChild(link); // If this category has children, add a nested ul if (category.children && category.children.length > 0) { const ul = document.createElement('ul'); ul.className = 'category-tree'; renderCategoryTree(category.children, ul); li.appendChild(ul); } container.appendChild(li); }); } // Add keyboard shortcuts function initKeyboardShortcuts() { document.addEventListener('keydown', function(e) { // Only process if not in an input element if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.isContentEditable) { return; } // Ctrl+E to edit current document if (e.ctrlKey && e.key === 'e') { e.preventDefault(); // Check if on document view page const path = window.location.pathname; if (path.match(/^\/document\/\d+$/)) { // Extract document ID and redirect to edit page const docId = path.split('/').pop(); window.location.href = `/document/${docId}/edit`; } } // 'n' to create new document if (e.key === 'n' && !e.ctrlKey && !e.altKey && !e.metaKey) { e.preventDefault(); window.location.href = '/document/new'; } // '/' to focus search (if we had one) if (e.key === '/') { e.preventDefault(); const searchInput = document.getElementById('search-input'); if (searchInput) { searchInput.focus(); } } // 'g h' sequence for home if (e.key === 'g') { const keySequence = function(e) { if (e.key === 'h') { window.location.href = '/'; } document.removeEventListener('keydown', keySequence); }; document.addEventListener('keydown', keySequence); } }); } // Fetch root categories for API function fetchCategories() { return fetch('/api/categories') .then(response => response.json()) .catch(error => { console.error('Error fetching categories:', error); return []; }); }