Skip to content

Commit

Permalink
Merge pull request #105 from mProjectsCode/main
Browse files Browse the repository at this point in the history
fix theme commands not getting unregistered
  • Loading branch information
mgmeyers authored Feb 3, 2023
2 parents 4e919df + e1ae7cf commit 248f65c
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '@simonwep/pickr/dist/themes/nano.min.css';
import './css/pickerOverrides.css';
import './css/settings.css';

import { Plugin } from 'obsidian';
import { Command, Plugin } from 'obsidian';
import { CSSSettingsManager } from './SettingsManager';
import { ClassToggle, ParsedCSSSettings } from './SettingHandlers';
import yaml from 'js-yaml';
Expand All @@ -25,6 +25,7 @@ export default class CSSSettingsPlugin extends Plugin {
settingsTab: CSSSettingsTab;
settingsList: ParsedCSSSettings[] = [];
errorList: ErrorList = [];
commandList: Command[] = [];
lightEl: HTMLElement;
darkEl: HTMLElement;

Expand Down Expand Up @@ -82,6 +83,14 @@ export default class CSSSettingsPlugin extends Plugin {
this.settingsList = [];
this.errorList = [];

// remove registered theme commands (sadly undocumented API)
for (const command of this.commandList) {
// @ts-ignore
this.app.commands.removeCommand(command.id);
}

this.commandList = [];

this.debounceTimer = window.setTimeout(() => {
const styleSheets = document.styleSheets;

Expand Down Expand Up @@ -225,7 +234,7 @@ export default class CSSSettingsPlugin extends Plugin {
return settings;
}

private registerSettingCommands() {
private registerSettingCommands(): void {
for (const section of this.settingsList) {
for (const setting of section.settings) {
if (
Expand All @@ -241,29 +250,31 @@ export default class CSSSettingsPlugin extends Plugin {
private addClassToggleCommand(
section: ParsedCSSSettings,
setting: ClassToggle
) {
this.addCommand({
id: `settings-search-toggle-${section.id}-${setting.id}`,
name: `Toggle ${setting.title}`,
callback: () => {
const value = !(this.settingsManager.getSetting(
section.id,
setting.id
) as boolean);
this.settingsManager.setSetting(section.id, setting.id, value);

if (value) {
document.body.classList.add(setting.id);
} else {
document.body.classList.remove(setting.id);
}
): void {
this.commandList.push(
this.addCommand({
id: `style-settings-class-toggle-${section.id}-${setting.id}`,
name: `Toggle ${setting.title}`,
callback: () => {
const value = !(this.settingsManager.getSetting(
section.id,
setting.id
) as boolean);
this.settingsManager.setSetting(section.id, setting.id, value);

if (value) {
document.body.classList.add(setting.id);
} else {
document.body.classList.remove(setting.id);
}

this.settingsTab.settingsMarkup.rerender();
for (const leaf of this.app.workspace.getLeavesOfType(viewType)) {
(leaf.view as SettingsView).settingsMarkup.rerender();
}
},
});
this.settingsTab.settingsMarkup.rerender();
for (const leaf of this.app.workspace.getLeavesOfType(viewType)) {
(leaf.view as SettingsView).settingsMarkup.rerender();
}
},
})
);
}

onunload() {
Expand Down

0 comments on commit 248f65c

Please sign in to comment.