ReadViewSDK/Sources/RDReaderView/EPUBCore/RDEPUBWebView+FixedLayout.swift

131 lines
5.0 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+FixedLayout.swift
// RDEPUBWebView
// Fixed Layout HTML
// 退
import UIKit
import WebKit
extension RDEPUBWebView {
/// 便 spread
public func loadFixedSpread(
parser: RDEPUBParser,
spread: EPUBFixedSpread,
viewportSize: CGSize,
contentInset: UIEdgeInsets,
backgroundColor: UIColor?
) {
let request = RDEPUBRenderRequest.fixed(
RDEPUBFixedRenderRequest(
spread: spread,
viewportSize: viewportSize,
contentInset: contentInset,
backgroundColorCSS: backgroundColor?.ss_hexString,
fit: .page,
searchPresentation: nil
)
)
load(publication: parser.makePublication(), request: request)
}
/// HTML WebView
func handleFixedLayoutLoad(
publication: RDEPUBPublication,
request: RDEPUBFixedRenderRequest,
loadSignature: String,
in webView: WKWebView
) {
currentPageIndex = 0
currentTotalPagesInChapter = 1
viewportSize = request.viewportSize
currentPadding = request.contentInset
currentFontSize = 16
currentLineHeightMultiple = 1.5
currentThemeBackgroundColor = request.backgroundColorCSS
currentThemeTextColor = nil
targetLocation = nil
pendingHighlights = []
fixedSpread = request.spread
isFixedLayout = true
pendingProgressionRequest = false
currentLoadSignature = loadSignature
scheduleFixedLayoutReadyFallback()
let html = RDEPUBFixedLayoutTemplate.html(for: request, publication: publication)
RDEPUBWebViewDebug.log(debugScope, message: "load fixed spread resources=\(request.spread.resources.map(\.href).joined(separator: ",")) fit=\(request.fit.rawValue)")
webView.loadHTMLString(
html,
baseURL: URL(string: "\(RDEPUBResourceURLSchemeHandler.scheme)://\(RDEPUBResourceURLSchemeHandler.host)/")
)
}
///
func fixedLayoutLoadSignature(publicationKey: String, request: RDEPUBFixedRenderRequest) -> String {
let searchSignature = [
request.searchPresentation?.keyword ?? "",
request.searchPresentation?.resources.map {
[
$0.href,
String($0.matchCount),
String($0.activeLocalMatchIndex ?? -1)
].joined(separator: "|")
}.joined(separator: ",") ?? ""
].joined(separator: "#")
return [
publicationKey,
"fixed",
request.spread.resources.map(\.href).joined(separator: "|"),
String(format: "%.2f", request.viewportSize.width),
String(format: "%.2f", request.viewportSize.height),
String(format: "%.2f", request.contentInset.top),
String(format: "%.2f", request.contentInset.left),
String(format: "%.2f", request.contentInset.bottom),
String(format: "%.2f", request.contentInset.right),
request.backgroundColorCSS ?? "",
request.fit.rawValue,
searchSignature
].joined(separator: "#")
}
/// 退1 ready
func scheduleFixedLayoutReadyFallback() {
let workItem = DispatchWorkItem { [weak self] in
RDEPUBWebViewDebug.log(self?.debugScope ?? "ReaderWebView", message: "fixed ready fallback fired")
self?.applyFixedPresentationIfNeeded {
self?.refreshNativeDecorationsIfNeeded {
self?.rendered()
}
}
}
fixedLayoutReadyWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: workItem)
}
/// 退
func cancelFixedLayoutReadyFallback() {
fixedLayoutReadyWorkItem?.cancel()
fixedLayoutReadyWorkItem = nil
}
func applyFixedPresentationIfNeeded(completion: (() -> Void)? = nil) {
guard let webView,
let currentRenderRequest,
case .fixed(let request) = currentRenderRequest else {
completion?()
return
}
let script = RDEPUBJavaScriptBridge.applyFixedPresentationScript(for: request)
webView.evaluateJavaScript(script) { [weak self] _, error in
guard let self else {
completion?()
return
}
if let error {
self.delegate?.epubWebView(self, didLogJavaScriptError: error.localizedDescription)
}
self.applySearchDecorationsIfNeeded(completion: completion)
}
}
}