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 { urlStripAttributes } from "./url";
type ImageLoadCallback = (url: string) => void;
@@ -7,16 +8,30 @@ export const imageLoad = (
callback: ImageLoadCallback,
postfix = "size=thumb"
) => {
callback(`${url}?${postfix}`);
if (
url.startsWith((import.meta as ImportMetaExtras).env.APP_URL) === true
) {
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);
}
};
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 (
url.startsWith((import.meta as ImportMetaExtras).env.APP_URL) ===
@@ -54,6 +69,11 @@ export const imageXLarge = (url: string) => {
return imageSize("xlarge", url);
};
// Large 2560 x 2560
export const imageXXLarge = (url: string) => {
return imageSize("xxlarge", url);
};
// Full size
export const imageFull = (url: string) => {
return imageSize("full", url);