Skip to content

Fix for lazy loading #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions src/lib/ImageGallery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@
let resizeSlideWrapperObserver: ResizeObserver;
let resizeThumbnailWrapperObserver: ResizeObserver;
let thumbnailMouseOverTimer: number | null = null;
let lazyLoaded: boolean[] = [];
let imageLoaded: boolean[] = [];

/** keep track of mouse vs keyboard usage for a11y */
let currentlyUsingMouseOrKeyboard: MouseOrKeyboard = 'mouse';
Expand Down Expand Up @@ -269,22 +267,6 @@
}
};

$: handleImageLoad = (customEvent: {
detail: {
index: number;
event: Event;
};
}) => {
const index = customEvent.detail.index;
const event = customEvent.detail.event;
const imageExists = imageLoaded[index];
if (!imageExists) {
imageLoaded[index] = true; // prevent from call again
// image just loaded, call onImageLoad
dispatch('imageload', { index, event });
}
};

$: handleImageError = (customEvent: {
detail: {
index: number;
Expand Down Expand Up @@ -395,10 +377,6 @@
}
}
};

$: onLazyLoad = (event: { detail: number }) => {
lazyLoaded[event.detail] = true;
};
</script>

<div
Expand Down Expand Up @@ -432,7 +410,6 @@
{stopPropagation}
{indexSeparator}
{lazyLoad}
{lazyLoaded}
{swipeThreshold}
{flickThreshold}
{transitionStyle}
Expand All @@ -445,8 +422,6 @@
}}
on:playtoggle={togglePlay}
on:fullscreentoggle={toggleFullscreen}
on:lazyload={onLazyLoad}
on:imageload={handleImageLoad}
on:imageerror={handleImageError}
on:click
/>
Expand Down Expand Up @@ -496,7 +471,6 @@
{stopPropagation}
{indexSeparator}
{lazyLoad}
{lazyLoaded}
{swipeThreshold}
{flickThreshold}
{transitionStyle}
Expand All @@ -509,8 +483,6 @@
}}
on:playtoggle={togglePlay}
on:fullscreentoggle={toggleFullscreen}
on:lazyload={onLazyLoad}
on:imageload={handleImageLoad}
on:imageerror={handleImageError}
on:click
/>
Expand Down
28 changes: 25 additions & 3 deletions src/lib/SlideWrapper.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
export let galleryWidth: number;
export let indexSeparator: string;
export let lazyLoad: boolean;
export let lazyLoaded: boolean[];
export let swipeThreshold: number;
export let flickThreshold: number;
export let transitionStyle: string; // how should transitions be made? CSS, i.e. 'transform 450ms ease-out'
Expand All @@ -47,6 +46,29 @@
$: canSlideLeft = infinite || (isRTL ? canSlideNext : canSlidePrevious);
$: canSlideRight = infinite || (isRTL ? canSlidePrevious : canSlideNext);

let imageLoaded: boolean[] = [];
let lazyLoaded: boolean[] = [];

// reset load flags when items change
$: {
items = items;
lazyLoaded = [];
imageLoaded = [];
}

function onLazyLoad(index: number) {
lazyLoaded[index] = true;
}

function handleImageLoad(index: number, event: Event) {
const imageExists = imageLoaded[index];
if (!imageExists) {
imageLoaded[index] = true; // prevent from call again
// image just loaded, call onImageLoad
dispatch('imageload', { index, event });
}
}

// the element of this wrapper, useful to observe resize changes
let elem: HTMLElement;

Expand Down Expand Up @@ -129,7 +151,7 @@
$: showItems = items.map((_, index) => {
const showItem = !lazyLoad || !!alignmentClasses[index] || lazyLoaded[index];
if (showItem && lazyLoad && !lazyLoaded[index]) {
dispatch('lazyload', index);
onLazyLoad(index);
}
return showItem;
});
Expand Down Expand Up @@ -197,7 +219,7 @@
showItem={showItems[index]}
{item}
isFullscreen={false}
on:imageload={(event) => dispatch('imageload', { index, event })}
on:imageload={(event) => handleImageLoad(index, event)}
on:imageerror={(event) => dispatch('imageerror', { index, event })}
on:click
/>
Expand Down