Skip to content

Commit

Permalink
Ensure classes are applied on load
Browse files Browse the repository at this point in the history
mgmeyers committed Apr 14, 2021

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 9bf8bb4 commit bb4d1d0
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
33 changes: 33 additions & 0 deletions src/SettingsManager.ts
Original file line number Diff line number Diff line change
@@ -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,
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@ export default class CSSSettingsPlugin extends Plugin {
}

this.settingsTab.setSettings(settingsList);
this.settingsManager.initClasses();
})
);

4 changes: 0 additions & 4 deletions src/settingHandlers.ts
Original file line number Diff line number Diff line change
@@ -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);

1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -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",

0 comments on commit bb4d1d0

Please sign in to comment.