Skip to content

Commit

Permalink
feat(util): insert a form template into current note command
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Jun 17, 2024
1 parent eda92a2 commit d60fe82
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
51 changes: 43 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { executeTemplate } from "./core/template/templateParser";
import { settingsStore } from "./store/store";
import { FormPickerModal } from "./suggesters/FormPickerModal";
import { NewNoteModal } from "./suggesters/NewNoteModal";
import { log_error, log_notice } from "./utils/Log";
import { log_error, log_notice, notifyWarning } from "./utils/Log";
import { file_exists } from "./utils/files";
import { FormImportModal } from "./views/FormImportView";
import { TemplateBuilderModal } from "./views/TemplateBuilderModal";
Expand Down Expand Up @@ -261,6 +261,37 @@ export default class ModalFormPlugin extends Plugin {
},
});

this.addCommand({
id: "insert-form-template",
name: "Insert form template",
editorCallback: (editor, ctx) => {
const formsWithTemplates = this.getFormsWithTemplates();
if (formsWithTemplates.length === 0) {
notifyWarning("No forms with templates found")(
`Make sure you have at least one form with a template`,
);
return;
}
if (formsWithTemplates.length === 1) {
const form = formsWithTemplates[0] as FormWithTemplate;
this.api.openForm(form).then((result) => {
editor.replaceSelection(
executeTemplate(form.template.parsedTemplate, result.getData()),
);
});
return;
}

new FormPickerModal(this.app, formsWithTemplates, (form) => {
this.api.openForm(form).then((result) => {
editor.replaceSelection(
executeTemplate(form.template.parsedTemplate, result.getData()),
);
});
}).open();
},
});

this.addCommand({
id: "edit-form",
name: "Edit form",
Expand Down Expand Up @@ -301,13 +332,8 @@ export default class ModalFormPlugin extends Plugin {
return destinationPath;
}

/**
* Checks if there are forms with templates, and presents a prompt
* to select a form, then opens the forms, and creates a new note
* with the template and the form values
*/
createNoteFromForm() {
const formsWithTemplates = pipe(
getFormsWithTemplates() {
return pipe(
this.settings!.formDefinitions,
A.filterMap((form) => {
if (form instanceof MigrationError) {
Expand All @@ -319,6 +345,15 @@ export default class ModalFormPlugin extends Plugin {
return O.none;
}),
);
}

/**
* Checks if there are forms with templates, and presents a prompt
* to select a form, then opens the forms, and creates a new note
* with the template and the form values
*/
createNoteFromForm() {
const formsWithTemplates = this.getFormsWithTemplates();
const onFormSelected = async (
form: FormWithTemplate,
noteName: string,
Expand Down
10 changes: 5 additions & 5 deletions src/utils/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export function log_notice(
el.append(head, body);
}

export function log_update(msg: string): void {
log_notice("Modal form update", msg);
}

export function log_error(e: Error | ModalFormError): void {
if (e instanceof ModalFormError && e.console_msg) {
log_notice("Modal from error: ", e.message + "\n" + e.console_msg, "var(--text-error)");
Expand All @@ -34,4 +30,8 @@ export function log_error(e: Error | ModalFormError): void {
* Use it to notify the user about important errors
* @param title
*/
export const notifyError = (title: string) => (msg: string) => log_notice(`🚨 ${title} 🚨`, msg, "notice-error");
export const notifyError = (title: string) => (msg: string) =>
log_notice(`🚨 ${title} 🚨`, msg, "notice-error");

export const notifyWarning = (title: string) => (msg: string) =>
log_notice(`⚠️ ${title} ⚠️`, msg, "notice-warning");

0 comments on commit d60fe82

Please sign in to comment.