ReadViewSDK/Sources/RDReaderView/EPUBCore/RDEPUBWebView+Reflowable.swift
shen 6f75b083f7 feat: EPUB 阅读器搜索、选中注释、书签 chrome 状态及大量重构优化
- 新增 RDEPUBReaderSearchCoordinator 与 RDEPUBSelectionState 管理搜索和选中状态
- 新增 BookmarkChromeStateTests、NavigationBackwardTests、SelectionAnnotateTests 等 UI 测试
- 新增多个边界测试 epub 样本(损坏结构、空归档、缺失文件、流式外链验证)
- 重构阅读器 chrome 状态管理,统一 tool bar 与 search bar 交互
- 优化大书分页缓存策略(RDEPUBChapterSummaryDiskCache、RDEPUBPageCountCache)
- 移除废弃的 RDEPUBLocationConverter 和 RDEPUBPageBreakPolicy
- 更新 epub-bridge.js 与 JS bridge 通信协议
- 全面更新现有 UI 测试以适配新的 helper 和状态管理
2026-06-13 22:48:56 +08:00

170 lines
7.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// RDEPUBWebView+Reflowable.swift
// RDEPUBWebView
// Reflowable
// JS
//
import UIKit
import WebKit
extension RDEPUBWebView {
/// 便
public func loadPage(
parser: RDEPUBParser,
spineIndex: Int,
pageIndex: Int,
totalPagesInChapter: Int,
viewportSize: CGSize,
padding: UIEdgeInsets,
fontSize: CGFloat,
lineHeightMultiple: CGFloat,
themeBackgroundColor: String?,
themeTextColor: String?,
targetLocation: RDEPUBLocation?,
highlights: [RDEPUBHighlight]
) {
let href = parser.spine.indices.contains(spineIndex) ? parser.spine[spineIndex].href : ""
let request = RDEPUBRenderRequest.reflowable(
RDEPUBReflowableRenderRequest(
spineIndex: spineIndex,
href: href,
pageIndex: pageIndex,
totalPagesInChapter: totalPagesInChapter,
presentation: RDEPUBPresentationStyle(
viewportSize: viewportSize,
contentInsets: padding,
fontSize: fontSize,
lineHeightMultiple: lineHeightMultiple,
themeBackgroundColor: themeBackgroundColor,
themeTextColor: themeTextColor
),
targetLocation: targetLocation,
highlights: highlights,
searchPresentation: nil
)
)
load(publication: parser.makePublication(), request: request)
}
///
func handleReflowableLoad(
publication: RDEPUBPublication,
request: RDEPUBReflowableRenderRequest,
loadSignature: String,
in webView: WKWebView
) {
guard publication.spine.indices.contains(request.spineIndex),
let requestURL = publication.resourceResolver.resourceURL(forRelativePath: request.href) else {
return
}
currentPageIndex = request.pageIndex
currentTotalPagesInChapter = max(1, request.totalPagesInChapter)
viewportSize = request.presentation.viewportSize
currentPadding = request.presentation.contentInsets
currentFontSize = request.presentation.fontSize
currentLineHeightMultiple = request.presentation.lineHeightMultiple
currentThemeBackgroundColor = request.presentation.themeBackgroundColor
currentThemeTextColor = request.presentation.themeTextColor
targetLocation = request.targetLocation
pendingHighlights = request.highlights
fixedSpread = nil
isFixedLayout = false
pendingProgressionRequest = true
if currentLoadSignature == loadSignature {
if didRenderCurrentRequest || webView.isLoading {
let reason = didRenderCurrentRequest ? "already-rendered" : "still-loading"
RDEPUBWebViewDebug.log(debugScope, message: "skip duplicate request href=\(request.href) page=\(request.pageIndex) reason=\(reason)")
return
}
if webView.url == requestURL {
RDEPUBWebViewDebug.log(debugScope, message: "reuse existing document href=\(request.href) page=\(request.pageIndex)")
applyPresentation()
return
}
}
currentLoadSignature = loadSignature
RDEPUBWebViewDebug.logNavigationEvent(debugScope, webView: webView, event: "load-request", url: requestURL)
webView.load(URLRequest(url: requestURL))
}
///
func reflowableLoadSignature(publicationKey: String, request: RDEPUBReflowableRenderRequest) -> String {
let targetSignature = [
request.targetLocation?.href ?? "",
String(request.targetLocation?.progression ?? 0),
String(request.targetLocation?.lastProgression ?? 0),
request.targetLocation?.fragment ?? "",
request.targetHighlightRangeInfo ?? ""
].joined(separator: "|")
let highlightSignature = request.highlights
.map { [$0.id, $0.rangeInfo ?? "", $0.color, $0.style.rawValue].joined(separator: "|") }
.joined(separator: ",")
let searchSignature = [
request.searchPresentation?.keyword ?? "",
request.searchPresentation?.resources.map {
[
$0.href,
String($0.matchCount),
String($0.activeLocalMatchIndex ?? -1)
].joined(separator: "|")
}.joined(separator: ",") ?? ""
].joined(separator: "#")
let presentation = request.presentation
return [
publicationKey,
"reflowable",
String(request.spineIndex),
request.href,
String(request.pageIndex),
String(request.totalPagesInChapter),
String(format: "%.2f", presentation.viewportSize.width),
String(format: "%.2f", presentation.viewportSize.height),
String(format: "%.2f", presentation.contentInsets.top),
String(format: "%.2f", presentation.contentInsets.left),
String(format: "%.2f", presentation.contentInsets.bottom),
String(format: "%.2f", presentation.contentInsets.right),
String(format: "%.2f", presentation.fontSize),
String(format: "%.2f", presentation.lineHeightMultiple),
String(presentation.numberOfColumns),
String(format: "%.2f", presentation.columnGap),
presentation.themeBackgroundColor ?? "",
presentation.themeTextColor ?? "",
targetSignature,
highlightSignature,
searchSignature
].joined(separator: "#")
}
/// JS CSS
func applyPresentation() {
guard let webView, let currentRenderRequest else { return }
guard case .reflowable(let request) = currentRenderRequest else {
return
}
let script = RDEPUBJavaScriptBridge.applyPresentationScript(for: request)
RDEPUBWebViewDebug.logJavaScript(
debugScope,
webView: webView,
action: "applyPresentation",
details: "spine=\(request.spineIndex) page=\(request.pageIndex) total=\(request.totalPagesInChapter)"
)
webView.evaluateJavaScript(script) { [weak self] _, error in
guard let self else { return }
if let error {
RDEPUBWebViewDebug.logJavaScript(self.debugScope, webView: webView, action: "applyPresentationFailed", details: error.localizedDescription)
self.delegate?.epubWebView(self, didLogJavaScriptError: error.localizedDescription)
return
}
self.applySearchDecorationsIfNeeded {
self.rendered()
}
}
}
}