nexfep 0.5.1 → 0.5.3

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 CHANGED
@@ -240,35 +240,18 @@ 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
- **参数说明**
243
+ ### 页面 API 的 Typescript 定义
254
244
 
255
- - `data` — 任意类型的可序列化数据,会被序列化为 JSON 字符串后发送
245
+ 在前端的 `tsconfig.json` 中添加以下内容即可在前端页面中使用 `nexfep` 定义的 API 类型:
256
246
 
257
- #### 监听消息
258
-
259
- 在主进程中通过 `onCustomMessage` 回调接收消息:
260
-
261
- ```typescript
262
- pool.onCustomMessage = (window, data) => {
263
- console.log(`来自窗口 ${window.id} 的消息:`, data);
264
- };
247
+ ```json
248
+ {
249
+ "compilerOptions": {
250
+ "types": ["nexfep/frontend"]
251
+ }
252
+ }
265
253
  ```
266
254
 
267
- **回调参数**
268
-
269
- - `window` — 发送消息的窗口对象
270
- - `data` — 消息内容,为对象类型(JSON 序列化后会自动通过 `JSON.parse` 转换为对象)
271
-
272
255
  ### 自定义事件
273
256
 
274
257
  #### 触发事件
@@ -405,6 +388,34 @@ window.addEventListener("custom-message", (event) => {
405
388
  console.log("当前窗口 ID:", window.id);
406
389
  ```
407
390
 
391
+ ### 自定义消息
392
+
393
+ #### 发送消息
394
+
395
+ 在页面中通过 `window.tell` 并指定 `to` 为 `0` 表示向主进程发送消息:
396
+
397
+ ```javascript
398
+ window.tell(0, "custom-message", { hello: "world" });
399
+ ```
400
+
401
+ #### 监听消息
402
+
403
+ 在主进程中通过 `onCustomMessage` 回调接收消息:
404
+
405
+ ```typescript
406
+ pool.onCustomMessage = (window, message, data) => {
407
+ console.log(`[${message}]来自窗口 ${window.id} 的消息:`, data);
408
+ };
409
+ ```
410
+
411
+ **回调参数**
412
+
413
+ - `window` — 发送消息的窗口对象
414
+ - `message` — 事件名称
415
+ - `data` — 消息内容,为对象类型(JSON 序列化后会自动通过 `JSON.parse` 转换为对象)
416
+
417
+ 我们更推荐使用 `window.invoke` 使页面与主进程通信,而非 `window.tell`。
418
+
408
419
  ### 窗口控制函数
409
420
 
410
421
  页面中可直接调用以下注入函数进行窗口控制:
package/README.md CHANGED
@@ -238,35 +238,18 @@ 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**
241
+ ### TypeScript Definitions for Frontend API
252
242
 
253
- - `data` — Any serializable data, will be serialized to JSON string before sending
243
+ Add the following content to your frontend's `tsconfig.json` to use the frontend API definitions:
254
244
 
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
- };
245
+ ```json
246
+ {
247
+ "compilerOptions": {
248
+ "types": ["nexfep/frontend"]
249
+ }
250
+ }
263
251
  ```
264
252
 
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
253
  ### Custom Events
271
254
 
272
255
  #### Invoke Events
@@ -403,6 +386,34 @@ Each window's ID can be accessed via `window.id`:
403
386
  console.log("This window ID:", window.id);
404
387
  ```
405
388
 
389
+ ### Custom Messages
390
+
391
+ #### Send Messages
392
+
393
+ In the page, use `window.tell` with `to` 0 to send a message to the main process.
394
+
395
+ ```javascript
396
+ window.tell(0, "custom-message", { hello: "world" });
397
+ ```
398
+
399
+ #### Listen for Messages
400
+
401
+ Receive messages via `onCustomMessage` callback in the main process:
402
+
403
+ ```typescript
404
+ pool.onCustomMessage = (window, message, data) => {
405
+ console.log(`[${message}] from window ${window.id}:`, data);
406
+ };
407
+ ```
408
+
409
+ **Callback Parameters**
410
+
411
+ - `window` — The window object that sent the message
412
+ - `message` — Event name
413
+ - `data` — Message content, an object type (automatically converted from JSON string via `JSON.parse`)
414
+
415
+ We recommend using `window.invoke` to communicate between pages and the main process, instead of `window.tell`.
416
+
406
417
  ### Window Control Functions
407
418
 
408
419
  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.1",
3
+ "version": "0.5.3",
4
4
  "description": "A desktop application framework based on @webviewjs/webview",
5
5
  "keywords": [
6
6
  "@webviewjs/webview",
@@ -20,6 +20,12 @@
20
20
  "bin": {
21
21
  "nexfep": "cli/index.mjs"
22
22
  },
23
+ "typesVersions": {
24
+ "*": {
25
+ ".": ["./index.d.ts"],
26
+ "frontend": ["./frontend/index.d.ts"]
27
+ }
28
+ },
23
29
  "files": [
24
30
  "index.js",
25
31
  "index.d.ts",
@@ -40,7 +46,8 @@
40
46
  "src/Logger.js",
41
47
  "src/Logger.d.ts",
42
48
  "src/SingleInstance.js",
43
- "src/SingleInstance.d.ts"
49
+ "src/SingleInstance.d.ts",
50
+ "frontend/index.d.ts"
44
51
  ],
45
52
  "type": "module",
46
53
  "main": "./index.js",
@@ -59,7 +66,6 @@
59
66
  "@types/node": "^22.0.0",
60
67
  "oxfmt": "^0.60.0",
61
68
  "oxlint": "^1.75.0",
62
- "tsx": "^4.0.0",
63
69
  "typescript": "^5.0.0"
64
70
  },
65
71
  "engines": {
@@ -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: string) => void;
38
+ onCustomMessage: (window: Window, message: string, data: any) => void;
39
39
  app: Application;
40
40
  logger: Logger;
41
41
  private windows;
@@ -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(`