diff --git a/blocks/theme/theme.js b/blocks/theme/theme.js index a73b6036..8572860c 100644 --- a/blocks/theme/theme.js +++ b/blocks/theme/theme.js @@ -1,4 +1,5 @@ import ComponentBase from '../../scripts/component-base.js'; +import { config } from '../../scripts/libs.js'; export default class Theme extends ComponentBase { constructor() { @@ -23,17 +24,42 @@ export default class Theme extends ComponentBase { 'background', 'margin', 'icon-size', + 'font-family', 'max-width', ]; this.headingVariables = ['font-size', 'font-weight', 'font-family']; } + fontFaceTemplate(item) { + const { 'font-face': fontFace } = item; + + if (fontFace.indexOf('-') > -1) { + const [name, ...rest] = fontFace.split('-'); + const params = rest.pop().split('.'); + const format = params.pop(); + const lastBit = params.pop(); + const fontWeight = config.fontWeights[lastBit] || 'regular'; + const fontStyle = lastBit === 'italic' ? lastBit : 'normal'; + return ` +@font-face { + font-family: ${name}; + font-weight: ${fontWeight}; + font-style: ${fontStyle}; + src: url('/fonts/${fontFace}') format(${format}); +} +`; + } + return ''; + } + fontTagsTemplate(item, keys) { return `${item['font-tag']} {${keys .map((key) => { if (this.headingVariables.includes(key) && item[key]) { return ` - ${key}: var(--scope-${key},${item[key]});`; + ${key}: var(--scope-${key}, ${ + item[key].indexOf(',') > -1 ? `'${item[key]}'` : item[key] + });`; } return ''; }) @@ -47,10 +73,14 @@ export default class Theme extends ComponentBase { ); this.tags = data .map((item) => { + let tags = ''; + if (item['font-face']) { + tags += this.fontFaceTemplate(item); + } if (item['font-tag']) { - return this.fontTagsTemplate(item, keys); + tags += this.fontTagsTemplate(item, keys); } - return ''; + return tags; }) .join('') .trim(); @@ -69,7 +99,8 @@ export default class Theme extends ComponentBase { } styles() { - this.innerHTML = ``; + ${this.tags}`; + document.head.appendChild(style); document.body.style.display = 'block'; } diff --git a/fonts/segoe-ui-bold.woff b/fonts/segoe-ui-bold.woff new file mode 100644 index 00000000..a6b0a974 Binary files /dev/null and b/fonts/segoe-ui-bold.woff differ diff --git a/fonts/segoe-ui-italic.woff b/fonts/segoe-ui-italic.woff new file mode 100644 index 00000000..1649115c Binary files /dev/null and b/fonts/segoe-ui-italic.woff differ diff --git a/fonts/segoe-ui.woff b/fonts/segoe-ui.woff new file mode 100644 index 00000000..a45efa32 Binary files /dev/null and b/fonts/segoe-ui.woff differ diff --git a/head.html b/head.html index 6f07eff5..68292790 100644 --- a/head.html +++ b/head.html @@ -1,5 +1,8 @@ + + +