From 1ae6d73f9476c3a0463b04ef01837df5805bf496 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Sun, 11 Jul 2021 19:39:03 +0300 Subject: [PATCH] move fonts function to a separate file --- modules/fonts.js | 80 ++++++++++++++++++++++++++++++++++++++++++++- modules/ui/style.js | 78 ------------------------------------------- 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/modules/fonts.js b/modules/fonts.js index d2082929..9ce75d77 100644 --- a/modules/fonts.js +++ b/modules/fonts.js @@ -1,4 +1,34 @@ -// helper finction to work with fonts +// helper finctions to work with fonts + +async function addFonts(url) { + $("head").append(''); + try { + const resp = await fetch(url); + const text = await resp.text(); + let s = document.createElement("style"); + s.innerHTML = text; + document.head.appendChild(s); + let styleSheet = Array.prototype.filter.call(document.styleSheets, sS => sS.ownerNode === s)[0]; + let FontRule = rule_1 => { + let family = rule_1.style.getPropertyValue("font-family"); + let font = family.replace(/['"]+/g, "").replace(/ /g, "+"); + let weight = rule_1.style.getPropertyValue("font-weight"); + if (weight && weight !== "400") font += ":" + weight; + if (fonts.indexOf(font) == -1) { + fonts.push(font); + fetched++; + } + }; + let fetched = 0; + for (let r of styleSheet.cssRules) { + FontRule(r); + } + document.head.removeChild(s); + return fetched; + } catch (err) { + return ERROR && console.error(err); + } +} function loadUsedFonts() { const fontsInUse = getFontsList(svg); @@ -61,3 +91,51 @@ function GFontToDataURI(url) { return Promise.all(fontProms); // wait for all this has been done }); } + +// fetch default fonts if not done before +function loadDefaultFonts() { + if (!$('link[href="fonts.css"]').length) { + $("head").append(''); + const fontsToAdd = ["Amatic+SC:700", "IM+Fell+English", "Great+Vibes", "MedievalSharp", "Metamorphous", "Nova+Script", "Uncial+Antiqua", "Underdog", "Caesar+Dressing", "Bitter", "Yellowtail", "Montez", "Shadows+Into+Light", "Fredericka+the+Great", "Orbitron", "Dancing+Script:700", "Architects+Daughter", "Kaushan+Script", "Gloria+Hallelujah", "Satisfy", "Comfortaa:700", "Cinzel"]; + fontsToAdd.forEach(function (f) { + if (fonts.indexOf(f) === -1) fonts.push(f); + }); + updateFontOptions(); + } +} + +function fetchFonts(url) { + return new Promise((resolve, reject) => { + if (url === "") return tip("Use a direct link to any @font-face declaration or just font name to fetch from Google Fonts"); + + if (url.indexOf("http") === -1) { + url = url.replace(url.charAt(0), url.charAt(0).toUpperCase()).split(" ").join("+"); + url = "https://fonts.googleapis.com/css?family=" + url; + } + + addFonts(url).then(fetched => { + if (fetched === undefined) return tip("Cannot fetch font for this value!", false, "error"); + if (fetched === 0) return tip("Already in the fonts list!", false, "error"); + + updateFontOptions(); + if (fetched === 1) { + tip("Font " + fonts[fonts.length - 1] + " is fetched"); + } else if (fetched > 1) { + tip(fetched + " fonts are added to the list"); + } + resolve(fetched); + }); + }); +} + +// Update font list for Label and Burg Editors +function updateFontOptions() { + styleSelectFont.innerHTML = ""; + for (let i = 0; i < fonts.length; i++) { + const opt = document.createElement("option"); + opt.value = i; + const font = fonts[i].split(":")[0].replace(/\+/g, " "); + opt.style.fontFamily = opt.innerHTML = font; + styleSelectFont.add(opt); + } +} diff --git a/modules/ui/style.js b/modules/ui/style.js index 58029969..60be6286 100644 --- a/modules/ui/style.js +++ b/modules/ui/style.js @@ -1134,81 +1134,3 @@ function updateMapFilter() { if (!filter) return; mapFilters.querySelector("#" + filter).classList.add("pressed"); } - -// FONTS -// fetch default fonts if not done before -function loadDefaultFonts() { - if (!$('link[href="fonts.css"]').length) { - $("head").append(''); - const fontsToAdd = ["Amatic+SC:700", "IM+Fell+English", "Great+Vibes", "MedievalSharp", "Metamorphous", "Nova+Script", "Uncial+Antiqua", "Underdog", "Caesar+Dressing", "Bitter", "Yellowtail", "Montez", "Shadows+Into+Light", "Fredericka+the+Great", "Orbitron", "Dancing+Script:700", "Architects+Daughter", "Kaushan+Script", "Gloria+Hallelujah", "Satisfy", "Comfortaa:700", "Cinzel"]; - fontsToAdd.forEach(function (f) { - if (fonts.indexOf(f) === -1) fonts.push(f); - }); - updateFontOptions(); - } -} - -function fetchFonts(url) { - return new Promise((resolve, reject) => { - if (url === "") return tip("Use a direct link to any @font-face declaration or just font name to fetch from Google Fonts"); - - if (url.indexOf("http") === -1) { - url = url.replace(url.charAt(0), url.charAt(0).toUpperCase()).split(" ").join("+"); - url = "https://fonts.googleapis.com/css?family=" + url; - } - - addFonts(url).then(fetched => { - if (fetched === undefined) return tip("Cannot fetch font for this value!", false, "error"); - if (fetched === 0) return tip("Already in the fonts list!", false, "error"); - - updateFontOptions(); - if (fetched === 1) { - tip("Font " + fonts[fonts.length - 1] + " is fetched"); - } else if (fetched > 1) { - tip(fetched + " fonts are added to the list"); - } - resolve(fetched); - }); - }); -} - -function addFonts(url) { - $("head").append(''); - return fetch(url) - .then(resp => resp.text()) - .then(text => { - let s = document.createElement("style"); - s.innerHTML = text; - document.head.appendChild(s); - let styleSheet = Array.prototype.filter.call(document.styleSheets, sS => sS.ownerNode === s)[0]; - let FontRule = rule => { - let family = rule.style.getPropertyValue("font-family"); - let font = family.replace(/['"]+/g, "").replace(/ /g, "+"); - let weight = rule.style.getPropertyValue("font-weight"); - if (weight && weight !== "400") font += ":" + weight; - if (fonts.indexOf(font) == -1) { - fonts.push(font); - fetched++; - } - }; - let fetched = 0; - for (let r of styleSheet.cssRules) { - FontRule(r); - } - document.head.removeChild(s); - return fetched; - }) - .catch(err => ERROR && console.error(err)); -} - -// Update font list for Label and Burg Editors -function updateFontOptions() { - styleSelectFont.innerHTML = ""; - for (let i = 0; i < fonts.length; i++) { - const opt = document.createElement("option"); - opt.value = i; - const font = fonts[i].split(":")[0].replace(/\+/g, " "); - opt.style.fontFamily = opt.innerHTML = font; - styleSelectFont.add(opt); - } -}