Files
doc_ai_frontend/src/components/ResultPanel.tsx

138 lines
5.6 KiB
TypeScript
Raw Normal View History

2025-12-22 17:37:41 +08:00
import { useState } from 'react';
import { Download, Code2, Check, Copy } from 'lucide-react';
import ReactMarkdown from 'react-markdown';
import remarkMath from 'remark-math';
2025-12-26 15:53:11 +08:00
import remarkBreaks from 'remark-breaks';
2025-12-22 17:37:41 +08:00
import rehypeKatex from 'rehype-katex';
import 'katex/dist/katex.min.css';
import { RecognitionResult } from '../types';
import ExportSidebar from './ExportSidebar';
interface ResultPanelProps {
result: RecognitionResult | null;
fileStatus?: 'pending' | 'processing' | 'completed' | 'failed';
}
/**
* Preprocess LaTeX content to fix common formatting issues
* that may cause KaTeX rendering to fail
*/
function preprocessLatex(content: string): string {
if (!content) return '';
2025-12-26 15:53:11 +08:00
2025-12-22 17:37:41 +08:00
let processed = content;
2025-12-26 15:53:11 +08:00
// Convert literal \n (escaped newlines from OCR) to actual newlines
// This handles cases where the API returns "\\n" as a string instead of actual newline characters
2025-12-22 17:37:41 +08:00
// Fix mixed display math delimiters: $$\[...\]$$ -> $$...$$
// This handles cases where \[ \] are incorrectly nested inside $$ $$
// Note: In JS replace(), $$ means "insert one $", so we need $$$$ to insert $$
processed = processed.replace(/\$\$\s*\\\[/g, '$$$$');
processed = processed.replace(/\\\]\s*\$\$/g, '$$$$');
2025-12-26 15:53:11 +08:00
2025-12-22 17:37:41 +08:00
// Also fix standalone \[ and \] that should be $$ for remark-math compatibility
// Replace \[ with $$ at the start of display math (not inside other math)
processed = processed.replace(/(?<!\$)\\\[(?!\$)/g, '$$$$');
processed = processed.replace(/(?<!\$)\\\](?!\$)/g, '$$$$');
2025-12-26 15:53:11 +08:00
2025-12-22 17:37:41 +08:00
// IMPORTANT: remark-math requires $$ to be on its own line for block math
// Ensure $$ followed by \begin has a newline: $$\begin -> $$\n\begin
processed = processed.replace(/\$\$([^\n$])/g, '$$$$\n$1');
// Ensure content followed by $$ has a newline: content$$ -> content\n$$
processed = processed.replace(/([^\n$])\$\$/g, '$1\n$$$$');
2025-12-26 15:53:11 +08:00
// // Fix: \left \{ -> \left\{ (remove space between \left and delimiter)
2025-12-22 17:37:41 +08:00
processed = processed.replace(/\\left\s+\\/g, '\\left\\');
processed = processed.replace(/\\left\s+\{/g, '\\left\\{');
processed = processed.replace(/\\left\s+\[/g, '\\left[');
processed = processed.replace(/\\left\s+\(/g, '\\left(');
2025-12-26 15:53:11 +08:00
// // Fix: \right \} -> \right\} (remove space between \right and delimiter)
2025-12-22 17:37:41 +08:00
processed = processed.replace(/\\right\s+\\/g, '\\right\\');
processed = processed.replace(/\\right\s+\}/g, '\\right\\}');
processed = processed.replace(/\\right\s+\]/g, '\\right]');
processed = processed.replace(/\\right\s+\)/g, '\\right)');
2025-12-26 15:53:11 +08:00
2025-12-22 17:37:41 +08:00
return processed;
}
export default function ResultPanel({ result, fileStatus }: ResultPanelProps) {
const [isExportSidebarOpen, setIsExportSidebarOpen] = useState(false);
if (!result) {
if (fileStatus === 'processing' || fileStatus === 'pending') {
return (
<div className="h-full flex flex-col items-center justify-center bg-white text-center p-8">
2025-12-26 15:53:11 +08:00
<div className="w-16 h-16 border-4 border-blue-600 border-t-transparent rounded-full animate-spin mb-6"></div>
2025-12-22 17:37:41 +08:00
<h3 className="text-xl font-semibold text-gray-900 mb-2">
{fileStatus === 'pending' ? 'Waiting in queue...' : 'Analyzing...'}
</h3>
<p className="text-gray-500 max-w-sm">
2025-12-26 15:53:11 +08:00
{fileStatus === 'pending'
? 'Your file is in the queue, please wait.'
2025-12-22 17:37:41 +08:00
: 'Texpixel is processing your file, this may take a moment.'}
</p>
</div>
);
}
return (
<div className="h-full flex flex-col items-center justify-center bg-white text-center p-8">
<div className="w-32 h-32 bg-gray-100 rounded-full flex items-center justify-center mb-6 shadow-inner">
<Code2 size={48} className="text-gray-900" />
</div>
<h3 className="text-xl font-semibold text-gray-900 mb-2">Waiting for recognition result</h3>
<p className="text-gray-500 max-w-sm">
After uploading the file, Texpixel will automatically recognize and display the result here
</p>
</div>
);
}
return (
<div className="flex flex-col h-full bg-white relative overflow-hidden">
{/* Top Header */}
<div className="h-16 px-6 border-b border-gray-200 flex items-center justify-between bg-white shrink-0 z-10">
<div className="flex items-center gap-4">
<h2 className="text-lg font-bold text-gray-900">Markdown</h2>
</div>
<button
onClick={() => setIsExportSidebarOpen(true)}
className={`px-4 py-2 bg-gray-900 text-white text-sm font-medium rounded-lg hover:bg-gray-800 transition-colors flex items-center gap-2 shadow-sm ${isExportSidebarOpen ? 'opacity-0 pointer-events-none' : ''}`}
>
<Download size={16} />
Export
</button>
</div>
{/* Content Area - Rendered Markdown */}
{
<div className="flex-1 overflow-auto p-8 custom-scrollbar flex justify-center">
<div className="prose prose-blue max-w-3xl w-full prose-headings:font-bold prose-h1:text-2xl prose-h2:text-xl prose-p:leading-relaxed prose-pre:bg-gray-50 prose-pre:border prose-pre:border-gray-100 [&_.katex-display]:text-center">
<ReactMarkdown
2025-12-26 15:53:11 +08:00
remarkPlugins={[remarkMath, remarkBreaks]}
rehypePlugins={[[rehypeKatex, {
throwOnError: false,
2025-12-22 17:37:41 +08:00
errorColor: '#cc0000',
2025-12-26 15:53:11 +08:00
strict: false
2025-12-22 17:37:41 +08:00
}]]}
>
{preprocessLatex(result.markdown_content || '')}
</ReactMarkdown>
</div>
</div>
}
<ExportSidebar
isOpen={isExportSidebarOpen}
onClose={() => setIsExportSidebarOpen(false)}
result={result}
/>
</div >
);
}