From 47d4d8cc6bebb1bf638c4dccf24fe4cc73094c36 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Sat, 6 Jan 2024 23:32:21 +0100 Subject: [PATCH] feat: command to directly edit a form --- src/main.ts | 73 +++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/src/main.ts b/src/main.ts index 22a4375e..37720282 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,6 +28,7 @@ import { O } from "@std"; import { executeTemplate } from "./core/template/templateParser"; import { NewNoteModal } from "./suggesters/NewNoteModal"; import { file_exists } from "./utils/files"; +import { FormPickerModal } from "./suggesters/FormPickerModal"; type ViewType = typeof EDIT_FORM_VIEW | typeof MANAGE_FORMS_VIEW; @@ -61,7 +62,7 @@ function notifyMigrationErrors(errors: MigrationError[]) { // This is the plugin entrypoint export default class ModalFormPlugin extends Plugin { public settings: ModalFormSettings | undefined; - private unsubscribeSettingsStore: () => void = () => { }; + private unsubscribeSettingsStore: () => void = () => {}; // This things will be setup in the onload function rather than constructor public api!: PublicAPI; @@ -84,9 +85,7 @@ export default class ModalFormPlugin extends Plugin { // then if you save another form you will unexpectedly save the mutated form too. // Maybe we could instead do a deep copy instead, but until this proven to be a bottleneck I will leave it like this. const savedSettings = await this.getSettings(); - const formDefinition = savedSettings.formDefinitions.find( - (form) => form.name === formName, - ); + const formDefinition = savedSettings.formDefinitions.find((form) => form.name === formName); if (!formDefinition) { throw new ModalFormError(`Form ${formName} not found`); } @@ -107,14 +106,10 @@ export default class ModalFormPlugin extends Plugin { async activateView(viewType: ViewType, state?: FormDefinition) { const { workspace } = this.app; - let leaf: WorkspaceLeaf | undefined = - workspace.getLeavesOfType(viewType)[0]; + let leaf: WorkspaceLeaf | undefined = workspace.getLeavesOfType(viewType)[0]; if (leaf) { console.info("found leaf, no reason to create a new one"); - } else if ( - Platform.isMobile || - this.settings?.editorPosition === "mainView" - ) { + } else if (Platform.isMobile || this.settings?.editorPosition === "mainView") { leaf = this.app.workspace.getLeaf("tab"); } else if (this.settings?.editorPosition === "right") { leaf = this.app.workspace.getRightLeaf(false); @@ -142,11 +137,10 @@ export default class ModalFormPlugin extends Plugin { const [migrationIsNeeded, settings] = pipe( parseSettings(data), E.map((settings): [boolean, ModalFormSettings] => { - const migrationIsNeeded = - settings.formDefinitions.some(formNeedsMigration); - const { right: formDefinitions, left: errors } = A.partitionMap( - migrateToLatest, - )(settings.formDefinitions); + const migrationIsNeeded = settings.formDefinitions.some(formNeedsMigration); + const { right: formDefinitions, left: errors } = A.partitionMap(migrateToLatest)( + settings.formDefinitions, + ); notifyParsingErrors(errors); const validSettings: ModalFormSettings = { ...settings, @@ -175,8 +169,8 @@ export default class ModalFormPlugin extends Plugin { attachShortcutToGlobalWindow() { if (!this.settings) { - log_error(new ModalFormError("Settings not loaded yet")) - return + log_error(new ModalFormError("Settings not loaded yet")); + return; } const globalNamespace = this.settings.globalNamespace; if (this.settings?.attachShortcutToGlobalWindow) { @@ -190,6 +184,13 @@ export default class ModalFormPlugin extends Plugin { await this.saveSettings(); } + get validFormDefinitions(): FormDefinition[] { + return pipe( + this.settings!.formDefinitions, + A.filterMap((form) => (form instanceof MigrationError ? O.none : O.some(form))), + ); + } + async onload() { const settings = await this.getSettings(); if (settings.formDefinitions.length === 0) { @@ -203,14 +204,8 @@ export default class ModalFormPlugin extends Plugin { }); this.api = new API(this.app, this); this.attachShortcutToGlobalWindow(); - this.registerView( - EDIT_FORM_VIEW, - (leaf) => new EditFormView(leaf, this), - ); - this.registerView( - MANAGE_FORMS_VIEW, - (leaf) => new ManageFormsView(leaf, this), - ); + this.registerView(EDIT_FORM_VIEW, (leaf) => new EditFormView(leaf, this)); + this.registerView(MANAGE_FORMS_VIEW, (leaf) => new ManageFormsView(leaf, this)); // This creates an icon in the left ribbon. this.addRibbonIcon("documents", "Edit forms", (evt: MouseEvent) => { @@ -239,6 +234,16 @@ export default class ModalFormPlugin extends Plugin { }, }); + this.addCommand({ + id: "edit-form", + name: "Edit form", + callback: async () => { + new FormPickerModal(this.app, this.validFormDefinitions, (formToEdit) => { + this.activateView(EDIT_FORM_VIEW, formToEdit); + }).open(); + }, + }); + // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new ModalFormSettingTab(this.app, this)); } @@ -250,13 +255,9 @@ export default class ModalFormPlugin extends Plugin { * @returns a unique name for the note, full path including the extension */ getUniqueNoteName(name: string, destinationFolder?: string): string { - const defaultNotesFolder = this.app.fileManager.getNewFileParent( - "", - "note.md", - ); + const defaultNotesFolder = this.app.fileManager.getNewFileParent("", "note.md"); function makePath(name: string, folder?: string, suffix?: number) { - return `${folder || defaultNotesFolder.path}/${name}${suffix ? "-" + suffix : "" - }.md`; + return `${folder || defaultNotesFolder.path}/${name}${suffix ? "-" + suffix : ""}.md`; } let destinationPath = makePath(name, destinationFolder); let i = 1; @@ -291,14 +292,8 @@ export default class ModalFormPlugin extends Plugin { destinationFolder: string, ) => { const formData = await this.api.openForm(form); - const newNoteFullPath = this.getUniqueNoteName( - noteName, - destinationFolder, - ); - const noteContent = executeTemplate( - form.template.parsedTemplate, - formData.getData(), - ); + const newNoteFullPath = this.getUniqueNoteName(noteName, destinationFolder); + const noteContent = executeTemplate(form.template.parsedTemplate, formData.getData()); console.log("new note content", noteContent); this.app.vault.create(newNoteFullPath, noteContent); };