chhoumann / quickadd

QuickAdd for Obsidian
https://quickadd.obsidian.guide
MIT License
1.52k stars 136 forks source link

[FEATURE REQUEST] Extend QuickAdd API with `executeNewChoice` Method #655

Open itsz-san opened 7 months ago

itsz-san commented 7 months ago

Is your feature request related to a problem? Please describe.

Currently, the Quickadd API exposes the executeChoice method but it works only with existing QuickAdd Choices through plugin.getChoiceByName(choiceName). The shortcomings that I've encountered with this are;

Describe the solution you'd like

Extend QuickAdd API with executeNewChoice, that takes three arguments; choiceType, choiceOptions and variables.

EXAMPLE USAGE

// Execute a template choice with custom options and variables
await quickAdd.executeNewChoice("Template",
  {
    templatePath: "/path/to/template.md",
    fileNameFormat: { enabled: true, format: "YYYY-MM-DD" },
    // ... other options
  }, { variable1: "value1", variable2: "value2" });

Possible Implementation

executeNewChoice: async (
  choiceType: <string>,
  choiceOptions: Record<string, unknown>,
  variables?: Record<string, unknown>
  ) => {

  const choice: IChoice;
  if(choiceType = "Template") choice = new TemplateChoice("Template Choice");
  if(choiceType = "Capture") choice = new CaptureChoice("Capture Choice");
  if (!choice) log.logError(`You left me with no choice.`);

  Object.assign(choice, choiceOptions);

  if (variables) {
    Object.keys(variables).forEach((key) => {
      choiceExecutor.variables.set(key, variables[key]);
    });
  }
  await choiceExecutor.execute(choice);
  choiceExecutor.variables.clear();
},

I suppose it's also possible to implement it as executeTemplateChoice and executeCaptureChoice.

Additional context

This enhancement would be particularly beneficial for users who want to leverage the QuickAdd functionality without the need for predefined QuickAdd Choice objects.

The ability to dynamically create QuickAdd Choices through the QuickAdd API seems so fundamental that I'm not sure if I'm missing something here.