feat: 整章翻译功能改进 — 独立显隐、UI 优化、自动恢复#9
Merged
Merged
Conversation
三个 bug 共同导致移动端导入 GBK/GB18030 编码的 TXT 文件显示乱码: 1. text-encoding polyfill 未生效:Hermes 原生 TextDecoder 已存在于 globalThis,导致 text-encoding 跳过安装自己的全编码版本,非 UTF-8 解码全部失败。修复:require 前临时隐藏原生 TextDecoder/TextEncoder, 强制 polyfill 安装,之后恢复原生版本。 2. UTF-8 验证在多字节字符边界误判:ensureUtf8Bytes 用 64KB subarray 做 fatal 模式验证,但截断点可能落在多字节 UTF-8 字符中间,导致 合法 UTF-8 文件被误判为非 UTF-8,再被错误地用 GB18030 解码。 修复:对 sample 边界做 UTF-8 continuation byte 回退对齐。 3. Shift-JIS 检测误判 GB18030:原逻辑只要找到一个符合 Shift-JIS 字节范围的字节对就判定为 Shift-JIS,但 GB18030 的 0xE0-0xFC 开头 双字节与 Shift-JIS 大量重叠。修复:改为统计型判断,分别计数 GBK 典型对和 Shift-JIS 独有对,只有后者明显多于前者才判定为 Shift-JIS。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- 原文/译文独立显隐:翻译完成后可分别隐藏原文或译文,支持纯译文/纯原文阅读 - 桌面端:翻译控制改为工具栏下拉菜单(DropdownMenu),不再占用独立栏位 - 移动端:翻译控制改为底部弹出 ActionSheet,替代直接触发模式 - 修复移动端翻译失败(No text to translate):WebView bridge 函数暴露到 window 对象 - 修复移动端翻译不渐进展示:injectChapterTranslations 同样修复作用域问题 - 三个 WebView handler 均添加 iframe fallback 兼容路径 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- 桌面端和移动端 getVisibleText 的 TreeWalker 过滤 .readany-translation 后代节点 - 译文元素添加 user-select: none 禁止选中/高亮/笔记 - 翻译时自动保存所选语言到 settings store,下次默认使用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- 打开已翻译章节时自动检测缓存并恢复翻译(桌面端+移动端) - hook 新增 ready 参数,确保 DOM 就绪后再恢复 - 隐藏原文只显示译文时,译文样式继承原文(去除灰色、缩进、边框) - CSS 新增 data-solo 属性控制译文独显样式 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
startTranslation 因内联函数每次 render 重建,导致 useEffect cleanup 取消 timer,翻译永远不触发。改用 ref 存最新 startTranslation 引用, 从 useEffect 依赖中移除。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- translationReady 改为在 relocate 事件后设置,确保 CFI 导航完成后再注入翻译 - 之前在 handleLoaded + 500ms timer 设置,但 CFI 解析在 load 事件后仍在进行 - 译文独显模式 (data-solo) 添加 margin-bottom: 0.8em 段落间距 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
译文不应禁止选中——只是不支持高亮笔记(因为 CFI 不覆盖注入的 DOM)。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Test plan
🤖 Generated with Claude Code