class CommandPalette { constructor() { this.overlay = document.getElementById('command-palette-overlay'); this.input = document.getElementById('command-palette-input'); this.results = document.getElementById('command-palette-results'); this.commands = []; this.selectedIndex = 0; this.filteredCommands = []; this.setupEventListeners(); } register(label, shortcut, action) { this.commands.push({ label, shortcut, action }); } open() { this.overlay.classList.remove('hidden'); this.input.value = ''; this.input.focus(); this.selectedIndex = 0; this.renderResults(''); } close() { this.overlay.classList.add('hidden'); } isOpen() { return !this.overlay.classList.contains('hidden'); } setupEventListeners() { this.input.addEventListener('input', () => { this.selectedIndex = 0; this.renderResults(this.input.value); }); this.overlay.addEventListener('click', (e) => { if (e.target === this.overlay) this.close(); }); this.input.addEventListener('keydown', (e) => { if (e.key === 'Escape') { e.preventDefault(); this.close(); } else if (e.key === 'Enter') { e.preventDefault(); this.executeSelected(); } else if (e.key === 'ArrowDown') { e.preventDefault(); this.selectedIndex = Math.min(this.selectedIndex + 1, this.filteredCommands.length - 1); this.updateSelection(); } else if (e.key === 'ArrowUp') { e.preventDefault(); this.selectedIndex = Math.max(this.selectedIndex - 1, 0); this.updateSelection(); } }); } renderResults(query) { this.filteredCommands = query ? this.commands.filter(cmd => cmd.label.toLowerCase().includes(query.toLowerCase())) : [...this.commands]; this.results.innerHTML = this.filteredCommands.map((cmd, i) => `