Skip to content

Commit

Permalink
Hotfix choice finding algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jul 2, 2021
1 parent 608ee47 commit b614c04
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "quickadd",
"name": "QuickAdd",
"version": "0.3.0",
"version": "0.3.1",
"minAppVersion": "0.12.5",
"description": "Quickly add new pages or content to your vault.",
"author": "Christian B. B. Houmann",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quickadd",
"version": "0.3.0",
"version": "0.3.1",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
17 changes: 1 addition & 16 deletions src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,7 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
}

protected async executeChoice(command: IChoiceCommand) {
const choices: IChoice[] = this.plugin.settings.choices;

const findChoice = (choiceArr: IChoice[]) => {
let tempChoice: IChoice;
for (const choice of choiceArr) {
tempChoice = choice;

if (choice.type === ChoiceType.Multi)
tempChoice = findChoice((<IMultiChoice> choice).choices);

if (tempChoice.id === command.choiceId)
return tempChoice;
}
}

const targetChoice = findChoice(choices);
const targetChoice = this.plugin.getChoiceById(command.choiceId);
if (!targetChoice) {
log.logError("choice could not be found.");
return;
Expand Down
27 changes: 18 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,29 @@ export default class QuickAdd extends Plugin {
}
}

public getChoice(choiceName: string): IChoice {
return this.settings.choices.find((choice) => this.getChoiceHelper(choiceName, choice));
public getChoiceById(choiceId: string): IChoice {
return this.getChoice("id", choiceId);
}

private getChoiceHelper(targetChoiceName: string, currentChoice: IChoice) {
if (currentChoice.type === ChoiceType.Multi) {
let foundChoice: IChoice = (currentChoice as IMultiChoice).choices
.find((choice) => this.getChoiceHelper(targetChoiceName, choice));
public getChoiceByName(choiceName: string): IChoice {
return this.getChoice("name", choiceName);
}

private getChoice(by: "name" | "id", targetPropertyValue: string): IChoice {
let tempChoice: IChoice;

if (foundChoice) return foundChoice;
const findChoice = (choice: IChoice) => {
if (choice[by] === targetPropertyValue) {
tempChoice = choice;
return tempChoice;
}

if (choice.type === ChoiceType.Multi) (choice as IMultiChoice).choices.forEach(findChoice);
}

if (currentChoice.name === targetChoiceName)
return currentChoice;
this.settings.choices.forEach(findChoice);

return tempChoice;
}

public removeCommandForChoice(choice: IChoice) {
Expand Down
2 changes: 1 addition & 1 deletion src/quickAddApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class QuickAddApi {
suggester: (displayItems: string[] | ((value: string, index?: number, arr?: string[]) => string[]), actualItems: string[]) => {return this.suggester(app, displayItems, actualItems)},
checkboxPrompt: (items: string[], selectedItems?: string[]) => {return this.checkboxPrompt(app, items, selectedItems)},
executeChoice: async (choiceName: string) => {
const choice: IChoice = plugin.getChoice(choiceName);
const choice: IChoice = plugin.getChoiceByName(choiceName);
if (!choice) log.logError(`choice named '${choiceName}' not found`);

await choiceExecutor.execute(choice);
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"0.2.16": "0.12.4",
"0.3.0": "0.12.5"
"0.3.1": "0.12.5"
}

0 comments on commit b614c04

Please sign in to comment.