Epub阅读器0.0.1
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user