Skip to content

Commit

Permalink
Adding a hotkey to insert a template
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentVoid13 committed Nov 8, 2020
1 parent e233ebf commit 03f4a19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ This plugin also offers some internal templates. A complete list of all the inte

## Usage

### 1. Define templates
### User templates

To start using this plugin, you need to define your own templates pattern, along with an associated system command. To configure that, go to the plugin settings.
#### 1. Define templates

To define your own templates, you need to define a template pattern, associated with a system command. To configure that, go to the plugin settings.

The left input field defines the template pattern. I strongly suggest placing the template word between braces like so `{{<template_word>}}` to avoid disruption with existing words.

The right input field defines the system command that will be run. The command output will replace the template pattern in your files. You can define multiple commands for the same template pattern, just have to separate the commands with a newline.
The right input field defines the system command that will be run. The command output will replace the template pattern in your template files. The command will be run as if it was in a shell, so you can chain commands, pipe them, etc. You can define multiple commands for the same template pattern, you just have to separate the commands with a newline.

### 2. Create template files
#### 2. Create template files

Now, you can start creating some template files that contains your template patterns defined in **step 1**. I suggest to group these template files into a dedicated folder. You can then specify this folder in the plugin settings.

### 3. Use your template files
#### 3. Use your template files

Now you can click on the **Templater** icon located on the left-side ribbon. You can also configure a **hotkey** to insert a template (default: `Alt+E`).

Now you can click on the **Templater** icon located on the left-side ribbon. You can now choose your template file created in **step 2**. It will then automatically replace all the template patterns with the corresponding output.
You can now choose your template file created in **step 2**. It will then automatically replace all the template patterns with the corresponding output.

## Internal Templates
### Internal Templates

Here is the list of all of the internal templates that are already included in this plugin. All internal templates patterns are prefixed with the keyword `templater_` to avoid disruption with user defined templates.

Expand Down
33 changes: 27 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const exec_promise = promisify(exec);

export default class TemplaterPlugin extends Plugin {
public settings: TemplaterSettings;
public modal: any;

async onload() {
let templates = this.app.internalPlugins.getPluginById("templates");
Expand All @@ -17,7 +18,7 @@ export default class TemplaterPlugin extends Plugin {
return;
}
if (templates.instance.modal === null) {
//new Notice("This plugin relies on the internal Templates plugin, enabling Templates ...");
console.log("Templater relies on the internal Templates plugin, enabling Templates ...");
templates.enable();
}

Expand Down Expand Up @@ -47,7 +48,6 @@ export default class TemplaterPlugin extends Plugin {
this.app = app;
this.templatePlugin = template_plugin;
this.settings = settings;
this.update_template_files();
}

onChooseOption(suggestionItem: TFile, evt: Event) {
Expand Down Expand Up @@ -126,19 +126,40 @@ export default class TemplaterPlugin extends Plugin {
}
}

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

// TODO: find a good icon
this.addRibbonIcon('three-horizontal-bars', 'Templater', async () => {
let plugin_template = new CustomPluginTemplates(this.app);

try {
let m = new CustomModalTemplates(this.app, plugin_template, this.settings);
m.open();
this.modal.update_template_files();
this.modal.open();
}
catch(error) {
new Notice(error);
}
});

this.addCommand({
id: "insert-templater",
name: "Insert Template",
hotkeys: [
{
modifiers: ["Alt"],
key: 'e',
},
],
callback: () => {
try {
this.modal.update_template_files();
this.modal.open();
}
catch(error) {
new Notice(error);
}
},
});

this.addSettingTab(new TemplaterSettingTab(this.app, this));
}

Expand Down

0 comments on commit 03f4a19

Please sign in to comment.