Skip to content

Commit 0a86387

Browse files
committed
Add runTemplaterFile button command
1 parent 7440d58 commit 0a86387

File tree

7 files changed

+165
-73
lines changed

7 files changed

+165
-73
lines changed

exampleVault/Buttons/Button Example.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ action:
2525
command: obsidian-meta-bind-plugin:open-playground
2626
```
2727

28+
Custom templater commands
29+
```meta-bind-button
30+
style: default
31+
icon: app-window-mac
32+
label: "Close tabs"
33+
actions:
34+
- type: templaterCreateNote
35+
templateFile: "templates/templater/Close unpinned tabs.md"
36+
templater: true
37+
```
38+
```meta-bind-button
39+
style: default
40+
icon: app-window-mac
41+
label: "Close tabs 2"
42+
actions:
43+
- type: runTemplaterFile
44+
templateFile: "templates/templater/Close unpinned tabs.md"
45+
```
46+
2847
And custom JS buttons as well
2948

3049
```meta-bind-button
@@ -209,6 +228,7 @@ actions:
209228
210229
```
211230

231+
212232
### Modifying Front-matter
213233

214234
```meta-bind-button
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<%*
2+
const closeUnpinnedLeaves = () => {
3+
const leaves = [];
4+
app.workspace.iterateRootLeaves(leaf => {
5+
leaves.push(leaf);
6+
});
7+
leaves.forEach(leaf => {
8+
if (!leaf.pinned) {
9+
leaf.detach();
10+
}
11+
});
12+
}
13+
14+
closeUnpinnedLeaves();
15+
16+
// the return below prevents the note from being created
17+
return;
18+
-%>

package.json

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
11
{
2-
"name": "obsidian-meta-bind-plugin",
3-
"version": "1.2.5",
4-
"description": "Make your notes interactive with inline input fields, metadata displays, and buttons.",
5-
"main": "main.js",
6-
"scripts": {
7-
"dev": "bun run automation/build/esbuild.dev.config.ts",
8-
"build": "bun run tsc && bun run automation/build/esbuild.config.ts",
9-
"dev-publish": "bun run automation/build/esbuild.publish.dev.config.ts",
10-
"build-publish": "bun run tsc && node automation/build/esbuild.publish.config.ts",
11-
"tsc": "tsc -noEmit -skipLibCheck",
12-
"test": "LOG_TESTS=false bun test --conditions=browser",
13-
"test:log": "LOG_TESTS=true bun test --conditions=browser",
14-
"format": "prettier --write --plugin prettier-plugin-svelte .",
15-
"format:check": "prettier --check --plugin prettier-plugin-svelte .",
16-
"lint": "eslint --max-warnings=0 packages/** --no-warn-ignored",
17-
"lint:fix": "eslint --max-warnings=0 --fix packages/** --no-warn-ignored",
18-
"svelte-check": "svelte-check --compiler-warnings \"unused-export-let:ignore\"",
19-
"types": "tsc -p \"./tsconfig.types.json\"",
20-
"check": "bun run format:check && bun run tsc && bun run svelte-check && bun run lint && bun run test",
21-
"check:fix": "bun run format && bun run tsc && bun run svelte-check && bun run lint:fix && bun run test",
22-
"pack:i": "bun run automation/installScript.ts",
23-
"pack:i:clean": "bun run automation/installScriptClean.ts",
24-
"release": "bun run automation/release.ts",
25-
"serve-publish": "bun --watch automation/publishServer.ts",
26-
"stats": "bun run automation/stats.ts"
27-
},
28-
"keywords": [],
29-
"author": "Moritz Jung",
30-
"license": "GPL-3.0",
31-
"devDependencies": {
32-
"@elysiajs/cors": "^1.1.1",
33-
"@happy-dom/global-registrator": "^14.12.3",
34-
"@tsconfig/svelte": "^5.0.4",
35-
"@types/bun": "^1.1.13",
36-
"builtin-modules": "^4.0.0",
37-
"elysia": "^1.1.24",
38-
"esbuild": "^0.24.0",
39-
"esbuild-plugin-copy-watch": "^2.3.1",
40-
"esbuild-svelte": "^0.8.2",
41-
"eslint": "^9.14.0",
42-
"eslint-plugin-import": "^2.31.0",
43-
"eslint-plugin-isaacscript": "^4.0.0",
44-
"eslint-plugin-no-relative-import-paths": "^1.5.5",
45-
"eslint-plugin-only-warn": "^1.1.0",
46-
"eslint-plugin-svelte": "^2.46.0",
47-
"prettier": "^3.3.3",
48-
"prettier-plugin-svelte": "^3.2.7",
49-
"string-argv": "^0.3.2",
50-
"svelte-check": "^4.0.5",
51-
"svelte-preprocess": "^6.0.3",
52-
"tslib": "^2.8.1",
53-
"typescript": "^5.6.3",
54-
"typescript-eslint": "^8.13.0",
55-
"yaml": "^2.6.0"
56-
},
57-
"dependencies": {
58-
"@codemirror/legacy-modes": "^6.4.2",
59-
"@lemons_dev/parsinom": "^0.0.12",
60-
"itertools-ts": "^1.27.1",
61-
"mathjs": "^13.2.0",
62-
"moment": "^2.30.1",
63-
"svelte": "^5.1.9",
64-
"zod": "^3.23.8",
65-
"zod-validation-error": "^3.4.0"
66-
},
67-
"private": true,
68-
"trustedDependencies": [
69-
"esbuild",
70-
"svelte-preprocess"
71-
]
2+
"name": "obsidian-meta-bind-plugin",
3+
"version": "1.2.5",
4+
"description": "Make your notes interactive with inline input fields, metadata displays, and buttons.",
5+
"main": "main.js",
6+
"scripts": {
7+
"dev": "bun run automation/build/esbuild.dev.config.ts",
8+
"build": "bun run tsc && bun run automation/build/esbuild.config.ts",
9+
"dev-publish": "bun run automation/build/esbuild.publish.dev.config.ts",
10+
"build-publish": "bun run tsc && node automation/build/esbuild.publish.config.ts",
11+
"tsc": "tsc -noEmit -skipLibCheck",
12+
"test": "LOG_TESTS=false bun test --conditions=browser",
13+
"test:log": "LOG_TESTS=true bun test --conditions=browser",
14+
"format": "prettier --write --plugin prettier-plugin-svelte .",
15+
"format:check": "prettier --check --plugin prettier-plugin-svelte .",
16+
"lint": "eslint --max-warnings=0 packages/** --no-warn-ignored",
17+
"lint:fix": "eslint --max-warnings=0 --fix packages/** --no-warn-ignored",
18+
"svelte-check": "svelte-check --compiler-warnings \"unused-export-let:ignore\"",
19+
"types": "tsc -p \"./tsconfig.types.json\"",
20+
"check": "bun run format:check && bun run tsc && bun run svelte-check && bun run lint && bun run test",
21+
"check:fix": "bun run format && bun run tsc && bun run svelte-check && bun run lint:fix && bun run test",
22+
"pack:i": "bun run automation/installScript.ts",
23+
"pack:i:clean": "bun run automation/installScriptClean.ts",
24+
"release": "bun run automation/release.ts",
25+
"serve-publish": "bun --watch automation/publishServer.ts",
26+
"stats": "bun run automation/stats.ts"
27+
},
28+
"keywords": [],
29+
"author": "Moritz Jung",
30+
"license": "GPL-3.0",
31+
"devDependencies": {
32+
"@elysiajs/cors": "^1.1.1",
33+
"@happy-dom/global-registrator": "^14.12.3",
34+
"@tsconfig/svelte": "^5.0.4",
35+
"@types/bun": "^1.1.13",
36+
"builtin-modules": "^4.0.0",
37+
"elysia": "^1.1.24",
38+
"esbuild": "^0.24.0",
39+
"esbuild-plugin-copy-watch": "^2.3.1",
40+
"esbuild-svelte": "^0.8.2",
41+
"eslint": "^9.14.0",
42+
"eslint-plugin-import": "^2.31.0",
43+
"eslint-plugin-isaacscript": "^4.0.0",
44+
"eslint-plugin-no-relative-import-paths": "^1.5.5",
45+
"eslint-plugin-only-warn": "^1.1.0",
46+
"eslint-plugin-svelte": "^2.46.0",
47+
"prettier": "^3.3.3",
48+
"prettier-plugin-svelte": "^3.2.7",
49+
"string-argv": "^0.3.2",
50+
"svelte-check": "^4.0.5",
51+
"svelte-preprocess": "^6.0.3",
52+
"tslib": "^2.8.1",
53+
"typescript": "^5.6.3",
54+
"typescript-eslint": "^8.13.0",
55+
"yaml": "^2.6.0"
56+
},
57+
"dependencies": {
58+
"@codemirror/legacy-modes": "^6.4.2",
59+
"@lemons_dev/parsinom": "^0.0.12",
60+
"itertools-ts": "^1.27.1",
61+
"mathjs": "^13.2.0",
62+
"moment": "^2.30.1",
63+
"svelte": "^5.1.9",
64+
"zod": "^3.23.8",
65+
"zod-validation-error": "^3.4.0"
66+
},
67+
"private": true,
68+
"trustedDependencies": [
69+
"esbuild",
70+
"svelte-preprocess"
71+
]
7272
}

packages/core/src/config/ButtonConfig.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export enum ButtonActionType {
1414
INPUT = 'input',
1515
SLEEP = 'sleep',
1616
TEMPLATER_CREATE_NOTE = 'templaterCreateNote',
17+
RUN_TEMPLATER_FILE = 'runTemplaterFile',
1718
UPDATE_METADATA = 'updateMetadata',
1819
CREATE_NOTE = 'createNote',
1920
REPLACE_IN_NOTE = 'replaceInNote',
@@ -59,6 +60,11 @@ export interface TemplaterCreateNoteButtonAction {
5960
openIfAlreadyExists?: boolean;
6061
}
6162

63+
export interface RunTemplaterFileButtonAction {
64+
type: ButtonActionType.RUN_TEMPLATER_FILE;
65+
templateFile: string;
66+
}
67+
6268
export interface UpdateMetadataButtonAction {
6369
type: ButtonActionType.UPDATE_METADATA;
6470
bindTarget: string;
@@ -120,7 +126,8 @@ export type ButtonAction =
120126
| ReplaceSelfButtonAction
121127
| RegexpReplaceInNoteButtonAction
122128
| InsertIntoNoteButtonAction
123-
| InlineJSButtonAction;
129+
| InlineJSButtonAction
130+
| RunTemplaterFileButtonAction;
124131

125132
export interface ButtonConfig {
126133
label: string;
@@ -166,4 +173,5 @@ export interface ButtonActionMap {
166173
[ButtonActionType.REGEXP_REPLACE_IN_NOTE]: RegexpReplaceInNoteButtonAction;
167174
[ButtonActionType.INSERT_INTO_NOTE]: InsertIntoNoteButtonAction;
168175
[ButtonActionType.INLINE_JS]: InlineJSButtonAction;
176+
[ButtonActionType.RUN_TEMPLATER_FILE]: RunTemplaterFileButtonAction;
169177
}

packages/core/src/config/ButtonConfigValidators.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import type {
1010
OpenButtonAction,
1111
RegexpReplaceInNoteButtonAction,
1212
ReplaceInNoteButtonAction,
13-
ReplaceSelfButtonAction,
13+
ReplaceSelfButtonAction, RunTemplaterFileButtonAction,
1414
SleepButtonAction,
1515
TemplaterCreateNoteButtonAction,
16-
UpdateMetadataButtonAction,
16+
UpdateMetadataButtonAction
1717
} from 'packages/core/src/config/ButtonConfig';
1818
import { ButtonActionType, ButtonStyleType } from 'packages/core/src/config/ButtonConfig';
1919
import { oneOf, schemaForType } from 'packages/core/src/utils/ZodUtils';
@@ -95,6 +95,14 @@ export const V_TemplaterCreateNoteButtonAction = schemaForType<TemplaterCreateNo
9595
).optional(),
9696
}),
9797
);
98+
99+
export const V_RunTemplaterFileButtonAction = schemaForType<RunTemplaterFileButtonAction>()(
100+
z.object({
101+
type: z.literal(ButtonActionType.RUN_TEMPLATER_FILE),
102+
templateFile: stringValidator('runTemplaterFile', 'templateFile', 'template file path'),
103+
}),
104+
);
105+
98106
export const V_UpdateMetadataButtonAction = schemaForType<UpdateMetadataButtonAction>()(
99107
z.object({
100108
type: z.literal(ButtonActionType.UPDATE_METADATA),
@@ -187,6 +195,7 @@ export const V_ButtonAction = schemaForType<ButtonAction>()(
187195
V_RegexpReplaceInNoteButtonAction,
188196
V_InsertIntoNoteButtonAction,
189197
V_InlineJSButtonAction,
198+
V_RunTemplaterFileButtonAction,
190199
]),
191200
);
192201

packages/core/src/fields/button/ButtonActionRunner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { UpdateMetadataButtonActionConfig } from 'packages/core/src/fields/butto
2323
import type { IPlugin } from 'packages/core/src/IPlugin';
2424
import { MDLinkParser } from 'packages/core/src/parsers/MarkdownLinkParser';
2525
import { ErrorLevel, MetaBindParsingError } from 'packages/core/src/utils/errors/MetaBindErrors';
26+
import { RunTemplaterFileButtonActionConfig } from './actions/RunTemplaterFileButtonActionConfig';
2627

2728
type ActionContexts = {
2829
[key in ButtonActionType]: AbstractButtonActionConfig<ButtonActionMap[key]>;
@@ -50,6 +51,7 @@ export class ButtonActionRunner {
5051
[ButtonActionType.REGEXP_REPLACE_IN_NOTE]: new RegexpReplaceInNoteButtonActionConfig(plugin),
5152
[ButtonActionType.INSERT_INTO_NOTE]: new InsertIntoNoteButtonActionConfig(plugin),
5253
[ButtonActionType.INLINE_JS]: new InlineJSButtonActionConfig(plugin),
54+
[ButtonActionType.RUN_TEMPLATER_FILE]: new RunTemplaterFileButtonActionConfig(plugin),
5355
};
5456
}
5557

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type {
2+
ButtonClickContext,
3+
ButtonConfig,
4+
ButtonContext, RunTemplaterFileButtonAction,
5+
} from 'packages/core/src/config/ButtonConfig';
6+
import { ButtonActionType } from 'packages/core/src/config/ButtonConfig';
7+
import { AbstractButtonActionConfig } from 'packages/core/src/fields/button/AbstractButtonActionConfig';
8+
import type { IPlugin } from 'packages/core/src/IPlugin';
9+
10+
export class RunTemplaterFileButtonActionConfig extends AbstractButtonActionConfig<RunTemplaterFileButtonAction> {
11+
constructor(plugin: IPlugin) {
12+
super(ButtonActionType.RUN_TEMPLATER_FILE, plugin);
13+
}
14+
15+
async run(
16+
_config: ButtonConfig | undefined,
17+
action: RunTemplaterFileButtonAction,
18+
_filePath: string,
19+
_context: ButtonContext,
20+
_click: ButtonClickContext,
21+
): Promise<void> {
22+
await this.plugin.internal.evaluateTemplaterTemplate(action.templateFile, action.templateFile);
23+
}
24+
25+
create(): Required<RunTemplaterFileButtonAction> {
26+
return {
27+
type: ButtonActionType.RUN_TEMPLATER_FILE,
28+
templateFile: '',
29+
};
30+
}
31+
32+
getActionLabel(): string {
33+
return 'Create a new note using Templater';
34+
}
35+
}

0 commit comments

Comments
 (0)