@@ -9,6 +9,8 @@ import robotoMono from "../res/fonts/RobotoMono.ttf";
99const fonts = new Map ( ) ;
1010const customFontNames = new Set ( ) ;
1111const CUSTOM_FONTS_KEY = "custom_fonts" ;
12+ const FONT_FACE_STYLE_ID = "font-face-style" ;
13+ const EDITOR_STYLE_ID = "editor-font-style" ;
1214
1315add (
1416 "Fira Code" ,
@@ -188,29 +190,14 @@ function has(name) {
188190async function setFont ( name ) {
189191 loader . showTitleLoader ( ) ;
190192 try {
191- const $style = tag . get ( "style#font-style" ) ?? (
192- < style id = "font-style" > </ style >
193- ) ;
194- let css = get ( name ) ;
195-
196- // Get all URL font references
197- const urls = [ ...css . matchAll ( / u r l \( ( .* ?) \) / g) ] . map ( ( match ) => match [ 1 ] ) ;
198-
199- urls ?. map ( async ( url ) => {
200- if ( ! / ^ h t t p s ? / . test ( url ) ) return ;
201- if ( / ^ h t t p s ? : \/ \/ l o c a l h o s t / . test ( url ) ) return ;
202- const fontFile = await downloadFont ( name , url ) ;
203- const internalUrl = await helpers . toInternalUri ( fontFile ) ;
204- css = css . replace ( url , internalUrl ) ;
205- } ) ,
206- ( $style . textContent = `${ css }
207- .editor-container.ace_editor{
193+ await loadFont ( name ) ;
194+ const $style = ensureStyleElement ( EDITOR_STYLE_ID ) ;
195+ $style . textContent = `.editor-container.ace_editor{
208196 font-family: "${ name } ", NotoMono, Monaco, MONOSPACE !important;
209197 }
210198 .ace_text{
211199 font-family: inherit !important;
212- }` ) ;
213- document . head . append ( $style ) ;
200+ }` ;
214201 } catch ( error ) {
215202 toast ( `${ name } font not found` , "error" ) ;
216203 setFont ( "Roboto Mono" ) ;
@@ -238,7 +225,7 @@ async function downloadFont(name, link) {
238225}
239226
240227async function loadFont ( name ) {
241- const $style = tag . get ( "style#font-style" ) ?? < style id = "font-style" > </ style > ;
228+ const $style = ensureStyleElement ( FONT_FACE_STYLE_ID ) ;
242229 let css = get ( name ) ;
243230
244231 if ( ! css ) {
@@ -260,12 +247,20 @@ async function loadFont(name) {
260247 // Add font face to document if not already present
261248 if ( ! $style . textContent . includes ( `font-family: '${ name } '` ) ) {
262249 $style . textContent = `${ $style . textContent } \n${ css } ` ;
263- document . head . append ( $style ) ;
264250 }
265251
266252 return css ;
267253}
268254
255+ function ensureStyleElement ( id ) {
256+ const selector = `style#${ id } ` ;
257+ const $style = tag . get ( selector ) ?? < style id = { id } > </ style > ;
258+ if ( ! $style . isConnected ) {
259+ document . head . append ( $style ) ;
260+ }
261+ return $style ;
262+ }
263+
269264export default {
270265 add,
271266 addCustom,
0 commit comments