Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
fix(#12): lazy load youtube video
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Dec 2, 2018
1 parent 466a16a commit de62e65
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export namespace Components {
interface DeckgoSocial {
'fullUrl': string;
'github': string;
'lazyLoadContent': () => Promise<void>;
'linkedin': string;
'medium': string;
'twitter': string;
Expand All @@ -66,6 +67,7 @@ export namespace Components {

interface DeckgoYoutube {
'height': number;
'lazyLoadContent': () => Promise<void>;
'src': string;
'updateIFrame': (width: number, height: number) => Promise<void>;
'width': number;
Expand Down
35 changes: 35 additions & 0 deletions src/components/extra/deckdeckgo-extra.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export interface DeckdeckgoExtra {
lazyLoadContent(): Promise<void>;
}

export class DeckdeckgoExtraUtils {

static lazyLoadContent(el: HTMLElement, tag: string): Promise<void> {
return new Promise<void>(async (resolve) => {
const promises = [];

const elements: HTMLElement[] = this.getAllElements(el, tag);

if (elements && elements.length > 0) {

elements.forEach((element: HTMLElement) => {
promises.push((element as any).lazyLoadContent());
});

await Promise.all(promises);

resolve();
}

resolve();
});
}

private static getAllElements(el: HTMLElement, tag: string): HTMLElement[] {
const allSlottedElements: NodeListOf<HTMLElement> = el.querySelectorAll(tag);
const allShadowsElements: NodeListOf<HTMLElement> = el.shadowRoot.querySelectorAll(tag);

return Array.from(allSlottedElements).concat(Array.from(allShadowsElements));
}

}
3 changes: 2 additions & 1 deletion src/components/extra/deckdeckgo-gif/deckdeckgo-gif.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {Component, Element, Prop, Method} from '@stencil/core';

import {DeckdeckgoUtils} from '../../utils/deckdeckgo-utils';
import {DeckdeckgoExtra} from '../deckdeckgo-extra';

@Component({
tag: 'deckgo-gif',
styleUrl: 'deckdeckgo-gif.scss',
shadow: true
})
export class DeckdeckgoGif {
export class DeckdeckgoGif implements DeckdeckgoExtra {

@Element() el: HTMLElement;

Expand Down
12 changes: 10 additions & 2 deletions src/components/extra/deckdeckgo-social/deckdeckgo-social.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {Component, Prop, State, Watch} from '@stencil/core';
import {Component, Method, Prop, State, Watch} from '@stencil/core';

import {DeckdeckgoExtra} from '../deckdeckgo-extra';

@Component({
tag: 'deckgo-social',
styleUrl: 'deckdeckgo-social.scss',
shadow: true
})
export class DeckdeckgoSocial {
export class DeckdeckgoSocial implements DeckdeckgoExtra {

@State() url: string;

Expand All @@ -24,6 +25,13 @@ export class DeckdeckgoSocial {
this.concatFullUrl();
}

@Method()
lazyLoadContent(): Promise<void> {
return new Promise<void>((resolve) => {
resolve();
});
}

@Watch('twitter')
concatTwitterUrl() {
if (!this.twitter) {
Expand Down
16 changes: 13 additions & 3 deletions src/components/extra/deckdeckgo-youtube/deckdeckgo-youtube.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {Component, Element, Method, Prop} from '@stencil/core';

import {DeckdeckgoExtra} from '../deckdeckgo-extra';

@Component({
tag: 'deckgo-youtube',
styleUrl: 'deckdeckgo-youtube.scss',
shadow: true
})
export class DeckdeckgoYoutube {
export class DeckdeckgoYoutube implements DeckdeckgoExtra {

@Element() el: HTMLElement;

Expand All @@ -16,8 +17,6 @@ export class DeckdeckgoYoutube {

async componentDidLoad() {
await this.addPreconnectLink();

await this.createIFrame();
}

private addPreconnectLink(): Promise<void> {
Expand Down Expand Up @@ -54,13 +53,24 @@ export class DeckdeckgoYoutube {
}
}

@Method()
lazyLoadContent(): Promise<void> {
return this.createIFrame();
}

private createIFrame(): Promise<void> {
return new Promise<void>((resolve) => {
if (!this.src) {
resolve();
return;
}

const iframe: HTMLIFrameElement = this.el.shadowRoot.querySelector('iframe');
if (iframe) {
resolve();
return;
}

const element: HTMLIFrameElement = document.createElement('iframe');

const allow: Attr = document.createAttribute('allow');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
} else {
return new Promise<void>((resolve) => {
resolve();
})
});
}
}

Expand Down
26 changes: 6 additions & 20 deletions src/components/slides/deckdeckgo-slide.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {DeckdeckgoUtils} from '../utils/deckdeckgo-utils';
import {DeckdeckgoExtraUtils} from '../extra/deckdeckgo-extra';

export interface DeckdeckgoSlide {
beforeSwipe(_swipeLeft: boolean): Promise<boolean>;
Expand Down Expand Up @@ -95,31 +96,16 @@ export class DeckdeckgoSlideUtils {

static lazyLoadContent(el: HTMLElement): Promise<void> {
return new Promise<void>(async (resolve) => {
await DeckdeckgoUtils.lazyLoadImages(el);
const promises = [];

const gifs: HTMLElement[] = this.getAllElements(el, 'deckgo-gif');
promises.push(DeckdeckgoUtils.lazyLoadImages(el));
promises.push(DeckdeckgoExtraUtils.lazyLoadContent(el, 'deckgo-gif'));
promises.push(DeckdeckgoExtraUtils.lazyLoadContent(el, 'deckgo-youtube'));

if (gifs && gifs.length > 0) {
const promises = [];

gifs.forEach((gif: HTMLDeckgoGifElement) => {
promises.push(gif.lazyLoadContent());
});

await Promise.all(promises);

resolve();
}
await Promise.all(promises);

resolve();
});
}

private static getAllElements(el: HTMLElement, tag: string): HTMLElement[] {
const allSlottedElements: NodeListOf<HTMLElement> = el.querySelectorAll(tag);
const allShadowsElements: NodeListOf<HTMLElement> = el.shadowRoot.querySelectorAll(tag);

return Array.from(allSlottedElements).concat(Array.from(allShadowsElements));
}

}

0 comments on commit de62e65

Please sign in to comment.