chore: backup before code cleanup

Backup commit before executing remove-unused-code proposal.
This includes all pending changes and new features.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
egg
2025-12-11 11:55:39 +08:00
parent eff9b0bcd5
commit 940a406dce
58 changed files with 8226 additions and 175 deletions

View File

@@ -0,0 +1,358 @@
import { useState } from 'react'
import { cn } from '@/lib/utils'
import { Check, ChevronDown, ChevronUp, FileText, Table, Settings, FileEdit, Layers, Cog } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import type { OCRPreset, OCRConfig, TableParsingMode, OCRPresetInfo } from '@/types/apiV2'
interface OCRPresetSelectorProps {
value: OCRPreset
onChange: (preset: OCRPreset) => void
customConfig?: OCRConfig
onCustomConfigChange?: (config: OCRConfig) => void
disabled?: boolean
className?: string
}
// Preset icons mapping
const PRESET_ICONS: Record<OCRPreset, React.ReactNode> = {
text_heavy: <FileText className="w-5 h-5" />,
datasheet: <Table className="w-5 h-5" />,
table_heavy: <Layers className="w-5 h-5" />,
form: <FileEdit className="w-5 h-5" />,
mixed: <Settings className="w-5 h-5" />,
custom: <Cog className="w-5 h-5" />,
}
// Preset configurations (matching backend OCR_PRESET_CONFIGS)
const PRESET_CONFIGS: Record<OCRPreset, OCRConfig> = {
text_heavy: {
table_parsing_mode: 'disabled',
enable_wired_table: false,
enable_wireless_table: false,
enable_chart_recognition: false,
enable_formula_recognition: false,
},
datasheet: {
table_parsing_mode: 'conservative',
enable_wired_table: true,
enable_wireless_table: false,
},
table_heavy: {
table_parsing_mode: 'full',
enable_wired_table: true,
enable_wireless_table: true,
},
form: {
table_parsing_mode: 'conservative',
enable_wired_table: true,
enable_wireless_table: false,
},
mixed: {
table_parsing_mode: 'classification_only',
enable_wired_table: true,
enable_wireless_table: false,
},
custom: {},
}
// Preset info for display
const PRESET_INFO: Record<OCRPreset, { displayName: string; description: string }> = {
text_heavy: {
displayName: '純文字文件',
description: '報告、文章、手冊等以文字為主的文件。禁用表格識別以提高處理速度。',
},
datasheet: {
displayName: '技術規格書',
description: '產品規格書、技術數據表 (TDS)。使用保守模式避免過度分割。',
},
table_heavy: {
displayName: '表格密集文件',
description: '財務報表、試算表。啟用完整表格識別以捕捉所有表格。',
},
form: {
displayName: '表單',
description: '申請表、問卷調查。識別表單欄位但避免過度分割。',
},
mixed: {
displayName: '混合內容',
description: '一般文件。只做表格區域分類,不做細胞分割。',
},
custom: {
displayName: '自訂設定',
description: '進階使用者可自行調整所有 PP-Structure 參數。',
},
}
export default function OCRPresetSelector({
value,
onChange,
customConfig,
onCustomConfigChange,
disabled = false,
className,
}: OCRPresetSelectorProps) {
const { t } = useTranslation()
const [showAdvanced, setShowAdvanced] = useState(false)
const presets: OCRPreset[] = ['datasheet', 'text_heavy', 'table_heavy', 'form', 'mixed', 'custom']
const getPresetInfo = (preset: OCRPreset) => PRESET_INFO[preset]
// Get effective config (preset config merged with custom overrides)
const getEffectiveConfig = (): OCRConfig => {
if (value === 'custom') {
return customConfig || {}
}
return { ...PRESET_CONFIGS[value], ...customConfig }
}
const handleCustomConfigChange = (key: keyof OCRConfig, val: any) => {
if (onCustomConfigChange) {
onCustomConfigChange({
...customConfig,
[key]: val,
})
}
}
return (
<div className={cn('border rounded-lg p-4 bg-white', className)}>
{/* Header */}
<div className="flex items-center justify-between mb-4">
<div className="flex items-center gap-2">
<Settings className="w-5 h-5 text-gray-600" />
<h3 className="text-lg font-semibold text-gray-900">OCR </h3>
</div>
<button
type="button"
onClick={() => setShowAdvanced(!showAdvanced)}
className="text-sm text-blue-600 hover:text-blue-800 flex items-center gap-1"
disabled={disabled}
>
{showAdvanced ? (
<>
<ChevronUp className="w-4 h-4" />
</>
) : (
<>
<ChevronDown className="w-4 h-4" />
</>
)}
</button>
</div>
{/* Preset Grid */}
<div className="grid grid-cols-2 md:grid-cols-3 gap-3">
{presets.map((preset) => {
const info = getPresetInfo(preset)
const isSelected = value === preset
return (
<button
key={preset}
type="button"
disabled={disabled}
onClick={() => onChange(preset)}
className={cn(
'flex flex-col items-center gap-2 p-4 rounded-lg border-2 transition-all text-center',
isSelected
? 'border-blue-500 bg-blue-50'
: 'border-gray-200 hover:border-gray-300 hover:bg-gray-50',
disabled && 'opacity-50 cursor-not-allowed'
)}
>
{/* Icon */}
<div
className={cn(
'p-3 rounded-lg',
isSelected ? 'bg-blue-100 text-blue-600' : 'bg-gray-100 text-gray-500'
)}
>
{PRESET_ICONS[preset]}
</div>
{/* Label */}
<div className="flex items-center gap-1">
<span
className={cn(
'font-medium text-sm',
isSelected ? 'text-blue-700' : 'text-gray-900'
)}
>
{info.displayName}
</span>
{isSelected && <Check className="w-4 h-4 text-blue-600" />}
</div>
{/* Recommended badge */}
{preset === 'datasheet' && (
<span className="text-xs bg-green-100 text-green-700 px-2 py-0.5 rounded-full">
</span>
)}
</button>
)
})}
</div>
{/* Selected Preset Description */}
<div className="mt-4 p-3 bg-gray-50 border border-gray-200 rounded-md">
<p className="text-sm text-gray-700">
<span className="font-medium">{PRESET_INFO[value].displayName}</span>
{PRESET_INFO[value].description}
</p>
</div>
{/* Advanced Settings Panel */}
{showAdvanced && (
<div className="mt-4 p-4 bg-gray-50 border border-gray-200 rounded-lg space-y-4">
<h4 className="font-medium text-gray-900 flex items-center gap-2">
<Cog className="w-4 h-4" />
</h4>
{/* Table Parsing Mode */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
</label>
<select
value={getEffectiveConfig().table_parsing_mode || 'conservative'}
onChange={(e) => handleCustomConfigChange('table_parsing_mode', e.target.value as TableParsingMode)}
disabled={disabled || value !== 'custom'}
className={cn(
'w-full px-3 py-2 border border-gray-300 rounded-md text-sm',
(disabled || value !== 'custom') && 'bg-gray-100 cursor-not-allowed'
)}
>
<option value="full"> ()</option>
<option value="conservative"> ()</option>
<option value="classification_only"></option>
<option value="disabled"></option>
</select>
<p className="mt-1 text-xs text-gray-500">
{value !== 'custom' && '選擇「自訂設定」預設以調整此參數'}
</p>
</div>
{/* Table Detection Options */}
<div className="grid grid-cols-2 gap-4">
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().enable_wired_table ?? true}
onChange={(e) => handleCustomConfigChange('enable_wired_table', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().enable_wireless_table ?? false}
onChange={(e) => handleCustomConfigChange('enable_wireless_table', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
</div>
{/* Recognition Modules */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
</label>
<div className="grid grid-cols-2 gap-4">
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().enable_chart_recognition ?? true}
onChange={(e) => handleCustomConfigChange('enable_chart_recognition', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().enable_formula_recognition ?? true}
onChange={(e) => handleCustomConfigChange('enable_formula_recognition', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().enable_seal_recognition ?? false}
onChange={(e) => handleCustomConfigChange('enable_seal_recognition', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().enable_region_detection ?? true}
onChange={(e) => handleCustomConfigChange('enable_region_detection', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
</div>
</div>
{/* Preprocessing Options */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
</label>
<div className="grid grid-cols-2 gap-4">
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().use_doc_orientation_classify ?? true}
onChange={(e) => handleCustomConfigChange('use_doc_orientation_classify', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
<label className="flex items-center gap-2">
<input
type="checkbox"
checked={getEffectiveConfig().use_doc_unwarping ?? false}
onChange={(e) => handleCustomConfigChange('use_doc_unwarping', e.target.checked)}
disabled={disabled || value !== 'custom'}
className="rounded border-gray-300"
/>
<span className="text-sm text-gray-700"></span>
</label>
</div>
</div>
{value !== 'custom' && (
<p className="text-xs text-amber-600 bg-amber-50 p-2 rounded">
調
</p>
)}
</div>
)}
{/* Info Note */}
<div className="mt-4 p-3 bg-blue-50 border border-blue-200 rounded-md">
<p className="text-sm text-blue-800">
PP-Structure
</p>
</div>
</div>
)
}