From f27d59eb7f8551fd9890eee1cbef45ddd1054ec7 Mon Sep 17 00:00:00 2001 From: SilentVoid13 <51264226+SilentVoid13@users.noreply.github.com> Date: Tue, 6 Apr 2021 01:37:52 +0200 Subject: [PATCH] feat: adding new hotkey to create new from template Default hotkey is Alt+n. This opens up a modal to pick a template, and then creates a new note containing that template named "Untitled" by default. re #73 --- src/TemplateParser.ts | 32 +++++++++++++++++++++++++++++--- src/TemplaterFuzzySuggest.ts | 32 +++++++++++++++++++++++++++++--- src/main.ts | 35 ++++++++++++++++++----------------- 3 files changed, 76 insertions(+), 23 deletions(-) diff --git a/src/TemplateParser.ts b/src/TemplateParser.ts index 7443a5a7..97927d65 100644 --- a/src/TemplateParser.ts +++ b/src/TemplateParser.ts @@ -85,8 +85,8 @@ export class TemplateParser extends TParser { varName: "tp", parse: { exec: "*", - interpolate: "", - raw: "~", + interpolate: "~", + raw: "", }, autoTrim: false, globalAwait: true, @@ -99,6 +99,32 @@ export class TemplateParser extends TParser { return content; } + replace_in_active_file(): void { + try { + let active_view = this.app.workspace.getActiveViewOfType(MarkdownView); + if (active_view === null) { + throw new Error("Active view is null"); + } + this.replace_templates_and_overwrite_in_file(active_view.file); + } + catch(error) { + this.plugin.log_error(error); + } + } + + async create_new_note_from_template(template_file: TFile) { + let template_content = await this.app.vault.read(template_file); + let created_note = await this.app.vault.create("Untitled.md", ""); + let content = await this.plugin.parser.parseTemplates(template_content, created_note, ContextMode.USER_INTERNAL); + await this.app.vault.modify(created_note, content); + + let active_leaf = this.app.workspace.activeLeaf; + if (!active_leaf) { + throw new Error("No active leaf"); + } + await active_leaf.openFile(created_note, {state: {mode: 'source'}, eState: {rename: 'all'}}); + } + async replace_templates_and_append(template_file: TFile) { let active_view = this.app.workspace.getActiveViewOfType(MarkdownView); if (active_view === null) { @@ -119,8 +145,8 @@ export class TemplateParser extends TParser { async replace_templates_and_overwrite_in_file(file: TFile) { let content = await this.app.vault.read(file); - let new_content = await this.parseTemplates(content, file, ContextMode.USER_INTERNAL); + if (new_content !== content) { await this.app.vault.modify(file, new_content); diff --git a/src/TemplaterFuzzySuggest.ts b/src/TemplaterFuzzySuggest.ts index a09c0e92..5caffd41 100644 --- a/src/TemplaterFuzzySuggest.ts +++ b/src/TemplaterFuzzySuggest.ts @@ -1,9 +1,15 @@ -import { App, FuzzySuggestModal, Notice, TFile, TFolder, normalizePath, Vault, TAbstractFile } from "obsidian"; +import { App, FuzzySuggestModal, TFile, TFolder, normalizePath, Vault, TAbstractFile } from "obsidian"; import TemplaterPlugin from './main'; +export enum OpenMode { + InsertTemplate, + CreateNoteTemplate, +}; + export class TemplaterFuzzySuggestModal extends FuzzySuggestModal { app: App; plugin: TemplaterPlugin; + open_mode: OpenMode; constructor(app: App, plugin: TemplaterPlugin) { super(app); @@ -44,10 +50,19 @@ export class TemplaterFuzzySuggestModal extends FuzzySuggestModal { } onChooseItem(item: TFile, _evt: MouseEvent | KeyboardEvent): void { - this.plugin.parser.replace_templates_and_append(item); + switch(this.open_mode) { + case OpenMode.InsertTemplate: + this.plugin.parser.replace_templates_and_append(item); + break; + case OpenMode.CreateNoteTemplate: + this.plugin.parser.create_new_note_from_template(item); + break; + } } - start(): void { + insert_template(): void { + this.open_mode = OpenMode.InsertTemplate; + // If there is only one file in the templates directory, we don't open the modal try { let files = this.getItems(); @@ -62,4 +77,15 @@ export class TemplaterFuzzySuggestModal extends FuzzySuggestModal { this.plugin.log_error(error); } } + + create_new_note_from_template() { + this.open_mode = OpenMode.CreateNoteTemplate; + + try { + this.open(); + } + catch(error) { + this.plugin.log_error(error); + } + } } diff --git a/src/main.ts b/src/main.ts index 9f6fa020..ffebecb3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,7 +28,7 @@ You can also find this message in the settings of Templater. This message will s this.registerMarkdownPostProcessor((el, ctx) => this.dynamic_templates_processor(el, ctx)); this.addRibbonIcon('three-horizontal-bars', 'Templater', async () => { - this.fuzzySuggest.start(); + this.fuzzySuggest.insert_template(); }); this.addCommand({ @@ -41,7 +41,7 @@ You can also find this message in the settings of Templater. This message will s }, ], callback: () => { - this.fuzzySuggest.start(); + this.fuzzySuggest.insert_template(); }, }); @@ -55,7 +55,7 @@ You can also find this message in the settings of Templater. This message will s }, ], callback: () => { - this.replace_in_active_file(); + this.parser.replace_in_active_file(); }, }); @@ -78,6 +78,20 @@ You can also find this message in the settings of Templater. This message will s } }); + this.addCommand({ + id: "create-new-note-from-template", + name: "Create new note from template", + hotkeys: [ + { + modifiers: ["Alt"], + key: "n", + }, + ], + callback: () => { + this.fuzzySuggest.create_new_note_from_template(); + } + }); + this.app.workspace.on("layout-ready", () => { // TODO: Find a way to not trigger this on files copy this.app.vault.on("create", async (file: TAbstractFile) => { @@ -102,20 +116,7 @@ You can also find this message in the settings of Templater. This message will s async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - } - - replace_in_active_file(): void { - try { - let active_view = this.app.workspace.getActiveViewOfType(MarkdownView); - if (active_view === null) { - throw new Error("Active view is null"); - } - this.parser.replace_templates_and_overwrite_in_file(active_view.file); - } - catch(error) { - this.log_error(error); - } - } + } log_error(msg: string, error?: string) { if (error) {