nitropage
Version:
A free and open source, extensible visual page builder based on SolidStart.
71 lines (63 loc) • 1.85 kB
text/typescript
import { Font, FontFace } from "../../../../types";
import { omitEqualProperties } from "../../../utils/object/omit";
import { logger } from "../log";
import { nullable, useDatabase } from "../prisma";
import { DEBUG_PAGE_UPDATE } from "../util";
const fontKeys = [
"fallbacks",
"cdn",
"preload",
"family",
] satisfies (keyof Font)[];
const faceKeys = [
"files",
"preload",
"style",
"weight",
] satisfies (keyof FontFace)[];
export const updateFonts = async (args: {
fonts: Font[];
prevFonts?: Font[];
}) => {
const db = useDatabase();
for (const font of args.fonts) {
const prevFont = args.prevFonts?.find((f) => f.id === font.id);
const nextData = omitEqualProperties(font, prevFont, fontKeys);
if (Object.keys(nextData).length) {
if (DEBUG_PAGE_UPDATE) logger.info("Update font", font.id);
if (nextData.cdn === false) nextData.cdn = null;
const fallback = nextData.fallbacks
? nextData.fallbacks.join(",")
: undefined;
delete nextData.fallbacks;
await db.nitroFont.update({
data: {
...nextData,
fallback,
cdn: nullable(nextData, "cdn"),
},
where: {
id: font.id,
},
});
}
for (const face of font.faces) {
const prevFace = prevFont?.faces.find((f) => f.id === face.id);
const nextData = omitEqualProperties(face, prevFace, faceKeys);
if (!Object.keys(nextData).length) {
continue;
}
if (DEBUG_PAGE_UPDATE) logger.info("Update font face", face.id);
await db.nitroFontFace.update({
data: {
...nextData,
files: nextData.files ? JSON.stringify(face.files) : undefined,
weight: nullable(nextData, "weight"),
},
where: {
id: face.id,
},
});
}
}
};