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

Commit

Permalink
feat(#26): lazy load images from slot background
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Dec 9, 2018
1 parent 1e8a50c commit 78e2f56
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/components/deck/deckdeckgo-deck/deckdeckgo-deck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export class DeckdeckgoDeck {

const lazyLoadContentFirstSlide = this.lazyLoadContent(0);
const lazyLoadContentSecondSlide = this.lazyLoadContent(1);
const lazyLoadImages = this.lazyBackgroungImages();

await Promise.all([lazyLoadContentFirstSlide, lazyLoadContentSecondSlide]);
await Promise.all([lazyLoadContentFirstSlide, lazyLoadContentSecondSlide, lazyLoadImages]);
}

private initWindowResize() {
Expand Down Expand Up @@ -403,6 +404,20 @@ export class DeckdeckgoDeck {
});
}

// Lazy load images from slot=background
private lazyBackgroungImages(): Promise<void> {
return new Promise<void>(async (resolve) => {
const allSlotedImages: NodeListOf<HTMLElement> = this.el.querySelectorAll('img[slot=\'background\']');
const allShadowImages: NodeListOf<HTMLElement> = this.el.querySelectorAll('[slot=\'background\'] img');

const images: HTMLElement[] = Array.from(allSlotedImages).concat(Array.from(allShadowImages));

await DeckdeckgoUtils.lazyLoadSelectedImages(images);

resolve();
});
}

@Method()
async slideTo(index: number, speed?: number | undefined, emitEvent: boolean = true) {
if (index > this.length || index < 0) {
Expand Down
15 changes: 14 additions & 1 deletion src/components/utils/deckdeckgo-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ export class DeckdeckgoUtils {
}

static async lazyLoadImages(el: HTMLElement): Promise<void> {
return new Promise<void>((resolve) => {
return new Promise<void>(async (resolve) => {
const images: HTMLElement[] = this.getAllImages(el);

await this.lazyLoadSelectedImages(images);

resolve();
});
};

static async lazyLoadSelectedImages(images: HTMLElement[]): Promise<void> {
return new Promise<void>((resolve) => {
if (!images) {
resolve();
return;
}

images.forEach((image: HTMLElement) => {
if (image.getAttribute('data-src')) {
image.setAttribute('src', image.getAttribute('data-src'));
Expand Down
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ <h1 slot="title">Manual code</h1>
}</code>
</deckgo-slide-code>

<img slot="background" data-src="https://deckdeckgo.com/assets/img/deckdeckgo.png" style="position: absolute; top: 0; left: 0;">

</deckgo-deck>

<script type="text/javascript">
Expand Down

0 comments on commit 78e2f56

Please sign in to comment.