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

Commit 04d3d8c

Browse files
fix(#10): hide lazy images on load
1 parent d2dae9c commit 04d3d8c

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

src/components/slides/deckdeckgo-slide-author/deckdeckgo-slide-author.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export class DeckdeckgoSlideAuthor implements DeckdeckgoSlide {
1616
@Prop() imgUrl: string;
1717

1818
async componentDidLoad() {
19+
await DeckDeckGoSlideUtils.hideLazyLoadImages(this.el);
20+
1921
this.slideDidLoad.emit();
2022
}
2123

src/components/slides/deckdeckgo-slide-code/deckdeckgo-slide-code.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
3737
private action: DeckdeckgoSlideCodeAction = null;
3838

3939
async componentDidLoad() {
40+
await DeckDeckGoSlideUtils.hideLazyLoadImages(this.el);
41+
4042
this.slideDidLoad.emit();
4143

4244
await this.loadLanguage();

src/components/slides/deckdeckgo-slide-content/deckdeckgo-slide-content.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export class DeckdeckgoSlideContent implements DeckdeckgoSlide {
1717
@Prop() revealShowFirst: boolean = false;
1818

1919
async componentDidLoad() {
20+
await DeckDeckGoSlideUtils.hideLazyLoadImages(this.el);
21+
2022
this.slideDidLoad.emit();
2123

2224
if (this.reveal) {
23-
await DeckDeckGoSlideUtils.hideElements(this.el, this.revealShowFirst);
25+
await DeckDeckGoSlideUtils.hideRevealElements(this.el, this.revealShowFirst);
2426
}
2527
}
2628

src/components/slides/deckdeckgo-slide-split/deckdeckgo-slide-split.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export class DeckdeckgoSlideSplit implements DeckdeckgoSlide {
1717
@Prop() revealShowFirst: boolean = false;
1818

1919
async componentDidLoad() {
20+
await DeckDeckGoSlideUtils.hideLazyLoadImages(this.el);
21+
2022
this.slideDidLoad.emit();
2123

2224
if (this.reveal) {
23-
await DeckDeckGoSlideUtils.hideElements(this.el, this.revealShowFirst);
25+
await DeckDeckGoSlideUtils.hideRevealElements(this.el, this.revealShowFirst);
2426
}
2527
}
2628

src/components/slides/deckdeckgo-slide-title/deckdeckgo-slide-title.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export class DeckdeckgoSlideTitle implements DeckdeckgoSlide {
1717
@Prop() revealShowFirst: boolean = false;
1818

1919
async componentDidLoad() {
20+
await DeckDeckGoSlideUtils.hideLazyLoadImages(this.el);
21+
2022
this.slideDidLoad.emit();
2123

2224
if (this.reveal) {
23-
await DeckDeckGoSlideUtils.hideElements(this.el, this.revealShowFirst);
25+
await DeckDeckGoSlideUtils.hideRevealElements(this.el, this.revealShowFirst);
2426
}
2527
}
2628

src/components/slides/deckdeckgo-slide.tsx

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export interface DeckdeckgoSlide {
66

77
export class DeckDeckGoSlideUtils {
88

9-
static hideElements(el: HTMLElement, revealShowFirst: boolean): Promise<void> {
9+
static hideRevealElements(el: HTMLElement, revealShowFirst: boolean): Promise<void> {
1010
return new Promise<void>((resolve) => {
1111
// No keyboard on mobile device to reveal elements
1212
if (this.isMobile()) {
1313
resolve();
1414
return;
1515
}
1616

17-
const elements: NodeListOf<HTMLElement> = el.querySelectorAll(revealShowFirst ? '[slot] > li:not(:first-child), [slot] > p:not(:first-child), [slot] > span:not(:first-child) [slot] > img:not(:first-child)' : '[slot] > li, [slot] > p, [slot] > span, [slot] > img');
17+
const elements: NodeListOf<HTMLElement> = el.querySelectorAll(revealShowFirst ? '[slot] > li:not(:first-child), [slot] > p:not(:first-child), [slot] > span:not(:first-child), [slot] > img:not(:first-child)' : '[slot] > li, [slot] > p, [slot] > span, [slot] > img, img');
1818

1919
if (!elements) {
2020
resolve();
@@ -26,7 +26,7 @@ export class DeckDeckGoSlideUtils {
2626
});
2727
}
2828

29-
private static showElement(el: HTMLElement): Promise<boolean> {
29+
private static showRevealElement(el: HTMLElement): Promise<boolean> {
3030
return new Promise<boolean>((resolve) => {
3131
const elements: NodeListOf<HTMLElement> = el.querySelectorAll('[slot] > li, [slot] > p, [slot] > span, [slot] > img');
3232

@@ -47,7 +47,7 @@ export class DeckDeckGoSlideUtils {
4747
});
4848
}
4949

50-
private static hideElement(el: HTMLElement): Promise<boolean> {
50+
private static hideRevealElement(el: HTMLElement): Promise<boolean> {
5151
return new Promise<boolean>((resolve) => {
5252
const elements: NodeListOf<HTMLElement> = el.querySelectorAll('[slot] > li, [slot] > p, [slot] > span, [slot] > img');
5353

@@ -72,25 +72,42 @@ export class DeckDeckGoSlideUtils {
7272
static beforeSwipe(el: HTMLElement, swipeLeft: boolean, reveal: boolean): Promise<boolean> {
7373
return new Promise<boolean>(async (resolve) => {
7474
if (reveal) {
75-
const couldSwipe: boolean = swipeLeft ? await this.showElement(el) : await this.hideElement(el);
75+
const couldSwipe: boolean = swipeLeft ? await this.showRevealElement(el) : await this.hideRevealElement(el);
7676
resolve(couldSwipe);
7777
} else {
7878
resolve(true);
7979
}
8080
});
8181
}
8282

83-
static async lazyLoadImages(el: HTMLElement): Promise<void> {
83+
static hideLazyLoadImages(el: HTMLElement): Promise<void> {
8484
return new Promise<void>((resolve) => {
85-
const allSlotedImages: NodeListOf<HTMLElement> = el.querySelectorAll('[slot] > img');
86-
const allShadowImages: NodeListOf<HTMLElement> = el.shadowRoot.querySelectorAll('img');
85+
let images: HTMLElement[] = this.getAllImages(el);
8786

88-
const images: HTMLElement[] = Array.from(allSlotedImages).concat(Array.from(allShadowImages));
87+
if (!images) {
88+
resolve();
89+
} else {
90+
images = images.filter((image: HTMLElement) => image.getAttribute('data-src'));
91+
92+
images.forEach((image: HTMLElement) => {
93+
image.style.setProperty('visibility', 'hidden');
94+
});
95+
96+
resolve();
97+
}
98+
});
99+
}
100+
101+
static async lazyLoadImages(el: HTMLElement): Promise<void> {
102+
return new Promise<void>((resolve) => {
103+
const images: HTMLElement[] = this.getAllImages(el);
89104

90105
images.forEach((image: HTMLElement) => {
91106
if (image.getAttribute('data-src')) {
92107
image.setAttribute('src', image.getAttribute('data-src'));
93108
image.removeAttribute('data-src');
109+
110+
image.style.setProperty('visibility', 'initial');
94111
}
95112

96113
// Furthermore to lazy loading, we set pointer-events to none. Doing so we prevent images of being dragged.
@@ -101,6 +118,13 @@ export class DeckDeckGoSlideUtils {
101118
});
102119
};
103120

121+
static getAllImages(el: HTMLElement): HTMLElement[] {
122+
const allSlotedImages: NodeListOf<HTMLElement> = el.querySelectorAll('[slot] > img');
123+
const allShadowImages: NodeListOf<HTMLElement> = el.shadowRoot.querySelectorAll('img');
124+
125+
return Array.from(allSlotedImages).concat(Array.from(allShadowImages));
126+
}
127+
104128
// Source: http://detectmobilebrowsers.com
105129
static isMobile(): boolean {
106130
if (!window) {

0 commit comments

Comments
 (0)