Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(obsidian): change how default values are displayed (#1789)

Elijah Pottereec6c94ead19
diff --git a/packages/obsidian-plugin/src/HarperSettingTab.ts b/packages/obsidian-plugin/src/HarperSettingTab.ts
index 7999b477b..b9c0dffc2 100644
--- a/packages/obsidian-plugin/src/HarperSettingTab.ts
+++ b/packages/obsidian-plugin/src/HarperSettingTab.ts
@@ -1,17 +1,23 @@
import './index.js';
import { Dialect } from 'harper.js';
import { startCase } from 'lodash-es';
-import { type App, PluginSettingTab, Setting } from 'obsidian';
+import type { ButtonComponent } from 'obsidian';
+import { type App, Notice, PluginSettingTab, Setting } from 'obsidian';
import type HarperPlugin from './index.js';
import type State from './State.js';
import type { Settings } from './State.js';
import { linesToString, stringToLines } from './textUtils';
+const LintSettingId = 'HarperLintSettings';
+
export class HarperSettingTab extends PluginSettingTab {
private state: State;
private settings: Settings;
private descriptionsHTML: Record<string, string>;
+ private defaultLintConfig: Record<string, boolean>;
+ private currentRuleSearchQuery = '';
private plugin: HarperPlugin;
+ private toggleAllButton?: ButtonComponent;
constructor(app: App, plugin: HarperPlugin, state: State) {
super(app, plugin);
@@ -22,6 +28,7 @@ export class HarperSettingTab extends PluginSettingTab {
const update = () => {
this.updateDescriptions();
this.updateSettings();
+ this.updateDefaults();
setTimeout(update, 1000);
};
@@ -31,6 +38,7 @@ export class HarperSettingTab extends PluginSettingTab {
updateSettings() {
this.state.getSettings().then((v) => {
this.settings = v;
+ this.updateToggleAllRulesButton();
});
}
@@ -40,6 +48,13 @@ export class HarperSettingTab extends PluginSettingTab {
});
}
+ updateDefaults() {
+ this.state.getDefaultLintConfig().then((v) => {
+ this.defaultLintConfig = v as unknown as Record<string, boolean>;
+ this.updateToggleAllRulesButton();
+ });
+ }
+
display() {
const { containerEl } = this;
containerEl.empty();
@@ -130,23 +145,86 @@ export class HarperSettingTab extends PluginSettingTab {
.setDesc('Search for a specific Harper rule.')
.addSearch((search) => {
search.setPlaceholder('Search for a rule...').onChange((query) => {
- this.renderLintSettingsToId(query, 'HarperLintSettings');
+ this.currentRuleSearchQuery = query;
+ this.renderLintSettingsToId(query, LintSettingId);
+ });
+ });
+
+ // Global reset for rule overrides
+ new Setting(containerEl)
+ .setName('Reset Rules to Defaults')
+ .setDesc(
+ 'Restore all rule overrides back to their default values. This does not affect other settings.',
+ )
+ .addButton((button) => {
+ button
+ .setButtonText('Reset All to Defaults')
+ .onClick(async () => {
+ const confirmed = confirm(
+ 'Reset all rule overrides to their defaults? This cannot be undone.',
+ );
+ if (!confirmed) return;
+ await this.state.resetAllRulesToDefaults();
+ this.settings = await this.state.getSettings();
+ this.renderLintSettingsToId(this.currentRuleSearchQuery, LintSettingId);
+ this.updateToggleAllRulesButton();
+ new Notice('Harper rules reset to defaults');
+ })
+ .setWarning();
+ });
+
+ // Single bulk toggle button: If any rules are enabled, turn all off; otherwise turn all on.
+ new Setting(containerEl)
+ .setName('Toggle All Rules')
+ .setDesc(
+ 'Enable or disable all rules in bulk. Overrides individual rule settings until changed again.',
+ )
+ .addButton((button) => {
+ this.toggleAllButton = button;
+ this.updateToggleAllRulesButton();
+ button.setWarning().onClick(async () => {
+ const anyEnabledNow = await this.state.areAnyRulesEnabled();
+ const action = anyEnabledNow ? 'Disable' : 'Enable';
+ const confirmed = confirm(`${action} all rules? This will override individual settings.`);
+ if (!confirmed) return;
+
+ await this.state.setAllRulesEnabled(!anyEnabledNow);
+ this.settings = await this.state.getSettings();
+ this.renderLintSettingsToId(this.currentRuleSearchQuery, LintSettingId);
+ this.updateToggleAllRulesButton();
+ new Notice(`All Harper rules ${anyEnabledNow ? 'disabled' : 'enabled'}`);
});
});
const lintSettings = document.createElement('DIV');
- lintSettings.id = 'HarperLintSettings';
+ lintSettings.id = LintSettingId;
containerEl.appendChild(lintSettings);
- this.renderLintSettings('', lintSettings);
+ // Ensure default config is loaded before initial render so values reflect defaults.
+ this.state.getDefaultLintConfig().then((v) => {
+ this.defaultLintConfig = v as unknown as Record<string, boolean>;
+ this.renderLintSettingsToId(this.currentRuleSearchQuery, lintSettings.id);
+ });
}
- renderLintSettingsToId(searchQuery: string, id: string) {
+ private async updateToggleAllRulesButton() {
+ if (!this.toggleAllButton) return;
+ const anyEnabled = await this.state.areAnyRulesEnabled();
+ this.toggleAllButton.setButtonText(anyEnabled ? 'Disable All Rules' : 'Enable All Rules');
+ }
+
+ async renderLintSettingsToId(searchQuery: string, id: string) {
const el = document.getElementById(id);
- this.renderLintSettings(searchQuery, el!);
+ if (!el) return;
+ const effective = await this.state.getEffectiveLintConfig();
+ this.renderLintSettings(searchQuery, el, effective);
}
- renderLintSettings(searchQuery: string, containerEl: HTMLElement) {
+ private renderLintSettings(
+ searchQuery: string,
+ containerEl: HTMLElement,
+ effectiveConfig: Record<string, boolean>,
+ ) {
containerEl.innerHTML = '';
const queryLower = searchQuery.toLowerCase();
@@ -170,44 +248,32 @@ export class HarperSettingTab extends PluginSettingTab {
template.innerHTML = descriptionHTML;
fragment.appendChild(template.content);
+ // Determine default for this rule (if available)
+ const defaultVal = this.defaultLintConfig?.[setting];
+
new Setting(containerEl)
.setName(startCase(setting))
.setDesc(fragment)
- .addDropdown((dropdown) =>
+ .addDropdown((dropdown) => {
+ const effective: boolean | undefined = effectiveConfig[setting];
+ const usingDefault = value === null;
+ const onLabel = usingDefault && defaultVal === true ? 'On (default)' : 'On';
+ const offLabel = usingDefault && defaultVal === false ? 'Off (default)' : 'Off';
dropdown
- .addOption('default', 'Default')
- .addOption('enable', 'On')
- .addOption('disable', 'Off')
- .setValue(valueToString(value))
- .onChange(async (value) => {
- this.settings.lintSettings[setting] = stringToValue(value);
+ .addOption('enable', onLabel)
+ .addOption('disable', offLabel)
+ .setValue(effective ? 'enable' : 'disable')
+ .onChange(async (v) => {
+ this.settings.lintSettings[setting] = v === 'enable';
await this.state.initializeFromSettings(this.settings);
- }),
- );
+ // Re-render to update labels (remove "(default)" once overridden)
+ this.renderLintSettingsToId(this.currentRuleSearchQuery, LintSettingId);
+ this.updateToggleAllRulesButton();
+ });
+ });
}
}
}
-function valueToString(value: boolean | null): string {
- switch (value) {
- case true:
- return 'enable';
- case false:
- return 'disable';
- case null:
- return 'default';
- }
-}
-
-function stringToValue(str: string): boolean | null {
- switch (str) {
- case 'enable':
- return true;
- case 'disable':
- return false;
- case 'default':
- return null;
- }
-
- throw 'Fell through case';
-}
+// Note: dropdowns present only On/Off. When using defaults (unset),
+// the matching option label includes "(default)".
diff --git a/packages/obsidian-plugin/src/State.test.ts b/packages/obsidian-plugin/src/State.test.ts
index 15dac647c..68aed0753 100644
--- a/packages/obsidian-plugin/src/State.test.ts
+++ b/packages/obsidian-plugin/src/State.test.ts
@@ -100,3 +100,60 @@ test('Can be initialized with incomplete lint settings and retain default state.
expect(await state.getSettings()).toStrictEqual(defaultSettings);
});
+
+test('resetAllRulesToDefaults sets all overrides to null', async () => {
+ const state = createEphemeralState();
+
+ // Start with all enabled, then reset
+ let settings = await state.getSettings();
+ for (const key of Object.keys(settings.lintSettings)) {
+ settings.lintSettings[key] = true;
+ }
+ await state.initializeFromSettings(settings);
+
+ await state.resetAllRulesToDefaults();
+ settings = await state.getSettings();
+ for (const key of Object.keys(settings.lintSettings)) {
+ expect(settings.lintSettings[key]).toBeNull();
+ }
+});
+
+test('setAllRulesEnabled toggles all rules on and off', async () => {
+ const state = createEphemeralState();
+
+ await state.setAllRulesEnabled(true);
+ let settings = await state.getSettings();
+ for (const key of Object.keys(settings.lintSettings)) {
+ expect(settings.lintSettings[key]).toBe(true);
+ }
+
+ await state.setAllRulesEnabled(false);
+ settings = await state.getSettings();
+ for (const key of Object.keys(settings.lintSettings)) {
+ expect(settings.lintSettings[key]).toBe(false);
+ }
+});
+
+test('getEffectiveLintConfig matches defaults after reset', async () => {
+ const state = createEphemeralState();
+ await state.resetAllRulesToDefaults();
+ const effective = await state.getEffectiveLintConfig();
+ const defaults = (await state.getDefaultLintConfig()) as Record<string, boolean>;
+ expect(Object.keys(effective).sort()).toStrictEqual(Object.keys(defaults).sort());
+ for (const k of Object.keys(defaults)) {
+ expect(effective[k]).toBe(defaults[k]);
+ }
+});
+
+test('getEffectiveLintConfig reflects explicit overrides', async () => {
+ const state = createEphemeralState();
+ const settings = await state.getSettings();
+ for (const key of Object.keys(settings.lintSettings)) {
+ settings.lintSettings[key] = true;
+ }
+ await state.initializeFromSettings(settings);
+ const effective = await state.getEffectiveLintConfig();
+ for (const k of Object.keys(effective)) {
+ expect(effective[k]).toBe(true);
+ }
+});
diff --git a/packages/obsidian-plugin/src/State.ts b/packages/obsidian-plugin/src/State.ts
index cd73d9121..d3e036a20 100644
--- a/packages/obsidian-plugin/src/State.ts
+++ b/packages/obsidian-plugin/src/State.ts
@@ -53,7 +53,7 @@ export default class State {
}
const defaultConfig = await this.harper.getDefaultLintConfig();
- for (const [key, value] of Object.entries(defaultConfig)) {
+ for (const key of Object.keys(defaultConfig)) {
if (settings.lintSettings[key] == undefined) {
settings.lintSettings[key] = null;
}
@@ -221,10 +221,67 @@ export default class State {
};
}
+ /**
+ * Reset all lint rule overrides back to their defaults (null).
+ * Persists and reinitializes state to apply changes.
+ */
+ public async resetAllRulesToDefaults(): Promise<void> {
+ const settings = await this.getSettings();
+ for (const key of Object.keys(settings.lintSettings)) {
+ settings.lintSettings[key] = null;
+ }
+ await this.initializeFromSettings(settings);
+ }
+
+ /**
+ * Enable or disable all lint rules in bulk by setting explicit values.
+ * This overrides individual rule settings until changed again.
+ */
+ public async setAllRulesEnabled(enabled: boolean): Promise<void> {
+ const settings = await this.getSettings();
+ for (const key of Object.keys(settings.lintSettings)) {
+ settings.lintSettings[key] = enabled;
+ }
+ await this.initializeFromSettings(settings);
+ }
+
public async getDescriptionHTML(): Promise<Record<string, string>> {
return await this.harper.getLintDescriptionsHTML();
}
+ /** Expose the default lint configuration for UI rendering. */
+ public async getDefaultLintConfig(): Promise<LintConfig> {
+ return await this.harper.getDefaultLintConfig();
+ }
+
+ /** Effective config: merges defaults with overrides (null/undefined uses default). */
+ public async getEffectiveLintConfig(): Promise<Record<string, boolean>> {
+ const defaults = (await this.getDefaultLintConfig()) as Record<string, boolean>;
+ const overrides = (await this.getSettings()).lintSettings as Record<
+ string,
+ boolean | null | undefined
+ >;
+ const effective: Record<string, boolean> = {};
+ for (const key of Object.keys(defaults)) {
+ const v = overrides[key];
+ effective[key] = v === null || v === undefined ? defaults[key] : Boolean(v);
+ }
+ return effective;
+ }
+
+ /** Determine if any rules are effectively enabled, considering defaults. */
+ public async areAnyRulesEnabled(): Promise<boolean> {
+ const settings = await this.getSettings();
+ const defaults = await this.getDefaultLintConfig();
+ for (const key of Object.keys(settings.lintSettings)) {
+ const v = settings.lintSettings[key] as boolean | null | undefined;
+ const def = (defaults as Record<string, boolean | undefined>)[key];
+ const effective = v === null || v === undefined ? def : v;
+ if (effective) return true;
+ }
+ return false;
+ }
+
/** Get a reference to the CM editor extensions.
* Do not mutate the returned value, except via methods on this class. */
public getCMEditorExtensions(): Extension[] {