|
| 1 | +import Foundation |
| 2 | + |
| 3 | +// MARK: - YouTubePlayer+HTMLBuilder |
| 4 | + |
| 5 | +public extension YouTubePlayer { |
| 6 | + |
| 7 | + /// A YouTube player HTML builder object. |
| 8 | + struct HTMLBuilder: Sendable { |
| 9 | + |
| 10 | + // MARK: Typealias |
| 11 | + |
| 12 | + /// The JSON encoded YouTube player options. |
| 13 | + public typealias JSONEncodedYouTubePlayerOptions = String |
| 14 | + |
| 15 | + /// A function type that provides the HTML content string based on builder configuration and the JSON encoded YouTube player options. |
| 16 | + public typealias HTMLProvider = @Sendable (Self, JSONEncodedYouTubePlayerOptions) throws -> String |
| 17 | + |
| 18 | + // MARK: Properties |
| 19 | + |
| 20 | + /// The YouTube player JavaScrpt variable name. |
| 21 | + public var youTubePlayerJavaScriptVariableName: String |
| 22 | + |
| 23 | + /// The YouTube player event callback url scheme. |
| 24 | + public var youTubePlayerEventCallbackURLScheme: String |
| 25 | + |
| 26 | + /// The YouTube player event callback data parameter name. |
| 27 | + public var youTubePlayerEventCallbackDataParameterName: String |
| 28 | + |
| 29 | + /// The YouTube player iFrame API source URL. |
| 30 | + public var youTubePlayerIframeAPISourceURL: URL |
| 31 | + |
| 32 | + /// A closure providing the template |
| 33 | + public var htmlProvider: HTMLProvider |
| 34 | + |
| 35 | + // MARK: Initializer |
| 36 | + |
| 37 | + /// Creates a new instance of ``YouTubePlayer.HTMLBuilder`` |
| 38 | + /// - Parameters: |
| 39 | + /// - youTubePlayerJavaScriptVariableName: The YouTube player JavaScrpt variable name. Default value `youtubePlayer` |
| 40 | + /// - youTubePlayerEventCallbackURLScheme: The YouTube player event callback url scheme. Default value `youtubeplayer` |
| 41 | + /// - youTubePlayerEventCallbackDataParameterName: The YouTube player event callback data parameter name. Default value `data` |
| 42 | + /// - youTubePlayerIframeAPISourceURL: The YouTube player iFrame API source URL. Default value `https://www.youtube.com/iframe_api` |
| 43 | + public init( |
| 44 | + youTubePlayerJavaScriptVariableName: String = "youtubePlayer", |
| 45 | + youTubePlayerEventCallbackURLScheme: String = "youtubeplayer", |
| 46 | + youTubePlayerEventCallbackDataParameterName: String = "data", |
| 47 | + youTubePlayerIframeAPISourceURL: URL = .init(string: "https://www.youtube.com/iframe_api")!, |
| 48 | + htmlProvider: @escaping HTMLProvider = Self.defaultHTMLProvider |
| 49 | + ) { |
| 50 | + self.youTubePlayerJavaScriptVariableName = youTubePlayerJavaScriptVariableName |
| 51 | + self.youTubePlayerEventCallbackURLScheme = youTubePlayerEventCallbackURLScheme |
| 52 | + self.youTubePlayerEventCallbackDataParameterName = youTubePlayerEventCallbackDataParameterName |
| 53 | + self.youTubePlayerIframeAPISourceURL = youTubePlayerIframeAPISourceURL |
| 54 | + self.htmlProvider = htmlProvider |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +// MARK: - Call as Function |
| 62 | + |
| 63 | +public extension YouTubePlayer.HTMLBuilder { |
| 64 | + |
| 65 | + /// Builds the HTML. |
| 66 | + /// - Parameter jsonEncodedYouTubePlayerOptions: The JSON encoded YouTube player options. |
| 67 | + func callAsFunction( |
| 68 | + jsonEncodedYouTubePlayerOptions: JSONEncodedYouTubePlayerOptions |
| 69 | + ) throws -> String { |
| 70 | + return try self.htmlProvider( |
| 71 | + self, |
| 72 | + jsonEncodedYouTubePlayerOptions |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +// MARK: - Equatable |
| 79 | + |
| 80 | +extension YouTubePlayer.HTMLBuilder: Equatable { |
| 81 | + |
| 82 | + /// Returns a Boolean value indicating whether two values are equal. |
| 83 | + /// - Parameters: |
| 84 | + /// - lhs: A value to compare. |
| 85 | + /// - rhs: Another value to compare. |
| 86 | + public static func == ( |
| 87 | + lhs: Self, |
| 88 | + rhs: Self |
| 89 | + ) -> Bool { |
| 90 | + lhs.youTubePlayerJavaScriptVariableName == rhs.youTubePlayerJavaScriptVariableName |
| 91 | + && lhs.youTubePlayerEventCallbackURLScheme == rhs.youTubePlayerEventCallbackURLScheme |
| 92 | + && lhs.youTubePlayerEventCallbackDataParameterName == rhs.youTubePlayerEventCallbackDataParameterName |
| 93 | + && lhs.youTubePlayerIframeAPISourceURL == rhs.youTubePlayerIframeAPISourceURL |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +// MARK: - Hashable |
| 99 | + |
| 100 | +extension YouTubePlayer.HTMLBuilder: Hashable { |
| 101 | + |
| 102 | + /// Hashes the essential components of this value by feeding them into the given hasher. |
| 103 | + /// - Parameter hasher: The hasher to use when combining the components of this instance. |
| 104 | + public func hash(into hasher: inout Hasher) { |
| 105 | + hasher.combine(self.youTubePlayerJavaScriptVariableName) |
| 106 | + hasher.combine(self.youTubePlayerEventCallbackURLScheme) |
| 107 | + hasher.combine(self.youTubePlayerEventCallbackDataParameterName) |
| 108 | + hasher.combine(self.youTubePlayerIframeAPISourceURL) |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +// MARK: - Default HTML Provider |
| 114 | + |
| 115 | +public extension YouTubePlayer.HTMLBuilder { |
| 116 | + |
| 117 | + /// The default HTML provider. |
| 118 | + static let defaultHTMLProvider: HTMLProvider = { htmlBuilder, jsonEncodedYouTubePlayerOptions in |
| 119 | + """ |
| 120 | + <!DOCTYPE html> |
| 121 | + <html> |
| 122 | + <head> |
| 123 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> |
| 124 | + <style> |
| 125 | + body { |
| 126 | + margin: 0; |
| 127 | + width: 100%; |
| 128 | + height: 100%; |
| 129 | + } |
| 130 | + html { |
| 131 | + width: 100%; |
| 132 | + height: 100%; |
| 133 | + } |
| 134 | + .player-container iframe, |
| 135 | + .player-container object, |
| 136 | + .player-container embed { |
| 137 | + position: absolute; |
| 138 | + top: 0; |
| 139 | + left: 0; |
| 140 | + width: 100% !important; |
| 141 | + height: 100% !important; |
| 142 | + } |
| 143 | + ::-webkit-scrollbar { |
| 144 | + display: none !important; |
| 145 | + } |
| 146 | + </style> |
| 147 | + </head> |
| 148 | + <body> |
| 149 | + <div class="player-container"> |
| 150 | + <div id="\(htmlBuilder.youTubePlayerJavaScriptVariableName)"></div> |
| 151 | + </div> |
| 152 | + |
| 153 | + <script src="\(htmlBuilder.youTubePlayerIframeAPISourceURL)" |
| 154 | + onerror="window.location.href='\(htmlBuilder.youTubePlayerEventCallbackURLScheme)://\(YouTubePlayer.JavaScriptEvent.Name.onIframeApiFailedToLoad.rawValue)'"> |
| 155 | + </script> |
| 156 | + |
| 157 | + <script> |
| 158 | + var \(htmlBuilder.youTubePlayerJavaScriptVariableName); |
| 159 | +
|
| 160 | + function onYouTubeIframeAPIReady() { |
| 161 | + \(htmlBuilder.youTubePlayerJavaScriptVariableName) = new YT.Player( |
| 162 | + '\(htmlBuilder.youTubePlayerJavaScriptVariableName)', |
| 163 | + \(jsonEncodedYouTubePlayerOptions) |
| 164 | + ); |
| 165 | + \(htmlBuilder.youTubePlayerJavaScriptVariableName).setSize( |
| 166 | + window.innerWidth, |
| 167 | + window.innerHeight |
| 168 | + ); |
| 169 | + sendYouTubePlayerEvent('\(YouTubePlayer.JavaScriptEvent.Name.onIframeApiReady.rawValue)'); |
| 170 | + } |
| 171 | + |
| 172 | + function sendYouTubePlayerEvent(eventName, event) { |
| 173 | + const url = new URL(`\(htmlBuilder.youTubePlayerEventCallbackURLScheme)://${eventName}`); |
| 174 | + if (event && event.data !== null) { |
| 175 | + url.searchParams.set('\(htmlBuilder.youTubePlayerEventCallbackDataParameterName)', event.data); |
| 176 | + } |
| 177 | + window.location.href = url.toString(); |
| 178 | + } |
| 179 | +
|
| 180 | + \( |
| 181 | + YouTubePlayer |
| 182 | + .JavaScriptEvent |
| 183 | + .Name |
| 184 | + .allCases |
| 185 | + .filter { $0 != .onIframeApiReady && $0 != .onIframeApiFailedToLoad } |
| 186 | + .map { javaScriptEventName in |
| 187 | + """ |
| 188 | + function \(javaScriptEventName)(event) { |
| 189 | + sendYouTubePlayerEvent('\(javaScriptEventName)', event); |
| 190 | + } |
| 191 | + """ |
| 192 | + } |
| 193 | + .joined(separator: "\n\n") |
| 194 | + ) |
| 195 | + </script> |
| 196 | + </body> |
| 197 | + </html> |
| 198 | + """ |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments