Skip to content

Commit e75a1e6

Browse files
committed
small changes
1 parent f35f63b commit e75a1e6

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

jsEngine/JsMDRC.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export class JsMDRC extends MarkdownRenderChild {
3535
}
3636

3737
buildExecutionContext(): JsExecutionContext {
38-
console.log(this.ctx);
38+
// console.log(this.ctx);
3939
const file = this.getExecutionFile()!;
4040
return {
4141
file: file,
4242
metadata: this.plugin.app.metadataCache.getFileCache(file),
43-
line: 0,
43+
block: undefined,
4444
};
4545
}
4646

@@ -108,12 +108,12 @@ export class JsMDRC extends MarkdownRenderChild {
108108
}
109109

110110
async onload(): Promise<void> {
111-
console.log('load');
111+
// console.log('load');
112112

113113
await this.render();
114114
}
115115

116116
onunload(): void {
117-
console.log('unload');
117+
// console.log('unload');
118118
}
119119
}

jsEngine/api/Internal.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type API } from './API';
22
import { type EngineExecutionParams } from '../engine/Engine';
3-
import { type JsExecution } from '../engine/JsExecution';
4-
import { type Component, TFile } from 'obsidian';
3+
import { type JsExecution, type JsExecutionContext } from '../engine/JsExecution';
4+
import { Component, TFile } from 'obsidian';
55
import { ResultRenderer } from '../engine/ResultRenderer';
66

77
/**
@@ -49,4 +49,37 @@ export class InternalAPI {
4949
fullParams.code = await this.apiInstance.app.vault.read(file);
5050
return await this.execute(fullParams);
5151
}
52+
53+
/**
54+
* Lead and execute the given file.
55+
* This method also handles the lifetime of the execution.
56+
* The component for the execution is created and destroyed automatically.
57+
*
58+
* @param path
59+
* @param params
60+
*/
61+
public async executeFileSimple(path: string, params?: Omit<EngineExecutionParams, 'code' | 'component'>): Promise<JsExecution> {
62+
const component = new Component();
63+
component.load();
64+
try {
65+
return await this.executeFile(path, { component: component, ...params });
66+
} finally {
67+
component.unload();
68+
}
69+
}
70+
71+
public async getContextForFile(path: string): Promise<JsExecutionContext> {
72+
const file = this.apiInstance.app.vault.getAbstractFileByPath(path);
73+
if (!file || !(file instanceof TFile)) {
74+
throw new Error(`File ${path} not found.`);
75+
}
76+
77+
const metadata = this.apiInstance.app.metadataCache.getFileCache(file);
78+
79+
return {
80+
file: file,
81+
metadata: metadata,
82+
block: undefined,
83+
};
84+
}
5285
}

jsEngine/engine/ExecutionStatsComponent.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
</tr>
2929
</thead>
3030
<tbody>
31-
{#each execution.globals as arg (arg.key)}
31+
{#each Object.entries(execution.globals) as arg (arg[0])}
3232
<tr>
33-
<td>{arg.key}</td>
34-
<td>{typeof arg.value}</td>
33+
<td>{arg[0]}</td>
34+
<td>{typeof arg[1]}</td>
3535
</tr>
3636
{:else}
3737
<tr>

jsEngine/engine/JsExecution.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export interface JsExecutionContext {
2828
/**
2929
* Currently unused.
3030
*/
31-
line: number;
31+
block: Block | undefined;
32+
}
33+
34+
export interface Block {
35+
from: number;
36+
to: number;
3237
}
3338

3439
/**

jsEngine/engine/ResultRenderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class ResultRenderer {
4545
}
4646

4747
if (value instanceof MarkdownString) {
48-
console.log(value.content);
48+
console.log('js-engine | Rendering Markdown String\n', value.content);
4949
await value.render(this.plugin.app, this.container, this.sourcePath, this.component);
5050
return;
5151
}

jsEngine/main.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type App, Component, Plugin, type PluginManifest } from 'obsidian';
1+
import { type App, Plugin, type PluginManifest } from 'obsidian';
22
import { JS_ENGINE_DEFAULT_SETTINGS, type JsEnginePluginSettings } from './Settings';
33
import { type Mode } from 'codemirror';
44
import { JsMDRC } from './JsMDRC';
@@ -40,17 +40,7 @@ export default class JsEnginePlugin extends Plugin {
4040
name: 'Execute JS File',
4141
callback: () => {
4242
new JSFileSelectModal(this, async selected => {
43-
const component = new Component();
44-
component.load();
45-
try {
46-
await this.api.internal.executeFile(selected.path, {
47-
component: component,
48-
});
49-
} catch (e) {
50-
console.warn(e);
51-
} finally {
52-
component.unload();
53-
}
43+
await this.api.internal.executeFileSimple(selected.path);
5444
}).open();
5545
},
5646
});

0 commit comments

Comments
 (0)