Skip to content

Commit b447776

Browse files
committed
add file runner and more comments
1 parent 97c8eac commit b447776

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

jsEngine/engine/JsExecution.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ export class JsExecution {
156156
try {
157157
this.result = await Promise.resolve(this.func(...Object.values(this.globals)));
158158
} catch (e) {
159+
console.warn('failed to execute JS', e);
160+
159161
if (e instanceof Error) {
160162
this.functionRunError = e;
161163

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {FuzzySuggestModal, TFile} from 'obsidian';
2+
import JsEnginePlugin from '../main';
3+
4+
export class JSFileSelectModal extends FuzzySuggestModal<TFile> {
5+
plugin: JsEnginePlugin;
6+
selectCallback: (selected: TFile) => void;
7+
8+
constructor(plugin: JsEnginePlugin, selectCallback: (selected: TFile) => void) {
9+
super(plugin.app);
10+
11+
this.plugin = plugin;
12+
this.selectCallback = selectCallback;
13+
}
14+
15+
getItems(): TFile[] {
16+
const allFiles = this.app.vault.getAllLoadedFiles().filter(file => file instanceof TFile) as TFile[];
17+
return allFiles.filter(file => file.extension === 'js');
18+
}
19+
20+
getItemText(item: TFile): string {
21+
return item.path;
22+
}
23+
24+
public onChooseItem(item: TFile, _evt: MouseEvent | KeyboardEvent): void {
25+
this.selectCallback(item);
26+
}
27+
}

jsEngine/main.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type App, Plugin, type PluginManifest } from 'obsidian';
1+
import {type App, Component, 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';
@@ -7,6 +7,7 @@ import { MessageManager } from './messages/MessageManager';
77
import { InstanceId, InstanceType } from './api/InstanceId';
88
import { Engine } from './engine/Engine';
99
import { javascript } from '@codemirror/legacy-modes/mode/javascript';
10+
import {JSFileSelectModal} from './fileRunner/JSFileSelectModal';
1011

1112
export default class JsEnginePlugin extends Plugin {
1213
settings: JsEnginePluginSettings | undefined;
@@ -34,13 +35,27 @@ export default class JsEnginePlugin extends Plugin {
3435
ctx.addChild(mdrc);
3536
});
3637

37-
await this.registerCodeMirrorMode();
38+
this.addCommand({
39+
id: 'execute-js-file',
40+
name: 'Execute JS File',
41+
callback: () => {
42+
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+
}
54+
}).open()
55+
}
56+
})
3857

39-
// this.registerView(JS_EDITOR_VIEW_TYPE, (leaf) => {
40-
// return new JsEditor(leaf);
41-
// })
42-
//
43-
// this.registerExtensions(['js'], JS_EDITOR_VIEW_TYPE);
58+
await this.registerCodeMirrorMode();
4459
}
4560

4661
onunload(): void {}

0 commit comments

Comments
 (0)