-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont-loader.js
More file actions
145 lines (126 loc) · 4.08 KB
/
Copy pathfont-loader.js
File metadata and controls
145 lines (126 loc) · 4.08 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Lazy load fonts using the FontFace API and IntersectionObserver.
* @author Andreas Nymark <andreas@nymark.co>
* @license MIT
* @version 1.3.0
* @link https://github.com/andreasnymark/font-loader
*/
export const config = Object.assign( {
eagerSelector: '[data-font-load="eager"]',
lazySelector: '[data-font-load="lazy"]',
metadataSelector: '#font-metadata',
fontLoadedClass: 'font-loaded',
fontLoadingClass: 'font-loading',
fontLoadingDelay: 300,
rootMargin: '300px',
threshold: 0,
applyFont: false,
}, window.FontLoaderConfig || {} );
const metadataElement = document.querySelector( config.metadataSelector );
if ( ! metadataElement ) {
console.warn( 'font-metadata element not found, font loading disabled' );
}
let fontMetadata = {};
if ( metadataElement ) {
try {
fontMetadata = JSON.parse( metadataElement.textContent );
} catch ( err ) {
console.error( 'Failed to parse font metadata:', err );
}
}
const fontLoadPromises = new Map();
// Delay adding the loading class so fast connections don't get a flash of it
export function trackLoadingClass( preview, promise, extraLoadedClasses = [] ) {
preview.classList.remove( config.fontLoadedClass, ...extraLoadedClasses );
const timeoutId = setTimeout( () => {
preview.classList.add( config.fontLoadingClass );
}, config.fontLoadingDelay );
promise.finally( () => {
clearTimeout( timeoutId );
preview.classList.remove( config.fontLoadingClass );
});
}
export function loadFont( fontFamily ) {
if ( fontLoadPromises.has( fontFamily ) ) {
return fontLoadPromises.get( fontFamily );
}
const fontData = fontMetadata[ fontFamily ];
if ( ! fontData ) {
console.warn( 'Font not found:', fontFamily );
return Promise.resolve();
}
const fontFace = new FontFace(
fontData.family,
`url(${fontData.url})`,
{
weight: fontData.weightValue || 'normal',
style: fontData.style || 'normal',
stretch: fontData.stretch || 'normal',
display: 'block',
}
);
// Add before loading so FontFaceSet events (loadingdone) fire on completion
document.fonts.add( fontFace );
const promise = fontFace.load()
.then( () => {
// FontFaceSet loadingdone is unreliable in WebKit; consumers listen for this instead
document.dispatchEvent( new CustomEvent( 'font-loaded', {
detail: { fontFamily, family: fontData.family }
} ) );
})
.catch( err => {
document.fonts.delete( fontFace );
console.error( 'Failed to load font:', fontData.name, err );
});
fontLoadPromises.set( fontFamily, promise );
return promise;
}
const previewObserver = new IntersectionObserver(
( entries ) => {
entries.forEach( entry => {
if ( entry.isIntersecting ) {
const preview = entry.target;
const fontFamily = preview.dataset.fontFamily;
if ( fontFamily ) {
const fontData = fontMetadata[ fontFamily ];
preview.style.fontStyle = fontData?.style || 'normal';
preview.style.fontWeight = fontData?.weightValue || 'normal';
const promise = loadFont( fontFamily );
trackLoadingClass( preview, promise );
promise.then( () => {
preview.classList.add( config.fontLoadedClass );
if ( config.applyFont ) preview.style.fontFamily = `'${fontData?.family}'`;
});
}
previewObserver.unobserve( preview );
}
} );
},{
rootMargin: config.rootMargin,
threshold: config.threshold
}
);
function init() {
document.querySelectorAll( config.eagerSelector ).forEach( preview => {
const fontFamily = preview.dataset.fontFamily;
if ( fontFamily ) {
const fontData = fontMetadata[ fontFamily ];
preview.style.fontStyle = fontData?.style || 'normal';
preview.style.fontWeight = fontData?.weightValue || 'normal';
const promise = loadFont( fontFamily );
trackLoadingClass( preview, promise );
promise.then( () => {
preview.classList.add( config.fontLoadedClass );
if ( config.applyFont ) preview.style.fontFamily = `'${fontData?.family}'`;
});
}
});
document.querySelectorAll( config.lazySelector ).forEach( preview => {
previewObserver.observe( preview );
});
}
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', init );
} else {
init();
}