Skip to content

Commit 06e17f0

Browse files
authored
Macros can execute choices. (#21)
1 parent e61ae33 commit 06e17f0

24 files changed

+178
-50
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "quickadd",
33
"name": "QuickAdd",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"minAppVersion": "0.12.00",
66
"description": "Quickly add new pages or content to your vault.",
77
"author": "Christian B. B. Houmann",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quickadd",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Quickly add new pages or content to your vault.",
55
"main": "main.js",
66
"scripts": {

src/IChoiceExecutor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type IChoice from "./types/choices/IChoice";
2+
3+
export interface IChoiceExecutor {
4+
execute(choice: IChoice): Promise<void>;
5+
}

src/MacrosManager.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import {App, ButtonComponent, Modal, Setting, TextComponent, ToggleComponent} fr
33
import {MacroBuilder} from "./gui/MacroBuilder";
44
import {QuickAddMacro} from "./types/macros/QuickAddMacro";
55
import {log} from "./logger/logManager";
6+
import type IChoice from "./types/choices/IChoice";
7+
import {ChoiceType} from "./types/choices/choiceType";
8+
import type IMultiChoice from "./types/choices/IMultiChoice";
69

710
export class MacrosManager extends Modal {
811
public waitForClose: Promise<IMacro[]>;
912
private resolvePromise: (macros: IMacro[]) => void;
1013
private rejectPromise: (reason?: any) => void;
1114
private updateMacroContainer: () => void;
1215

13-
constructor(public app: App, private macros: IMacro[]) {
16+
constructor(public app: App, private macros: IMacro[], private choices: IChoice[]) {
1417
super(app)
1518

1619
this.waitForClose = new Promise<IMacro[]>(
@@ -82,7 +85,20 @@ export class MacrosManager extends Modal {
8285
configureButton.buttonEl.style.marginRight = "0";
8386

8487
configureButton.setButtonText("Configure").onClick(async evt => {
85-
const newMacro = await new MacroBuilder(this.app, macro).waitForClose;
88+
const getReachableChoices = (choices: IChoice[]) => {
89+
let reachableChoices: IChoice[] = [];
90+
choices.forEach(choice => {
91+
if (choice.type === ChoiceType.Multi)
92+
reachableChoices.push(...getReachableChoices((<IMultiChoice>choice).choices));
93+
94+
if (choice.type !== ChoiceType.Multi)
95+
reachableChoices.push(choice);
96+
})
97+
return reachableChoices;
98+
}
99+
100+
const reachableChoices = getReachableChoices(this.choices);
101+
const newMacro = await new MacroBuilder(this.app, macro, reachableChoices).waitForClose;
86102

87103
if (newMacro) {
88104
this.updateMacro(newMacro);

src/choiceExecutor.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ import {log} from "./logger/logManager";
99
import {TemplateChoiceEngine} from "./engine/TemplateChoiceEngine";
1010
import {CaptureChoiceEngine} from "./engine/CaptureChoiceEngine";
1111
import {MacroChoiceEngine} from "./engine/MacroChoiceEngine";
12+
import type {IChoiceExecutor} from "./IChoiceExecutor";
1213

13-
export class ChoiceExecutor {
14-
constructor(private app: App, private plugin: QuickAdd, private choice: IChoice) { }
14+
export class ChoiceExecutor implements IChoiceExecutor {
15+
private variables: Map<string, string> = new Map<string, string>();
1516

16-
public async execute(): Promise<void> {
17-
switch (this.choice.type) {
17+
constructor(private app: App, private plugin: QuickAdd) { }
18+
19+
async execute(choice: IChoice): Promise<void> {
20+
switch (choice.type) {
1821
case ChoiceType.Template:
19-
const templateChoice: ITemplateChoice = this.choice as ITemplateChoice;
22+
const templateChoice: ITemplateChoice = choice as ITemplateChoice;
2023
await this.onChooseTemplateType(templateChoice);
2124
break;
2225
case ChoiceType.Capture:
23-
const captureChoice: ICaptureChoice = this.choice as ICaptureChoice;
26+
const captureChoice: ICaptureChoice = choice as ICaptureChoice;
2427
await this.onChooseCaptureType(captureChoice);
2528
break;
2629
case ChoiceType.Macro:
27-
const macroChoice: IMacroChoice = this.choice as IMacroChoice;
30+
const macroChoice: IMacroChoice = choice as IMacroChoice;
2831
await this.onChooseMacroType(macroChoice);
2932
break;
3033
default:
@@ -38,7 +41,7 @@ export class ChoiceExecutor {
3841
return;
3942
}
4043

41-
await new TemplateChoiceEngine(this.app, this.plugin, templateChoice).run();
44+
await new TemplateChoiceEngine(this.app, this.plugin, templateChoice, this).run();
4245
}
4346

4447
private async onChooseCaptureType(captureChoice: ICaptureChoice) {
@@ -47,10 +50,15 @@ export class ChoiceExecutor {
4750
return;
4851
}
4952

50-
await new CaptureChoiceEngine(this.app, this.plugin, captureChoice).run();
53+
await new CaptureChoiceEngine(this.app, this.plugin, captureChoice, this).run();
5154
}
5255

5356
private async onChooseMacroType(macroChoice: IMacroChoice) {
54-
await new MacroChoiceEngine(this.app, macroChoice, this.plugin.settings.macros).run();
57+
const macroEngine = await new MacroChoiceEngine(this.app, this.plugin, macroChoice, this.plugin.settings.macros, this, this.variables);
58+
await macroEngine.run();
59+
60+
Object.keys(macroEngine.params.variables).forEach(key => {
61+
this.variables.set(key, macroEngine.params.variables[key]);
62+
});
5563
}
5664
}

src/engine/CaptureChoiceEngine.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ import {MARKDOWN_FILE_EXTENSION_REGEX, VALUE_SYNTAX} from "../constants";
77
import type QuickAdd from "../main";
88
import {QuickAddChoiceEngine} from "./QuickAddChoiceEngine";
99
import {SingleTemplateEngine} from "./SingleTemplateEngine";
10+
import type {IChoiceExecutor} from "../IChoiceExecutor";
1011

1112
export class CaptureChoiceEngine extends QuickAddChoiceEngine {
1213
choice: ICaptureChoice;
1314
private formatter: CaptureChoiceFormatter;
1415
private readonly plugin: QuickAdd;
1516

16-
constructor(app: App, plugin: QuickAdd, choice: ICaptureChoice) {
17+
constructor(app: App, plugin: QuickAdd, choice: ICaptureChoice, private choiceExecutor: IChoiceExecutor) {
1718
super(app);
1819
this.choice = choice;
1920
this.plugin = plugin;
20-
this.formatter = new CaptureChoiceFormatter(app, plugin);
21+
this.formatter = new CaptureChoiceFormatter(app, plugin, choiceExecutor);
2122
}
2223

2324
async run(): Promise<void> {
@@ -46,7 +47,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
4647
await this.app.vault.modify(file, newFileContent);
4748
} else if (this.choice?.createFileIfItDoesntExist?.enabled) {
4849
const singleTemplateEngine: SingleTemplateEngine =
49-
new SingleTemplateEngine(this.app, this.plugin, this.choice.createFileIfItDoesntExist.template);
50+
new SingleTemplateEngine(this.app, this.plugin, this.choice.createFileIfItDoesntExist.template, this.choiceExecutor);
5051

5152
const fileContent: string = await singleTemplateEngine.run();
5253
const createdFile: TFile = await this.createFileWithInput(filePath, fileContent);

src/engine/MacroChoiceEngine.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,31 @@ import type {ICommand} from "../types/macros/ICommand";
1010
import {QuickAddChoiceEngine} from "./QuickAddChoiceEngine";
1111
import type {IMacro} from "../types/macros/IMacro";
1212
import GenericSuggester from "../gui/GenericSuggester/genericSuggester";
13+
import type {IChoiceCommand} from "../types/macros/IChoiceCommand";
14+
import type IChoice from "../types/choices/IChoice";
15+
import type QuickAdd from "../main";
16+
import type {IChoiceExecutor} from "../IChoiceExecutor";
17+
import {ChoiceType} from "../types/choices/choiceType";
18+
import type IMultiChoice from "../types/choices/IMultiChoice";
1319

1420
export class MacroChoiceEngine extends QuickAddChoiceEngine {
1521
public choice: IMacroChoice;
1622
protected output: string;
17-
protected readonly params = {app: this.app, quickAddApi: QuickAddApi.GetApi(this.app)};
23+
protected macros: IMacro[];
24+
protected choiceExecutor: IChoiceExecutor;
25+
public params = {app: this.app, quickAddApi: QuickAddApi.GetApi(this.app), variables: {}};
26+
protected readonly plugin: QuickAdd;
1827

19-
constructor(app: App, choice: IMacroChoice, protected macros: IMacro[]) {
28+
constructor(app: App, plugin: QuickAdd, choice: IMacroChoice, macros: IMacro[], choiceExecutor: IChoiceExecutor, variables: Map<string, string>) {
2029
super(app);
2130
this.choice = choice;
31+
this.plugin = plugin;
32+
this.macros = macros;
33+
this.choiceExecutor = choiceExecutor;
34+
35+
variables?.forEach(((value, key) => {
36+
this.params.variables[key] = value;
37+
}));
2238
}
2339

2440
async run(): Promise<void> {
@@ -34,6 +50,8 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
3450
await this.executeObsidianCommand(command as IObsidianCommand);
3551
if (command?.type === CommandType.UserScript)
3652
await this.executeUserScript(command as IUserScript);
53+
if (command?.type === CommandType.Choice)
54+
await this.executeChoice(command as IChoiceCommand);
3755
}
3856
}
3957

@@ -108,5 +126,29 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
108126
// @ts-ignore
109127
this.app.commands.executeCommandById(command.commandId);
110128
}
111-
}
112129

130+
protected async executeChoice(command: IChoiceCommand) {
131+
const choices: IChoice[] = this.plugin.settings.choices;
132+
133+
const findChoice = (choiceArr: IChoice[]) => {
134+
let tempChoice: IChoice;
135+
for (const choice of choiceArr) {
136+
tempChoice = choice;
137+
138+
if (choice.type === ChoiceType.Multi)
139+
tempChoice = findChoice((<IMultiChoice> choice).choices);
140+
141+
if (tempChoice.id === command.choiceId)
142+
return tempChoice;
143+
}
144+
}
145+
146+
const targetChoice = findChoice(choices);
147+
if (!targetChoice) {
148+
log.logError("choice could not be found.");
149+
return;
150+
}
151+
152+
await this.choiceExecutor.execute(targetChoice);
153+
}
154+
}

src/engine/SingleMacroEngine.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import type {App} from "obsidian";
22
import type {IMacro} from "../types/macros/IMacro";
33
import {MacroChoiceEngine} from "./MacroChoiceEngine";
4-
import {QuickAddApi} from "../quickAddApi";
4+
import type QuickAdd from "../main";
5+
import type {IChoiceExecutor} from "../IChoiceExecutor";
56

67
export class SingleMacroEngine extends MacroChoiceEngine {
7-
public readonly params = {app: this.app, quickAddApi: QuickAddApi.GetApi(this.app), variables: {}};
88
private memberAccess: string[];
99

10-
constructor(app: App, macros: IMacro[], private variables: Map<string, string>) {
11-
super(app, null, macros);
12-
13-
variables.forEach(((value, key) => {
14-
this.params.variables[key] = value;
15-
}));
10+
constructor(app: App, plugin: QuickAdd, macros: IMacro[], choiceExecutor: IChoiceExecutor, variables: Map<string, string>) {
11+
super(app, plugin, null, macros, choiceExecutor, variables);
1612
}
1713

1814
public async runAndGetOutput(macroName: string): Promise<string> {

src/engine/SingleTemplateEngine.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {TemplateEngine} from "./TemplateEngine";
22
import type {App} from "obsidian";
33
import type QuickAdd from "../main";
4+
import type {IChoiceExecutor} from "../IChoiceExecutor";
45

56
export class SingleTemplateEngine extends TemplateEngine {
6-
constructor(app: App, plugin: QuickAdd, private templatePath: string) {
7-
super(app, plugin);
7+
constructor(app: App, plugin: QuickAdd, private templatePath: string, choiceExecutor: IChoiceExecutor) {
8+
super(app, plugin, choiceExecutor);
89
}
910
public async run(): Promise<string> {
1011
let templateContent: string = await this.getTemplateContent(this.templatePath);

src/engine/StartupMacroEngine.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type {App} from "obsidian";
22
import type {IMacro} from "../types/macros/IMacro";
33
import {MacroChoiceEngine} from "./MacroChoiceEngine";
4+
import type QuickAdd from "../main";
5+
import type {IChoiceExecutor} from "../IChoiceExecutor";
46

57
export class StartupMacroEngine extends MacroChoiceEngine {
6-
constructor(app: App, macros: IMacro[]) {
7-
super(app, null, macros);
8+
constructor(app: App, plugin: QuickAdd, macros: IMacro[], choiceExecutor: IChoiceExecutor) {
9+
super(app, plugin, null, macros, choiceExecutor, null);
810
}
911

1012
async run(): Promise<void> {

0 commit comments

Comments
 (0)