Skip to content

Commit

Permalink
feat(templates): post-process templates using templater
Browse files Browse the repository at this point in the history
fixes #309 [Feature request] Templater integration directly into "template" section of Modal forms
  • Loading branch information
danielo515 committed Dec 17, 2024
1 parent a3f9d4e commit ea01cd9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
37 changes: 31 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { A, E, O, pipe } from "@std";
import { A, E, O, pipe, TE } from "@std";
import { Platform, Plugin, WorkspaceLeaf } from "obsidian";
import { API } from "src/API";
import { ModalFormSettingTab } from "src/ModalFormSettingTab";
Expand All @@ -15,16 +15,19 @@ import { ModalFormError } from "src/utils/ModalFormError";
import { EDIT_FORM_VIEW, EditFormView } from "src/views/EditFormView";
import { MANAGE_FORMS_VIEW, ManageFormsView } from "src/views/ManageFormsView";
import {
InvalidData,
MigrationError,
formNeedsMigration,
InvalidData,
migrateToLatest,
MigrationError,
} from "./core/formDefinitionSchema";
import { TemplateService } from "./core/template/TemplateService";
import { getTemplateService } from "./core/template/getTemplateService";
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, notifyWarning } from "./utils/Log";
import { logger } from "./utils/Logger";
import { file_exists } from "./utils/files";
import { FormImportModal } from "./views/FormImportView";
import { TemplateBuilderModal } from "./views/TemplateBuilderModal";
Expand Down Expand Up @@ -66,6 +69,7 @@ export default class ModalFormPlugin extends Plugin {
private unsubscribeSettingsStore: () => void = () => {};
// This things will be setup in the onload function rather than constructor
public api!: PublicAPI;
private templateService!: TemplateService;

manageForms() {
return this.activateView(MANAGE_FORMS_VIEW);
Expand Down Expand Up @@ -230,6 +234,7 @@ export default class ModalFormPlugin extends Plugin {
});
this.api = new API(this.app, this);
this.attachShortcutToGlobalWindow();
this.templateService = getTemplateService(this.app, logger);
this.registerView(EDIT_FORM_VIEW, (leaf) => new EditFormView(leaf, this));
this.registerView(MANAGE_FORMS_VIEW, (leaf) => new ManageFormsView(leaf, this));
this.registerView(TEMPLATE_BUILDER_VIEW, (leaf) => new TemplateBuilderView(leaf, this));
Expand Down Expand Up @@ -360,11 +365,31 @@ 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());
console.log("new note content", noteContent);
this.app.vault.create(newNoteFullPath, noteContent);

// Use template service instead of directly creating the file
await pipe(
this.templateService.createNoteFromTemplate(
noteContent,
destinationFolder,
noteName,
false, // don't open the new note
),
TE.match(
(error) => {
log_error(error);
notifyWarning("Failed to create note from template");
},
() => {
log_notice(
"Note created successfully",
`Note ${noteName} created in ${destinationFolder}`,
);
},
),
)();
};

const picker = new NewNoteModal(
this.app,
formsWithTemplates,
Expand Down
14 changes: 6 additions & 8 deletions src/typings/obsidian-ex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { DataviewApi } from "api/plugin-api";
import type moment from "moment";
import "obsidian";
import { TemplaterApi } from "src/core/template";
import { PublicAPI } from "src/main";

declare module "obsidian" {
Expand All @@ -19,17 +20,14 @@ declare module "obsidian" {
dataview?: {
api: DataviewApi;
};
"templater-obsidian"?: { templater: TemplaterApi };
};
};
}

interface Workspace {
/** Sent to rendered dataview components to tell them to possibly refresh */
on(
name: "dataview:refresh-views",
callback: () => void,
ctx?: unknown,
): EventRef;
on(name: "dataview:refresh-views", callback: () => void, ctx?: unknown): EventRef;
}
}

Expand All @@ -42,7 +40,7 @@ declare global {
}

declare global {
interface Window {
moment: typeof moment;
}
interface Window {
moment: typeof moment;
}
}

0 comments on commit ea01cd9

Please sign in to comment.