Skip to content

Commit 5f3d591

Browse files
committed
add internal api
1 parent af8c6aa commit 5f3d591

File tree

5 files changed

+73
-63
lines changed

5 files changed

+73
-63
lines changed

exampleVault/Test.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
text: taasd
2+
text: asdadasd
33
number: 12234234
44
---
55

@@ -52,6 +52,11 @@ let lib = await engine.importJs("lib.js");
5252
return lib.getGreeting();
5353
```
5454

55+
```js-engine
56+
let lib = await self.require.import("lib.js");
57+
return lib.getGreeting();
58+
```
59+
5560
```js-engine
5661
let lib = await engine.importJs("svelte_project/dist/assets/index-9a060cab.js");
5762
console.log(lib);

src/JsMDRC.ts

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,63 +48,24 @@ export class JsMDRC extends MarkdownRenderChild {
4848
};
4949
}
5050

51-
buildExecutionArgs(container: HTMLElement): ExecutionArgument[] {
52-
return [
53-
{
54-
key: 'component',
55-
value: this,
56-
},
57-
{
58-
key: 'container',
59-
value: container,
60-
},
61-
];
62-
}
63-
64-
async tryRun(args: ExecutionArgument[], context: ExecutionContext): Promise<JsExecution> {
65-
return this.plugin.jsEngine.execute(this.content, args, context);
51+
async tryRun(context: ExecutionContext): Promise<JsExecution> {
52+
return this.plugin.jsEngine.execute({
53+
code: this.content,
54+
context: context,
55+
contextOverrides: {},
56+
component: this,
57+
container: this.containerEl,
58+
});
6659
}
6760

6861
async renderResults(container: HTMLElement): Promise<void> {
69-
const args = this.buildExecutionArgs(container);
7062
const context = this.buildExecutionContext();
71-
this.jsExecution = await this.tryRun(args, context);
72-
let result = this.jsExecution.result;
73-
74-
if (!result) {
75-
return;
76-
}
77-
78-
if (typeof result === 'string') {
79-
container.innerText = result;
80-
return;
81-
}
8263

83-
if (result instanceof MarkdownBuilder) {
84-
result = result.toMarkdown();
85-
}
86-
87-
if (result instanceof MarkdownString) {
88-
console.log(result.content);
89-
await result.render(container, this.ctx.sourcePath, this);
90-
return;
91-
}
64+
this.jsExecution = await this.tryRun(context);
65+
const result = this.jsExecution.result;
9266

93-
if (result instanceof HTMLElement) {
94-
container.append(result);
95-
}
96-
97-
if (result instanceof MessageWrapper) {
98-
new MessageComponent({
99-
target: container,
100-
props: {
101-
messageWrapper: result,
102-
messageManager: this.plugin.messageManager,
103-
showDeleteButton: false,
104-
showMessageSource: false,
105-
},
106-
});
107-
}
67+
const renderer = new ResultRenderer(this.plugin, container, this.ctx.sourcePath, this);
68+
await renderer.render(result);
10869
}
10970

11071
renderExecutionStats(container: HTMLElement): void {

src/api/Internal.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { API } from "./API";
2+
import { EngineExecutionParams } from "../jsEngine/JsEngine";
3+
import { JsExecution } from "../jsEngine/JsExecution";
4+
import { Component } from "obsidian";
5+
import { ResultRenderer } from "../ResultRenderer";
6+
7+
/**
8+
* The internal API provides access to some of js engines internals.
9+
*/
10+
export class InternalAPI {
11+
private readonly apiInstance: API;
12+
13+
constructor(apiInstance: API) {
14+
this.apiInstance = apiInstance;
15+
}
16+
17+
public async excute(params: EngineExecutionParams): Promise<JsExecution> {
18+
return await this.apiInstance.plugin.jsEngine.execute(params);
19+
}
20+
21+
public createRenderer(container: HTMLElement, sourcePath: string, component: Component): ResultRenderer {
22+
return new ResultRenderer(this.apiInstance.plugin, container, sourcePath, component);
23+
}
24+
}

src/jsEngine/JsEngine.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import JsEnginePlugin from '../main';
22
import { App } from 'obsidian';
33
import { ExecutionArgument, ExecutionContext } from '../ArgumentManager';
4-
import { JsExecution } from './JsExecution';
4+
import { JsExecution, JsExecutionParams } from "./JsExecution";
55
import { ExecutionStatsModal } from './ExecutionStatsModal';
66

7+
export type EngineExecutionParams = Omit<JsExecutionParams, 'app' | 'plugin'>
8+
79
export class JsEngine {
810
private readonly app: App;
911
private readonly plugin: JsEnginePlugin;
@@ -19,8 +21,8 @@ export class JsEngine {
1921
this.activeExecutions = new Map<string, JsExecution>();
2022
}
2123

22-
async execute(code: string, args: ExecutionArgument[], context: ExecutionContext): Promise<JsExecution> {
23-
const execution = new JsExecution(this.app, this.plugin, code, args, context);
24+
async execute(params: EngineExecutionParams): Promise<JsExecution> {
25+
const execution = new JsExecution({app: this.app, plugin: this.plugin, ...params});
2426
this.activeExecutions.set(execution.uuid, execution);
2527

2628
execution.buildFunction();

src/jsEngine/JsExecution.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
import { App } from 'obsidian';
1+
import { App, Component } from "obsidian";
22
import JsEnginePlugin from '../main';
33
import { ExecutionArgument, ExecutionContext } from '../ArgumentManager';
44
import { MessageType, MessageWrapper } from '../messages/MessageManager';
55
import { API } from '../api/API';
66
import { InstanceId, InstanceType } from '../api/InstanceId';
7+
import { Obj } from "tern";
78

89
const AsyncFunction = async function (): Promise<void> {}.constructor;
910

11+
export interface JsExecutionParams {
12+
app: App;
13+
plugin: JsEnginePlugin;
14+
code: string;
15+
component: Component;
16+
container?: HTMLElement | undefined;
17+
context?: ExecutionContext | undefined;
18+
contextOverrides?: Record<string, unknown> | undefined;
19+
}
20+
1021
export class JsExecution {
1122
readonly app: App;
1223
readonly plugin: JsEnginePlugin;
1324

1425
uuid: string;
1526
code: string;
1627
args: ExecutionArgument[];
17-
context: ExecutionContext | undefined;
28+
context: ExecutionContext & Record<string, unknown>;
1829
apiInstance: API;
1930
messages: MessageWrapper[];
2031

@@ -27,12 +38,12 @@ export class JsExecution {
2738
functionBuildTime: number | undefined;
2839
functionRunTime: number | undefined;
2940

30-
constructor(app: App, plugin: JsEnginePlugin, code: string, args: ExecutionArgument[], context?: ExecutionContext) {
31-
this.app = app;
32-
this.plugin = plugin;
41+
constructor(params: JsExecutionParams) {
42+
this.app = params.app;
43+
this.plugin = params.plugin;
3344

34-
this.code = code;
35-
this.context = context;
45+
this.code = params.code;
46+
this.context = Object.assign({}, params.context, params.contextOverrides);
3647

3748
this.uuid = self.crypto.randomUUID();
3849
this.apiInstance = new API(this.app, this.plugin, new InstanceId(InstanceType.JS_EXECUTION, this.uuid));
@@ -53,7 +64,14 @@ export class JsExecution {
5364
key: 'context',
5465
value: this.context,
5566
},
56-
...args,
67+
{
68+
key: 'component',
69+
value: params.component,
70+
},
71+
{
72+
key: 'container',
73+
value: params.container,
74+
},
5775
];
5876
}
5977

0 commit comments

Comments
 (0)