Spaces:
Runtime error
Runtime error
File size: 1,469 Bytes
229b3b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import { ICompactFont, IFont } from '@/interfaces/editor';
import { groupBy } from 'lodash';
export const loadFonts = (fonts: { name: string; url: string }[]) => {
const promisesList = fonts.map((font) => {
return new FontFace(font.name, `url(${font.url})`)
.load()
.catch((err) => err);
});
return new Promise((resolve, reject) => {
Promise.all(promisesList)
.then((res) => {
res.forEach((uniqueFont) => {
if (uniqueFont && uniqueFont.family) {
document.fonts.add(uniqueFont);
resolve(true);
}
});
})
.catch((err) => reject(err));
});
};
const findDefaultFont = (fonts: IFont[]): IFont => {
const regularFont = fonts.find((font) =>
font.fullName.toLowerCase().includes('regular'),
);
return regularFont ? regularFont : fonts[0];
};
export const getCompactFontData = (fonts: IFont[]): ICompactFont[] => {
const compactFontsMap: { [key: string]: ICompactFont } = {};
// lodash groupby
const fontsGroupedByFamily = groupBy(fonts, (font) => font.family);
Object.keys(fontsGroupedByFamily).forEach((family) => {
const fontsInFamily = fontsGroupedByFamily[family];
const defaultFont = findDefaultFont(fontsInFamily);
const compactFont: ICompactFont = {
family: family,
styles: fontsInFamily,
default: defaultFont,
};
compactFontsMap[family] = compactFont;
});
return Object.values(compactFontsMap);
};
|