Skip to content

Commit

Permalink
Adding overload daily notes option, closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentVoid13 committed Nov 19, 2020
1 parent 79802b3 commit ca62c7f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
67 changes: 60 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { replace_internal_command_templates } from './internal_command_templates

const exec_promise = promisify(exec);

let daily_note_callback: any;
let templater: TemplaterPlugin;

export default class TemplaterPlugin extends Plugin {
public settings: TemplaterSettings;
public modal: any;
Expand Down Expand Up @@ -52,12 +55,10 @@ export default class TemplaterPlugin extends Plugin {
}

onChooseOption(suggestionItem: TFile, evt: Event) {
this.replace_templates(suggestionItem);
this.replace_templates_and_append(suggestionItem);
}

async replace_templates(file: TFile) {
let content = await this.app.vault.read(file);

async replace_templates_and_append(template_file: TFile) {
let activeLeaf = this.app.workspace.activeLeaf;
if (!(activeLeaf) || !(activeLeaf.view instanceof MarkdownView)) {
return;
Expand All @@ -66,6 +67,28 @@ export default class TemplaterPlugin extends Plugin {
let editor = activeLeaf.view.sourceMode.cmEditor;
let doc = editor.getDoc();

let content = await this.app.vault.read(template_file);
content = await this.replace_templates(content);

doc.replaceSelection(content);
editor.focus();
}

async replace_templates_and_overwrite_in_current_file() {
let activeLeaf = this.app.workspace.activeLeaf;
if (!(activeLeaf) || !(activeLeaf.view instanceof MarkdownView)) {
return;
}

let file = activeLeaf.view.file;

let content = await this.app.vault.read(file);
content = await this.replace_templates(content);

await this.app.vault.modify(file, content);
}

async replace_templates(content: string) {
// User defined templates
for (let i = 0; i < this.settings.templates_pairs.length; i++) {
let template_pair = this.settings.templates_pairs[i];
Expand Down Expand Up @@ -96,8 +119,7 @@ export default class TemplaterPlugin extends Plugin {
// Internal templates
content = await replace_internal_templates(this.app, content);

doc.replaceSelection(content);
editor.focus();
return content;
}

update_template_files() {
Expand Down Expand Up @@ -136,7 +158,14 @@ export default class TemplaterPlugin extends Plugin {

let plugin_template = new CustomPluginTemplates(this.app);
this.modal = new CustomModalTemplates(this.app, plugin_template, this.settings);


daily_note_callback = this.app.internalPlugins.getPluginById("daily-notes").ribbonActions[0].callback;
templater = this;

if (this.settings.overload_daily_notes) {
this.overload_daily_notes();
}

// TODO: find a good icon
this.addRibbonIcon('three-horizontal-bars', 'Templater', async () => {
try {
Expand Down Expand Up @@ -171,7 +200,31 @@ export default class TemplaterPlugin extends Plugin {
this.addSettingTab(new TemplaterSettingTab(this.app, this));
}

async overload_daily_notes() {
this.app.internalPlugins.getPluginById("daily-notes").ribbonActions[0].callback = new_daily_note_callback;
this.app.internalPlugins.getPluginById("daily-notes").commands[0].callback = new_daily_note_callback;

// We have to reload the plugin to get the callback working (it will use the old one otherwise idk why)
await this.app.internalPlugins.getPluginById("daily-notes").disable();
await this.app.internalPlugins.getPluginById("daily-notes").enable();
}

async unload_daily_notes() {
this.app.internalPlugins.getPluginById("daily-notes").ribbonActions[0].callback = daily_note_callback;
this.app.internalPlugins.getPluginById("daily-notes").commands[0].callback = daily_note_callback;

// We have to reload the plugin to get the callback working (it will use the old one otherwise idk why)
await this.app.internalPlugins.getPluginById("daily-notes").disable();
await this.app.internalPlugins.getPluginById("daily-notes").enable();
}

async onunload() {
await this.saveData(this.settings);
}
}

async function new_daily_note_callback() {
await daily_note_callback();
templater.modal.update_template_files();
templater.modal.replace_templates_and_overwrite_in_current_file();
}
18 changes: 18 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TemplaterPlugin from './main';
export class TemplaterSettings {
command_timeout = 5;
template_folder = "";
overload_daily_notes = false;
templates_pairs: Array<[string, string]> = [["", ""]];
}

Expand Down Expand Up @@ -41,6 +42,23 @@ export class TemplaterSettingTab extends PluginSettingTab {
})
});

new Setting(containerEl)
.setName("Overload Daily Notes")
.setDesc("This will trigger Templater when using the Daily Notes core plugin.")
.addToggle(toggle => {
toggle.setValue(plugin.settings.overload_daily_notes)
.onChange((new_value) => {
plugin.settings.overload_daily_notes = new_value;

if (new_value) {
plugin.overload_daily_notes();
}
else {
plugin.unload_daily_notes();
}
})
});

let i = 1;
plugin.settings.templates_pairs.forEach((template_pair) => {
let div = containerEl.createEl('div');
Expand Down

0 comments on commit ca62c7f

Please sign in to comment.