Skip to content

Commit

Permalink
✨ feat(api): 添加 run-js-code 的 api
Browse files Browse the repository at this point in the history
  • Loading branch information
frostime committed Nov 13, 2023
1 parent cce2c07 commit d002c4a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
26 changes: 24 additions & 2 deletions README.md
Expand Up @@ -53,6 +53,13 @@
public runCodeBlock(id: BlockId)
```

- `runJsCode`

```ts
public async runJsCode(code: string)
```


- `createRunButton`

```ts
Expand Down Expand Up @@ -134,13 +141,28 @@ main();
At runtime, `'parameter1'` and `'parameter2'` will be combined into the `args` array.
## Developer
## EventBus
Expose a custom event to global.
The plugin extends `eventBus` to add events that can be called externally.
```ts
interface MyEventBusMap extends IEventBusMap {
'run-code-block': BlockId;
'run-js-code': string;
}
```
1. `run-code-block`, input parameter is BlockID, run the js code block with specified ID.
2. `run-js-code`, input parameter is a js code string, run the specified code.
```ts
let bus = window.siyuan.ws.app.plugins.find(p => p.name === 'sy-run-js')?.eventBus;
if (bus) {
bus.emit("run-code-block", blockID);
bus.emit("run-js-code", `
console.log("Hello world");
`);
}
```
> But you can actually call the `plugin.runCodeBlock` and `plugin.runJsCode` functions directly without using eventBus.
25 changes: 23 additions & 2 deletions README_zh_CN.md
Expand Up @@ -52,6 +52,12 @@
public runCodeBlock(id: BlockId)
```

- `runJsCode`

```ts
public async runJsCode(code: string)
```

- `createRunButton`

```ts
Expand Down Expand Up @@ -133,13 +139,28 @@ main();
运行时 '参数1', '参数2' 将会组成 `args` 数组。
## 开发者
## EventBus
插件拓展了 `eventBus`,增加了外部可以调用的事件
插件对外暴露 `eventBus` 类型 `run-code-block`, 输入参数为 BlockID.
```ts
interface MyEventBusMap extends IEventBusMap {
'run-code-block': BlockId;
'run-js-code': string;
}
```
1. `run-code-block`, 输入参数为 BlockID, 运行指定 ID 的 js 代码块
2. `run-js-code`, 输入参数为 js 代码字符串,运行指定的代码
```ts
let bus = window.siyuan.ws.app.plugins.find(p => p.name === 'sy-run-js')?.eventBus;
if (bus) {
bus.emit("run-code-block", blockID);
bus.emit("run-js-code", `
console.log("Hello world");
`);
}
```
> 但是实际上你也可以直接调用 `plugin.runCodeBlock``plugin.runJsCode` 函数而不必使用 eventBus.
2 changes: 1 addition & 1 deletion plugin.json
Expand Up @@ -2,7 +2,7 @@
"name": "sy-run-js",
"author": "frostime",
"url": "https://github.com/frostime/sy-run-js",
"version": "0.4.1",
"version": "0.4.2",
"minAppVersion": "2.10.5",
"backends": ["all"],
"frontends": ["all"],
Expand Down
33 changes: 29 additions & 4 deletions src/index.ts
Expand Up @@ -3,7 +3,7 @@
* @Author : Yp Z
* @Date : 2023-08-14 18:01:15
* @FilePath : /src/index.ts
* @LastEditTime : 2023-10-03 19:17:26
* @LastEditTime : 2023-11-13 19:48:03
* @Description :
*/
import {
Expand All @@ -12,6 +12,8 @@ import {
getFrontend,
openTab,
Menu,
EventBus,
IEventBusMap,
IMenuItemOption
} from "siyuan";
import siyuan from "siyuan";
Expand All @@ -20,14 +22,25 @@ import "@/index.scss";
import * as api from "@/api";

import { Client } from "@siyuan-community/siyuan-sdk";
import { CANCELLED } from "dns";

const client = new Client({
//@ts-ignore
token: window.siyuan.config.api.token
});


interface MyEventBusMap extends IEventBusMap {
'run-code-block': string;
'run-js-code': string;
}
type MyEventBus = EventBus & {
on<
K extends keyof MyEventBusMap,
D = MyEventBusMap[K],
>(type: K, listener: (event: CustomEvent<D>) => any): void;
}


const SAVED_CODE = "SavedCode.json";
const CALLABLE = "Callable.json";

Expand Down Expand Up @@ -103,6 +116,7 @@ export default class RunJsPlugin extends Plugin {
SAVE_CODE: { [key: string]: IAction[] }
CALLABLE: { [key: string]: BlockId }
};
declare eventBus: MyEventBus;

async onload() {
this.addIcons(`<symbol id="iconJS" viewBox="0 0 1024 1024"><path d="M640 128H576v256h64V128zM832 320h-192v64h192V320zM896 896H128v64h768v-64z" p-id="4062"></path><path d="M640 64H128v128h64V128h421.76L832 346.24V960h64V320l-256-256zM256 384H192v349.44q0 42.24-34.56 42.24h-19.84V832h28.16Q256 832 256 736V384z" p-id="4063"></path><path d="M448 384a131.84 131.84 0 0 0-87.04 28.16 94.72 94.72 0 0 0-33.28 77.44 87.68 87.68 0 0 0 34.56 73.6 208.64 208.64 0 0 0 73.6 31.36 256 256 0 0 1 59.52 21.12 45.44 45.44 0 0 1 26.24 41.6c0 33.28-23.68 49.28-71.04 49.28a71.04 71.04 0 0 1-49.28-14.08 88.96 88.96 0 0 1-21.76-52.48H320a120.96 120.96 0 0 0 132.48 128c87.68 0 131.84-38.4 131.84-115.84A89.6 89.6 0 0 0 549.12 576a225.28 225.28 0 0 0-75.52-33.92 391.68 391.68 0 0 1-60.16-22.4 37.76 37.76 0 0 1-23.68-32 35.84 35.84 0 0 1 16-32.64A69.76 69.76 0 0 1 448 448a70.4 70.4 0 0 1 46.72 12.8 72.32 72.32 0 0 1 21.76 40.32H576A113.28 113.28 0 0 0 448 384zM224 256a32 32 0 1 0 32 32 32 32 0 0 0-32-32z" p-id="4064"></path></symbol>`)
Expand Down Expand Up @@ -136,6 +150,9 @@ export default class RunJsPlugin extends Plugin {
this.eventBus.on("run-code-block", ({ detail }) => {
this.runCodeBlock(detail);
});
this.eventBus.on("run-js-code", ({detail}) => {
this.runJsCode(detail);
});

await Promise.all([this.loadData(SAVED_CODE), this.loadData(CALLABLE)]);
this.data[SAVED_CODE] = this.data[SAVED_CODE] || {};
Expand Down Expand Up @@ -300,12 +317,20 @@ export default class RunJsPlugin extends Plugin {
let code = block.content;
console.log('Code Block:', block.id);
console.log(code);
this.runJsCode(code);
console.groupEnd();
}

/**
* 运行指定的代码
* @param code string, 代码字符串
*/
public async runJsCode(code: string) {
let func = new Function(
'siyuan', 'client', 'api', 'plugin', 'thisBlock',
code
);
func(siyuan, client, api, this, block);
console.groupEnd();
return func(siyuan, client, api, this, null);
}

private showTopbarMenu(rect?: DOMRect) {
Expand Down

0 comments on commit d002c4a

Please sign in to comment.