ChWebsiteApp/CHDataManagement/Generator/Blocks/PhoneScreensBlock.swift
2025-02-20 23:44:12 +01:00

105 lines
3.2 KiB
Swift

struct PhoneScreensBlock: OrderedKeyBlockProcessor {
enum Key: String {
case id
case frame
case tall
case wide
}
static let blockId: ContentBlock = .screens
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
}
func process(_ arguments: [(key: Key, value: String)], markdown: Substring) -> String {
print("Processing Phone Screens Block")
guard let frameId = arguments.first(where: {$0.key == .frame })?.value else {
invalid(markdown)
return ""
}
guard let frameFile = content.file(frameId) else {
results.missing(file: frameId, source: "Wallpaper Block")
return ""
}
guard let id = arguments.first(where: {$0.key == .id })?.value else {
invalid(markdown)
return ""
}
let remaining = arguments.filter { $0.key == .tall || $0.key == .wide }
var images = [WallpaperSlider.Image]()
var tall: FileResource?
var wide: FileResource?
for (key, fileId) in remaining {
guard let file = content.file(fileId) else {
print("Missing file: \(fileId)")
results.missing(file: fileId, source: "Wallpaper Block")
return ""
}
if key == .tall {
if tall != nil {
print("Another tall image: \(file.id)")
invalid(markdown)
return ""
}
if let other = wide {
let image = WallpaperSlider.Image(
tall: file,
wide: other,
language: language,
results: results)
images.append(image)
wide = nil
continue
}
tall = file
continue
}
// key == .wide
if wide != nil {
print("Another wide image: \(file.id)")
invalid(markdown)
return ""
}
guard let other = tall else {
wide = file
continue
}
let image = WallpaperSlider.Image(
tall: other,
wide: file,
language: language,
results: results)
images.append(image)
tall = nil
}
if tall != nil || wide != nil {
invalid(markdown)
print("Wide/tall does not have the same number of items")
return ""
}
let frame = WallpaperSlider.Frame(frame: frameFile, language: language, results: results)
let slider = WallpaperSlider(frame: frame, images: images, id: id)
results.require(footer: slider.script)
results.require(headers: .swiperJs, .swiperCss)
results.require(header: .style(slider.style))
return slider.content
}
}