From 0b8ec47dd2335222f1dec32edfe193888818f452 Mon Sep 17 00:00:00 2001 From: Florin Raducan Date: Fri, 13 Sep 2024 16:16:42 +0300 Subject: [PATCH] Simplify theming config calls --- blocks/theming/theming.js | 53 ++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/blocks/theming/theming.js b/blocks/theming/theming.js index b2669067..6acd93d0 100644 --- a/blocks/theming/theming.js +++ b/blocks/theming/theming.js @@ -89,20 +89,24 @@ export default class Theming extends ComponentBase { document.body.classList.add(...themeMeta, 'color-default', 'font-default'); } - processFragment(responseData, type = 'color') { - this.themeJson[type] = responseData; - if (type === 'fontface') { - this.fontFaceTemplate(responseData); - } else if (type === 'component') { - Object.keys(responseData).forEach((key) => { - if (key.indexOf(':') === 0 || responseData[key].data.length === 0) return; - this.componentsConfig[key] = this.componentsConfig[key] || {}; - this.componentsConfig[key] = readValue(responseData[key].data, this.componentsConfig[key]); - }); - } else { - this.variations = readValue(responseData.data, this.variations); - this.defineVariations(); + async processFragment(response, type = 'color') { + if (response.ok) { + const responseData = await response.json(); + this.themeJson[type] = responseData; + if (type === 'fontface') { + this.fontFaceTemplate(responseData); + } else if (type === 'component') { + Object.keys(responseData).forEach((key) => { + if (key.indexOf(':') === 0 || responseData[key].data.length === 0) return; + this.componentsConfig[key] = this.componentsConfig[key] || {}; + this.componentsConfig[key] = readValue(responseData[key].data, this.componentsConfig[key]); + }); + } else { + this.variations = readValue(responseData.data, this.variations); + } + return this.themeJson[type]; } + return false; } defineVariations() { @@ -163,26 +167,13 @@ export default class Theming extends ComponentBase { async loadFragment() { const themeConfigs = getMetaGroup(metaTags.themeConfig.metaNamePrefix); - // Get the configs async - const configsResponses = await Promise.allSettled( - themeConfigs.map(async ({ name, content }) => { - const response = await fetch(`${content}.json`); - if (response.ok) { - return { - type: name, - data: await response.json(), - }; - } - return { type: null, data: null }; - }), + await Promise.allSettled( + themeConfigs.map(async ({ name, content }) => + fetch(`${content}.json`).then((response) => this.processFragment(response, name)), + ), ); - // Create the the Styles synchronous - configsResponses.forEach(({ value = {} }) => { - if (!value.type) return; - this.processFragment(value.data, value.type); - }); - + this.defineVariations(); this.styles(); } }