- 实现EPUB阅读器搜索功能及选中注释功能 - 优化CFI模块,修复代码审查发现的11个问题 - 实现大书远距目录跳转与后台补全优化方案 - 优化设置面板与章节运行时联动 - 重构及大量改进优化
53 lines
2.3 KiB
Swift
53 lines
2.3 KiB
Swift
|
|
import Foundation
|
|
|
|
enum RDEPUBFixedLayoutTemplate {
|
|
|
|
static func html(for request: RDEPUBFixedRenderRequest, publication: RDEPUBPublication) -> String {
|
|
|
|
let panes = request.spread.resources.enumerated().compactMap { index, resource -> String? in
|
|
guard let url = publication.resourceResolver.resourceURL(forRelativePath: resource.href)?.absoluteString else {
|
|
return nil
|
|
}
|
|
|
|
let pageType: String
|
|
if request.spread.resources.count == 1 {
|
|
pageType = "single"
|
|
} else if request.spread.resources.count == 2 {
|
|
pageType = index == 0 ? "left" : "right"
|
|
} else {
|
|
pageType = "center"
|
|
}
|
|
|
|
return """
|
|
<div class=\"ss-fixed-viewport\" data-page-type=\"\(pageType)\">
|
|
<iframe class=\"ss-fixed-page\" data-page-type=\"\(pageType)\" scrolling=\"no\" src=\"\(url)\"></iframe>
|
|
</div>
|
|
"""
|
|
}.joined()
|
|
|
|
let background = request.backgroundColorCSS ?? "#FFFFFF"
|
|
let viewportWidth = max(1, Int((request.viewportSize.width - request.contentInset.left - request.contentInset.right).rounded(.down)))
|
|
let viewportHeight = max(1, Int((request.viewportSize.height - request.contentInset.top - request.contentInset.bottom).rounded(.down)))
|
|
let insetTop = Int(request.contentInset.top.rounded(.down))
|
|
let insetRight = Int(request.contentInset.right.rounded(.down))
|
|
let insetBottom = Int(request.contentInset.bottom.rounded(.down))
|
|
let insetLeft = Int(request.contentInset.left.rounded(.down))
|
|
|
|
return RDEPUBAssetRepository.string(
|
|
for: .fixedLayoutTemplate,
|
|
replacements: [
|
|
"BACKGROUND": background,
|
|
"INSET_TOP": String(insetTop),
|
|
"INSET_RIGHT": String(insetRight),
|
|
"INSET_BOTTOM": String(insetBottom),
|
|
"INSET_LEFT": String(insetLeft),
|
|
"VIEWPORT_WIDTH": String(viewportWidth),
|
|
"VIEWPORT_HEIGHT": String(viewportHeight),
|
|
"PANES": panes,
|
|
"FIT_MODE": request.fit.rawValue,
|
|
"FIXED_LAYOUT_READY_MESSAGE": RDEPUBJavaScriptBridgeMessage.fixedLayoutReady.rawValue
|
|
]
|
|
)
|
|
}
|
|
} |