import { LitElement, html, css } from "lit" export default class DocVideo extends LitElement { static override styles = css` .doc-video { width: 100%; height: auto; } .doc-video { width: 100% !important; height: auto !important; border-radius: 8px; border: 1px solid #e0e0e0; } .youtube-video { aspect-ratio: 16 / 9; width: 100%; } ` static override properties = { src: { type: String }, } src!: string override render() { const player = this.getPlayer() console.log({ player }) if (player === "youtube") { const youtubeUrl = this.getYouTubeEmbedUrl(this.src) return html`` } else if (player === "loom") { // it is common to receive a share link, so we convert it to an embed link const _src = this.src.includes("share") ? this.src.replace("share", "embed") : this.src // Add Loom embed logic here if needed return html`
Unsupported video url.
` } } getPlayer(): "local" | "youtube" | "loom" | "unknown" { if (!this.src?.startsWith("http")) { return "local" } else { const url = new URL(this.src) console.log({ url }) if (url.hostname.includes("youtube") || url.hostname.includes("youtu.be")) { return "youtube" } else if (url.hostname.includes("loom")) { return "loom" } } return "unknown" } /** * Converts a YouTube video URL to an embed URL. * @param {string} url - The YouTube video URL. * @returns {string} - The YouTube embed URL. */ getYouTubeEmbedUrl(url: string): string { const videoId = this.extractYouTubeVideoId(url) return `https://www.youtube.com/embed/${videoId}` } /** * Extracts the video ID from a YouTube URL. * @param {string} url - The YouTube video URL. * @returns {string} - The YouTube video ID. */ extractYouTubeVideoId(url: string): string { const urlObj = new URL(url) if (urlObj.hostname === "youtu.be") { return urlObj.pathname.slice(1) } else if (urlObj.hostname === "youtube.com") { return urlObj.searchParams.get("v") || "" } return "" } } customElements.define("doc-video", DocVideo)