This commit is contained in:
2025-09-01 10:15:35 +08:00
parent 5681c46c89
commit 94c090be16
2 changed files with 69 additions and 33 deletions

View File

@@ -163,7 +163,11 @@
}
.option-item {
display: inline-block; /* 保持行内块级特性 */
display: block !important; /* 强制块级显示 */
margin: 0.5em 0; /* 上下间距 */
padding: 0.3em; /* 内边距 */
width: 100%; /* 占满容器宽度 */
box-sizing: border-box; /* 确保padding不会溢出 */
padding-right: 15px; /* 序号与内容间距 */
font-weight: bold; /* 突出显示选项序号 */
}
@@ -196,7 +200,7 @@
height: 100%;
min-height: 200px;
color: #666;
background-color: rgba(249, 249, 249, 0.7); /* 调整透明度为0.7 */
background-color: rgba(249, 249, 249, 0.7); /* 将不透明度从0.95调整为0.7 */
}
.spinner {
@@ -339,35 +343,40 @@
function recognizeFormula() {
const imageUrl = document.getElementById('formulaImageUrl').value.trim();
const resultArea = document.getElementById('formulaResult');
if (!imageUrl) { alert('请输入图片URL'); return; }
resultArea.innerHTML = '<div class="loading-animation"><div class="spinner"></div><div>正在识别公式...</div></div>';
fetch('/api/recognize-formula', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image_url: imageUrl })
})
.then(response => response.json())
.then(data => {
if (data.error) { resultArea.innerHTML = `<div style="color:red">识别失败: ${data.error}</div>`; return; }
const content = data.Data?.content || '';
if (!content) { resultArea.innerHTML = '<div>未识别到公式内容</div>'; return; }
try {
resultArea.innerHTML = `<div class="tex2jax_process">$$${content}$$</div>`;
renderWithMathJax(resultArea);
} catch (error) {
console.error('公式处理错误:', error);
resultArea.innerHTML = `<div style="color:red">公式处理失败: ${error.message}</div>`;
}
})
.catch(error => {
console.error('请求错误:', error);
resultArea.innerHTML = `<div style="color:red">请求出错,请重试</div>`;
});
// 添加延迟模拟API调用
setTimeout(() => {
// 读取本地JSON文件
fetch('Formula.json')
.then(response => {
console.log('JSON请求响应:', response);
if (!response.ok) throw new Error('无法加载本地JSON文件');
return response.json();
})
.then(data => {
if (data.error) { resultArea.innerHTML = `<div style="color:red">识别失败: ${data.error}</div>`; return; }
const content = data.Data?.content || '';
if (!content) { resultArea.innerHTML = '<div>未识别到公式内容</div>'; return; }
try {
resultArea.innerHTML = `<div class="tex2jax_process">$$${content}$$</div>`;
renderWithMathJax(resultArea);
} catch (error) {
console.error('公式处理错误:', error);
resultArea.innerHTML = `<div style="color:red">公式处理失败: ${error.message}</div>`;
}
})
.catch(error => {
console.error('请求错误:', error);
const errorMsg = '加载本地JSON失败: ' + error.message;
resultArea.innerHTML = `<div style="color:red">${errorMsg}</div>`;
});
}, 1000); // 1秒延迟
}
// 识别试题
@@ -397,10 +406,24 @@
console.log("处理前:"+content);
try {
// 处理选项格式 - 序号与内容同行,选项间换行
content = content.replace(/([A-D])\s*\.\s*/g, '<span class="option-item">$$$1. </span><br>');
console.log("处理后:"+content);
// 确保不替换已添加的<br>标签
// 彻底修复选项标签闭合问题 - 采用分而治之策略
// 1. 首先拆分所有选项为独立数组
const options = content.split(/(?=[A-D]\s*\.)/);
// 2. 逐个处理每个选项确保标签完整
content = options.map(option => {
// 仅处理以选项标签开头的部分
if (/^[A-D]\s*\./.test(option)) {
return `<span class="option-item">$${option}$</span>`; // 添加$符号
}
return option;
}).join('');
// 3. 清理可能的空标签
content = content.replace(/<span class="option-item"><\/span>/g, '');
if (!content.includes('<span class="option-item">')) {
content = content.replace(/((?:[A-D]\s*\.\s*[^.]+)+)/g, '<span class="option-item">$1</span>');
}
// 添加选项间的分隔处理
content = content.replace(/<\/span>\s*<span class="option-item">/g, '</span>\n<span class="option-item">');
const latexContainer = document.createElement('div');
latexContainer.className = 'tex2jax_process';
latexContainer.innerHTML = content;