From eb4f2310c91f2932fafbeb9f08d070389407d4e5 Mon Sep 17 00:00:00 2001 From: biati-digital Date: Mon, 1 Apr 2024 08:03:46 -0600 Subject: [PATCH] refactor: minimal code changes --- packages/video/src/video.ts | 38 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/packages/video/src/video.ts b/packages/video/src/video.ts index 4bb29fa..1c38202 100644 --- a/packages/video/src/video.ts +++ b/packages/video/src/video.ts @@ -68,6 +68,12 @@ export default class VideoSlide extends GLightboxPlugin { } }, }; + private iframeAttrs: { [key: string]: string } = { + 'allow': `autoplay; fullscreen; picture-in-picture`, + 'class': 'gl-video', + 'frameborder': '0', + 'data-type': '', + }; constructor(options: Partial = {}) { super(); @@ -151,12 +157,10 @@ export default class VideoSlide extends GLightboxPlugin { url: videoUrl, appendTo: slide, attrs: { + ...this.iframeAttrs, 'id': config.id, - 'allow': `autoplay; fullscreen; picture-in-picture`, - 'class': 'gl-video', - 'frameborder': '0', - 'data-type': 'vimeo', - }, + 'data-type': 'vimeo' + } }) as HTMLIFrameElement; iframe.onload = () => { @@ -174,7 +178,12 @@ export default class VideoSlide extends GLightboxPlugin { private buildYoutube(slide, config) { return new Promise((resolve) => { - const youtubeID = this.getYoutubeID(config.url); + const [a, , b] = config.url.replace(/(>|<)/gi, '').split(/^.*(?:(?:youtu\.?be(\.com)?\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|&v(?:i)?=))([^#&?]*).*/) + const youtubeID = b !== undefined ? b.split(/[^0-9a-z_-]/i)[0] : a + if (!youtubeID) { + throw new Error('Unable to get Youtube video ID'); + } + const params = this.buildURLParams(this.options?.youtube?.params ?? {}); const videoUrl = `https://www.youtube.com/embed/${youtubeID}?${params}` @@ -187,12 +196,10 @@ export default class VideoSlide extends GLightboxPlugin { url: videoUrl, appendTo: slide, attrs: { + ...this.iframeAttrs, 'id': config.id, - 'allow': `autoplay; fullscreen; picture-in-picture`, - 'class': 'gl-video', - 'frameborder': '0', - 'data-type': 'youtube', - }, + 'data-type': 'youtube' + } }) as HTMLIFrameElement; iframe.onload = () => { @@ -287,15 +294,6 @@ export default class VideoSlide extends GLightboxPlugin { return false; } - private getYoutubeID(url: string): string { - const [a, , b] = url.replace(/(>|<)/gi, '').split(/^.*(?:(?:youtu\.?be(\.com)?\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|&v(?:i)?=))([^#&?]*).*/) - if (b !== undefined) { - return b.split(/[^0-9a-z_-]/i)[0] - } else { - return a - } - } - private buildURLParams(params: Record): string { return Object.keys(params).map(key => { let val = params[key];