From d60fe825aaee824c3f9f506675e86af2f5ba4e91 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Mon, 17 Jun 2024 11:03:01 +0200 Subject: [PATCH] feat(util): insert a form template into current note command --- src/main.ts | 51 ++++++++++++++++++++++++++++++++++++++++-------- src/utils/Log.ts | 10 +++++----- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/main.ts b/src/main.ts index d8ebe921..347c62fc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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"; @@ -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", @@ -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) { @@ -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, diff --git a/src/utils/Log.ts b/src/utils/Log.ts index ca738828..e11ba45f 100644 --- a/src/utils/Log.ts +++ b/src/utils/Log.ts @@ -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)"); @@ -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");