/** * ComparePreview - Document Comparison Preview Component * * Features: * - Compare two documents using Monaco Editor * - Support PDF and Word (.docx) files * - Automatic text extraction and line-by-line comparison * - Navigation between differences * * Props: * - doc1Path: Original document path * - doc2Path: Comparison document path */ import React, { useState, useRef, useEffect } from "react"; import { DiffEditor, loader } from "@monaco-editor/react"; import type { editor } from "monaco-editor"; import { pdfjs } from 'react-pdf'; import mammoth from 'mammoth'; import { toastService } from '~/components/ui/Toast'; import { DOCUMENT_URL } from '~/config/api-config'; // Setup PDF.js worker pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.js'; // 配置 Monaco Editor 使用本地资源(避免 CDN 加载超时) // Monaco Editor 资源已通过 npm run copy-monaco 复制到 public/monaco-editor if (typeof window !== 'undefined') { console.log('[Monaco] 使用本地资源加载'); loader.config({ paths: { vs: '/monaco-editor/vs' } }); // 添加加载超时监控和错误处理 const initTimeout = setTimeout(() => { console.error('[Monaco] 加载超时(30秒)'); toastService.error('代码编辑器加载超时,请刷新页面重试'); }, 30000); loader.init().then(() => { clearTimeout(initTimeout); console.log('[Monaco] ✅ 加载成功'); }).catch((error: Error) => { clearTimeout(initTimeout); console.error('[Monaco] ❌ 加载失败:', error); toastService.error(`代码编辑器加载失败: ${error.message}`); }); } // Document type enum type DocumentType = 'pdf' | 'docx' | 'unknown'; // PDF type enum type PdfType = 'text' | 'scanned' | 'unknown'; // PDF info interface interface PdfInfo { type: PdfType; numPages: number; textLength: number; confidence: number; } // Document info interface interface DocumentInfo { fileType: DocumentType; pdfType?: PdfType; numPages?: number; textLength: number; confidence: number; } // Component Props interface interface ComparePreviewProps { doc1Path: string; doc2Path: string; } export function ComparePreview({ doc1Path, doc2Path }: ComparePreviewProps): JSX.Element { // 如果没有模板合同路径,直接返回提示 if (!doc2Path || doc2Path.trim() === '') { return (
该文档类型暂未上传模板合同,无法进行对比分析。