diff --git a/src/manager/commands/leftRibbonManager.ts b/src/manager/commands/leftRibbonManager.ts index a59c6c2..2a513f8 100644 --- a/src/manager/commands/leftRibbonManager.ts +++ b/src/manager/commands/leftRibbonManager.ts @@ -32,21 +32,21 @@ export default class LeftRibbonManager extends CommandManagerBase { this.plugin.settings.leftRibbon.push(pair); await this.plugin.saveSettings(); } - if (isModeActive(pair.mode)) { - this.plugin.addRibbonIcon(pair.icon, pair.name, () => - app.commands.executeCommandById(pair.id) - ); + this.plugin.addRibbonIcon(pair.icon, pair.name, () => + app.commands.executeCommandById(pair.id) + ); + // @ts-expect-error + const nativeAction = app.workspace.leftRibbon.items.find((i, index) => { + const pairId = `${this.plugin.manifest.id}:${pair.name}`; + return i.id === pairId; + }); + if (nativeAction) { + nativeAction.hidden = !isModeActive(pair.mode); // @ts-expect-error - const nativeAction = app.workspace.leftRibbon.items.find( - // @ts-expect-error - (i) => i.icon === pair.icon && i.name === i.name - ); - if (nativeAction) { - nativeAction.buttonEl.style.color = - pair.color === "#000000" || pair.color === undefined - ? "inherit" - : pair.color; - } + nativeAction.buttonEl.style.color = + pair.color === "#000000" || pair.color === undefined + ? "inherit" + : pair.color; this.plugin.register(() => this.removeCommand(pair, false)); } } @@ -60,10 +60,10 @@ export default class LeftRibbonManager extends CommandManagerBase { await this.plugin.saveSettings(); } // @ts-expect-error - const nativeAction = app.workspace.leftRibbon.items.find( - // @ts-expect-error - (i) => i.icon === pair.icon && i.name === i.name - ); + const nativeAction = app.workspace.leftRibbon.items.find((i, index) => { + const pairId = `${this.plugin.manifest.id}:${pair.name}`; + return i.id === pairId; + }); if (nativeAction) { nativeAction.buttonEl.remove(); } @@ -78,4 +78,11 @@ export default class LeftRibbonManager extends CommandManagerBase { this.addCommand(pair, false); }); } + + public update(): void { + this.plugin.settings.leftRibbon.forEach((pair) => { + this.addCommand(pair, false); + }); + } + } diff --git a/src/ui/components/commandViewerComponent.tsx b/src/ui/components/commandViewerComponent.tsx index 2dcbf4e..1194215 100644 --- a/src/ui/components/commandViewerComponent.tsx +++ b/src/ui/components/commandViewerComponent.tsx @@ -71,9 +71,10 @@ export default function CommandViewer({ handleRename={async ( name ): Promise => { - cmd.name = name; + manager.removeCommand(cmd); await plugin.saveSettings(); - manager.reorder(); + cmd.name = name; + manager.addCommand(cmd); this.forceUpdate(); }} handleNewIcon={async (): Promise => { @@ -84,7 +85,7 @@ export default function CommandViewer({ if (newIcon && newIcon !== cmd.icon) { cmd.icon = newIcon; await plugin.saveSettings(); - manager.reorder(); + manager.update(); this.forceUpdate(); } dispatchEvent( @@ -109,7 +110,7 @@ export default function CommandViewer({ cmd.mode = mode || modes[currentIdx + 1]; await plugin.saveSettings(); - manager.reorder(); + manager.update(); this.forceUpdate(); }} handleColorChange={async ( @@ -117,7 +118,7 @@ export default function CommandViewer({ ): Promise => { cmd.color = color; await plugin.saveSettings(); - manager.reorder(); + manager.update(); }} /> ); @@ -146,7 +147,6 @@ export default function CommandViewer({ onClick={async (): Promise => { const pair = await chooseNewCommand(plugin); await manager.addCommand(pair); - manager.reorder(); this.forceUpdate(); }} >