Skip to content

Commit 83b3ac7

Browse files
committed
Merge branch 'reply2za-master'
2 parents 12e5370 + 627f1ce commit 83b3ac7

File tree

13 files changed

+107
-5
lines changed

13 files changed

+107
-5
lines changed

automation/build/esbuild.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import builtins from 'builtin-modules';
22
import esbuild from 'esbuild';
33
import esbuildSvelte from 'esbuild-svelte';
4-
import { sveltePreprocess } from 'svelte-preprocess';
54
import { getBuildBanner } from 'build/buildBanner';
65

76
const banner = getBuildBanner('Release Build', version => version);

automation/build/esbuild.dev.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import esbuild from 'esbuild';
22
import copy from 'esbuild-plugin-copy-watch';
33
import esbuildSvelte from 'esbuild-svelte';
4-
import { sveltePreprocess } from 'svelte-preprocess';
54
import manifest from '../../manifest.json' assert { type: 'json' };
65
import { getBuildBanner } from 'build/buildBanner';
76

automation/build/esbuild.publish.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import builtins from 'builtin-modules';
22
import esbuild from 'esbuild';
33
import esbuildSvelte from 'esbuild-svelte';
44
import process from 'process';
5-
import { sveltePreprocess } from 'svelte-preprocess';
65
import { getBuildBanner } from 'build/buildBanner';
76

87
const banner = getBuildBanner('Publish Release Build', version => `Publish Release Build (based on ${version})`);

automation/build/esbuild.publish.dev.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import builtins from 'builtin-modules';
22
import esbuild from 'esbuild';
33
import esbuildSvelte from 'esbuild-svelte';
44
import process from 'process';
5-
import { sveltePreprocess } from 'svelte-preprocess';
65
import { getBuildBanner } from 'build/buildBanner';
76

87
const banner = getBuildBanner('Publish Dev Build', _ => `Publish Dev Build`);

exampleVault/Buttons/Button Example.md

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

28+
Custom templater commands
29+
30+
```meta-bind-button
31+
style: default
32+
label: "Run a templater file"
33+
actions:
34+
- type: runTemplaterFile
35+
templateFile: "templates/templater/Say Hello Command.md"
36+
```
37+
2838
And custom JS buttons as well
2939

3040
```meta-bind-button
@@ -209,6 +219,7 @@ actions:
209219
210220
```
211221

222+
212223
### Modifying Front-matter
213224

214225
```meta-bind-button
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%*
2+
new Notice("Hello!");
3+
-%>

packages/core/src/config/ButtonConfig.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export enum ButtonActionType {
2626
INPUT = 'input',
2727
SLEEP = 'sleep',
2828
TEMPLATER_CREATE_NOTE = 'templaterCreateNote',
29+
RUN_TEMPLATER_FILE = 'runTemplaterFile',
2930
UPDATE_METADATA = 'updateMetadata',
3031
CREATE_NOTE = 'createNote',
3132
REPLACE_IN_NOTE = 'replaceInNote',
@@ -71,6 +72,11 @@ export interface TemplaterCreateNoteButtonAction {
7172
openIfAlreadyExists?: boolean;
7273
}
7374

75+
export interface RunTemplaterFileButtonAction {
76+
type: ButtonActionType.RUN_TEMPLATER_FILE;
77+
templateFile: string;
78+
}
79+
7480
export interface UpdateMetadataButtonAction {
7581
type: ButtonActionType.UPDATE_METADATA;
7682
bindTarget: string;
@@ -132,7 +138,8 @@ export type ButtonAction =
132138
| ReplaceSelfButtonAction
133139
| RegexpReplaceInNoteButtonAction
134140
| InsertIntoNoteButtonAction
135-
| InlineJSButtonAction;
141+
| InlineJSButtonAction
142+
| RunTemplaterFileButtonAction;
136143

137144
export interface ButtonConfig {
138145
/**
@@ -228,4 +235,5 @@ export interface ButtonActionMap {
228235
[ButtonActionType.REGEXP_REPLACE_IN_NOTE]: RegexpReplaceInNoteButtonAction;
229236
[ButtonActionType.INSERT_INTO_NOTE]: InsertIntoNoteButtonAction;
230237
[ButtonActionType.INLINE_JS]: InlineJSButtonAction;
238+
[ButtonActionType.RUN_TEMPLATER_FILE]: RunTemplaterFileButtonAction;
231239
}

packages/core/src/config/validators/ButtonConfigValidators.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
RegexpReplaceInNoteButtonAction,
1212
ReplaceInNoteButtonAction,
1313
ReplaceSelfButtonAction,
14+
RunTemplaterFileButtonAction,
1415
SleepButtonAction,
1516
TemplaterCreateNoteButtonAction,
1617
UpdateMetadataButtonAction,
@@ -95,6 +96,14 @@ export const V_TemplaterCreateNoteButtonAction = schemaForType<TemplaterCreateNo
9596
).optional(),
9697
}),
9798
);
99+
100+
export const V_RunTemplaterFileButtonAction = schemaForType<RunTemplaterFileButtonAction>()(
101+
z.object({
102+
type: z.literal(ButtonActionType.RUN_TEMPLATER_FILE),
103+
templateFile: actionFieldString('runTemplaterFile', 'templateFile', 'template file path'),
104+
}),
105+
);
106+
98107
export const V_UpdateMetadataButtonAction = schemaForType<UpdateMetadataButtonAction>()(
99108
z.object({
100109
type: z.literal(ButtonActionType.UPDATE_METADATA),
@@ -187,6 +196,7 @@ export const V_ButtonAction = schemaForType<ButtonAction>()(
187196
V_RegexpReplaceInNoteButtonAction,
188197
V_InsertIntoNoteButtonAction,
189198
V_InlineJSButtonAction,
199+
V_RunTemplaterFileButtonAction,
190200
]),
191201
);
192202

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { OpenButtonActionConfig } from 'packages/core/src/fields/button/actions/
1717
import { RegexpReplaceInNoteButtonActionConfig } from 'packages/core/src/fields/button/actions/RegexpReplaceInNoteButtonActionConfig';
1818
import { ReplaceInNoteButtonActionConfig } from 'packages/core/src/fields/button/actions/ReplaceInNoteButtonActionConfig';
1919
import { ReplaceSelfButtonActionConfig } from 'packages/core/src/fields/button/actions/ReplaceSelfButtonActionConfig';
20+
import { RunTemplaterFileButtonActionConfig } from 'packages/core/src/fields/button/actions/RunTemplaterFileButtonActionConfig';
2021
import { SleepButtonActionConfig } from 'packages/core/src/fields/button/actions/SleepButtonActionConfig';
2122
import { TemplaterCreateNoteButtonActionConfig } from 'packages/core/src/fields/button/actions/TemplaterCreateNoteButtonActionConfig';
2223
import { UpdateMetadataButtonActionConfig } from 'packages/core/src/fields/button/actions/UpdateMetadataButtonActionConfig';
@@ -49,6 +50,7 @@ export class ButtonActionRunner {
4950
[ButtonActionType.REGEXP_REPLACE_IN_NOTE]: new RegexpReplaceInNoteButtonActionConfig(plugin),
5051
[ButtonActionType.INSERT_INTO_NOTE]: new InsertIntoNoteButtonActionConfig(plugin),
5152
[ButtonActionType.INLINE_JS]: new InlineJSButtonActionConfig(plugin),
53+
[ButtonActionType.RUN_TEMPLATER_FILE]: new RunTemplaterFileButtonActionConfig(plugin),
5254
};
5355
}
5456

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

packages/core/src/modals/modalContents/buttonBuilder/ButtonBuilderModalComponent.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import type { ButtonBuilderModal } from 'packages/core/src/modals/modalContents/buttonBuilder/ButtonBuilderModal';
88
import CommandActionSettings from 'packages/core/src/modals/modalContents/buttonBuilder/CommandActionSettings.svelte';
99
import CreateNoteActionSettings from 'packages/core/src/modals/modalContents/buttonBuilder/CreateNoteActionSettings.svelte';
10+
import RunTemplaterFileActionSettings from 'packages/core/src/modals/modalContents/buttonBuilder/RunTemplaterFileActionSettings.svelte';
1011
import InlineJsActionSettings from 'packages/core/src/modals/modalContents/buttonBuilder/InlineJsActionSettings.svelte';
1112
import InputActionSettings from 'packages/core/src/modals/modalContents/buttonBuilder/InputActionSettings.svelte';
1213
import InsertIntoNoteActionSettings from 'packages/core/src/modals/modalContents/buttonBuilder/InsertIntoNoteActionSettings.svelte';
@@ -229,6 +230,10 @@ Add action of type
229230
<CreateNoteActionSettings action={action} plugin={plugin}></CreateNoteActionSettings>
230231
{/if}
231232

233+
{#if action.type === ButtonActionType.RUN_TEMPLATER_FILE}
234+
<RunTemplaterFileActionSettings action={action} plugin={plugin}></RunTemplaterFileActionSettings>
235+
{/if}
236+
232237
{#if action.type === ButtonActionType.REPLACE_IN_NOTE}
233238
<ReplaceInNoteActionSettings action={action} plugin={plugin}></ReplaceInNoteActionSettings>
234239
{/if}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script lang="ts">
2+
import type { RunTemplaterFileButtonAction } from 'packages/core/src/config/ButtonConfig';
3+
import type { IPlugin } from 'packages/core/src/IPlugin';
4+
import SettingComponent from 'packages/core/src/utils/components/SettingComponent.svelte';
5+
6+
const {
7+
plugin,
8+
action,
9+
}: {
10+
plugin: IPlugin;
11+
action: RunTemplaterFileButtonAction;
12+
} = $props();
13+
</script>
14+
15+
<SettingComponent
16+
name="File path: {action.templateFile || 'default'}"
17+
description="The path from the vault to the templater file."
18+
>
19+
<input type="text" bind:value={action.templateFile} placeholder="some path" />
20+
</SettingComponent>

tests/fields/Button.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ const buttonActionTests: Record<ButtonActionType, () => void> = {
253253
}).not.toThrow();
254254
});
255255
},
256+
[ButtonActionType.RUN_TEMPLATER_FILE]: () => {
257+
test('does not throw', () => {
258+
expect(async () => {
259+
await simplifiedRunAction({
260+
type: ButtonActionType.RUN_TEMPLATER_FILE,
261+
templateFile: 'test',
262+
});
263+
}).not.toThrow();
264+
});
265+
},
256266
};
257267

258268
describe('Button', () => {

0 commit comments

Comments
 (0)