Refactor ZenThemesImporter to improve theme handling and preferences management

This commit is contained in:
mauro-balades 2024-09-21 18:05:53 +02:00
parent ef5da75e5e
commit 9ae715276c

View file

@ -172,60 +172,60 @@ var gZenThemeImporter = new (class {
} }
writeToDom(themesWithPreferences) { writeToDom(themesWithPreferences) {
const browser = ZenThemesCommon.currentBrowser; for (const browser of ZenThemesCommon.browsers) {
for (const { enabled, preferences, name } of themesWithPreferences) {
const sanitizedName = `theme-${name?.replaceAll(/\s/g, '-')?.replaceAll(/[^A-z_-]+/g, '')}`;
for (const { enabled, preferences, name } of themesWithPreferences) { if (enabled !== undefined && !enabled) {
const sanitizedName = `theme-${name?.replaceAll(/\s/g, '-')?.replaceAll(/[^A-z_-]+/g, '')}`; const element = browser.document.getElementById(sanitizedName);
if (enabled !== undefined && !enabled) { if (element) {
const element = browser.document.getElementById(sanitizedName); element.remove();
}
if (element) { for (const { property } of preferences.filter(({ type }) => type !== 'checkbox')) {
element.remove(); const sanitizedProperty = property?.replaceAll(/\./g, '-');
browser.document.querySelector(':root').style.removeProperty(`--${sanitizedProperty}`);
}
continue;
} }
for (const { property } of preferences.filter(({ type }) => type !== 'checkbox')) { for (const { property, type } of preferences) {
const value = Services.prefs.getStringPref(property, '');
const sanitizedProperty = property?.replaceAll(/\./g, '-'); const sanitizedProperty = property?.replaceAll(/\./g, '-');
browser.document.querySelector(':root').style.removeProperty(`--${sanitizedProperty}`); switch (type) {
} case 'dropdown': {
if (value !== '') {
let element = browser.document.getElementById(sanitizedName);
continue; if (!element) {
} element = browser.document.createElement('div');
for (const { property, type } of preferences) { element.style.display = 'none';
const value = Services.prefs.getStringPref(property, ''); element.setAttribute('id', sanitizedName);
const sanitizedProperty = property?.replaceAll(/\./g, '-');
switch (type) { browser.document.body.appendChild(element);
case 'dropdown': { }
if (value !== '') {
let element = browser.document.getElementById(sanitizedName);
if (!element) { element.setAttribute(sanitizedProperty, value);
element = browser.document.createElement('div');
element.style.display = 'none';
element.setAttribute('id', sanitizedName);
browser.document.body.appendChild(element);
} }
break;
element.setAttribute(sanitizedProperty, value);
} }
break;
}
case 'string': { case 'string': {
if (value === '') { if (value === '') {
browser.document.querySelector(':root').style.removeProperty(`--${sanitizedProperty}`); browser.document.querySelector(':root').style.removeProperty(`--${sanitizedProperty}`);
} else { } else {
browser.document.querySelector(':root').style.setProperty(`--${sanitizedProperty}`, value); browser.document.querySelector(':root').style.setProperty(`--${sanitizedProperty}`, value);
}
break;
} }
break;
}
default: { default: {
}
} }
} }
} }