cj-tauri 2.0.0 → 2.1.1
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.md +206 -139
- package/bin/cj-tauri.js +287 -287
- package/cjpm.toml +63 -52
- package/cli/cjpm.toml +22 -22
- package/cli/src/doctor.cj +308 -306
- package/cli/src/main.cj +129 -129
- package/cli/src/manifest.cj +643 -558
- package/cli/src/manifest_test.cj +87 -0
- package/cli/src/project.cj +770 -770
- package/cli/src/resolve.cj +348 -348
- package/cli/src/scaffold.cj +369 -369
- package/cli/src/sync.cj +638 -526
- package/cli/src/util.cj +168 -168
- package/cli/templates/app/README.md +51 -48
- package/cli/templates/app/assets/capabilities/default.json +7 -7
- package/cli/templates/app/assets/ui/index.html +120 -118
- package/cli/templates/app/cj-tauri.toml +32 -22
- package/cli/templates/app/cjpm.toml +34 -34
- package/cli/templates/app/src/main.cj +101 -90
- package/cli/templates/app-react/README.md +56 -56
- package/cli/templates/app-react/assets/capabilities/default.json +7 -7
- package/cli/templates/app-react/assets/ui/index.html +25 -25
- package/cli/templates/app-react/assets/ui/package.json +19 -19
- package/cli/templates/app-react/assets/ui/src/App.css +67 -67
- package/cli/templates/app-react/assets/ui/src/App.jsx +124 -124
- package/cli/templates/app-react/assets/ui/src/main.jsx +6 -6
- package/cli/templates/app-react/assets/ui/vite.config.js +24 -24
- package/cli/templates/app-react/cj-tauri.toml +33 -23
- package/cli/templates/app-react/cjpm.toml +34 -34
- package/cli/templates/app-react/src/main.cj +91 -91
- package/cli/templates/app-vue/README.md +56 -56
- package/cli/templates/app-vue/assets/capabilities/default.json +7 -7
- package/cli/templates/app-vue/assets/ui/index.html +25 -25
- package/cli/templates/app-vue/assets/ui/package.json +18 -18
- package/cli/templates/app-vue/assets/ui/src/App.vue +176 -176
- package/cli/templates/app-vue/assets/ui/src/main.js +4 -4
- package/cli/templates/app-vue/assets/ui/vite.config.js +24 -24
- package/cli/templates/app-vue/cj-tauri.toml +33 -23
- package/cli/templates/app-vue/cjpm.toml +34 -34
- package/cli/templates/app-vue/src/main.cj +91 -91
- package/cli/templates/ohos-skeleton/AppScope/app.json5 +13 -13
- package/cli/templates/ohos-skeleton/AppScope/resources/base/element/string.json +8 -8
- package/cli/templates/ohos-skeleton/AppScope/resources/base/media/layered_image.json +6 -6
- package/cli/templates/ohos-skeleton/README.md +84 -79
- package/cli/templates/ohos-skeleton/build-hap.sh +15 -0
- package/cli/templates/ohos-skeleton/build-profile.json5 +46 -46
- package/cli/templates/ohos-skeleton/entry/build-profile.json5 +26 -26
- package/cli/templates/ohos-skeleton/entry/cjpm.toml +51 -43
- package/cli/templates/ohos-skeleton/entry/hvigorfile.ts +6 -6
- package/cli/templates/ohos-skeleton/entry/oh-package.json5 +9 -9
- package/cli/templates/ohos-skeleton/entry/src/main/cangjie/ability_stage.cj +17 -17
- package/cli/templates/ohos-skeleton/entry/src/main/cangjie/app_host.cj +94 -94
- package/cli/templates/ohos-skeleton/entry/src/main/cangjie/dialog_host.cj +217 -0
- package/cli/templates/ohos-skeleton/entry/src/main/cangjie/index.cj +527 -167
- package/cli/templates/ohos-skeleton/entry/src/main/cangjie/main_ability.cj +139 -139
- package/cli/templates/ohos-skeleton/entry/src/main/module.json5 +57 -49
- package/cli/templates/ohos-skeleton/entry/src/main/resources/base/element/color.json +8 -8
- package/cli/templates/ohos-skeleton/entry/src/main/resources/base/element/string.json +16 -16
- package/cli/templates/ohos-skeleton/entry/src/main/resources/base/media/layered_image.json +6 -6
- package/cli/templates/ohos-skeleton/entry/src/main/resources/base/profile/main_pages.json +2 -2
- package/cli/templates/ohos-skeleton/entry/src/main/resources/rawfile/capabilities.json +6 -6
- package/cli/templates/ohos-skeleton/entry/src/main/resources/rawfile/index.html +92 -92
- package/cli/templates/ohos-skeleton/hvigor/hvigor-config.json5 +5 -5
- package/cli/templates/ohos-skeleton/hvigorfile.ts +6 -6
- package/cli/templates/ohos-skeleton/oh-package.json5 +10 -10
- package/native/build_win.bat +62 -62
- package/native/core/bridge_core.c +1681 -1626
- package/native/core/bridge_core.h +675 -618
- package/native/core/bridge_js.h +108 -108
- package/native/platforms/harmonyos/README.md +115 -6
- package/native/platforms/linux/bridge_linux.c +2787 -2485
- package/native/platforms/windows/bridge_win.c +3745 -3248
- package/native/tests/gtk_threading_probe.c +143 -143
- package/native/tests/test_bridge_core.c +1339 -1228
- package/package.json +53 -53
- package/src/api_system.cj +67 -67
- package/src/app.cj +335 -969
- package/src/app_events.cj +166 -0
- package/src/app_host_ops.cj +287 -0
- package/src/app_run.cj +283 -0
- package/src/app_spec.cj +76 -76
- package/src/arkweb_menu.cj +216 -0
- package/src/arkweb_script.cj +142 -142
- package/src/capability.cj +409 -409
- package/src/clipboard_bridge.cj +178 -0
- package/src/dialog_spec.cj +79 -0
- package/src/dragdrop.cj +26 -26
- package/src/embedded_assets.cj +98 -98
- package/src/host.cj +752 -607
- package/src/host_arkweb.cj +924 -778
- package/src/host_webkit.cj +663 -570
- package/src/host_webview2.cj +663 -570
- package/src/hotkey.cj +66 -66
- package/src/ipc_hub.cj +401 -337
- package/src/ipc_message.cj +286 -286
- package/src/launch.cj +62 -62
- package/src/logging.cj +127 -127
- package/src/menu.cj +197 -197
- package/src/platform/platform_app_context.cj +77 -77
- package/src/platform/platform_assets.cj +33 -33
- package/src/platform/platform_discover.cj +176 -176
- package/src/platform/platform_env.cj +29 -29
- package/src/platform/platform_feature.cj +31 -31
- package/src/platform/platform_page_source.cj +87 -87
- package/src/platform/platform_target_info.cj +32 -32
- package/src/platform_host_provider.cj +87 -87
- package/src/plugin.cj +333 -333
- package/src/plugins/clipboard/plugin_clipboard.cj +152 -152
- package/src/plugins/dialog/plugin_dialog.cj +209 -209
- package/src/plugins/fs/plugin_fs.cj +105 -1
- package/src/plugins/http/http_backend.cj +131 -0
- package/src/plugins/http/http_desktop.cj +195 -0
- package/src/plugins/http/http_ohos.cj +204 -0
- package/src/plugins/http/plugin_http.cj +310 -459
- package/src/plugins/log/plugin_log.cj +105 -105
- package/src/plugins/menu/plugin_menu.cj +158 -158
- package/src/plugins/notification/plugin_notification.cj +154 -154
- package/src/plugins/serial/plugin_serial.cj +458 -454
- package/src/plugins/serial/serial_bridge.cj +194 -194
- package/src/plugins/serial/serial_manager_ohos.cj +92 -0
- package/src/plugins/serial/serial_ohos.cj +529 -0
- package/src/plugins/shell/plugin_shell.cj +258 -258
- package/src/plugins/sqlite/plugin_sqlite.cj +705 -710
- package/src/plugins/sqlite/sqlite_ffi.cj +206 -211
- package/src/plugins/ws/plugin_ws.cj +488 -470
- package/src/process_hidden.cj +174 -174
- package/src/scope.cj +391 -391
- package/src/shell_event.cj +69 -69
- package/src/tests/app_spec_test.cj +130 -127
- package/src/tests/capability_test.cj +243 -243
- package/src/tests/dialog_spec_test.cj +73 -0
- package/src/tests/fake_host.cj +201 -201
- package/src/tests/ipc_hub_test.cj +563 -487
- package/src/tests/logging_test.cj +62 -62
- package/src/tests/platform_contract_test.cj +264 -251
- package/src/tests/plugin_clipboard_test.cj +222 -222
- package/src/tests/plugin_dialog_test.cj +237 -237
- package/src/tests/plugin_fs_test.cj +207 -0
- package/src/tests/plugin_http_test.cj +124 -124
- package/src/tests/plugin_notification_test.cj +205 -205
- package/src/tests/plugin_serial_test.cj +48 -0
- package/src/tests/plugin_test.cj +605 -585
- package/src/tests/plugin_ws_test.cj +40 -40
- package/src/tests/process_hidden_test.cj +129 -129
- package/src/tests/scope_test.cj +350 -350
- package/src/tests/system_host_test.cj +99 -81
- package/src/tests/urlutil_test.cj +53 -53
- package/src/tests/version_test.cj +75 -75
- package/src/tests/window_registry_test.cj +123 -123
- package/src/tray.cj +90 -90
- package/src/urlutil.cj +159 -159
- package/src/version.cj +44 -44
- package/src/window.cj +51 -51
- package/src/window_registry.cj +116 -116
- package/prebuilt/win32-x64/cj-tauri.exe +0 -0
- package/src/plugins/serial/serial_ohos_stub.cj +0 -61
- package/src/plugins/sqlite/sqlite_ohos_stub.cj +0 -91
package/src/arkweb_script.cj
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
package cjTauri
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 鸿蒙 ArkWeb 侧的页面桥脚本与注入片段
|
|
5
|
-
*
|
|
6
|
-
* ## 为什么要另存一份(而不是复用 `native/core/bridge_js.h`)
|
|
7
|
-
*
|
|
8
|
-
* 那一份是 **C 宏**(`CJ_BRIDGE_JS(POST_STMT, RELOAD_STMT)`),只有编 C 桥的平台能用;
|
|
9
|
-
* 鸿蒙没有 C 桥(宿主在仓颉侧直接驱动 ArkWeb),拿不到宏展开结果。所以这里按同一份语义
|
|
10
|
-
* 落成仓颉常量——**改动要两边同步**,判断「哪边漏了」的判据是:页面上 `invoke` / `emit` /
|
|
11
|
-
* `listen` / `reload` / `_dispatch` 五个入口的行为是否与桌面一致(桥只负责路由与回执匹配,
|
|
12
|
-
* 不含业务)。
|
|
13
|
-
*
|
|
14
|
-
* ## 与桌面版的唯一差异
|
|
15
|
-
*
|
|
16
|
-
* `post`:桌面走 `window.chrome.webview.postMessage` / `window.webkit.messageHandlers`,
|
|
17
|
-
* 鸿蒙走 `javaScriptProxy` 注入的同步方法(`window.__CJ_TAURI_HOST__.post(json)`)。
|
|
18
|
-
* 同步调用不携带返回值语义——它是「投递即返回」,回执仍由 `_dispatch` 从仓颉侧推回来。
|
|
19
|
-
*
|
|
20
|
-
* ## 转义口径
|
|
21
|
-
*
|
|
22
|
-
* 用 raw string(`#"..."#`):三引号字符串会先处理反斜杠转义(AGENTS §4 踩过,内联 JS 会被
|
|
23
|
-
* 拆断而 stderr 一行提示都没有),raw string 原样保留。因此本文件里的 JS **不要**出现 `"#`
|
|
24
|
-
* 这个序列(那是 raw string 的收尾)。
|
|
25
|
-
*/
|
|
26
|
-
@When[env == "ohos"]
|
|
27
|
-
public let ARKWEB_PROXY_NAME: String = "__CJ_TAURI_HOST__"
|
|
28
|
-
|
|
29
|
-
@When[env == "ohos"]
|
|
30
|
-
let ARKWEB_BRIDGE_JS: String = #"window.__CJ_TAURI__ = (function () {
|
|
31
|
-
var seq = 0;
|
|
32
|
-
var pending = {};
|
|
33
|
-
var listeners = {};
|
|
34
|
-
var coalesceSlots = Object.create(null);
|
|
35
|
-
var coalesceTimer = null;
|
|
36
|
-
var coalesceRaf = 0;
|
|
37
|
-
function flushCoalesced() {
|
|
38
|
-
if (coalesceTimer !== null) { clearTimeout(coalesceTimer); coalesceTimer = null; }
|
|
39
|
-
coalesceRaf = 0;
|
|
40
|
-
var due = coalesceSlots;
|
|
41
|
-
coalesceSlots = Object.create(null);
|
|
42
|
-
for (var name in due) {
|
|
43
|
-
var arr = listeners[name] || [];
|
|
44
|
-
for (var i = 0; i < arr.length; i++) arr[i](due[name]);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function scheduleCoalesced() {
|
|
48
|
-
if (coalesceTimer !== null || coalesceRaf) return;
|
|
49
|
-
coalesceTimer = setTimeout(flushCoalesced, 50);
|
|
50
|
-
}
|
|
51
|
-
var cachedLabel = null;
|
|
52
|
-
function labelOf() {
|
|
53
|
-
if (cachedLabel === null) {
|
|
54
|
-
var v = (typeof window !== 'undefined') ? window.__CJ_TAURI_LABEL__ : null;
|
|
55
|
-
cachedLabel = (typeof v === 'string' && v.length > 0) ? v : 'main';
|
|
56
|
-
}
|
|
57
|
-
return cachedLabel;
|
|
58
|
-
}
|
|
59
|
-
function post(obj) {
|
|
60
|
-
try {
|
|
61
|
-
var host = window.__CJ_TAURI_HOST__;
|
|
62
|
-
if (host && typeof host.post === 'function') host.post(JSON.stringify(obj));
|
|
63
|
-
} catch (e) { }
|
|
64
|
-
}
|
|
65
|
-
/*
|
|
66
|
-
* 原生 -> 页面的推送:页面**只负责叫一声**,脚本由原生在 post 回调里用
|
|
67
|
-
* `controller.runJavaScript` 推回来。
|
|
68
|
-
*
|
|
69
|
-
* 为什么不是「页面 poll 取返回值」:2026-10-19 真机实测,`javaScriptProxy` 的
|
|
70
|
-
* funcList 返回值**传不回 JS**(换上无条件返回固定串的桩,页面拿到的仍是 null),
|
|
71
|
-
* 而 post 方向一切正常。所以同步取数模型不成立,只能反过来推。
|
|
72
|
-
* 心跳周期的代价只是一次同步调用;队列空时原生什么都不做。
|
|
73
|
-
*/
|
|
74
|
-
var pumping = false;
|
|
75
|
-
function pump() {
|
|
76
|
-
if (pumping) return;
|
|
77
|
-
pumping = true;
|
|
78
|
-
try {
|
|
79
|
-
var host = window.__CJ_TAURI_HOST__;
|
|
80
|
-
if (host && typeof host.post === 'function') {
|
|
81
|
-
host.post('{"type":"pull","window":' + JSON.stringify(labelOf()) + '}');
|
|
82
|
-
}
|
|
83
|
-
} catch (e2) { } finally { pumping = false; }
|
|
84
|
-
}
|
|
85
|
-
try { setInterval(pump, 50); } catch (e3) { }
|
|
86
|
-
return {
|
|
87
|
-
invoke: function (cmd, args) {
|
|
88
|
-
var id = ++seq;
|
|
89
|
-
var p = new Promise(function (resolve, reject) { pending[id] = { resolve: resolve, reject: reject }; });
|
|
90
|
-
post({ type: 'invoke', id: id, cmd: cmd, args: args || {}, window: labelOf() });
|
|
91
|
-
return p;
|
|
92
|
-
},
|
|
93
|
-
listen: function (event, cb) {
|
|
94
|
-
if (!listeners[event]) listeners[event] = [];
|
|
95
|
-
listeners[event].push(cb);
|
|
96
|
-
return function () {
|
|
97
|
-
var arr = listeners[event] || [];
|
|
98
|
-
var i = arr.indexOf(cb);
|
|
99
|
-
if (i >= 0) arr.splice(i, 1);
|
|
100
|
-
};
|
|
101
|
-
},
|
|
102
|
-
emit: function (event, payload) {
|
|
103
|
-
var id = ++seq;
|
|
104
|
-
var p = new Promise(function (resolve, reject) { pending[id] = { resolve: resolve, reject: reject }; });
|
|
105
|
-
post({ type: 'emit', id: id, event: event, payload: payload === undefined ? {} : payload, window: labelOf() });
|
|
106
|
-
return p;
|
|
107
|
-
},
|
|
108
|
-
reload: function () {
|
|
109
|
-
post({ type: 'reload', window: labelOf() });
|
|
110
|
-
},
|
|
111
|
-
_dispatch: function (msg) {
|
|
112
|
-
if (!msg) return;
|
|
113
|
-
if (msg.type === 'resolve') {
|
|
114
|
-
var p = pending[msg.id];
|
|
115
|
-
if (!p) return;
|
|
116
|
-
delete pending[msg.id];
|
|
117
|
-
if (msg.ok) p.resolve(msg.data); else p.reject(new Error(msg.error || 'invoke failed'));
|
|
118
|
-
} else if (msg.type === 'event') {
|
|
119
|
-
if (msg.window && msg.window !== labelOf()) return;
|
|
120
|
-
if (msg.coalesce) {
|
|
121
|
-
coalesceSlots[msg.event] = msg.payload;
|
|
122
|
-
scheduleCoalesced();
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
var arr = listeners[msg.event] || [];
|
|
126
|
-
for (var i = 0; i < arr.length; i++) arr[i](msg.payload);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
})();"#
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 窗口 label 注入片段(等价于桌面 C 侧的 document-start 注入)
|
|
134
|
-
*
|
|
135
|
-
* 单引号包住 JSON 字符串:label 由应用给定,实际取值来自 `WindowConfig.label` / 默认 label,
|
|
136
|
-
* 里层用 JSON 转义保证引号与反斜杠安全(不做手工拼接转义)。
|
|
137
|
-
*/
|
|
138
|
-
@When[env == "ohos"]
|
|
139
|
-
public func arkWebLabelScript(label: String): String {
|
|
140
|
-
return "window.__CJ_TAURI_LABEL__ = ${jsStringLiteral(label)};"
|
|
141
|
-
}
|
|
142
|
-
|
|
1
|
+
package cjTauri
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 鸿蒙 ArkWeb 侧的页面桥脚本与注入片段
|
|
5
|
+
*
|
|
6
|
+
* ## 为什么要另存一份(而不是复用 `native/core/bridge_js.h`)
|
|
7
|
+
*
|
|
8
|
+
* 那一份是 **C 宏**(`CJ_BRIDGE_JS(POST_STMT, RELOAD_STMT)`),只有编 C 桥的平台能用;
|
|
9
|
+
* 鸿蒙没有 C 桥(宿主在仓颉侧直接驱动 ArkWeb),拿不到宏展开结果。所以这里按同一份语义
|
|
10
|
+
* 落成仓颉常量——**改动要两边同步**,判断「哪边漏了」的判据是:页面上 `invoke` / `emit` /
|
|
11
|
+
* `listen` / `reload` / `_dispatch` 五个入口的行为是否与桌面一致(桥只负责路由与回执匹配,
|
|
12
|
+
* 不含业务)。
|
|
13
|
+
*
|
|
14
|
+
* ## 与桌面版的唯一差异
|
|
15
|
+
*
|
|
16
|
+
* `post`:桌面走 `window.chrome.webview.postMessage` / `window.webkit.messageHandlers`,
|
|
17
|
+
* 鸿蒙走 `javaScriptProxy` 注入的同步方法(`window.__CJ_TAURI_HOST__.post(json)`)。
|
|
18
|
+
* 同步调用不携带返回值语义——它是「投递即返回」,回执仍由 `_dispatch` 从仓颉侧推回来。
|
|
19
|
+
*
|
|
20
|
+
* ## 转义口径
|
|
21
|
+
*
|
|
22
|
+
* 用 raw string(`#"..."#`):三引号字符串会先处理反斜杠转义(AGENTS §4 踩过,内联 JS 会被
|
|
23
|
+
* 拆断而 stderr 一行提示都没有),raw string 原样保留。因此本文件里的 JS **不要**出现 `"#`
|
|
24
|
+
* 这个序列(那是 raw string 的收尾)。
|
|
25
|
+
*/
|
|
26
|
+
@When[env == "ohos"]
|
|
27
|
+
public let ARKWEB_PROXY_NAME: String = "__CJ_TAURI_HOST__"
|
|
28
|
+
|
|
29
|
+
@When[env == "ohos"]
|
|
30
|
+
let ARKWEB_BRIDGE_JS: String = #"window.__CJ_TAURI__ = (function () {
|
|
31
|
+
var seq = 0;
|
|
32
|
+
var pending = {};
|
|
33
|
+
var listeners = {};
|
|
34
|
+
var coalesceSlots = Object.create(null);
|
|
35
|
+
var coalesceTimer = null;
|
|
36
|
+
var coalesceRaf = 0;
|
|
37
|
+
function flushCoalesced() {
|
|
38
|
+
if (coalesceTimer !== null) { clearTimeout(coalesceTimer); coalesceTimer = null; }
|
|
39
|
+
coalesceRaf = 0;
|
|
40
|
+
var due = coalesceSlots;
|
|
41
|
+
coalesceSlots = Object.create(null);
|
|
42
|
+
for (var name in due) {
|
|
43
|
+
var arr = listeners[name] || [];
|
|
44
|
+
for (var i = 0; i < arr.length; i++) arr[i](due[name]);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function scheduleCoalesced() {
|
|
48
|
+
if (coalesceTimer !== null || coalesceRaf) return;
|
|
49
|
+
coalesceTimer = setTimeout(flushCoalesced, 50);
|
|
50
|
+
}
|
|
51
|
+
var cachedLabel = null;
|
|
52
|
+
function labelOf() {
|
|
53
|
+
if (cachedLabel === null) {
|
|
54
|
+
var v = (typeof window !== 'undefined') ? window.__CJ_TAURI_LABEL__ : null;
|
|
55
|
+
cachedLabel = (typeof v === 'string' && v.length > 0) ? v : 'main';
|
|
56
|
+
}
|
|
57
|
+
return cachedLabel;
|
|
58
|
+
}
|
|
59
|
+
function post(obj) {
|
|
60
|
+
try {
|
|
61
|
+
var host = window.__CJ_TAURI_HOST__;
|
|
62
|
+
if (host && typeof host.post === 'function') host.post(JSON.stringify(obj));
|
|
63
|
+
} catch (e) { }
|
|
64
|
+
}
|
|
65
|
+
/*
|
|
66
|
+
* 原生 -> 页面的推送:页面**只负责叫一声**,脚本由原生在 post 回调里用
|
|
67
|
+
* `controller.runJavaScript` 推回来。
|
|
68
|
+
*
|
|
69
|
+
* 为什么不是「页面 poll 取返回值」:2026-10-19 真机实测,`javaScriptProxy` 的
|
|
70
|
+
* funcList 返回值**传不回 JS**(换上无条件返回固定串的桩,页面拿到的仍是 null),
|
|
71
|
+
* 而 post 方向一切正常。所以同步取数模型不成立,只能反过来推。
|
|
72
|
+
* 心跳周期的代价只是一次同步调用;队列空时原生什么都不做。
|
|
73
|
+
*/
|
|
74
|
+
var pumping = false;
|
|
75
|
+
function pump() {
|
|
76
|
+
if (pumping) return;
|
|
77
|
+
pumping = true;
|
|
78
|
+
try {
|
|
79
|
+
var host = window.__CJ_TAURI_HOST__;
|
|
80
|
+
if (host && typeof host.post === 'function') {
|
|
81
|
+
host.post('{"type":"pull","window":' + JSON.stringify(labelOf()) + '}');
|
|
82
|
+
}
|
|
83
|
+
} catch (e2) { } finally { pumping = false; }
|
|
84
|
+
}
|
|
85
|
+
try { setInterval(pump, 50); } catch (e3) { }
|
|
86
|
+
return {
|
|
87
|
+
invoke: function (cmd, args) {
|
|
88
|
+
var id = ++seq;
|
|
89
|
+
var p = new Promise(function (resolve, reject) { pending[id] = { resolve: resolve, reject: reject }; });
|
|
90
|
+
post({ type: 'invoke', id: id, cmd: cmd, args: args || {}, window: labelOf() });
|
|
91
|
+
return p;
|
|
92
|
+
},
|
|
93
|
+
listen: function (event, cb) {
|
|
94
|
+
if (!listeners[event]) listeners[event] = [];
|
|
95
|
+
listeners[event].push(cb);
|
|
96
|
+
return function () {
|
|
97
|
+
var arr = listeners[event] || [];
|
|
98
|
+
var i = arr.indexOf(cb);
|
|
99
|
+
if (i >= 0) arr.splice(i, 1);
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
emit: function (event, payload) {
|
|
103
|
+
var id = ++seq;
|
|
104
|
+
var p = new Promise(function (resolve, reject) { pending[id] = { resolve: resolve, reject: reject }; });
|
|
105
|
+
post({ type: 'emit', id: id, event: event, payload: payload === undefined ? {} : payload, window: labelOf() });
|
|
106
|
+
return p;
|
|
107
|
+
},
|
|
108
|
+
reload: function () {
|
|
109
|
+
post({ type: 'reload', window: labelOf() });
|
|
110
|
+
},
|
|
111
|
+
_dispatch: function (msg) {
|
|
112
|
+
if (!msg) return;
|
|
113
|
+
if (msg.type === 'resolve') {
|
|
114
|
+
var p = pending[msg.id];
|
|
115
|
+
if (!p) return;
|
|
116
|
+
delete pending[msg.id];
|
|
117
|
+
if (msg.ok) p.resolve(msg.data); else p.reject(new Error(msg.error || 'invoke failed'));
|
|
118
|
+
} else if (msg.type === 'event') {
|
|
119
|
+
if (msg.window && msg.window !== labelOf()) return;
|
|
120
|
+
if (msg.coalesce) {
|
|
121
|
+
coalesceSlots[msg.event] = msg.payload;
|
|
122
|
+
scheduleCoalesced();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
var arr = listeners[msg.event] || [];
|
|
126
|
+
for (var i = 0; i < arr.length; i++) arr[i](msg.payload);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
})();"#
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 窗口 label 注入片段(等价于桌面 C 侧的 document-start 注入)
|
|
134
|
+
*
|
|
135
|
+
* 单引号包住 JSON 字符串:label 由应用给定,实际取值来自 `WindowConfig.label` / 默认 label,
|
|
136
|
+
* 里层用 JSON 转义保证引号与反斜杠安全(不做手工拼接转义)。
|
|
137
|
+
*/
|
|
138
|
+
@When[env == "ohos"]
|
|
139
|
+
public func arkWebLabelScript(label: String): String {
|
|
140
|
+
return "window.__CJ_TAURI_LABEL__ = ${jsStringLiteral(label)};"
|
|
141
|
+
}
|
|
142
|
+
|