ReadViewSDK/Sources/RDReaderView/EPUBCore/RDEPUBWebView+Reflowable.swift
shen 54798ba578 refactor: 添加中文注释 + 优化模块结构
- 给全部 78 个 Swift 源文件添加详细的中文注释(文件级、类级、方法级)
- 删除 LegacyRDReaderController/ 死代码目录(16 文件 4592 行)
- 根目录翻页容器文件移入 ReaderView/ 目录
- Resources/ 移入 EPUBCore/Resources/(与使用者归属一致)
- RDEPUBTextIndexTable.swift 移入 EPUBTextRendering/(消除反向依赖)
- RDURLReaderController.swift 移入 EPUBUI/(入口控制器归入 UI 层)
- 更新 podspec 资源路径
2026-05-25 10:19:14 +08:00

156 lines
6.6 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 ?? ""
].joined(separator: "|")
let highlightSignature = request.highlights
.map { [$0.id, $0.rangeInfo ?? "", $0.color, $0.style.rawValue].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),
presentation.themeBackgroundColor ?? "",
presentation.themeTextColor ?? "",
targetSignature,
highlightSignature
].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()
}
}
}
}