/** * @fileoverview YouTube Embed Component for Rantii * @author retoor * @description Embeds YouTube videos with preview thumbnail * @keywords youtube, video, embed, media, player */ import { BaseComponent } from './base-component.js'; import { getYoutubeVideoId, getYoutubeThumbnail, getYoutubeEmbedUrl } from '../utils/url.js'; class YoutubeEmbed extends BaseComponent { static get observedAttributes() { return ['url', 'video-id']; } init() { this.isPlaying = false; this.render(); this.bindEvents(); } render() { let videoId = this.getAttr('video-id'); const url = this.getAttr('url'); if (!videoId && url) { videoId = getYoutubeVideoId(url); } if (!videoId) { this.setHtml(''); return; } this.addClass('youtube-embed'); if (this.isPlaying) { const embedUrl = getYoutubeEmbedUrl(videoId); this.setHtml(`
`); } else { const thumbnail = getYoutubeThumbnail(videoId); this.setHtml(`
Video thumbnail YouTube
`); } } bindEvents() { this.on(this, 'click', this.handleClick); } handleClick(e) { const playBtn = e.target.closest('.youtube-play'); const preview = e.target.closest('.youtube-preview'); if (playBtn || preview) { e.preventDefault(); this.play(); } } play() { this.isPlaying = true; this.render(); } stop() { this.isPlaying = false; this.render(); } onAttributeChanged(name, oldValue, newValue) { this.isPlaying = false; this.render(); } } customElements.define('youtube-embed', YoutubeEmbed); export { YoutubeEmbed };