Skip to content

Commit

Permalink
feat: duplicate form
Browse files Browse the repository at this point in the history
fixes #20
  • Loading branch information
danielo515 committed Sep 12, 2023
1 parent 96c29a4 commit 7a0f1c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 15 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/views/ManageFormsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
})
})
;
})
}

Expand Down

0 comments on commit 7a0f1c6

Please sign in to comment.