Skip to content

Commit 5e03062

Browse files
committed
fix #242
1 parent c03d2f1 commit 5e03062

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

packages/core/src/api/InternalAPI.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ export abstract class InternalAPI<Plugin extends IPlugin> {
9595
*
9696
* @param filePath
9797
* @param callingFilePath
98+
* @param configOverrides
9899
* @param container
99100
*
100101
* @returns Cleanup callback.
101102
*/
102-
abstract jsEngineRunFile(filePath: string, callingFilePath: string, container?: HTMLElement): Promise<() => void>;
103+
abstract jsEngineRunFile(
104+
filePath: string,
105+
callingFilePath: string,
106+
configOverrides: Record<string, unknown>,
107+
container?: HTMLElement,
108+
): Promise<() => void>;
103109

104110
/**
105111
* Run code using js engine.

packages/core/src/config/ButtonConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface CommandButtonAction {
2323
export interface JSButtonAction {
2424
type: ButtonActionType.JS;
2525
file: string;
26+
args?: Record<string, unknown>;
2627
}
2728

2829
export interface OpenButtonAction {

packages/core/src/config/ButtonConfigValidators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const V_JSButtonAction = schemaForType<JSButtonAction>()(
2525
z.object({
2626
type: z.literal(ButtonActionType.JS),
2727
file: z.string(),
28+
args: z.record(z.unknown()).optional(),
2829
}),
2930
);
3031

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ export class ButtonActionRunner {
3939
* Run the action(s) defined in the button config.
4040
* Will show a notice if an error occurs.
4141
*
42-
* @param buttonConfig
42+
* @param config
4343
* @param filePath
4444
*/
45-
async runButtonAction(buttonConfig: ButtonConfig, filePath: string): Promise<void> {
45+
async runButtonAction(config: ButtonConfig, filePath: string): Promise<void> {
4646
try {
47-
if (buttonConfig.action) {
48-
await this.plugin.api.buttonActionRunner.runAction(buttonConfig.action, filePath);
49-
} else if (buttonConfig.actions) {
50-
for (const action of buttonConfig.actions) {
51-
await this.plugin.api.buttonActionRunner.runAction(action, filePath);
47+
if (config.action) {
48+
await this.plugin.api.buttonActionRunner.runAction(config, config.action, filePath);
49+
} else if (config.actions) {
50+
for (const action of config.actions) {
51+
await this.plugin.api.buttonActionRunner.runAction(config, action, filePath);
5252
}
5353
} else {
5454
console.warn('meta-bind | ButtonMDRC >> no action defined');
@@ -101,15 +101,16 @@ export class ButtonActionRunner {
101101
* Run a specific button action.
102102
* Will throw.
103103
*
104+
* @param config
104105
* @param action
105106
* @param filePath
106107
*/
107-
async runAction(action: ButtonAction, filePath: string): Promise<void> {
108+
async runAction(config: ButtonConfig, action: ButtonAction, filePath: string): Promise<void> {
108109
if (action.type === ButtonActionType.COMMAND) {
109110
await this.runCommandAction(action);
110111
return;
111112
} else if (action.type === ButtonActionType.JS) {
112-
await this.runJSAction(action, filePath);
113+
await this.runJSAction(config, action, filePath);
113114
return;
114115
} else if (action.type === ButtonActionType.OPEN) {
115116
await this.runOpenAction(action, filePath);
@@ -135,8 +136,12 @@ export class ButtonActionRunner {
135136
this.plugin.internal.executeCommandById(action.command);
136137
}
137138

138-
async runJSAction(action: JSButtonAction, filePath: string): Promise<void> {
139-
const unloadCallback = await this.plugin.internal.jsEngineRunFile(action.file, filePath);
139+
async runJSAction(config: ButtonConfig, action: JSButtonAction, filePath: string): Promise<void> {
140+
const configOverrides: Record<string, unknown> = {
141+
buttonConfig: config,
142+
args: action.args,
143+
};
144+
const unloadCallback = await this.plugin.internal.jsEngineRunFile(action.file, filePath, configOverrides);
140145
unloadCallback();
141146
}
142147

packages/obsidian/src/ObsidianInternalAPI.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class ObsidianInternalAPI extends InternalAPI<MetaBindPlugin> {
7070
public async jsEngineRunFile(
7171
filePath: string,
7272
callingFilePath: string,
73+
contextOverrides: Record<string, unknown>,
7374
container?: HTMLElement,
7475
): Promise<() => void> {
7576
const jsEngineAPI = getJsEnginePluginAPI(this.plugin);
@@ -89,6 +90,7 @@ export class ObsidianInternalAPI extends InternalAPI<MetaBindPlugin> {
8990
file: callingFile,
9091
line: 0,
9192
},
93+
contextOverrides: contextOverrides,
9294
});
9395

9496
return () => component.unload();

packages/publish/src/PublishInternalAPI.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export class PublishInternalAPI extends InternalAPI<MetaBindPublishPlugin> {
2828
return false;
2929
}
3030

31-
public jsEngineRunFile(_filePath: string, _callingFilePath: string, _container?: HTMLElement): Promise<() => void> {
31+
public jsEngineRunFile(
32+
_filePath: string,
33+
_callingFilePath: string,
34+
_configOverrides: Record<string, unknown>,
35+
_container?: HTMLElement,
36+
): Promise<() => void> {
3237
return Promise.reject(new Error('not implemented'));
3338
}
3439

tests/__mocks__/TestInternalAPI.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export class TestInternalAPI extends InternalAPI<TestPlugin> {
2828
return false;
2929
}
3030

31-
public jsEngineRunFile(_filePath: string, _callingFilePath: string, _container?: HTMLElement): Promise<() => void> {
31+
public jsEngineRunFile(
32+
_filePath: string,
33+
_callingFilePath: string,
34+
_contextOverrides: Record<string, unknown>,
35+
_container?: HTMLElement,
36+
): Promise<() => void> {
3237
return Promise.resolve(() => {});
3338
}
3439

0 commit comments

Comments
 (0)