diff --git a/src/MacrosManager.ts b/src/MacrosManager.ts index b8d07b7..381fc29 100644 --- a/src/MacrosManager.ts +++ b/src/MacrosManager.ts @@ -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"; @@ -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( - (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) { diff --git a/src/choiceExecutor.ts b/src/choiceExecutor.ts index f4f1b73..afff4e5 100644 --- a/src/choiceExecutor.ts +++ b/src/choiceExecutor.ts @@ -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"; @@ -19,23 +18,23 @@ export class ChoiceExecutor implements IChoiceExecutor { async execute(choice: IChoice): Promise { 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; diff --git a/src/gui/MacroGUIs/CommandList.svelte b/src/gui/MacroGUIs/CommandList.svelte index 7d7c70a..935bab3 100644 --- a/src/gui/MacroGUIs/CommandList.svelte +++ b/src/gui/MacroGUIs/CommandList.svelte @@ -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"; @@ -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; } diff --git a/src/gui/MacroGUIs/MacroBuilder.ts b/src/gui/MacroGUIs/MacroBuilder.ts index 3c3a7aa..855ae1a 100644 --- a/src/gui/MacroGUIs/MacroBuilder.ts +++ b/src/gui/MacroGUIs/MacroBuilder.ts @@ -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; @@ -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( diff --git a/src/gui/choiceList/AddChoiceBox.svelte b/src/gui/choiceList/AddChoiceBox.svelte index 17e619b..174280e 100644 --- a/src/gui/choiceList/AddChoiceBox.svelte +++ b/src/gui/choiceList/AddChoiceBox.svelte @@ -24,10 +24,10 @@
diff --git a/src/gui/choiceList/ChoiceList.svelte b/src/gui/choiceList/ChoiceList.svelte index 1fe69b9..2125b21 100644 --- a/src/gui/choiceList/ChoiceList.svelte +++ b/src/gui/choiceList/ChoiceList.svelte @@ -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"; @@ -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"} 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"; @@ -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; @@ -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}'. @@ -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)); } @@ -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); @@ -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)); @@ -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; @@ -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; } diff --git a/src/gui/suggesters/choiceSuggester.ts b/src/gui/suggesters/choiceSuggester.ts index d29f3b2..da47cd7 100644 --- a/src/gui/suggesters/choiceSuggester.ts +++ b/src/gui/suggesters/choiceSuggester.ts @@ -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"; @@ -49,7 +48,7 @@ export default class ChoiceSuggester extends FuzzySuggestModal { item: IChoice, evt: MouseEvent | KeyboardEvent ): Promise { - if (item.type === ChoiceType.Multi) + if (item.type === "Multi") this.onChooseMultiType(item); else await this.choiceExecutor.execute(item); } diff --git a/src/main.ts b/src/main.ts index ee013f9..6163249 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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"; @@ -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((choice).choices); } @@ -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, diff --git a/src/migrations/isCaptureChoice.ts b/src/migrations/isCaptureChoice.ts index dd30dc8..a092c43 100644 --- a/src/migrations/isCaptureChoice.ts +++ b/src/migrations/isCaptureChoice.ts @@ -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"; } diff --git a/src/migrations/isMultiChoice.ts b/src/migrations/isMultiChoice.ts index e48610b..2eb0a41 100644 --- a/src/migrations/isMultiChoice.ts +++ b/src/migrations/isMultiChoice.ts @@ -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 { @@ -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; } diff --git a/src/migrations/migrateToMacroIDFromEmbeddedMacro.ts b/src/migrations/migrateToMacroIDFromEmbeddedMacro.ts index 1e3eab3..f1bebfe 100644 --- a/src/migrations/migrateToMacroIDFromEmbeddedMacro.ts +++ b/src/migrations/migrateToMacroIDFromEmbeddedMacro.ts @@ -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"; @@ -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 @@ -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) { diff --git a/src/types/choices/CaptureChoice.ts b/src/types/choices/CaptureChoice.ts index ac4ca23..3490c33 100644 --- a/src/types/choices/CaptureChoice.ts +++ b/src/types/choices/CaptureChoice.ts @@ -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"; @@ -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 = ""; diff --git a/src/types/choices/MacroChoice.ts b/src/types/choices/MacroChoice.ts index 09ab8cf..f98d840 100644 --- a/src/types/choices/MacroChoice.ts +++ b/src/types/choices/MacroChoice.ts @@ -1,5 +1,4 @@ import { Choice } from "./Choice"; -import { ChoiceType } from "./choiceType"; import type IMacroChoice from "./IMacroChoice"; import type { IMacro } from "../macros/IMacro"; @@ -8,6 +7,6 @@ export class MacroChoice extends Choice implements IMacroChoice { macroId = ""; constructor(name: string) { - super(name, ChoiceType.Macro); + super(name, "Macro"); } } diff --git a/src/types/choices/MultiChoice.ts b/src/types/choices/MultiChoice.ts index 4644e0c..80077e7 100644 --- a/src/types/choices/MultiChoice.ts +++ b/src/types/choices/MultiChoice.ts @@ -1,6 +1,5 @@ import { Choice } from "./Choice"; import type IChoice from "./IChoice"; -import { ChoiceType } from "./choiceType"; import type IMultiChoice from "./IMultiChoice"; export class MultiChoice extends Choice implements IMultiChoice { @@ -8,7 +7,7 @@ export class MultiChoice extends Choice implements IMultiChoice { collapsed: boolean; constructor(name: string) { - super(name, ChoiceType.Multi); + super(name, "Multi"); } public addChoice(choice: IChoice): MultiChoice { diff --git a/src/types/choices/TemplateChoice.ts b/src/types/choices/TemplateChoice.ts index e77d02c..71d4ddd 100644 --- a/src/types/choices/TemplateChoice.ts +++ b/src/types/choices/TemplateChoice.ts @@ -1,4 +1,3 @@ -import { ChoiceType } from "./choiceType"; import type ITemplateChoice from "./ITemplateChoice"; import { Choice } from "./Choice"; import { NewTabDirection } from "../newTabDirection"; @@ -27,7 +26,7 @@ export class TemplateChoice extends Choice implements ITemplateChoice { setFileExistsBehavior: boolean; constructor(name: string) { - super(name, ChoiceType.Template); + super(name, "Template"); this.templatePath = ""; this.fileNameFormat = { enabled: false, format: "" }; diff --git a/src/types/choices/choiceType.ts b/src/types/choices/choiceType.ts index c81ae19..53c7428 100644 --- a/src/types/choices/choiceType.ts +++ b/src/types/choices/choiceType.ts @@ -1,6 +1 @@ -export enum ChoiceType { - Capture = "Capture", - Macro = "Macro", - Multi = "Multi", - Template = "Template", -} +export type ChoiceType = "Capture" | "Macro" | "Multi" | "Template"; diff --git a/src/utilityObsidian.ts b/src/utilityObsidian.ts index 78d136c..ab0f914 100644 --- a/src/utilityObsidian.ts +++ b/src/utilityObsidian.ts @@ -8,7 +8,6 @@ import type { MultiChoice } from "./types/choices/MultiChoice"; import type { CaptureChoice } from "./types/choices/CaptureChoice"; import type { MacroChoice } from "./types/choices/MacroChoice"; import type IChoice from "./types/choices/IChoice"; -import { ChoiceType } from "./types/choices/choiceType"; import { log } from "./logger/logManager"; export function getTemplater(app: App) { @@ -211,13 +210,13 @@ export function getChoiceType< T extends TemplateChoice | MultiChoice | CaptureChoice | MacroChoice >(choice: IChoice): choice is T { const isTemplate = (choice: IChoice): choice is TemplateChoice => - choice.type === ChoiceType.Template; + choice.type === "Template"; const isMacro = (choice: IChoice): choice is MacroChoice => - choice.type === ChoiceType.Macro; + choice.type === "Macro"; const isCapture = (choice: IChoice): choice is CaptureChoice => - choice.type === ChoiceType.Capture; + choice.type === "Capture"; const isMulti = (choice: IChoice): choice is MultiChoice => - choice.type === ChoiceType.Multi; + choice.type === "Multi"; return ( isTemplate(choice) ||