Skip to content

Commit

Permalink
fix: some commands exist but appear unavailable (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgurney committed Nov 5, 2024
1 parent 4031e76 commit d9b45c9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
33 changes: 26 additions & 7 deletions src/Settings/UI/Modals/ToolbarSettingsModal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, ButtonComponent, DropdownComponent, Menu, MenuItem, Modal, Notice, Platform, Setting, TFile, TFolder, debounce, getIcon, normalizePath, setIcon, setTooltip } from 'obsidian';
import { App, ButtonComponent, Command, DropdownComponent, Menu, MenuItem, Modal, Notice, Platform, Setting, TFile, TFolder, debounce, getIcon, normalizePath, setIcon, setTooltip } from 'obsidian';
import { arraymove, debugLog, getElementPosition, removeComponentVisibility, addComponentVisibility, moveElement, getUUID, importArgs } from 'Utils/Utils';
import { emptyMessageFr, learnMoreFr, createToolbarPreviewFr, displayHelpSection, showWhatsNewIfNeeded, pluginLinkFr } from "../Utils/SettingsUIUtils";
import NoteToolbarPlugin from 'main';
Expand Down Expand Up @@ -919,14 +919,22 @@ export default class ToolbarSettingsModal extends Modal {
new Setting(fieldDiv)
.setClass("note-toolbar-setting-item-field-link")
.addSearch((cb) => {
new CommandSuggester(this.app, cb.inputEl);
new CommandSuggester(this.app, cb.inputEl, async (command) => {
this.updateItemComponentStatus(command.id, SettingType.Command, cb.inputEl.parentElement);
cb.inputEl.value = command.name;
toolbarItem.link = command.name;
toolbarItem.linkAttr.commandId = command.id;
toolbarItem.linkAttr.type = type;
await this.plugin.settingsManager.save();
this.renderPreview(toolbarItem);
});
cb.setPlaceholder(t('setting.item.option-command-placeholder'))
.setValue(toolbarItem.link)
.onChange(debounce(async (command) => {
let commandId = command ? (cb.inputEl?.getAttribute('data-command-id') ?? COMMAND_DOES_NOT_EXIST) : '';
let isValid = this.updateItemComponentStatus(commandId, SettingType.Command, cb.inputEl.parentElement);
toolbarItem.link = isValid ? command : '';
toolbarItem.linkAttr.commandId = isValid ? commandId : '';
.onChange(debounce(async (commandName) => {
const commandId = commandName ? this.getCommandIdByName(commandName) : '';
const isValid = this.updateItemComponentStatus(commandId, SettingType.Command, cb.inputEl.parentElement);
toolbarItem.link = isValid && commandName ? commandName : '';
toolbarItem.linkAttr.commandId = isValid && commandId ? commandId : '';
toolbarItem.linkAttr.type = type;
await this.plugin.settingsManager.save();
this.renderPreview(toolbarItem);
Expand Down Expand Up @@ -1858,6 +1866,17 @@ export default class ToolbarSettingsModal extends Modal {
* UTILITIES
*************************************************************************/

/**
* Get command ID by name.
* @param commandName name of the command to look for.
* @returns command ID or undefined.
*/
getCommandIdByName(commandName: string): string {
const availableCommands: Command[] = Object.values(this.plugin.app.commands.commands);
const matchedCommand = availableCommands.find(command => command.name === commandName);
return matchedCommand ? matchedCommand.id : COMMAND_DOES_NOT_EXIST;
}

/**
* Returns a URI that opens a search of the toolbar name in the toolbar property across all notes.
* @param toolbarName name of the toolbar to look for.
Expand Down
17 changes: 4 additions & 13 deletions src/Settings/UI/Suggesters/CommandSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@ import { AbstractInputSuggest, App, Command } from "obsidian";

export class CommandSuggester extends AbstractInputSuggest<Command> {

private inputEl: HTMLInputElement;
private commands: Command[];
private callback: (command: Command) => void

constructor(app: App, inputEl: HTMLInputElement) {
constructor(app: App, inputEl: HTMLInputElement, callback: (command: Command) => void) {
super(app, inputEl);
this.inputEl = inputEl;
this.callback = callback;
this.commands = Object.values(this.app.commands.commands);
}

getSuggestions(inputStr: string): Command[] {
const suggestions: Command[] = [];
const lowerCaseInputStr = inputStr.toLowerCase();
this.inputEl.removeAttribute("data-command-id");

this.commands.forEach((command: Command) => {
if (command.name.toLowerCase().includes(lowerCaseInputStr)) {
suggestions.push(command);
}
});

// if there's only one result and it matches the suggestion, just select it
if ((suggestions.length === 1) && suggestions[0].name.toLowerCase() === lowerCaseInputStr) {
this.inputEl.setAttribute("data-command-id", suggestions[0].id);
}

return suggestions;
}

Expand All @@ -35,10 +29,7 @@ export class CommandSuggester extends AbstractInputSuggest<Command> {
}

selectSuggestion(command: Command): void {
this.inputEl.value = command.name;
this.inputEl.setAttribute("data-command-id", command.id);
this.inputEl.trigger("input");
this.inputEl.blur();
this.callback(command);
this.close();
}
}

0 comments on commit d9b45c9

Please sign in to comment.