allow adding custom font faces in theme
This commit is contained in:
parent
b3510aa2b4
commit
2f6fc6bba2
1 changed files with 23 additions and 0 deletions
23
src/theme.js
23
src/theme.js
|
@ -43,8 +43,23 @@ function clearCustomTheme() {
|
||||||
document.body.style.removeProperty(prop);
|
document.body.style.removeProperty(prop);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const customFontFaceStyle = document.querySelector("head > style[title='custom-theme-font-faces']");
|
||||||
|
if (customFontFaceStyle) {
|
||||||
|
customFontFaceStyle.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateCustomFontFaceCSS(faces) {
|
||||||
|
return Object.entries(faces).map(([fontName, face]) => {
|
||||||
|
const src = Object.entries(face.src).map(([format, url]) => {
|
||||||
|
return `url('${url}') format('${format}')`;
|
||||||
|
}).join(", ");
|
||||||
|
return `@font-face {` +
|
||||||
|
` font-family: '${fontName}';` +
|
||||||
|
` src: ${src};` +
|
||||||
|
`}`;
|
||||||
|
}).join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
function setCustomThemeVars(customTheme) {
|
function setCustomThemeVars(customTheme) {
|
||||||
const {style} = document.body;
|
const {style} = document.body;
|
||||||
|
@ -72,6 +87,14 @@ function setCustomThemeVars(customTheme) {
|
||||||
}
|
}
|
||||||
if (customTheme.fonts) {
|
if (customTheme.fonts) {
|
||||||
const {fonts} = customTheme;
|
const {fonts} = customTheme;
|
||||||
|
if (fonts.faces) {
|
||||||
|
const css = generateCustomFontFaceCSS(fonts.faces);
|
||||||
|
const style = document.createElement("style");
|
||||||
|
style.setAttribute("title", "custom-theme-font-faces");
|
||||||
|
style.setAttribute("type", "text/css");
|
||||||
|
style.appendChild(document.createTextNode(css));
|
||||||
|
document.head.appendChild(style);
|
||||||
|
}
|
||||||
if (fonts.general) {
|
if (fonts.general) {
|
||||||
style.setProperty("--font-family", fonts.general);
|
style.setProperty("--font-family", fonts.general);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue