mirror of
https://github.com/jsmorabito/obsidian-commander.git
synced 2026-07-22 06:40:31 +00:00
Applies one consistent rule across main.ts and every command manager
(leftRibbon/pageHeader/statusBar/titleBar/explorer/menu):
- Inside an already-async handler (onClick, onclick, event listeners),
await the addCommand/removeCommand/reorder/removeMenu call so
settings are actually saved before the handler resolves, matching
the style already used by the adjacent Change Icon/Rename handlers
in the same files.
- In genuinely fire-and-forget contexts (executeStartupMacros,
forEach over settings on construction), mark the call with `void`
instead, since making the caller async there isn't warranted and
the existing concurrent-fire behavior is intentional.
- Drop `async` from a few addEventListener("transitionend", ...)
callbacks that never awaited anything - the async was doing nothing
but triggering the misused-promises warning.
- executeMacro's COMMAND/LOOP actions no longer await
executeCommandById, since it's synchronous (returns void, not a
Promise) - the await was already a no-op.
- Reject with Error objects instead of plain strings in the three
awaitSelection() modals (addCommand/chooseIcon/chooseCustomName),
since nothing reads the specific string value of these rejections.
No sequencing that previously happened is removed; this only adds
missing awaits/void or removes awaits/async that were already inert.
Worth exercising the affected menus (status bar, title bar, page
header, explorer, left ribbon, right-click "Add/Change Icon/Rename/
Delete") to confirm they still save and update correctly.
75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
import CommanderPlugin from "src/main";
|
|
import { CommandIconPair } from "src/types";
|
|
import CommandManagerBase from "./commandManager";
|
|
import { isModeActive } from "src/util";
|
|
|
|
export default class LeftRibbonManager extends CommandManagerBase {
|
|
public plugin: CommanderPlugin;
|
|
//private addBtn: HTMLDivElement;
|
|
|
|
public constructor(plugin: CommanderPlugin) {
|
|
super(plugin, plugin.settings.leftRibbon);
|
|
this.plugin = plugin;
|
|
|
|
this.plugin.settings.leftRibbon.forEach((pair) => {
|
|
void this.addCommand(pair, false);
|
|
});
|
|
|
|
this.plugin.app.workspace.onLayoutReady(() => {
|
|
// if (this.plugin.settings.showAddCommand) {
|
|
// this.plugin.addRibbonIcon("plus", t("Add new"), async () =>
|
|
// this.addCommand(await chooseNewCommand(plugin))
|
|
// );
|
|
// }
|
|
});
|
|
}
|
|
|
|
public async addCommand(
|
|
pair: CommandIconPair,
|
|
newlyAdded = true
|
|
): Promise<void> {
|
|
if (newlyAdded) {
|
|
this.plugin.settings.leftRibbon.push(pair);
|
|
await this.plugin.saveSettings();
|
|
}
|
|
if (isModeActive(pair.mode, this.plugin)) {
|
|
this.plugin.addRibbonIcon(pair.icon, pair.name, () =>
|
|
this.plugin.app.commands.executeCommandById(pair.id)
|
|
);
|
|
const nativeAction = this.plugin.app.workspace.leftRibbon.items.find(
|
|
(i) => i.icon === pair.icon && i.title === pair.name
|
|
);
|
|
if (nativeAction) {
|
|
nativeAction.buttonEl.style.color =
|
|
pair.color === "#000000" || pair.color === undefined
|
|
? "inherit"
|
|
: pair.color;
|
|
}
|
|
this.plugin.register(() => this.removeCommand(pair, false));
|
|
}
|
|
}
|
|
|
|
public async removeCommand(
|
|
pair: CommandIconPair,
|
|
remove = true
|
|
): Promise<void> {
|
|
if (remove) {
|
|
this.plugin.settings.leftRibbon.remove(pair);
|
|
await this.plugin.saveSettings();
|
|
}
|
|
const nativeAction = this.plugin.app.workspace.leftRibbon.items.find(
|
|
(i) => i.icon === pair.icon && i.title === pair.name
|
|
);
|
|
if (nativeAction) {
|
|
nativeAction.buttonEl.remove();
|
|
this.plugin.app.workspace.leftRibbon.items.remove(nativeAction);
|
|
}
|
|
}
|
|
|
|
public reorder(): void {
|
|
this.plugin.settings.leftRibbon.forEach((pair) => {
|
|
void this.removeCommand(pair, false);
|
|
void this.addCommand(pair, false);
|
|
});
|
|
}
|
|
}
|