@npio/internals
Version:
A free visual website editor, powered with your own SolidJS components.
123 lines (104 loc) • 2.96 kB
text/typescript
import type { Dimensions } from "./server/media/crop";
export const ROOT_ELEMENT_ID = "root";
export const baseFilename = function (filePath: string) {
const fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
return fileName.split(".")[0];
};
const slashRegex = /\/\/+/gm;
export const urlJoin = (...segments: string[]) => {
const separator = "://";
const url = segments.join("/");
const parts = url.split(separator);
const hasSeparator = parts.length > 1;
const pathClean = (hasSeparator ? parts[1] : parts[0]).replace(
slashRegex,
"/",
);
if (!hasSeparator) {
return pathClean;
}
const result = parts[0] + separator + pathClean;
return result;
};
const ratioCache: Record<string, number> = {};
export const convertAspectRatio = function (aspectRatio: string) {
if (ratioCache[aspectRatio]) {
return ratioCache[aspectRatio];
}
const parts = aspectRatio.split("/");
if (parts.length !== 2) {
return 1;
}
const result = Number.parseInt(parts[1]) / parseInt(parts[0]);
ratioCache[aspectRatio] = result;
return result;
};
export const getImageOptimizationTarget = ({
size,
original,
aspectRatio,
dpr,
}: {
size: number;
original: Dimensions;
aspectRatio?: string;
dpr?: number;
}) => {
const proportionalHeight = Math.round(
(size / original.width) * original.height,
);
const target = !aspectRatio
? {
width: size,
height: proportionalHeight,
}
: getTargetFromAspectRatio(aspectRatio, size);
if (dpr === 2) {
(target.width = target.width * 2), (target.height = target.height * 2);
}
return target;
};
const getTargetFromAspectRatio = function (aspectRatio: string, size: number) {
const ar = convertAspectRatio(aspectRatio);
if (ar > 1) {
return {
width: Math.round(size / ar),
height: size,
};
}
return {
width: size,
height: Math.round(size * ar),
};
};
/**
* Shrinks the crop target size, fitting it proportionally inside the original size
*
* E.g. if the original size is 300/300 and target is 400/300, this returns 300/225.
* With target 300/400, this returns 225/300.
*
* In either case, the new target size is never larger than the original size
*
*/
export const getProportionalCropTarget = function (
original: Dimensions,
target: Dimensions,
) {
target = { ...target };
if (target.width > original.width) {
target.height = Math.round((target.height / target.width) * original.width);
target.width = original.width;
}
if (target.height > original.height) {
target.width = Math.round((target.width / target.height) * original.height);
target.height = original.height;
}
return target;
};
export const getFontFormat = (mime: string) => fontFormats[mime];
export const fontFormats: Record<string, string | undefined> = {
"font/woff2": "woff2",
"font/woff": "woff",
"font/otf": "opentype",
"font/ttf": "truetype",
};