This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserveFonts.js
49 lines (46 loc) · 1.62 KB
/
observeFonts.js
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
import FontFaceObserver from 'fontfaceobserver';
/**
* Function that returns a promise, that resolves when all fonts,
* specified in fontsJSON and typographyJSON have been loaded.
*
* @exports observeFonts
* @kind function
*
* @param {Object|Array} fontsJSON
* @param {Object} typographyJSON
* @returns {Promise}
*/
export default function observeFonts(fontsJSON, typographyJSON) {
/* Render vis again after fonts have been loaded */
const fonts = new Set(
Array.isArray(fontsJSON)
? []
: Object.keys(fontsJSON).filter(key => fontsJSON[key].type === 'font')
);
Object.keys(typographyJSON.fontFamilies || {}).forEach(fontFamily => {
typographyJSON.fontFamilies[fontFamily].forEach(fontface => {
/* If this font is being used in a font family */
if (fonts.has(fontface.name)) {
/* Remove it form the list of fonts to wait for */
fonts.delete(fontface.name);
/* And add it again with theme-defined weight and style */
fonts.add({
family: fontFamily,
props: {
weight: fontface.weight || 400,
style: fontface.style || 'normal'
}
});
}
});
});
const observers = [];
fonts.forEach(font => {
const obs =
typeof font === 'string'
? new FontFaceObserver(font)
: new FontFaceObserver(font.family, font.props);
observers.push(obs.load(null, 5000));
});
return Promise.all(observers);
}