Skip to content

Commit

Permalink
refactor: minimal code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
biati-digital committed Apr 1, 2024
1 parent 5539626 commit eb4f231
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions packages/video/src/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<VideoOptions> = {}) {
super();
Expand Down Expand Up @@ -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 = () => {
Expand All @@ -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}`

Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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, string | number | boolean>): string {
return Object.keys(params).map(key => {
let val = params[key];
Expand Down

0 comments on commit eb4f231

Please sign in to comment.