8.8 KiB
8.8 KiB
模块对比快速参考指南
一句话总结
✅ 高质量的模块提取,保留了93%的功能完整性,但有3个高优先级bug需要修复。
快速检查表
TextFittingAdapter ✅⚠️
| 检查项 | 状态 | 备注 |
|---|---|---|
| 初始化逻辑 | ✅ | 完整 |
| 字号预处理 | ❌ | BUG: BBOX_NORMALIZED_RANGE 未定义 |
| 文本绘制 | ✅ | Canvas渲染算法完美移植 |
| 换行算法 | ✅ | CJK处理完整 |
| 公式缓存 | ✅ | 优化的缓存机制 |
| 参数验证 | ❌ | 缺少ctx、参数检查 |
快速修复:
// 在 line 71 添加
const BBOX_NORMALIZED_RANGE = 1000;
PDFExporter ✅⚠️⚠️
| 检查项 | 状态 | 备注 |
|---|---|---|
| PDF加载 | ✅ | 完整 |
| 字体嵌入 | ⚠️ | fontkit失败继续(可能乱码) |
| 文本布局 | ❌ | BUG: Canvas和PDF公式不一致 |
| 文本换行 | ✅ | 正确实现 |
| 参数验证 | ❌ | showNotification类型检查缺失 |
关键差异:
// Canvas版本 (正确)
const totalHeight = lines.length > 0
? (lines.length - 1) * lineHeight + mid * 1.2
: 0;
// PDF版本 (错误)
const totalHeight = lines.length > 0
? (lines.length - 1) * lineHeight + mid // ❌ 差异 20%
: 0;
快速修复: 使用相同的公式
SegmentManager ⚠️✅
| 检查项 | 状态 | 备注 |
|---|---|---|
| 段分割 | ✅ | 算法完美 |
| DOM创建 | ⚠️ | 缺少容器验证 |
| 懒加载 | ❌ | BUG: 事件监听器无法清理 |
| 渲染管理 | ✅ | 防并发逻辑完善 |
| 资源清理 | ❌ | destroy()不能真正清理 |
快速修复: 保存事件处理函数引用以便移除
文件位置速查
f:\pb\paper-burner\
├── js\history\
│ ├── history_pdf_compare.js ← 原始文件
│ └── modules\
│ ├── TextFittingAdapter.js ← 文本适配 (420行)
│ ├── PDFExporter.js ← PDF导出 (433行)
│ └── SegmentManager.js ← 段管理 (420行)
├── CODE_REVIEW_MODULES.md ← 详细审查报告
├── MODULE_COMPARISON_DETAILED.md ← 详细对比
├── MODULE_FIX_RECOMMENDATIONS.md ← 修复建议
├── CODE_REVIEW_SUMMARY.md ← 总结报告 (本文件)
└── QUICK_REFERENCE.md ← 快速参考 (本文件)
3分钟速览问题
问题1: TextFittingAdapter.js line 71
❌ 现在的代码:
const height = (bbox[3] - bbox[1]) / BBOX_NORMALIZED_RANGE; // undefined!
✅ 应该是:
const BBOX_NORMALIZED_RANGE = 1000; // 添加这行
const height = (bbox[3] - bbox[1]) / BBOX_NORMALIZED_RANGE;
影响: 运行时NaN错误 → 字号计算失败
问题2: PDFExporter.js line 272-274 vs 1463-1465
❌ 不一致的公式:
// Canvas版 (history_pdf_compare.js 1463-1465)
const totalHeight = (lines.length - 1) * lineHeight + mid * 1.2;
// PDF版 (PDFExporter.js 272-274)
const totalHeight = (lines.length - 1) * lineHeight + mid; // 差异 20%
✅ 应该统一为:
// 两个版本都使用相同公式
const totalHeight = lines.length > 0
? (lines.length - 1) * lineHeight + mid * 1.2
: 0;
影响: PDF导出文本大小与Canvas显示不同
问题3: SegmentManager.js line 230-232, 399-404
❌ 当前的清理:
destroy() {
// 这些注释说明了问题
// "注意:由于事件监听使用了箭头函数,无法直接移除"
this.segments = []; // ❌ 这不能停止事件
}
✅ 应该改为:
// 在初始化时保存处理函数
this._originalScrollHandler = () => onScroll(this.originalScroll);
// 在destroy中移除
this.originalScroll.removeEventListener('scroll', this._originalScrollHandler);
影响: 内存泄漏,事件持续执行
修复优先级
| 优先级 | 问题 | 耗时 | 修复后测试 |
|---|---|---|---|
| 🔴1 | BBOX_NORMALIZED_RANGE | 5分钟 | preprocessGlobalFontSizes() |
| 🔴2 | 文本高度公式 | 10分钟 | exportStructuredTranslation() |
| 🔴3 | 事件监听清理 | 20分钟 | destroy() 事件追踪 |
| 🟡4+ | 参数验证 | 30分钟 | 提供无效参数测试 |
总计修复时间: ~1.5小时(仅高优先级)
使用流程检查
✅ 正确的初始化顺序
// 1. 创建适配器
const textFitter = new TextFittingAdapter({
globalFontScale: 0.85
});
textFitter.initialize(); // 检查TextFittingEngine
// 2. 创建段管理器
const segmentMgr = new SegmentManager(pdfDoc);
// 3. 注入依赖 (必须)
segmentMgr.setDependencies({
renderPageBboxesToCtx: bboxRenderer,
renderPageTranslationToCtx: translationRenderer,
// ... 其他依赖
});
// 4. 设置容器 (必须)
segmentMgr.setContainers(
document.getElementById('original-segments'),
document.getElementById('translation-segments'),
document.getElementById('original-scroll'),
document.getElementById('translation-scroll')
);
// 5. 渲染
await segmentMgr.renderAllPagesContinuous();
// 6. 导出
const exporter = new PDFExporter();
await exporter.exportStructuredTranslation(pdfBase64, translatedData);
// 7. 清理 (必须)
segmentMgr.destroy(); // 移除事件监听
textFitter.clearCache();
方法映射表
| 原始 (PDFCompareView) | 迁移到 | 状态 |
|---|---|---|
| initializeTextFitting() | TextFittingAdapter.initialize() | ✅ |
| preprocessGlobalFontSizes() | TextFittingAdapter.preprocessGlobalFontSizes() | ⚠️ BUG |
| drawPlainTextInBox() | TextFittingAdapter.drawPlainTextInBox() | ✅ |
| drawPlainTextWithFitting() | TextFittingAdapter.drawPlainTextWithFitting() | ✅ |
| wrapText() | TextFittingAdapter.wrapText() | ✅ |
| renderFormulasInText() | TextFittingAdapter.renderFormulasInText() | ✅ |
| --- | --- | --- |
| exportStructuredTranslation() | PDFExporter.exportStructuredTranslation() | ⚠️ BUG |
| calculatePdfTextLayout() | PDFExporter.calculatePdfTextLayout() | ⚠️ BUG |
| wrapTextForPdf() | PDFExporter.wrapTextForPdf() | ✅ |
| loadPdfLib() | PDFExporter.loadPdfLib() | ⚠️ |
| --- | --- | --- |
| renderAllPagesContinuous() | SegmentManager.renderAllPagesContinuous() | ✅ |
| createSegmentDom() | SegmentManager.createSegmentDom() | ⚠️ |
| initLazyLoadingSegments() | SegmentManager.initLazyLoadingSegments() | ❌ BUG |
| renderVisibleSegments() | SegmentManager.renderVisibleSegments() | ✅ |
| renderSegment() | SegmentManager.renderSegment() | ✅ |
| renderSegmentOverlays() | SegmentManager.renderSegmentOverlays() | ✅ |
| destroy() | SegmentManager.destroy() | ❌ BUG |
测试命令
// 验证TextFittingAdapter bug fix
const adapter = new TextFittingAdapter();
adapter.preprocessGlobalFontSizes(
[{type: 'text', bbox: [0, 0, 1000, 100]}],
[{text: 'test'}]
);
// 如果能执行说明bug已修复
// 验证PDF导出公式一致性
const pdf = await exporter.exportStructuredTranslation(...);
// 检查PDF文本大小是否与Canvas一致
// 验证事件清理
const mgr = new SegmentManager(pdfDoc);
// 设置容器和依赖...
await mgr.renderAllPagesContinuous();
mgr.destroy();
// 检查滚动事件是否停止触发
常见问题
Q: 为什么要修复这些bug? A: 这三个bug会导致:
- 文本字号计算错误(NaN)
- PDF导出文本大小不一致
- 内存泄漏和性能问题
Q: 修复后会影响现有代码吗? A: 不会,这些都是bug修复,不改变API。
Q: 模块化的好处是什么? A:
- ✅ 可复用(其他项目可直接使用)
- ✅ 可测试(单独测试每个模块)
- ✅ 易维护(职责清晰)
- ✅ 易扩展(支持插件化)
Q: 能在生产环境使用吗? A: 修复3个高优先级bug后,是的。
Q: 需要改变现有的使用方式吗? A: 需要一些重构,见"使用流程检查"部分。
得分卡
模块评分 (满分100)
TextFittingAdapter : 88/100 ⭐⭐⭐⭐
PDFExporter : 83/100 ⭐⭐⭐⭐
SegmentManager : 82/100 ⭐⭐⭐⭐
─────────────────────────────────
平均得分 : 84/100 ⭐⭐⭐⭐
修复后预期得分: 94/100 ⭐⭐⭐⭐⭐
下一步行动
立即进行
- 修复3个高优先级bug(1-2小时)
- 运行基础测试验证(30分钟)
- 提交代码审查(10分钟)
本周内
- 添加中优先级改进(4小时)
- 完善测试覆盖(3小时)
- 性能基准测试(2小时)
下周
- 部署到测试环境
- 用户验收测试
- 性能监控
总工作量:
- 高优先级: 2小时
- 中优先级: 4小时
- 低优先级: 2小时
- 合计: ~8小时
审查报告: 详见 CODE_REVIEW_MODULES.md