feat: load fonts only on use

This commit is contained in:
koehr 2023-12-10 17:26:39 +01:00 committed by Norman Köhring
parent 1201178a26
commit c354352476
2 changed files with 10 additions and 12 deletions

View file

@ -22,7 +22,7 @@ export default defineConfigWithTheme<ThemeConfig>({
['meta', { content: "width=device-width,initial-scale=1.0", name: "viewport" }], ['meta', { content: "width=device-width,initial-scale=1.0", name: "viewport" }],
], ],
themeConfig: { themeConfig: {
defaultHeaderFont: 'basic', defaultHeaderFont: 'Basic',
commands: [{ commands: [{
command: 'about', command: 'about',
aliases: ['info'], aliases: ['info'],

View file

@ -1,24 +1,22 @@
import { FLFParser, FontLayoutManager } from '@figlet-ts/lib' import { FLFParser, FontLayoutManager } from '@figlet-ts/lib'
const fontGlob = import.meta.glob('./fonts/*.flf', { as: 'raw' })
async function loadFont(name: string) { async function loadFont(name: string) {
const key = Object.keys(fontGlob).find(path => path.toLowerCase() === `./fonts/${name.toLowerCase()}.flf`) try {
if (!key) console.error(`Cannot find font "${name}"!`) const font = await import(`./fonts/${name}.flf?raw`)
return font.default
console.log('found font', name, key) } catch (e) {
const font = await fontGlob[key]() console.error('Cannot load font', name, e)
return font }
} }
async function getFont(name: string, fallback: string) { async function getFont(name: string, fallback: string) {
name = name ?? fallback ?? 'standard' name = name ?? fallback ?? 'Standard'
const font = await loadFont(name) ?? await loadFont(fallback) ?? await loadFont('standard') const font = await loadFont(name) ?? await loadFont(fallback) ?? await loadFont('Standard')
const flf = FLFParser.parse(font) const flf = FLFParser.parse(font)
return flf.font return flf.font
} }
export default function useFiglet(defaultFontName = 'standard', htmlElement?: HTMLElement) { export default function useFiglet(defaultFontName = 'Standard', htmlElement?: HTMLElement) {
const flm = new FontLayoutManager() const flm = new FontLayoutManager()
let inputElement: HTMLElement | null = null let inputElement: HTMLElement | null = null