Add image gallery block

This commit is contained in:
Christoph Hagen
2025-05-02 10:00:22 +02:00
parent b3c982b2b9
commit fa2f749b70
7 changed files with 152 additions and 35 deletions

View File

@ -17,6 +17,8 @@ enum ContentBlock: String, CaseIterable {
case route
case gallery
var processor: BlockProcessor.Type {
switch self {
case .audio: return AudioBlock.self
@ -27,6 +29,7 @@ enum ContentBlock: String, CaseIterable {
case .labels: return LabelsBlock.self
case .screens: return PhoneScreensBlock.self
case .route: return RouteBlock.self
case .gallery: return GalleryBlock.self
}
}
}

View File

@ -0,0 +1,46 @@
struct GalleryBlock: BlockLineProcessor {
static let blockId: ContentBlock = .gallery
let content: Content
let results: PageGenerationResults
let language: ContentLanguage
init(content: Content, results: PageGenerationResults, language: ContentLanguage) {
self.content = content
self.results = results
self.language = language
}
private var imageWidth: Int {
content.settings.pages.contentWidth
}
func process(_ lines: [String], markdown: Substring) -> String {
var images = [FileResource]()
for line in lines {
let imageId = line.trimmed
guard !imageId.isEmpty else { continue }
guard let image = content.image(imageId) else {
results.missing(file: imageId, source: "Route block")
continue
}
images.append(image)
}
guard let firstImage = images.first else { return "" }
let imageSets = images.map {
$0.imageSet(width: imageWidth, height: imageWidth, language: language)
}
imageSets.forEach(results.require)
let id = firstImage.id.replacingOccurrences(of: ".", with: "-")
let gallery = ImageGallery(id: id, images: imageSets, standalone: true)
results.require(footer: gallery.standaloneFooter)
results.require(headers: .swiperJs, .swiperCss)
return gallery.content
}
}

View File

@ -84,14 +84,7 @@ final class FeedPageGenerator {
}
func swiperInitScript(posts: [FeedEntryData]) -> String {
var result = "<script> window.onload = () => { "
for post in posts {
guard post.requiresSwiper else {
continue
}
result += ImageGallery.swiperInit(id: post.entryId)
}
result += "}; </script>"
return result
let ids = posts.filter { $0.requiresSwiper }.map { $0.entryId }
return ImageGallery.combinedFootor(ids: ids)
}
}