From 5c3ed9b00ca49606caa730381c16e25a3c17b07c Mon Sep 17 00:00:00 2001 From: Aleix Soler Date: Sat, 19 Jul 2025 13:59:23 +0200 Subject: [PATCH] fix: reload commands when settings changed --- core/commandsService.ts | 12 ++++-------- core/settingsService.ts | 1 + integrations/commands/commandsIntegration.ts | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/commandsService.ts b/core/commandsService.ts index de2dfa5..5f57a80 100644 --- a/core/commandsService.ts +++ b/core/commandsService.ts @@ -310,17 +310,13 @@ export class CommandsService { return statuses.length > 0 ? statuses.map((s) => s.name) : ["unknown"]; } - public unload(): void { - // Remove existing commands - this.removeQuickStatusCommands(); - } - /** * Remove all quick status commands */ private removeQuickStatusCommands(): void { const allStatuses = BaseNoteStatusService.getAllAvailableStatuses(); + //TODO: If there is an update the allStatuses.forEach((status) => { // @ts-ignore this.app.commands.removeCommand( @@ -333,9 +329,9 @@ export class CommandsService { }); } - destroy(): void { - // Commands are automatically cleaned up when the plugin is disabled - // But we can track them for debugging purposes + public destroy(): void { + // Remove existing commands + this.removeQuickStatusCommands(); this.registeredCommands.clear(); } } diff --git a/core/settingsService.ts b/core/settingsService.ts index b570e45..068ef4a 100644 --- a/core/settingsService.ts +++ b/core/settingsService.ts @@ -18,6 +18,7 @@ class SettingsService { if (!(key in this.settings)) { throw new Error(`The "${key}" setting is not a known setting key`); } + // const oldValue = this.settings[key]; // TODO: Send the old value this.settings[key] = value; this.saveSettings().catch(console.error); // INFO: Send the propgation event diff --git a/integrations/commands/commandsIntegration.ts b/integrations/commands/commandsIntegration.ts index 3e3d4db..148066e 100644 --- a/integrations/commands/commandsIntegration.ts +++ b/integrations/commands/commandsIntegration.ts @@ -1,3 +1,4 @@ +import eventBus from "@/core/eventBus"; import { Plugin } from "obsidian"; import { CommandsService } from "../../core/commandsService"; @@ -24,11 +25,25 @@ export class CommandsIntegration { async integrate(): Promise { // Register all commands from the service this.commandsService.registerAllCommands(); + + eventBus.subscribe( + "plugin-settings-changed", + ({ value, key }) => { + if ( + key === "quickStatusCommands" || + key === "useMultipleStatuses" + ) { + this.commandsService.destroy(); + /// BUG: if removed a command will persist because is not removed, you need the oldStates to send it to be disabled // const oldValue = this.settings[key]; // TODO: Send the old value + this.commandsService.registerAllCommands(); // INFO: Reset the registered commands + } + }, + "statusBarIntegrationSubscription2", + ); } destroy(): void { if (this.commandsService) { - this.commandsService.unload(); this.commandsService.destroy(); } CommandsIntegration.instance = null;