Skip to content

Commit fdd5ce0

Browse files
committed
more button actions
1 parent f4b6d44 commit fdd5ce0

File tree

14 files changed

+434
-87
lines changed

14 files changed

+434
-87
lines changed

exampleVault/Button Example.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ style: default
1818
label: Run Custom JS
1919
action:
2020
type: js
21-
jsFile: testJsFile.js
21+
file: testJsFile.js
2222
```
2323

2424
And open internal and external links
@@ -76,6 +76,65 @@ actions:
7676
- type: command
7777
command: workspace:new-tab
7878
- type: js
79-
jsFile: "testJsFile.js"
79+
file: "testJsFile.js"
80+
81+
```
82+
83+
84+
## Button Types
85+
86+
```meta-bind-button
87+
label: Input
88+
hidden: false
89+
id: ""
90+
style: default
91+
actions:
92+
- type: command
93+
command: command-palette:open
94+
- type: input
95+
str: help
96+
97+
```
98+
99+
```meta-bind-button
100+
label: Templater
101+
hidden: false
102+
id: ""
103+
style: default
104+
actions:
105+
- type: templaterCreateNote
106+
templateFile: "templates/templater/Templater Template.md"
107+
fileName: Button Templater Test
108+
109+
```
110+
111+
```meta-bind-button
112+
label: Sleep
113+
hidden: false
114+
id: ""
115+
style: default
116+
actions:
117+
- type: command
118+
command: command-palette:open
119+
- type: sleep
120+
ms: 1000
121+
- type: input
122+
str: help
123+
124+
```
125+
126+
127+
```meta-bind-button
128+
label: Show PF2e Examples with Delay
129+
hidden: false
130+
id: ""
131+
style: default
132+
actions:
133+
- type: command
134+
command: switcher:open
135+
- type: sleep
136+
ms: 500
137+
- type: input
138+
str: PF2e
80139
81140
```

exampleVault/New Syntax Playground.md

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<% tp.file.title %>

src/api/API.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import { ViewFieldFactory } from '../fields/viewFields/ViewFieldFactory';
2626
import { getUUID } from '../utils/Utils';
2727
import { parsePropPath } from '../utils/prop/PropParser';
2828
import { RenderChildType } from '../config/FieldConfigs';
29-
import { ButtonActionRunner } from '../fields/button/ButtonActionRunner';
29+
import { type ButtonActionRunner } from '../fields/button/ButtonActionRunner';
3030
import { ButtonManager } from '../fields/button/ButtonManager';
3131
import { type BindTargetDeclaration, BindTargetStorageType } from '../parsers/BindTargetDeclaration';
32+
import { ObsidianButtonActionRunner } from '../fields/button/ObsidianButtonActionRunner';
3233

3334
export class API implements IAPI {
3435
public plugin: MetaBindPlugin;
@@ -56,7 +57,7 @@ export class API implements IAPI {
5657
this.inputFieldFactory = new InputFieldFactory(this.plugin);
5758
this.viewFieldFactory = new ViewFieldFactory(this.plugin);
5859

59-
this.buttonActionRunner = new ButtonActionRunner(this.plugin);
60+
this.buttonActionRunner = new ObsidianButtonActionRunner(this.plugin);
6061
this.buttonManager = new ButtonManager();
6162
}
6263

src/config/ButtonConfig.ts

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import { type RefinementCtx, z } from 'zod';
2-
3-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4-
function schemaForType<T>(): <S extends z.ZodType<T, any, any>>(arg: S) => S {
5-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6-
return function <S extends z.ZodType<T, any, any>>(arg: S): S {
7-
return arg;
8-
};
9-
}
1+
import { z } from 'zod';
2+
import { oneOf, schemaForType } from '../utils/ZodHelpers';
103

114
export enum ButtonStyleType {
125
DEFAULT = 'default',
@@ -19,6 +12,10 @@ export enum ButtonActionType {
1912
COMMAND = 'command',
2013
JS = 'js',
2114
OPEN = 'open',
15+
INPUT = 'input',
16+
SLEEP = 'sleep',
17+
TEMPLATER_CREATE_NOTE = 'templaterCreateNote',
18+
QUICK_SWITCHER = 'quickSwitcher',
2219
}
2320

2421
export interface CommandButtonAction {
@@ -35,13 +32,13 @@ export const CommandButtonActionValidator = schemaForType<CommandButtonAction>()
3532

3633
export interface JSButtonAction {
3734
type: ButtonActionType.JS;
38-
jsFile: string;
35+
file: string;
3936
}
4037

4138
export const JSButtonActionValidator = schemaForType<JSButtonAction>()(
4239
z.object({
4340
type: z.literal(ButtonActionType.JS),
44-
jsFile: z.string(),
41+
file: z.string(),
4542
}),
4643
);
4744

@@ -57,10 +54,79 @@ export const OpenButtonActionValidator = schemaForType<OpenButtonAction>()(
5754
}),
5855
);
5956

60-
export type ButtonAction = CommandButtonAction | JSButtonAction | OpenButtonAction;
57+
export interface InputButtonAction {
58+
type: ButtonActionType.INPUT;
59+
str: string;
60+
}
61+
62+
export const InputButtonActionValidator = schemaForType<InputButtonAction>()(
63+
z.object({
64+
type: z.literal(ButtonActionType.INPUT),
65+
str: z.string(),
66+
}),
67+
);
68+
69+
export interface SleepButtonAction {
70+
type: ButtonActionType.SLEEP;
71+
ms: number;
72+
}
73+
74+
export const SleepButtonActionValidator = schemaForType<SleepButtonAction>()(
75+
z.object({
76+
type: z.literal(ButtonActionType.SLEEP),
77+
ms: z.number(),
78+
}),
79+
);
80+
81+
export interface TemplaterCreateNoteButtonAction {
82+
type: ButtonActionType.TEMPLATER_CREATE_NOTE;
83+
templateFile: string;
84+
folderPath?: string;
85+
fileName?: string;
86+
openNote?: boolean;
87+
}
88+
89+
export const TemplaterCreateNoteButtonActionValidator = schemaForType<TemplaterCreateNoteButtonAction>()(
90+
z.object({
91+
type: z.literal(ButtonActionType.TEMPLATER_CREATE_NOTE),
92+
templateFile: z.string(),
93+
folderPath: z.string().optional(),
94+
fileName: z.string().optional(),
95+
openNote: z.boolean().optional(),
96+
}),
97+
);
98+
99+
export interface QuickSwitcherButtonAction {
100+
type: ButtonActionType.QUICK_SWITCHER;
101+
filter: string;
102+
}
103+
104+
export const QuickSwitcherButtonActionValidator = schemaForType<QuickSwitcherButtonAction>()(
105+
z.object({
106+
type: z.literal(ButtonActionType.QUICK_SWITCHER),
107+
filter: z.string(),
108+
}),
109+
);
110+
111+
export type ButtonAction =
112+
| CommandButtonAction
113+
| JSButtonAction
114+
| OpenButtonAction
115+
| InputButtonAction
116+
| SleepButtonAction
117+
| TemplaterCreateNoteButtonAction
118+
| QuickSwitcherButtonAction;
61119

62120
export const ButtonActionValidator = schemaForType<ButtonAction>()(
63-
z.union([CommandButtonActionValidator, JSButtonActionValidator, OpenButtonActionValidator]),
121+
z.union([
122+
CommandButtonActionValidator,
123+
JSButtonActionValidator,
124+
OpenButtonActionValidator,
125+
InputButtonActionValidator,
126+
SleepButtonActionValidator,
127+
TemplaterCreateNoteButtonActionValidator,
128+
QuickSwitcherButtonActionValidator,
129+
]),
64130
);
65131

66132
export interface ButtonConfig {
@@ -75,25 +141,6 @@ export interface ButtonConfig {
75141
type Tuple<T> = [T, ...T[]];
76142
export const ButtonStyleValidator = z.enum(Object.values(ButtonStyleType) as Tuple<ButtonStyleType>);
77143

78-
function oneOf<
79-
A,
80-
K1 extends Extract<keyof A, string>,
81-
K2 extends Extract<keyof A, string>,
82-
R extends A &
83-
((Required<Pick<A, K1>> & { [P in K2]: undefined }) | (Required<Pick<A, K2>> & { [P in K1]: undefined })),
84-
>(key1: K1, key2: K2): (arg: A, ctx: RefinementCtx) => arg is R {
85-
return (arg, ctx): arg is R => {
86-
if ((arg[key1] === undefined) === (arg[key2] === undefined)) {
87-
ctx.addIssue({
88-
code: z.ZodIssueCode.custom,
89-
message: `Either ${key1} or ${key2} must be filled, but not both`,
90-
});
91-
return false;
92-
}
93-
return true;
94-
};
95-
}
96-
97144
export const ButtonConfigValidator = schemaForType<ButtonConfig>()(
98145
z
99146
.object({

src/fields/button/ButtonActionRunner.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { type IPlugin } from '../../IPlugin';
21
import {
32
type ButtonAction,
43
ButtonActionType,
54
type CommandButtonAction,
5+
type InputButtonAction,
66
type JSButtonAction,
77
type OpenButtonAction,
8+
type QuickSwitcherButtonAction,
9+
type SleepButtonAction,
10+
type TemplaterCreateNoteButtonAction,
811
} from '../../config/ButtonConfig';
912
import { MDLinkParser } from '../../parsers/MarkdownLinkParser';
1013
import { DocsHelper } from '../../utils/DocsHelper';
14+
import { type IPlugin } from '../../IPlugin';
1115

1216
export class ButtonActionRunner {
1317
plugin: IPlugin;
@@ -23,6 +27,14 @@ export class ButtonActionRunner {
2327
await this.runJSAction(action, filePath);
2428
} else if (action.type === ButtonActionType.OPEN) {
2529
await this.runOpenAction(action, filePath);
30+
} else if (action.type === ButtonActionType.INPUT) {
31+
await this.runInputAction(action);
32+
} else if (action.type === ButtonActionType.SLEEP) {
33+
await this.runSleepAction(action);
34+
} else if (action.type === ButtonActionType.TEMPLATER_CREATE_NOTE) {
35+
await this.runTemplaterCreateNoteAction(action);
36+
} else if (action.type === ButtonActionType.QUICK_SWITCHER) {
37+
await this.runQuickSwitcherAction(action);
2638
}
2739
}
2840

@@ -31,7 +43,7 @@ export class ButtonActionRunner {
3143
}
3244

3345
async runJSAction(action: JSButtonAction, filePath: string): Promise<void> {
34-
const unloadCallback = await this.plugin.internal.jsEngineRunFile(action.jsFile, filePath);
46+
const unloadCallback = await this.plugin.internal.jsEngineRunFile(action.file, filePath);
3547
unloadCallback();
3648
}
3749

@@ -44,4 +56,24 @@ export class ButtonActionRunner {
4456
DocsHelper.open(link.target);
4557
}
4658
}
59+
60+
async runInputAction(action: InputButtonAction): Promise<void> {
61+
const el = document.activeElement;
62+
if (el && el instanceof HTMLInputElement) {
63+
el.setRangeText(action.str, el.selectionStart!, el.selectionEnd!, 'end');
64+
el.trigger('input');
65+
}
66+
}
67+
68+
async runSleepAction(action: SleepButtonAction): Promise<void> {
69+
await new Promise(resolve => setTimeout(resolve, action.ms));
70+
}
71+
72+
async runTemplaterCreateNoteAction(_action: TemplaterCreateNoteButtonAction): Promise<void> {
73+
throw new Error('Not supported');
74+
}
75+
76+
async runQuickSwitcherAction(_action: QuickSwitcherButtonAction): Promise<void> {
77+
// TODO
78+
}
4779
}

0 commit comments

Comments
 (0)