Skip to content

Commit d002c4a

Browse files
committedNov 13, 2023
✨ feat(api): 添加 run-js-code 的 api
·
v0.7.4v0.4.2
1 parent cce2c07 commit d002c4a

File tree

4 files changed

+77
-9
lines changed

4 files changed

+77
-9
lines changed
 

‎README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@
5353
public runCodeBlock(id: BlockId)
5454
```
5555

56+
- `runJsCode`
57+
58+
```ts
59+
public async runJsCode(code: string)
60+
```
61+
62+
5663
- `createRunButton`
5764

5865
```ts
@@ -134,13 +141,28 @@ main();
134141
135142
At runtime, `'parameter1'` and `'parameter2'` will be combined into the `args` array.
136143
137-
## Developer
144+
## EventBus
138145
139-
Expose a custom event to global.
146+
The plugin extends `eventBus` to add events that can be called externally.
147+
148+
```ts
149+
interface MyEventBusMap extends IEventBusMap {
150+
'run-code-block': BlockId;
151+
'run-js-code': string;
152+
}
153+
```
154+
155+
1. `run-code-block`, input parameter is BlockID, run the js code block with specified ID.
156+
2. `run-js-code`, input parameter is a js code string, run the specified code.
140157
141158
```ts
142159
let bus = window.siyuan.ws.app.plugins.find(p => p.name === 'sy-run-js')?.eventBus;
143160
if (bus) {
144161
bus.emit("run-code-block", blockID);
162+
bus.emit("run-js-code", `
163+
console.log("Hello world");
164+
`);
145165
}
146166
```
167+
168+
> But you can actually call the `plugin.runCodeBlock` and `plugin.runJsCode` functions directly without using eventBus.

‎README_zh_CN.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
public runCodeBlock(id: BlockId)
5353
```
5454

55+
- `runJsCode`
56+
57+
```ts
58+
public async runJsCode(code: string)
59+
```
60+
5561
- `createRunButton`
5662

5763
```ts
@@ -133,13 +139,28 @@ main();
133139
运行时 '参数1', '参数2' 将会组成 `args` 数组。
134140
135141
136-
## 开发者
142+
## EventBus
143+
144+
插件拓展了 `eventBus`,增加了外部可以调用的事件
137145
138-
插件对外暴露 `eventBus` 类型 `run-code-block`, 输入参数为 BlockID.
146+
```ts
147+
interface MyEventBusMap extends IEventBusMap {
148+
'run-code-block': BlockId;
149+
'run-js-code': string;
150+
}
151+
```
152+
153+
1. `run-code-block`, 输入参数为 BlockID, 运行指定 ID 的 js 代码块
154+
2. `run-js-code`, 输入参数为 js 代码字符串,运行指定的代码
139155
140156
```ts
141157
let bus = window.siyuan.ws.app.plugins.find(p => p.name === 'sy-run-js')?.eventBus;
142158
if (bus) {
143159
bus.emit("run-code-block", blockID);
160+
bus.emit("run-js-code", `
161+
console.log("Hello world");
162+
`);
144163
}
145164
```
165+
166+
> 但是实际上你也可以直接调用 `plugin.runCodeBlock``plugin.runJsCode` 函数而不必使用 eventBus.

‎plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sy-run-js",
33
"author": "frostime",
44
"url": "https://github.com/frostime/sy-run-js",
5-
"version": "0.4.1",
5+
"version": "0.4.2",
66
"minAppVersion": "2.10.5",
77
"backends": ["all"],
88
"frontends": ["all"],

‎src/index.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @Author : Yp Z
44
* @Date : 2023-08-14 18:01:15
55
* @FilePath : /src/index.ts
6-
* @LastEditTime : 2023-10-03 19:17:26
6+
* @LastEditTime : 2023-11-13 19:48:03
77
* @Description :
88
*/
99
import {
@@ -12,6 +12,8 @@ import {
1212
getFrontend,
1313
openTab,
1414
Menu,
15+
EventBus,
16+
IEventBusMap,
1517
IMenuItemOption
1618
} from "siyuan";
1719
import siyuan from "siyuan";
@@ -20,14 +22,25 @@ import "@/index.scss";
2022
import * as api from "@/api";
2123

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

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

3031

32+
interface MyEventBusMap extends IEventBusMap {
33+
'run-code-block': string;
34+
'run-js-code': string;
35+
}
36+
type MyEventBus = EventBus & {
37+
on<
38+
K extends keyof MyEventBusMap,
39+
D = MyEventBusMap[K],
40+
>(type: K, listener: (event: CustomEvent<D>) => any): void;
41+
}
42+
43+
3144
const SAVED_CODE = "SavedCode.json";
3245
const CALLABLE = "Callable.json";
3346

@@ -103,6 +116,7 @@ export default class RunJsPlugin extends Plugin {
103116
SAVE_CODE: { [key: string]: IAction[] }
104117
CALLABLE: { [key: string]: BlockId }
105118
};
119+
declare eventBus: MyEventBus;
106120

107121
async onload() {
108122
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>`)
@@ -136,6 +150,9 @@ export default class RunJsPlugin extends Plugin {
136150
this.eventBus.on("run-code-block", ({ detail }) => {
137151
this.runCodeBlock(detail);
138152
});
153+
this.eventBus.on("run-js-code", ({detail}) => {
154+
this.runJsCode(detail);
155+
});
139156

140157
await Promise.all([this.loadData(SAVED_CODE), this.loadData(CALLABLE)]);
141158
this.data[SAVED_CODE] = this.data[SAVED_CODE] || {};
@@ -300,12 +317,20 @@ export default class RunJsPlugin extends Plugin {
300317
let code = block.content;
301318
console.log('Code Block:', block.id);
302319
console.log(code);
320+
this.runJsCode(code);
321+
console.groupEnd();
322+
}
323+
324+
/**
325+
* 运行指定的代码
326+
* @param code string, 代码字符串
327+
*/
328+
public async runJsCode(code: string) {
303329
let func = new Function(
304330
'siyuan', 'client', 'api', 'plugin', 'thisBlock',
305331
code
306332
);
307-
func(siyuan, client, api, this, block);
308-
console.groupEnd();
333+
return func(siyuan, client, api, this, null);
309334
}
310335

311336
private showTopbarMenu(rect?: DOMRect) {

0 commit comments

Comments
 (0)
Please sign in to comment.