fix: correct ribbon item matching and stray global app references

- leftRibbonManager was comparing i.name === i.name (always true)
  instead of i.name === pair.name when locating a ribbon item to
  recolor/remove, so items sharing an icon with a different-named
  command could be matched incorrectly. Now compares against pair.name.
- Replace two stray references to the global `app` with the plugin's
  own `app` reference (this.plugin.app / plugin.app), consistent with
  the rest of the codebase.
- Use Object.prototype.hasOwnProperty.call() instead of calling
  hasOwnProperty directly on values that may not inherit from
  Object.prototype.
- Import Command/PluginManifest in types.ts so the Obsidian module
  augmentation resolves them instead of relying on ambient globals.
- Drop a leftover console.log of an expected/benign cancellation
  error in the "Add command" menu action.
This commit is contained in:
johnny1093 2026-07-11 09:26:19 -04:00
parent ebeae5c6c8
commit 8c08808823
6 changed files with 9 additions and 9 deletions

View file

@ -39,7 +39,7 @@ export default class LeftRibbonManager extends CommandManagerBase {
// @ts-expect-error
const nativeAction = this.plugin.app.workspace.leftRibbon.items.find(
// @ts-expect-error
(i) => i.icon === pair.icon && i.name === i.name
(i) => i.icon === pair.icon && i.name === pair.name
);
if (nativeAction) {
nativeAction.buttonEl.style.color =
@ -62,13 +62,13 @@ export default class LeftRibbonManager extends CommandManagerBase {
// @ts-expect-error
const nativeAction = this.plugin.app.workspace.leftRibbon.items.find(
// @ts-expect-error
(i) => i.icon === pair.icon && i.name === i.name
(i) => i.icon === pair.icon && i.name === pair.name
);
if (nativeAction) {
nativeAction.buttonEl.remove();
}
// @ts-expect-error
app.workspace.leftRibbon.items.remove(nativeAction);
this.plugin.app.workspace.leftRibbon.items.remove(nativeAction);
}
public reorder(): void {

View file

@ -163,8 +163,7 @@ abstract class Base extends CommandManagerBase {
commandList.push(pair);
await plugin.saveSettings();
} catch (error) {
//Do some proper handling here
console.log(error);
// User cancelled command/icon selection, nothing to do
}
});
});

View file

@ -1,4 +1,5 @@
import { h } from "preact";
import { Command, PluginManifest } from "obsidian";
export enum Action {
COMMAND,

View file

@ -100,8 +100,8 @@ export default function CommandComponent({
const owningPlugin = plugin.app.plugins.manifests[owningPluginID!];
const isInternal = !owningPlugin;
const isChecked =
cmd.hasOwnProperty("checkCallback") ||
cmd.hasOwnProperty("editorCheckCallback");
Object.prototype.hasOwnProperty.call(cmd, "checkCallback") ||
Object.prototype.hasOwnProperty.call(cmd, "editorCheckCallback");
const modeIcon = getModeIcon(pair.mode);
const modeName = pair.mode.match(/desktop|mobile|any/)

View file

@ -19,7 +19,7 @@ export function LeftRibbonHider({
useEffect(() => {
setRibbonCommands(
// @ts-expect-error
app.workspace.leftRibbon.items.map((item) => ({
plugin.app.workspace.leftRibbon.items.map((item) => ({
name: item.title,
icon: item.icon,
}))

View file

@ -24,7 +24,7 @@ export async function chooseNewCommand(
const command = await new AddCommandModal(plugin).awaitSelection();
let icon;
if (!command.hasOwnProperty("icon")) {
if (!Object.prototype.hasOwnProperty.call(command, "icon")) {
icon = await new ChooseIconModal(plugin).awaitSelection();
}