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" }],
],
themeConfig: {
defaultHeaderFont: 'basic',
defaultHeaderFont: 'Basic',
commands: [{
command: 'about',
aliases: ['info'],

View file

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