Skip to content

Commit

Permalink
Merge pull request #18 from yajamon/well-binding
Browse files Browse the repository at this point in the history
Well binding
  • Loading branch information
yajamon authored Jan 5, 2022
2 parents f3c2b26 + 8445921 commit 76589eb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-command-alias-plugin",
"name": "Command Alias",
"version": "1.1.0",
"version": "1.2.0",
"minAppVersion": "0.9.12",
"description": "This plugin gives aliases to Obsidian commands.",
"author": "yajamon<[email protected]>",
Expand Down
49 changes: 40 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { CommandAliasPluginSettingTab } from "./setting-tab";
import { CommandSuggestionModal } from "./add-alias-modal";
interface CommandAliasPluginSettings {
aliases: AliasMap;
commandDetection: {
maxTry: number;
msecOfInterval: number;
}
}

type AliasMap = {
Expand All @@ -15,7 +19,19 @@ interface Alias {
}

const DEFAULT_SETTINGS: CommandAliasPluginSettings = {
aliases: {}
aliases: {},
commandDetection: {
maxTry: 5,
msecOfInterval: 200
}
}

async function timeoutPromise(msec: number) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(null);
}, msec);
});
}

export default class CommandAliasPlugin extends Plugin {
Expand All @@ -37,22 +53,35 @@ export default class CommandAliasPlugin extends Plugin {
},
});

this.addSettingTab(new CommandAliasPluginSettingTab(this.app, this));

let promises: Array<Promise<void>> = [];
for (const aliasId in this.settings.aliases) {
if (!Object.prototype.hasOwnProperty.call(this.settings.aliases, aliasId)) {
continue;
}
this.addAliasCommand(aliasId);
let p = this.addAliasCommand(aliasId);
promises.push(p);
}

this.addSettingTab(new CommandAliasPluginSettingTab(this.app, this));
await Promise.all(promises);
}

private addAliasCommand(aliasId: string) {
private async addAliasCommand(aliasId: string) {
let app = this.app as AppExtension;
const { maxTry, msecOfInterval } = this.settings.commandDetection;

const alias = this.settings.aliases[aliasId];
const target = app.commands.commands[alias.commandId];
if (target) {
const commandDetection = new Promise(async (resolve, reject) => {
for (let tried = 0; tried < maxTry; tried += 1) {
let ref = app.commands.commands[alias.commandId];
if (ref != null) {
resolve(ref);
return;
}
await timeoutPromise(msecOfInterval)
}
reject("Missing command");
}).then((target: Command) => {
let command: Command = {
id: `alias:${aliasId}`,
name: `${alias.name}: ${target.name}`,
Expand Down Expand Up @@ -82,7 +111,7 @@ export default class CommandAliasPlugin extends Plugin {
}
}
this.addCommand(command);
} else {
}).catch((reason) => {
// fallback
let command: Command = {
id: `alias:${aliasId}`,
Expand All @@ -93,7 +122,9 @@ export default class CommandAliasPlugin extends Plugin {
}
}
this.addCommand(command);
}
});

return commandDetection;
}

onunload() {
Expand Down
25 changes: 25 additions & 0 deletions src/setting-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ export class CommandAliasPluginSettingTab extends PluginSettingTab {
this.display();
}));

// command detection
containerEl.createEl('h3', { text: 'Command detection' });
new Setting(containerEl)
.setName('Maximum number of trials')
.addSlider(slider => slider
.setLimits(1, 10, 1)
.setValue(this.plugin.settings.commandDetection.maxTry)
.setDynamicTooltip()
.onChange(async value => {
this.plugin.settings.commandDetection.maxTry = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Trial interval (msec)')
.addSlider(slider => slider
.setLimits(100, 1000, 100)
.setValue(this.plugin.settings.commandDetection.msecOfInterval)
.setDynamicTooltip()
.onChange(async value => {
this.plugin.settings.commandDetection.msecOfInterval = value;
await this.plugin.saveSettings();
})
);

// remove alias
containerEl.createEl('h3', { text: 'Register aliases' });

Expand Down

0 comments on commit 76589eb

Please sign in to comment.