nexfep 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-CN.md +28 -29
- package/README.md +28 -29
- package/frontend/index.d.ts +21 -0
- package/package.json +8 -3
- package/src/WindowManager.d.ts +1 -1
- package/src/WindowManager.js +5 -10
package/README-CN.md
CHANGED
|
@@ -240,35 +240,6 @@ window.setSize(800, 600);
|
|
|
240
240
|
window.openDevTools();
|
|
241
241
|
```
|
|
242
242
|
|
|
243
|
-
### 自定义消息
|
|
244
|
-
|
|
245
|
-
#### 发送消息
|
|
246
|
-
|
|
247
|
-
在页面中通过 `window.postMessage` 发送消息:
|
|
248
|
-
|
|
249
|
-
```javascript
|
|
250
|
-
window.postMessage({ hello: "world" });
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
**参数说明**
|
|
254
|
-
|
|
255
|
-
- `data` — 任意类型的可序列化数据,会被序列化为 JSON 字符串后发送
|
|
256
|
-
|
|
257
|
-
#### 监听消息
|
|
258
|
-
|
|
259
|
-
在主进程中通过 `onCustomMessage` 回调接收消息:
|
|
260
|
-
|
|
261
|
-
```typescript
|
|
262
|
-
pool.onCustomMessage = (window, data) => {
|
|
263
|
-
console.log(`来自窗口 ${window.id} 的消息:`, data);
|
|
264
|
-
};
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
**回调参数**
|
|
268
|
-
|
|
269
|
-
- `window` — 发送消息的窗口对象
|
|
270
|
-
- `data` — 消息内容,为对象类型(JSON 序列化后会自动通过 `JSON.parse` 转换为对象)
|
|
271
|
-
|
|
272
243
|
### 自定义事件
|
|
273
244
|
|
|
274
245
|
#### 触发事件
|
|
@@ -405,6 +376,34 @@ window.addEventListener("custom-message", (event) => {
|
|
|
405
376
|
console.log("当前窗口 ID:", window.id);
|
|
406
377
|
```
|
|
407
378
|
|
|
379
|
+
### 自定义消息
|
|
380
|
+
|
|
381
|
+
#### 发送消息
|
|
382
|
+
|
|
383
|
+
在页面中通过 `window.tell` 并指定 `to` 为 `0` 表示向主进程发送消息:
|
|
384
|
+
|
|
385
|
+
```javascript
|
|
386
|
+
window.tell(0, "custom-message", { hello: "world" });
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
#### 监听消息
|
|
390
|
+
|
|
391
|
+
在主进程中通过 `onCustomMessage` 回调接收消息:
|
|
392
|
+
|
|
393
|
+
```typescript
|
|
394
|
+
pool.onCustomMessage = (window, message, data) => {
|
|
395
|
+
console.log(`[${message}]来自窗口 ${window.id} 的消息:`, data);
|
|
396
|
+
};
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
**回调参数**
|
|
400
|
+
|
|
401
|
+
- `window` — 发送消息的窗口对象
|
|
402
|
+
- `message` — 事件名称
|
|
403
|
+
- `data` — 消息内容,为对象类型(JSON 序列化后会自动通过 `JSON.parse` 转换为对象)
|
|
404
|
+
|
|
405
|
+
我们更推荐使用 `window.invoke` 使页面与主进程通信,而非 `window.tell`。
|
|
406
|
+
|
|
408
407
|
### 窗口控制函数
|
|
409
408
|
|
|
410
409
|
页面中可直接调用以下注入函数进行窗口控制:
|
package/README.md
CHANGED
|
@@ -238,35 +238,6 @@ window.setSize(800, 600);
|
|
|
238
238
|
window.openDevTools();
|
|
239
239
|
```
|
|
240
240
|
|
|
241
|
-
### Custom Messages
|
|
242
|
-
|
|
243
|
-
#### Send Messages
|
|
244
|
-
|
|
245
|
-
Send messages via `window.postMessage` in the page:
|
|
246
|
-
|
|
247
|
-
```javascript
|
|
248
|
-
window.postMessage({ hello: "world" });
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
**Parameters**
|
|
252
|
-
|
|
253
|
-
- `data` — Any serializable data, will be serialized to JSON string before sending
|
|
254
|
-
|
|
255
|
-
#### Listen for Messages
|
|
256
|
-
|
|
257
|
-
Receive messages via `onCustomMessage` callback in the main process:
|
|
258
|
-
|
|
259
|
-
```typescript
|
|
260
|
-
pool.onCustomMessage = (window, data) => {
|
|
261
|
-
console.log(`Message from window ${window.id}:`, data);
|
|
262
|
-
};
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
**Callback Parameters**
|
|
266
|
-
|
|
267
|
-
- `window` — The window object that sent the message
|
|
268
|
-
- `data` — Message content, an object type (automatically converted from JSON string via `JSON.parse`)
|
|
269
|
-
|
|
270
241
|
### Custom Events
|
|
271
242
|
|
|
272
243
|
#### Invoke Events
|
|
@@ -403,6 +374,34 @@ Each window's ID can be accessed via `window.id`:
|
|
|
403
374
|
console.log("This window ID:", window.id);
|
|
404
375
|
```
|
|
405
376
|
|
|
377
|
+
### Custom Messages
|
|
378
|
+
|
|
379
|
+
#### Send Messages
|
|
380
|
+
|
|
381
|
+
In the page, use `window.tell` with `to` 0 to send a message to the main process.
|
|
382
|
+
|
|
383
|
+
```javascript
|
|
384
|
+
window.tell(0, "custom-message", { hello: "world" });
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
#### Listen for Messages
|
|
388
|
+
|
|
389
|
+
Receive messages via `onCustomMessage` callback in the main process:
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
pool.onCustomMessage = (window, message, data) => {
|
|
393
|
+
console.log(`[${message}] from window ${window.id}:`, data);
|
|
394
|
+
};
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
**Callback Parameters**
|
|
398
|
+
|
|
399
|
+
- `window` — The window object that sent the message
|
|
400
|
+
- `message` — Event name
|
|
401
|
+
- `data` — Message content, an object type (automatically converted from JSON string via `JSON.parse`)
|
|
402
|
+
|
|
403
|
+
We recommend using `window.invoke` to communicate between pages and the main process, instead of `window.tell`.
|
|
404
|
+
|
|
406
405
|
### Window Control Functions
|
|
407
406
|
|
|
408
407
|
The following injected functions can be directly called in the page for window control:
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export {}
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface Window {
|
|
5
|
+
tell: (to: number, message: string, data?: any) => void
|
|
6
|
+
broadcast: (message: string, data?: any) => void
|
|
7
|
+
invoke: (event: string, data?: any) => Promise<any>
|
|
8
|
+
close: () => void
|
|
9
|
+
minimize: () => void
|
|
10
|
+
unminimize: () => void
|
|
11
|
+
maximize: () => void
|
|
12
|
+
unmaximize: () => void
|
|
13
|
+
setTitle: (title: string) => void
|
|
14
|
+
openDevTools: () => void
|
|
15
|
+
closeDevTools: () => void
|
|
16
|
+
setGlobal: (name: string, value: any) => void
|
|
17
|
+
getGlobal: (name: string) => Promise<any>
|
|
18
|
+
id: number
|
|
19
|
+
isNexfepLoadDone: boolean
|
|
20
|
+
}
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexfep",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "A desktop application framework based on @webviewjs/webview",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"@webviewjs/webview",
|
|
@@ -20,6 +20,11 @@
|
|
|
20
20
|
"bin": {
|
|
21
21
|
"nexfep": "cli/index.mjs"
|
|
22
22
|
},
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
"frontend": ["./frontend/index.d.ts"]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
23
28
|
"files": [
|
|
24
29
|
"index.js",
|
|
25
30
|
"index.d.ts",
|
|
@@ -40,7 +45,8 @@
|
|
|
40
45
|
"src/Logger.js",
|
|
41
46
|
"src/Logger.d.ts",
|
|
42
47
|
"src/SingleInstance.js",
|
|
43
|
-
"src/SingleInstance.d.ts"
|
|
48
|
+
"src/SingleInstance.d.ts",
|
|
49
|
+
"frontend/index.d.ts"
|
|
44
50
|
],
|
|
45
51
|
"type": "module",
|
|
46
52
|
"main": "./index.js",
|
|
@@ -59,7 +65,6 @@
|
|
|
59
65
|
"@types/node": "^22.0.0",
|
|
60
66
|
"oxfmt": "^0.60.0",
|
|
61
67
|
"oxlint": "^1.75.0",
|
|
62
|
-
"tsx": "^4.0.0",
|
|
63
68
|
"typescript": "^5.0.0"
|
|
64
69
|
},
|
|
65
70
|
"engines": {
|
package/src/WindowManager.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ declare class Window {
|
|
|
35
35
|
loadHTML(html: string): Promise<void>;
|
|
36
36
|
}
|
|
37
37
|
declare class WindowPool {
|
|
38
|
-
onCustomMessage: (window: Window, data:
|
|
38
|
+
onCustomMessage: (window: Window, message: string, data: any) => void;
|
|
39
39
|
app: Application;
|
|
40
40
|
logger: Logger;
|
|
41
41
|
private windows;
|
package/src/WindowManager.js
CHANGED
|
@@ -127,8 +127,8 @@ class WindowPool {
|
|
|
127
127
|
}
|
|
128
128
|
process.env.WEBVIEW2_USER_DATA_FOLDER = userDataDir;
|
|
129
129
|
}
|
|
130
|
-
this.onCustomMessage = (window, data) => {
|
|
131
|
-
console.log("Get Custom Message:", data, "from window", window.id);
|
|
130
|
+
this.onCustomMessage = (window, message, data) => {
|
|
131
|
+
console.log("Get Custom Message:", message, data, "from window", window.id);
|
|
132
132
|
};
|
|
133
133
|
this.handlers = new Map();
|
|
134
134
|
this.app = app;
|
|
@@ -221,11 +221,6 @@ class WindowPool {
|
|
|
221
221
|
window.ipc.postMessage(JSON.stringify(MessageBody));
|
|
222
222
|
};
|
|
223
223
|
|
|
224
|
-
window.postMessage = (data) => {
|
|
225
|
-
const MessageBody = { type: 'CustomMessage', data: data }
|
|
226
|
-
window.ipc.postMessage(JSON.stringify(MessageBody));
|
|
227
|
-
};
|
|
228
|
-
|
|
229
224
|
window.eventCount = 0;
|
|
230
225
|
|
|
231
226
|
window.invoke = async (event, data = null) => {
|
|
@@ -367,9 +362,6 @@ class WindowPool {
|
|
|
367
362
|
else if (dataObj.type == "NexfepCloseDevTools") {
|
|
368
363
|
webview.closeDevtools();
|
|
369
364
|
}
|
|
370
|
-
else if (dataObj.type == "CustomMessage") {
|
|
371
|
-
this.onCustomMessage(windowObj, dataObj.data);
|
|
372
|
-
}
|
|
373
365
|
else if (dataObj.type == "NexfepInvoke") {
|
|
374
366
|
const handlers = this.handlers.get(dataObj.event) || [];
|
|
375
367
|
handlers.forEach(async (handler) => {
|
|
@@ -405,6 +397,9 @@ class WindowPool {
|
|
|
405
397
|
});
|
|
406
398
|
}
|
|
407
399
|
else if (dataObj.type == "NexfepTell") {
|
|
400
|
+
if (dataObj.to == 0) {
|
|
401
|
+
this.onCustomMessage(windowObj, dataObj.message, dataObj.data);
|
|
402
|
+
}
|
|
408
403
|
this.windows.forEach(async (w) => {
|
|
409
404
|
if (w.id == dataObj.to) {
|
|
410
405
|
w.webview.evaluateScript(`
|