improve imageLoad methiod

This commit is contained in:
2023-03-13 22:33:13 +10:00
parent 0f48f21dde
commit c0e7adcc42

View File

@@ -1,4 +1,5 @@
import { ImportMetaExtras } from "../../../import-meta"; import { ImportMetaExtras } from "../../../import-meta";
import { urlStripAttributes } from "./url";
type ImageLoadCallback = (url: string) => void; type ImageLoadCallback = (url: string) => void;
@@ -7,16 +8,30 @@ export const imageLoad = (
callback: ImageLoadCallback, callback: ImageLoadCallback,
postfix = "size=thumb" postfix = "size=thumb"
) => { ) => {
callback(`${url}?${postfix}`); if (
const tmp = new Image(); url.startsWith((import.meta as ImportMetaExtras).env.APP_URL) === true
tmp.onload = function () { ) {
callback(urlStripAttributes(url) + "?" + postfix);
const tmp = new Image();
tmp.onload = function () {
callback(url);
};
tmp.src = url;
} else {
// Image is not one we control
callback(url); callback(url);
}; }
tmp.src = url;
}; };
export const imageSize = (size: string, url: string) => { export const imageSize = (size: string, url: string) => {
const availableSizes = ["thumb", "small", "medium", "large", "xlarge"]; const availableSizes = [
"thumb",
"small",
"medium",
"large",
"xlarge",
"xxlarge",
];
if (availableSizes.includes(size)) { if (availableSizes.includes(size)) {
if ( if (
url.startsWith((import.meta as ImportMetaExtras).env.APP_URL) === url.startsWith((import.meta as ImportMetaExtras).env.APP_URL) ===
@@ -54,6 +69,11 @@ export const imageXLarge = (url: string) => {
return imageSize("xlarge", url); return imageSize("xlarge", url);
}; };
// Large 2560 x 2560
export const imageXXLarge = (url: string) => {
return imageSize("xxlarge", url);
};
// Full size // Full size
export const imageFull = (url: string) => { export const imageFull = (url: string) => {
return imageSize("full", url); return imageSize("full", url);