Skip to content

Commit

Permalink
Fix a bug where image were loaded multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Nov 17, 2022
1 parent a70e634 commit a3480ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/ui/atoms/Figure/Figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class Figure<T extends BaseProps = BaseProps> extends withMountWhenInView
this.$refs.img.src = value;
}

/**
* Get the original source.
*/
get original() {
return this.$refs.img.dataset.src;
}

/**
* Load on mount.
*/
Expand All @@ -66,7 +73,7 @@ export class Figure<T extends BaseProps = BaseProps> extends withMountWhenInView
throw new Error('[Figure] The `img` ref must be an `<img>` element.');
}

const { src } = img.dataset;
const src = this.original;

if (this.$options.lazy && src && src !== this.src) {
let tempImg = new Image();
Expand Down
21 changes: 14 additions & 7 deletions packages/ui/atoms/Figure/FigureTwicpics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class FigureTwicpics<T extends BaseProps = BaseProps> extends Figure<
/**
* Get the Twicpics path.
*/
get path():string {
get path(): string {
return withoutTrailingSlash(withoutLeadingSlash(this.$options.path));
}

Expand All @@ -67,15 +67,22 @@ export class FigureTwicpics<T extends BaseProps = BaseProps> extends Figure<
}

/**
* Add Twicpics transforms, path and domain to the URL.
* Get formattted original source.
*/
set src(value: string) {
const url = new URL(value, 'https://localhost');
get original() {
return this.formatSrc(super.original);
}

/**
* Format the source for Twicpics.
*/
formatSrc(src: string): string {
const url = new URL(src, 'https://localhost');
url.host = this.domain;
url.port = '';

if (this.path) {
url.pathname = `/${this.path}${url.pathname}`
url.pathname = `/${this.path}${url.pathname}`;
}

const width = normalizeSize(this, 'offsetWidth');
Expand All @@ -90,14 +97,14 @@ export class FigureTwicpics<T extends BaseProps = BaseProps> extends Figure<

url.search = decodeURIComponent(url.search);

super.src = url.toString();
return url.toString();
}

/**
* Reassign the source from the original on resize.
*/
resized() {
this.src = this.$refs.img.dataset.src;
this.src = this.original;
}

/**
Expand Down

0 comments on commit a3480ca

Please sign in to comment.