ChWebsiteApp/CHDataManagement/Model/LinkPreview.swift
2025-02-05 12:24:33 +01:00

59 lines
1.5 KiB
Swift

import Foundation
/**
The information to use when constructing the link preview of a page.
The information will be placed in the `<head>` of the page as `<meta>` tags.
*/
final class LinkPreview: ObservableObject {
/// The description to show when linking to a page (contained in the `<head>` of the page)
@Published
var title: String?
/// The image id of the thumbnail to attach to the link preview (contained in the `<head>` of the page)
@Published
var description: String?
/// The title to show for a link preview (contained in the `<head>` of the page)
@Published
var image: FileResource?
init(title: String? = nil, description: String? = nil, image: FileResource? = nil) {
self.title = title
self.description = description
self.image = image
}
/**
Remove a file if it is used in the link preview.
*/
func remove(_ file: FileResource) {
if image == file {
image = nil
}
}
// MARK: Storage
var data: Data {
.init(title: title, description: description, image: image?.id)
}
init(context: LoadingContext, data: Data) {
self.title = data.title
self.description = data.description
self.image = data.image.map(context.image)
}
}
extension LinkPreview {
/// The object to serialize a link preview for storage
struct Data: Codable, Equatable {
let title: String?
let description: String?
let image: String?
}
}