Refactoring - Added GenerateFieldGroups()

This commit is contained in:
Noah Boos 2025-07-08 21:09:23 +02:00
parent a86b5e8855
commit e694935aa2
4 changed files with 57 additions and 91 deletions

View file

@ -19,7 +19,13 @@ import {
ReadFileContent
} from "../utils";
import {ProcessAddNote} from "../AnkiConnect";
import {GenerateDeckSelector, GenerateModelSelector, GenerateSubmitButton, GenerateTagsSection} from "./modalsUtils";
import {
GenerateFieldGroups,
GenerateDeckSelector,
GenerateModelSelector,
GenerateSubmitButton,
GenerateTagsSection
} from "./modalsUtils";
import {Drop} from "esbuild";
/**
@ -102,7 +108,7 @@ export class AddNoteFromCodeBlockModal extends Modal {
*/
modelSelector.onChange(async (value) => {
const codeBlockParameters = await this.GetCodeBlockParameters();
this.AddFieldsGroupsToModal(inputContainer, value, codeBlockParameters);
GenerateFieldGroups(this.plugin, inputContainer, value, codeBlockParameters);
});
this.onOpenAsync(deckSelector, modelSelector, tagsBody, tagsBodyParagraph, inputContainer);
@ -125,47 +131,6 @@ export class AddNoteFromCodeBlockModal extends Modal {
contentEl.empty();
}
/**
* Adds as many fields groups as the currently selected model has fields to the modal.
* @param {HTMLDivElement} inputContainer - DIV containing all the generated inputs.
* @param {any} selectedValue - Currently selected model select value of the modelSelector (DropdownComponent).
* @param {any} inputValues - The ??? of the currently active note.
*/
AddFieldsGroupsToModal(inputContainer: HTMLDivElement, selectedValue: any, inputValues: any) {
/**
* @type {Object} selectedModel
* @description The model object corresponding to the selected model name.
*/
const selectedModel: Object = FetchModelByName(this.plugin, selectedValue);
/**
* @type {Array} fieldsGroupData
* @description An array of input data storing as separate object (1 object = 1 input) the keys used to create each label-input pair and the values of each input.
*/
const fieldsGroupData: Array<Object> = [];
inputContainer.empty();
/**
* @description
* Checks the currently selected option of the dropdown.
* If its value is default, it displays a message requesting the user to select a model.
* Else, it means that a model is selected, therefore, it creates the fields groups data and displays them.
*/
if (selectedValue === "default") {
AddParagraph(inputContainer, "Select a model to see its fields.");
return;
} else {
if (inputValues) {
CreateFieldsGroupData(fieldsGroupData, selectedModel["fields"], inputValues);
console.log(inputValues);
AddFieldGroups(inputContainer, fieldsGroupData);
} else {
CreateFieldsGroupData(fieldsGroupData, selectedModel["fields"]);
AddFieldGroups(inputContainer, fieldsGroupData);
}
}
}
/**
* onOpen() async equivalent allowing asynchronous operations.
* @param {DropdownComponent} deckSelector - Dropdown component that allows the user to select a deck.
@ -181,7 +146,7 @@ export class AddNoteFromCodeBlockModal extends Modal {
const codeBlockParameters: Object = await this.GetCodeBlockParameters();
// console.log(codeBlockParameters);
if (!codeBlockParameters) {
this.AddFieldsGroupsToModal(inputContainer, modelSelector.getValue(), null);
GenerateFieldGroups(this.plugin, inputContainer, modelSelector.getValue(), null);
} else {
/**
* @description Functions called to pre-select and pre-fill both dropdowns and input fields.

View file

@ -17,7 +17,13 @@ import {
AddTitle, AutoAssignDeck, AutoAssignModel, AutoGenerateFields, BuildTagsArray, CreateFieldsGroupData,
FetchModelByName
} from "../utils";
import {GenerateDeckSelector, GenerateModelSelector, GenerateSubmitButton, GenerateTagsSection} from "./modalsUtils";
import {
GenerateFieldGroups,
GenerateDeckSelector,
GenerateModelSelector,
GenerateSubmitButton,
GenerateTagsSection
} from "./modalsUtils";
/**
* A modal dialog for creating a new Anki note by using metadata as pre-filled values.
@ -119,7 +125,7 @@ export class AddNoteFromMetadataModal extends Modal {
* @param {string} value - The selected model name.
*/
modelSelector.onChange(async (value) => {
this.AddFieldsGroupsToModal(inputContainer, value, yaml);
GenerateFieldGroups(this.plugin, inputContainer, value, yaml);
});
/**
@ -150,44 +156,4 @@ export class AddNoteFromMetadataModal extends Modal {
// Clear the content of the modal.
contentEl.empty();
}
/**
* Adds as many fields groups as the currently selected model has fields to the modal.
* @param {HTMLDivElement} inputContainer - DIV containing all the generated inputs.
* @param {any} selectedValue - Currently selected model select value of the modelSelector (DropdownComponent).
* @param {FrontMatterCache} inputValues - The YAML metadata of the currently active note.
*/
AddFieldsGroupsToModal(inputContainer: HTMLDivElement, selectedValue: any, inputValues: FrontMatterCache) {
inputContainer.empty();
/**
* @type {Object} selectedModel
* @description The model object corresponding to the selected model name.
*/
const selectedModel: Object = FetchModelByName(this.plugin, selectedValue);
/**
* @type {Array} fieldsGroupData
* @description An array of input data storing as separate object (1 object = 1 input) the keys used to create each label-input pair and the values of each input.
*/
const fieldsGroupData: Array<Object> = [];
/**
* @description
* Checks the currently selected option of the dropdown.
* If its value is default, it displays a message requesting the user to select a model.
* Else, it means that a model is selected, therefore, it creates the fields groups data and displays them.
*/
if (selectedValue === "default") {
AddParagraph(inputContainer, "Select a model to see its fields.");
return;
} else {
if (inputValues) {
CreateFieldsGroupData(fieldsGroupData, selectedModel["fields"], inputValues);
AddFieldGroups(inputContainer, fieldsGroupData);
} else {
CreateFieldsGroupData(fieldsGroupData, selectedModel["fields"]);
AddFieldGroups(inputContainer, fieldsGroupData);
}
}
}
}

View file

@ -1,15 +1,15 @@
import {ButtonComponent, DropdownComponent, Modal} from "obsidian";
import {ButtonComponent, DropdownComponent, FrontMatterCache, Modal} from "obsidian";
import {
AddButton,
AddContainer,
AddDropdown,
AddDropdown, AddFieldGroups,
AddOptionsToDropdownFromDataset,
AddParagraph,
AddSubtitle,
AddTagInputGroup, BuildTagsArray
AddTagInputGroup, BuildTagsArray, CreateFieldsGroupData, FetchModelByName
} from "../utils";
import {ProcessAddNote} from "../AnkiConnect";
import {Drop} from "esbuild";
import AnkiIntegration from "../main";
export function GenerateDeckSelector(parent: HTMLDivElement, ankiData: Object) {
/**
@ -134,4 +134,38 @@ export function GenerateSubmitButton(parent: HTMLElement, deckSelector: Dropdown
await ProcessAddNote(deckSelector, modelSelector, inputContainer, tags, modal);
}
})
}
export function GenerateFieldGroups(plugin: AnkiIntegration, inputContainer: HTMLDivElement, selectedValue: any, inputValues:FrontMatterCache | Object | null) {
inputContainer.empty();
/**
* @type {Object} selectedModel
* @description The model object corresponding to the selected model name.
*/
const selectedModel: Object = FetchModelByName(plugin, selectedValue);
/**
* @type {Array} fieldsGroupData
* @description An array of input data storing as separate object (1 object = 1 input) the keys used to create each label-input pair and the values of each input.
*/
const fieldsGroupData: Array<Object> = [];
/**
* @description
* Checks the currently selected option of the dropdown.
* If its value is default, it displays a message requesting the user to select a model.
* Else, it means that a model is selected, therefore, it creates the fields groups data and displays them.
*/
if (selectedValue === "default") {
AddParagraph(inputContainer, "Select a model to see its fields.");
return;
} else {
if (inputValues) {
CreateFieldsGroupData(fieldsGroupData, selectedModel["fields"], inputValues);
AddFieldGroups(inputContainer, fieldsGroupData);
} else {
CreateFieldsGroupData(fieldsGroupData, selectedModel["fields"]);
AddFieldGroups(inputContainer, fieldsGroupData);
}
}
}

View file

@ -2,6 +2,7 @@ import AnkiIntegration from "./main";
import {ButtonComponent, DropdownComponent, Modal, TFile} from "obsidian";
import {AddNoteFromMetadataModal} from "./modals/AddNoteFromMetadataModal";
import {AddNoteFromCodeBlockModal} from "./modals/AddNoteFromCodeBlockModal";
import {GenerateFieldGroups} from "./modals/modalsUtils";
/**
* Fetches a model that has been saved in data.json by SynchronizeData().
@ -396,7 +397,7 @@ export function AutoGenerateFields(modal: AddNoteFromMetadataModal | AddNoteFrom
if (!modelSelectorHasNoteParametersModel) {
AddParagraph(inputContainer, "Select a model to see its fields.");
} else {
modal.AddFieldsGroupsToModal(inputContainer, modelSelector.getValue(), noteParameters);
GenerateFieldGroups(modal.plugin, inputContainer, modelSelector.getValue(), noteParameters);
}
}