2024-12-10 15:21:28 +01:00

124 lines
3.6 KiB
Swift

/// HTML video options
enum VideoOption {
/// Specifies that video controls should be displayed (such as a play/pause button etc).
case controls
/// Specifies that the video will start playing as soon as it is ready
case autoplay
/// Specifies that the video will start over again, every time it is finished
case loop
/// Specifies that the audio output of the video should be muted
case muted
/// Mobile browsers will play the video right where it is instead of the default, which is to open it up fullscreen while it plays
case playsinline
/// Sets the height of the video player
case height(Int)
/// Sets the width of the video player
case width(Int)
/// Specifies if and how the author thinks the video should be loaded when the page loads
case preload(VideoPreloadOption)
/// Specifies an image to be shown while the video is downloading, or until the user hits the play button
case poster(image: String)
/// Specifies the URL of the video file
case src(String)
init?(rawValue: String) {
switch rawValue {
case "controls":
self = .controls
return
case "autoplay":
self = .autoplay
return
case "muted":
self = .muted
return
case "loop":
self = .loop
return
case "playsinline":
self = .playsinline
return
default: break
}
let parts = rawValue.components(separatedBy: "=")
guard parts.count == 2 else {
return nil
}
let optionName = parts[0]
let value = parts[1].removingSurroundingQuotes
switch optionName {
case "height":
guard let height = Int(value) else {
return nil
}
self = .height(height)
case "width":
guard let width = Int(value) else {
return nil
}
self = .width(width)
case "preload":
guard let preloadOption = VideoPreloadOption(rawValue: value) else {
return nil
}
self = .preload(preloadOption)
case "poster":
self = .poster(image: value)
case "src":
self = .src(value)
default:
return nil
}
return
}
var rawValue: String {
switch self {
case .controls: return "controls"
case .autoplay: return "autoplay"
case .muted: return "muted"
case .loop: return "loop"
case .playsinline: return "playsinline"
case .height(let height): return "height='\(height)'"
case .width(let width): return "width='\(width)'"
case .preload(let option): return "preload='\(option)'"
case .poster(let image): return "poster='\(image)'"
case .src(let url): return "src='\(url)'"
}
}
}
/**
The `preload` attribute specifies if and how the author thinks that the video should be loaded when the page loads.
The `preload` attribute allows the author to provide a hint to the browser about what he/she thinks will lead to the best user experience.
This attribute may be ignored in some instances.
Note: The `preload` attribute is ignored if `autoplay` is present.
*/
enum VideoPreloadOption: String {
/// The author thinks that the browser should load the entire video when the page loads
case auto
/// The author thinks that the browser should load only metadata when the page loads
case metadata
/// The author thinks that the browser should NOT load the video when the page loads
case none
}