实现说明
活动部件是函数 drawStatusBadge。
异步部分依次 await 结果,顺序可预测。
使用要点
- 用到了
document,需要在浏览器环境运行,Node 里直接跑会缺 API。 - 每一步打印都带着自己的标签:
icon fonts ready:。
常见问题
「document.fonts.load 等图标字体就绪」能在裸 Node.js 里跑吗?
不能——document 的家在浏览器里,Node 进程提供不了。执行时请用页面(或 DevTools 控制台)。
主签名形如 async function drawStatusBadge()
核心语句
const loaded = await document.fonts.load('700 16px "OpsIcons"', '✓ ✗ ↻');
const families = loaded.map((face) => face.family).join(', ');
代码示例
async function drawStatusBadge(): Promise<void> {
// Ask for the exact faces the icon glyphs need; resolves with FontFace[]
// once those glyphs are downloadable and usable.
const loaded = await document.fonts.load('700 16px "OpsIcons"', '✓ ✗ ↻');
// An empty result with a failing check means the family is not available
// at all: fall back to text instead of drawing tofu boxes.
if (loaded.length === 0 && !document.fonts.check('700 16px "OpsIcons"')) {
console.log('icon font unavailable — falling back to text labels');
return;
}
const families = loaded.map((face) => face.family).join(', ');
console.log('icon fonts ready:', families || 'already loaded');
}
drawStatusBadge();