Skip to content

Commit

Permalink
feat: adding new hotkey to create new from template
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SilentVoid13 committed Apr 6, 2021
1 parent 3e00435 commit f27d59e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
32 changes: 29 additions & 3 deletions src/TemplateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export class TemplateParser extends TParser {
varName: "tp",
parse: {
exec: "*",
interpolate: "",
raw: "~",
interpolate: "~",
raw: "",
},
autoTrim: false,
globalAwait: true,
Expand All @@ -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) {
Expand All @@ -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);

Expand Down
32 changes: 29 additions & 3 deletions src/TemplaterFuzzySuggest.ts
Original file line number Diff line number Diff line change
@@ -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<TFile> {
app: App;
plugin: TemplaterPlugin;
open_mode: OpenMode;

constructor(app: App, plugin: TemplaterPlugin) {
super(app);
Expand Down Expand Up @@ -44,10 +50,19 @@ export class TemplaterFuzzySuggestModal extends FuzzySuggestModal<TFile> {
}

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();
Expand All @@ -62,4 +77,15 @@ export class TemplaterFuzzySuggestModal extends FuzzySuggestModal<TFile> {
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);
}
}
}
35 changes: 18 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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();
},
});

Expand All @@ -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();
},
});

Expand All @@ -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) => {
Expand All @@ -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) {
Expand Down

0 comments on commit f27d59e

Please sign in to comment.