Skip to content

Commit

Permalink
fix: existing choice in the macro now works when using shortcut butto…
Browse files Browse the repository at this point in the history
…n (not MacrosManager)

Nesting in Multis would get folded out when clicking the Macros Manager 'configure macro' button.
But since adding a shortcut to the macro from the choice settings, this wouldn't get done. Fix moves
the calculation to the macro builder.

fix #433
  • Loading branch information
chhoumann committed Apr 3, 2023
1 parent 9da92ee commit 78aab04
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 89 deletions.
23 changes: 1 addition & 22 deletions src/MacrosManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
import { MacroBuilder } from "./gui/MacroGUIs/MacroBuilder";
import { log } from "./logger/logManager";
import type IChoice from "./types/choices/IChoice";
import { ChoiceType } from "./types/choices/choiceType";
import type IMultiChoice from "./types/choices/IMultiChoice";
import type QuickAdd from "./main";
import { settingsStore } from "./settingsStore";

Expand Down Expand Up @@ -132,30 +130,11 @@ export class MacrosManager extends Modal {
configureButton
.setButtonText("Configure")
.onClick(async (evt) => {
const getReachableChoices = (choices: IChoice[]) => {
const reachableChoices: IChoice[] = [];
choices.forEach((choice) => {
if (choice.type === ChoiceType.Multi)
reachableChoices.push(
...getReachableChoices(
(<IMultiChoice>choice).choices
)
);

if (choice.type !== ChoiceType.Multi)
reachableChoices.push(choice);
});
return reachableChoices;
};

const reachableChoices = getReachableChoices(
this.choices
);
const newMacro = await new MacroBuilder(
this.app,
this.plugin,
macro,
reachableChoices
this.choices
).waitForClose;

if (newMacro) {
Expand Down
9 changes: 4 additions & 5 deletions src/choiceExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { App } from "obsidian";
import type QuickAdd from "./main";
import type IChoice from "./types/choices/IChoice";
import { ChoiceType } from "./types/choices/choiceType";
import type ITemplateChoice from "./types/choices/ITemplateChoice";
import type ICaptureChoice from "./types/choices/ICaptureChoice";
import type IMacroChoice from "./types/choices/IMacroChoice";
Expand All @@ -19,23 +18,23 @@ export class ChoiceExecutor implements IChoiceExecutor {

async execute(choice: IChoice): Promise<void> {
switch (choice.type) {
case ChoiceType.Template: {
case "Template": {
const templateChoice: ITemplateChoice =
choice as ITemplateChoice;
await this.onChooseTemplateType(templateChoice);
break;
}
case ChoiceType.Capture: {
case "Capture": {
const captureChoice: ICaptureChoice = choice as ICaptureChoice;
await this.onChooseCaptureType(captureChoice);
break;
}
case ChoiceType.Macro: {
case "Macro": {
const macroChoice: IMacroChoice = choice as IMacroChoice;
await this.onChooseMacroType(macroChoice);
break;
}
case ChoiceType.Multi: {
case "Multi": {
const multiChoice: IMultiChoice = choice as IMultiChoice;
this.onChooseMultiType(multiChoice);
break;
Expand Down
9 changes: 4 additions & 5 deletions src/gui/MacroGUIs/CommandList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import {CommandType} from "../../types/macros/CommandType";
import WaitCommand from "./Components/WaitCommand.svelte";
import NestedChoiceCommand from "./Components/NestedChoiceCommand.svelte";
import {ChoiceType} from "../../types/choices/choiceType";
import {TemplateChoiceBuilder} from "../ChoiceBuilder/templateChoiceBuilder";
import {CaptureChoiceBuilder} from "../ChoiceBuilder/captureChoiceBuilder";
import type ICaptureChoice from "../../types/choices/ICaptureChoice";
Expand Down Expand Up @@ -76,12 +75,12 @@
function getChoiceBuilder(choice: IChoice) {
switch (choice.type) {
case ChoiceType.Template:
case "Template":
return new TemplateChoiceBuilder(app, choice as ITemplateChoice, plugin);
case ChoiceType.Capture:
case "Capture":
return new CaptureChoiceBuilder(app, choice as ICaptureChoice, plugin);
case ChoiceType.Macro:
case ChoiceType.Multi:
case "Macro":
case "Multi":
default:
break;
}
Expand Down
21 changes: 20 additions & 1 deletion src/gui/MacroGUIs/MacroBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ import { SelectActiveLineCommand } from "../../types/macros/EditorCommands/Selec
import { SelectLinkOnActiveLineCommand } from "../../types/macros/EditorCommands/SelectLinkOnActiveLineCommand";
import GenericYesNoPrompt from "../GenericYesNoPrompt/GenericYesNoPrompt";
import { GenericTextSuggester } from "../suggesters/genericTextSuggester";
import type { MultiChoice } from "src/types/choices/MultiChoice";

function getChoicesAsList(nestedChoices: IChoice[]): IChoice[] {
const arr: IChoice[] = [];

const recursive = (choices: IChoice[]) => {
choices.forEach((choice) => {
if (choice.type === "Multi") {
recursive((choice as MultiChoice).choices);
} else {
arr.push(choice);
}
});
}

recursive(nestedChoices);

return arr;
}

export class MacroBuilder extends Modal {
public macro: IMacro;
Expand All @@ -51,7 +70,7 @@ export class MacroBuilder extends Modal {
super(app);
this.macro = macro;
this.svelteElements = [];
this.choices = choices;
this.choices = getChoicesAsList(choices);
this.plugin = plugin;

this.waitForClose = new Promise<IMacro>(
Expand Down
8 changes: 4 additions & 4 deletions src/gui/choiceList/AddChoiceBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<div class="addChoiceBox">
<input type="text" placeholder="Name" bind:value={name}>
<select id="addChoiceTypeSelector" bind:value={type}>
<option value={ChoiceType.Template}>{ChoiceType.Template}</option>
<option value={ChoiceType.Capture}>{ChoiceType.Capture}</option>
<option value={ChoiceType.Macro}>{ChoiceType.Macro}</option>
<option value={ChoiceType.Multi}>{ChoiceType.Multi}</option>
<option value={"Template"}>{"Template"}</option>
<option value={"Capture"}>{"Capture"}</option>
<option value={"Macro"}>{"Macro"}</option>
<option value={"Multi"}>{"Multi"}</option>
</select>
<button class="mod-cta" on:click={addChoice}>Add Choice</button>
</div>
Expand Down
3 changes: 1 addition & 2 deletions src/gui/choiceList/ChoiceList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import IChoice from "../../types/choices/IChoice";
import ChoiceListItem from "./ChoiceListItem.svelte";
import MultiChoiceListItem from "./MultiChoiceListItem.svelte";
import {ChoiceType} from "../../types/choices/choiceType";
import {DndEvent, dndzone, SHADOW_PLACEHOLDER_ITEM_ID, SOURCES} from "svelte-dnd-action";
import {createEventDispatcher} from "svelte";
Expand Down Expand Up @@ -49,7 +48,7 @@
class="choiceList"
style="{choices.length === 0 ? 'padding-bottom: 0.5rem' : ''}">
{#each choices.filter(c => c.id !== SHADOW_PLACEHOLDER_ITEM_ID) as choice(choice.id)}
{#if choice.type !== ChoiceType.Multi}
{#if choice.type !== "Multi"}
<ChoiceListItem
bind:dragDisabled={dragDisabled}
on:mousedown={startDrag}
Expand Down
37 changes: 18 additions & 19 deletions src/gui/choiceList/ChoiceView.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import IChoice from "../../types/choices/IChoice";
import {ChoiceType} from "../../types/choices/choiceType";
import ChoiceList from "./ChoiceList.svelte";
import IMultiChoice from "../../types/choices/IMultiChoice";
import AddChoiceBox from "./AddChoiceBox.svelte";
Expand Down Expand Up @@ -47,19 +46,19 @@
const {name, type} = event.detail;
switch (type) {
case ChoiceType.Template:
case "Template":
const templateChoice: ITemplateChoice = new TemplateChoice(name);
choices = [...choices, templateChoice];
break;
case ChoiceType.Capture:
case "Capture":
const captureChoice: ICaptureChoice = new CaptureChoice(name);
choices = [...choices, captureChoice];
break;
case ChoiceType.Macro:
case "Macro":
const macroChoice: IMacroChoice = new MacroChoice(name);
choices = [...choices, macroChoice];
break;
case ChoiceType.Multi:
case "Multi":
const multiChoice: IMultiChoice = new MultiChoice(name);
choices = [...choices, multiChoice];
break;
Expand All @@ -71,8 +70,8 @@
async function deleteChoice(e: any) {
const choice: IChoice = e.detail.choice;
const hasOwnMacro = choice.type === ChoiceType.Macro && macros.some(macro => macro.name === (choice as MacroChoice).name);
const isMulti = choice.type === ChoiceType.Multi;
const hasOwnMacro = choice.type === "Macro" && macros.some(macro => macro.name === (choice as MacroChoice).name);
const isMulti = choice.type === "Multi";
const userConfirmed: boolean = await GenericYesNoPrompt.Prompt(app,
`Confirm deletion of choice`, `Please confirm that you wish to delete '${choice.name}'.
Expand All @@ -97,7 +96,7 @@
}
function deleteChoiceHelper(id: string, value: IChoice): boolean {
if (value.type === ChoiceType.Multi) {
if (value.type === "Multi") {
(value as IMultiChoice).choices = (value as IMultiChoice).choices
.filter((v) => deleteChoiceHelper(id, v));
}
Expand All @@ -109,7 +108,7 @@
const {choice: oldChoice} = e.detail;
let updatedChoice: MultiChoice | TemplateChoice | CaptureChoice | MacroChoice;
if (oldChoice.type === ChoiceType.Multi) {
if (oldChoice.type === "Multi") {
updatedChoice = oldChoice;
const name = await GenericInputPrompt.Prompt(app, `Rename ${oldChoice.name}`, '', oldChoice.name);
Expand Down Expand Up @@ -160,21 +159,21 @@
let newChoice;
switch ((choice as IChoice).type) {
case ChoiceType.Template:
case "Template":
newChoice = new TemplateChoice(`${choice.name} (copy)`);
break;
case ChoiceType.Capture:
case "Capture":
newChoice = new CaptureChoice(`${choice.name} (copy)`);
break;
case ChoiceType.Macro:
case "Macro":
newChoice = new MacroChoice(`${choice.name} (copy)`);
break;
case ChoiceType.Multi:
case "Multi":
newChoice = new MultiChoice(`${choice.name} (copy)`);
break;
}
if (choice.type !== ChoiceType.Multi) {
if (choice.type !== "Multi") {
Object.assign(newChoice, excludeKeys(choice, ['id', 'name']));
} else {
(newChoice as IMultiChoice).choices = (choice as IMultiChoice).choices.map(c => duplicateChoice(c));
Expand All @@ -189,7 +188,7 @@
return oldChoice;
}
if (oldChoice.type === ChoiceType.Multi) {
if (oldChoice.type === "Multi") {
const multiChoice = (oldChoice as IMultiChoice);
const multiChoiceChoices = multiChoice.choices.map(c => updateChoiceHelper(c, newChoice))
return {...multiChoice, choices: multiChoiceChoices} as IChoice;
Expand All @@ -200,13 +199,13 @@
function getChoiceBuilder(choice: IChoice) {
switch (choice.type) {
case ChoiceType.Template:
case "Template":
return new TemplateChoiceBuilder(app, choice as ITemplateChoice, plugin);
case ChoiceType.Capture:
case "Capture":
return new CaptureChoiceBuilder(app, choice as ICaptureChoice, plugin);
case ChoiceType.Macro:
case "Macro":
return new MacroChoiceBuilder(app, choice as IMacroChoice, macros, settingsStore.getState().choices);
case ChoiceType.Multi:
case "Multi":
default:
break;
}
Expand Down
3 changes: 1 addition & 2 deletions src/gui/suggesters/choiceSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { FuzzyMatch} from "obsidian";
import { FuzzySuggestModal, MarkdownRenderer } from "obsidian";
import type IChoice from "../../types/choices/IChoice";
import { ChoiceExecutor } from "../../choiceExecutor";
import { ChoiceType } from "../../types/choices/choiceType";
import { MultiChoice } from "../../types/choices/MultiChoice";
import type IMultiChoice from "../../types/choices/IMultiChoice";
import type QuickAdd from "../../main";
Expand Down Expand Up @@ -49,7 +48,7 @@ export default class ChoiceSuggester extends FuzzySuggestModal<IChoice> {
item: IChoice,
evt: MouseEvent | KeyboardEvent
): Promise<void> {
if (item.type === ChoiceType.Multi)
if (item.type === "Multi")
this.onChooseMultiType(<IMultiChoice>item);
else await this.choiceExecutor.execute(item);
}
Expand Down
5 changes: 2 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { log } from "./logger/logManager";
import { ConsoleErrorLogger } from "./logger/consoleErrorLogger";
import { GuiLogger } from "./logger/guiLogger";
import { StartupMacroEngine } from "./engine/StartupMacroEngine";
import { ChoiceType } from "./types/choices/choiceType";
import { ChoiceExecutor } from "./choiceExecutor";
import type IChoice from "./types/choices/IChoice";
import type IMultiChoice from "./types/choices/IMultiChoice";
Expand Down Expand Up @@ -119,7 +118,7 @@ export default class QuickAdd extends Plugin {
}

public addCommandForChoice(choice: IChoice) {
if (choice.type === ChoiceType.Multi) {
if (choice.type === "Multi") {
this.addCommandsForChoices((<IMultiChoice>choice).choices);
}

Expand Down Expand Up @@ -163,7 +162,7 @@ export default class QuickAdd extends Plugin {
if (choice[by] === targetPropertyValue) {
return choice;
}
if (choice.type === ChoiceType.Multi) {
if (choice.type === "Multi") {
const subChoice = this.getChoice(
by,
targetPropertyValue,
Expand Down
3 changes: 1 addition & 2 deletions src/migrations/isCaptureChoice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { CaptureChoice } from "src/types/choices/CaptureChoice";
import { ChoiceType } from "src/types/choices/choiceType";
import type IChoice from "src/types/choices/IChoice";

export function isCaptureChoice(choice: IChoice): choice is CaptureChoice {
return choice.type === ChoiceType.Capture;
return choice.type === "Capture";
}
3 changes: 1 addition & 2 deletions src/migrations/isMultiChoice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ChoiceType } from "src/types/choices/choiceType";
import type { MultiChoice } from "src/types/choices/MultiChoice";

export function isMultiChoice(choice: unknown): choice is MultiChoice {
Expand All @@ -11,5 +10,5 @@ export function isMultiChoice(choice: unknown): choice is MultiChoice {
return false;
}

return choice.type === ChoiceType.Multi && choice.choices !== undefined;
return choice.type === "Multi" && choice.choices !== undefined;
}
5 changes: 2 additions & 3 deletions src/migrations/migrateToMacroIDFromEmbeddedMacro.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type QuickAdd from "src/main";
import { ChoiceType } from "src/types/choices/choiceType";
import type IChoice from "src/types/choices/IChoice";
import type IMacroChoice from "src/types/choices/IMacroChoice";
import type IMultiChoice from "src/types/choices/IMultiChoice";
Expand All @@ -10,7 +9,7 @@ export default {
// Did not make sense to have copies of macros in the choices when they are maintained for themselves.
// Instead we reference by id now. Have to port this over for all users.
function convertMacroChoiceMacroToIdHelper(choice: IChoice): IChoice {
if (choice.type === ChoiceType.Multi) {
if (choice.type === "Multi") {
let multiChoice = choice as IMultiChoice;
const multiChoices = multiChoice.choices.map(
convertMacroChoiceMacroToIdHelper
Expand All @@ -19,7 +18,7 @@ export default {
return multiChoice;
}

if (choice.type !== ChoiceType.Macro) return choice;
if (choice.type !== "Macro") return choice;
const macroChoice = choice as IMacroChoice;

if (macroChoice.macro) {
Expand Down
3 changes: 1 addition & 2 deletions src/types/choices/CaptureChoice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Choice } from "./Choice";
import { ChoiceType } from "./choiceType";
import type ICaptureChoice from "./ICaptureChoice";
import { NewTabDirection } from "../newTabDirection";
import type { FileViewMode } from "../fileViewMode";
Expand Down Expand Up @@ -33,7 +32,7 @@ export class CaptureChoice extends Choice implements ICaptureChoice {
openFileInMode: FileViewMode;

constructor(name: string) {
super(name, ChoiceType.Capture);
super(name, "Capture");

this.appendLink = false;
this.captureTo = "";
Expand Down
3 changes: 1 addition & 2 deletions src/types/choices/MacroChoice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Choice } from "./Choice";
import { ChoiceType } from "./choiceType";
import type IMacroChoice from "./IMacroChoice";
import type { IMacro } from "../macros/IMacro";

Expand All @@ -8,6 +7,6 @@ export class MacroChoice extends Choice implements IMacroChoice {
macroId = "";

constructor(name: string) {
super(name, ChoiceType.Macro);
super(name, "Macro");
}
}
Loading

1 comment on commit 78aab04

@vercel
Copy link

@vercel vercel bot commented on 78aab04 Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quickadd – ./

quickadd-git-master-chrisbbh.vercel.app
quickadd-chrisbbh.vercel.app
quickadd.obsidian.guide

Please sign in to comment.