Zum Inhalt

apple/PhysarumApp/Sources/Capture/EmbedSnippet.swift

#if !os(tvOS)
import Foundation
import PhysarumEngine

/// Builds a paste-ready, self-contained web embed for a preset: the v1.4.0 `<physarum-player>` custom
/// element (deployed on pages.jkaindl.de) loaded with the preset's params inline as a `json:` source.
/// No hosting and no token encoder — the deployed player resolves the bare `PresetParams` through its
/// Zod schema. Visual-only (the web player ignores audio reactivity). Native mirror of `src/share/embedCode.ts`.
enum EmbedSnippet {
    /// The canonical deployed standalone player bundle (matches `src/player/constants.ts` PLAYER_BUNDLE_URL).
    static let bundleURL = "https://pages.jkaindl.de/physarum/physarum-player.js"

    static func make(_ params: PresetParams) -> String {
        let json = (try? JSONEncoder().encode(params)).flatMap { String(data: $0, encoding: .utf8) } ?? "{}"
        // The params JSON holds only numbers and known enum strings (no single quotes), so it is safe
        // inside the single-quoted `preset` attribute — the double-quoted JSON keys need no escaping.
        return """
        <script type="module" src="\(bundleURL)"></script>
        <physarum-player preset='json:\(json)'></physarum-player>
        """
    }
}
#endif