From 49038ea5f8303d74210ac9b5e1aeecb658e11948 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Wed, 18 Dec 2024 00:11:03 +0100 Subject: [PATCH] chore: fix basic template service --- src/core/template/BasicTemplateService.ts | 28 +++++++++++++++-------- src/core/template/TemplateService.ts | 2 -- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/core/template/BasicTemplateService.ts b/src/core/template/BasicTemplateService.ts index f71ebb9..94a526a 100644 --- a/src/core/template/BasicTemplateService.ts +++ b/src/core/template/BasicTemplateService.ts @@ -1,5 +1,5 @@ import { TE } from "@std"; -import { App, normalizePath } from "obsidian"; +import { App, normalizePath, TFile } from "obsidian"; import { Logger } from "src/utils/Logger"; import { TemplateError } from "./TemplateError"; import { TemplateService } from "./TemplateService"; @@ -14,13 +14,23 @@ export class BasicTemplateService implements TemplateService { ) {} createNoteFromTemplate = ( - template: string, - targetPath: string, + templateContent: string, + targetFolder: string, + filename: string, + openNewNote: boolean, ): TE.TaskEither => - TE.tryCatch( - async () => { - await this.app.vault.create(normalizePath(targetPath), template); - }, - TemplateError.of("Error creating note from template"), - ); + TE.tryCatch(async () => { + const fullPath = normalizePath(`${targetFolder}/${filename}.md`); + await this.app.vault.create(fullPath, templateContent); + if (openNewNote) { + const file = this.app.vault.getAbstractFileByPath(fullPath); + if (!file) { + this.logger.error("File not found", fullPath); + return; + } + if (file instanceof TFile) { + await this.app.workspace.getLeaf("split").openFile(file); + } + } + }, TemplateError.of("Error creating note from template")); } diff --git a/src/core/template/TemplateService.ts b/src/core/template/TemplateService.ts index d9f62d2..0620a62 100644 --- a/src/core/template/TemplateService.ts +++ b/src/core/template/TemplateService.ts @@ -4,8 +4,6 @@ import { TemplateError } from "./TemplateError"; export interface TemplateService { /** * Creates a note from a template content - * @param template The template content - * @param targetPath Path where the new note should be created */ createNoteFromTemplate( templateContent: string,