diff --git a/main.ts b/main.ts index 6f3da4a7..b25b4f63 100644 --- a/main.ts +++ b/main.ts @@ -37,6 +37,21 @@ export default class ModalFormPlugin extends Plugin { return this.activateView(EDIT_FORM_VIEW); } + formExists(formName: string): boolean { + return this.settings?.formDefinitions.some(form => form.name === formName) ?? false; + } + + async duplicateForm(form: FormDefinition) { + let newForm = { ...form }; + newForm.name = form.name + '-copy'; + let i = 1; + while (this.formExists(newForm.name)) { + newForm.name = form.name + '-copy-' + i; + i++; + } + await this.saveForm(newForm); + } + async editForm(formName: string) { // By reading settings from the disk we get a copy of the form // effectively preventing any unexpected side effects to the running configuration diff --git a/src/views/ManageFormsView.ts b/src/views/ManageFormsView.ts index f5541e57..f6b85705 100644 --- a/src/views/ManageFormsView.ts +++ b/src/views/ManageFormsView.ts @@ -54,6 +54,7 @@ export class ManageFormsView extends ItemView { .setName(form.title) .then((setting) => { console.log(setting) + // This moves the separator of the settings container from he top to the bottom setting.settingEl.setCssStyles({ borderTop: 'none', borderBottom: '1px solid var(--background-modifier-border)' }) }) .addButton((button) => { @@ -72,7 +73,14 @@ export class ManageFormsView extends ItemView { await this.plugin.editForm(form.name); }); } - ); + ) + .addButton(btn => { + btn.setTooltip('duplicate ' + form.name) + btn.setButtonText('Duplicate').onClick(() => { + this.plugin.duplicateForm(form); + }) + }) + ; }) }