// 代码解释与修复助手 - 前端JavaScript交互逻辑 // DOM元素 const codeInput = document.getElementById('codeInput'); const languageSelect = document.getElementById('languageSelect'); const taskTypeSelect = document.getElementById('taskTypeSelect'); const depthSelect = document.getElementById('depthSelect'); const depthGroup = document.getElementById('depthGroup'); const startBtn = document.getElementById('startBtn'); const clearBtn = document.getElementById('clearBtn'); const statusIndicator = document.getElementById('statusIndicator'); const loadingIndicator = document.getElementById('loadingIndicator'); const resultContent = document.getElementById('resultContent'); // 任务类型切换 function toggleDepthOption() { if (taskTypeSelect.value === 'explain') { depthGroup.style.display = 'block'; } else { depthGroup.style.display = 'none'; } } // 初始化事件监听 function initEventListeners() { // 任务类型选择变化 taskTypeSelect.addEventListener('change', toggleDepthOption); // 开始分析按钮 startBtn.addEventListener('click', analyzeCode); // 清空按钮 clearBtn.addEventListener('click', clearAll); // 键盘快捷键 codeInput.addEventListener('keydown', (e) => { // Ctrl+Enter 或 Cmd+Enter 触发分析 if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); analyzeCode(); } }); } // 更新状态显示 function updateStatus(status, message) { statusIndicator.className = `status status-${status}`; statusIndicator.innerHTML = ` ${message}`; } // 显示加载状态 function showLoading() { loadingIndicator.style.display = 'flex'; resultContent.style.display = 'none'; } // 隐藏加载状态 function hideLoading() { loadingIndicator.style.display = 'none'; resultContent.style.display = 'block'; } // 显示结果 function showResult(data) { if (data.success) { resultContent.innerHTML = `
无结果
'; // 简单的Markdown到HTML转换 let html = result .replace(/^# (.*$)/gm, '$1')
.replace(/```([\s\S]*?)```/g, '$1')
.replace(/\n/g, '输入代码并点击"开始分析"按钮,获取AI分析结果