From bb4d1d0550c78be5cd1d2d6832f1780f741face5 Mon Sep 17 00:00:00 2001 From: mgmeyers Date: Tue, 13 Apr 2021 19:10:41 -0700 Subject: [PATCH] Ensure classes are applied on load --- manifest.json | 2 +- src/SettingsManager.ts | 33 +++++++++++++++++++++++++++++++++ src/main.ts | 1 + src/settingHandlers.ts | 4 ---- versions.json | 1 + 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/manifest.json b/manifest.json index f09095a..78ef769 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-style-settings", "name": "Style Settings", - "version": "0.2.0", + "version": "0.2.1", "minAppVersion": "0.11.5", "description": "Offers controls for adjusting theme, plugin, and snippet CSS variables.", "author": "mgmeyers", diff --git a/src/SettingsManager.ts b/src/SettingsManager.ts index 1cfd7eb..303db43 100644 --- a/src/SettingsManager.ts +++ b/src/SettingsManager.ts @@ -239,6 +239,7 @@ export class CSSSettingsManager { cleanup() { this.styleTag.remove(); + this.removeClasses(); } async save() { @@ -250,6 +251,38 @@ export class CSSSettingsManager { this.settings = Object.assign({}, await this.plugin.loadData()); } + initClasses() { + Object.keys(this.config).forEach(section => { + const config = this.config[section]; + + Object.keys(config).forEach(settingId => { + const setting = config[settingId]; + + if (setting.type === 'class-toggle') { + if (this.getSetting(section, settingId)) { + document.body.classList.add(setting.id); + } + } + }) + }) + } + + removeClasses() { + Object.keys(this.config).forEach(section => { + const config = this.config[section]; + + Object.keys(config).forEach(settingId => { + const setting = config[settingId]; + + if (setting.type === 'class-toggle') { + if (this.getSetting(section, settingId)) { + document.body.classList.remove(setting.id); + } + } + }) + }) + } + setCSSVariables() { const [vars, themedLight, themedDark] = getCSSVariables( this.settings, diff --git a/src/main.ts b/src/main.ts index 27a3774..15cbb34 100644 --- a/src/main.ts +++ b/src/main.ts @@ -55,6 +55,7 @@ export default class CSSSettingsPlugin extends Plugin { } this.settingsTab.setSettings(settingsList); + this.settingsManager.initClasses(); }) ); diff --git a/src/settingHandlers.ts b/src/settingHandlers.ts index 1f64fa1..b54ac2c 100644 --- a/src/settingHandlers.ts +++ b/src/settingHandlers.ts @@ -111,10 +111,6 @@ export function createClassToggle(opts: { .addToggle((toggle) => { const value = settingsManager.getSetting(sectionId, config.id); - if (value) { - document.body.classList.add(config.id); - } - toggle.setValue((value as boolean) || false).onChange((value) => { settingsManager.setSetting(sectionId, config.id, value); diff --git a/versions.json b/versions.json index 1c1003c..923b98c 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,5 @@ { + "0.2.1": "0.11.5", "0.2.0": "0.11.5", "0.1.1": "0.11.5", "0.1.0": "0.11.5",