73 lines
4.8 KiB
JavaScript
73 lines
4.8 KiB
JavaScript
/* runtime-desktop-bridge.js —— 桌面端(Tauri)能力桥(2026-11-16)
|
||
*
|
||
* 为什么单独一个模块:
|
||
* src/*.js 不允许反向引用 app.js 定义的 window 全局(architecture-boundary-guard 会红)。
|
||
* 而「原生另存为 / 默认下载文件夹 / 打开数据目录」的实现必须在 app.js 的保存链路里。
|
||
* 所以这里只做**登记 + 转发**:app.js 启动时 configure() 注入实现,其它模块只认 IWBDesktop。
|
||
*
|
||
* 网页版:不会 configure ⇒ ready() 为 false ⇒ 各模块据此隐藏桌面专属 UI,行为与原来完全一致。
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
const state = { impl: null }
|
||
const bridge = {
|
||
// app.js 启动时调用一次,注入真实实现
|
||
configure(impl) { state.impl = impl || null },
|
||
ready() { return !!state.impl },
|
||
isTauri() { return !!(state.impl && state.impl.isTauri()) },
|
||
// 保存 Blob:桌面端走原生另存为/静默写盘,网页端由 app.js 内部回退到 <a download>
|
||
saveBlob(blob, name) { return state.impl ? state.impl.saveBlob(blob, name) : undefined },
|
||
getDownloadDir() { return state.impl ? state.impl.getDownloadDir() : '' },
|
||
setDownloadDir(dir) { if (state.impl) state.impl.setDownloadDir(dir) },
|
||
pickDownloadDir() { return state.impl ? state.impl.pickDownloadDir() : Promise.resolve('') },
|
||
getSilentSave() { return !!(state.impl && state.impl.getSilentSave()) },
|
||
setSilentSave(on) { if (state.impl) state.impl.setSilentSave(on) },
|
||
// 自动下载(v3.25.20):与「点下载是否弹框」解耦,只决定出图后是否自动落盘
|
||
getAutoDownload() { return !!(state.impl && state.impl.getAutoDownload && state.impl.getAutoDownload()) },
|
||
setAutoDownload(on) { if (state.impl && state.impl.setAutoDownload) state.impl.setAutoDownload(on) },
|
||
openDataDir() { return state.impl ? state.impl.openDataDir() : Promise.reject(new Error('仅桌面端可用')) },
|
||
// 2026-11-24:插件要「打开本地图库目录」「读剪贴板导入分享」——都落在宿主桥里,app.js 只做装配。
|
||
openDataSubpath(rel) { return state.impl && state.impl.openDataSubpath ? state.impl.openDataSubpath(rel) : Promise.reject(new Error('仅桌面端可用')) },
|
||
clipboardReadText() { return state.impl && state.impl.clipboardReadText ? state.impl.clipboardReadText() : Promise.resolve('') },
|
||
// 原生剪贴板图片(v3.26):WKWebView 里 paste 事件拿不到 Finder 复制的图片,
|
||
// navigator.clipboard.write 也常不可用,统一走 Rust 侧 arboard。
|
||
clipboardReadImage() { return state.impl && state.impl.clipboardReadImage ? state.impl.clipboardReadImage() : Promise.resolve(null) },
|
||
clipboardWriteImage(blob) { return state.impl && state.impl.clipboardWriteImage ? state.impl.clipboardWriteImage(blob) : Promise.resolve(false) },
|
||
market: {
|
||
list() { return state.impl && state.impl.marketList ? state.impl.marketList() : Promise.resolve([]) },
|
||
installed() { return state.impl && state.impl.marketInstalled ? state.impl.marketInstalled() : Promise.resolve([]) },
|
||
install(id) { return state.impl && state.impl.marketInstall ? state.impl.marketInstall(id) : Promise.resolve({ ok: false, error: '仅桌面端可用' }) },
|
||
uninstall(id) { return state.impl && state.impl.marketUninstall ? state.impl.marketUninstall(id) : Promise.resolve({ ok: false, error: '仅桌面端可用' }) },
|
||
},
|
||
}
|
||
// 2026-11-24:插件侧「打开资料目录子路径」「读剪贴板文本」两个小能力。
|
||
// 放在桥里而不是 app.js:一是 app.js 有行数棘轮,二是它们只依赖 __TAURI__。
|
||
const helpers = {
|
||
async openGalleryDir () {
|
||
try {
|
||
const T = (typeof window !== 'undefined') ? window.__TAURI__ : null
|
||
if (!T || !T.core || typeof T.core.invoke !== 'function') return { ok: false, error: '仅桌面端可打开本地目录' }
|
||
const p = await T.core.invoke('open_data_subpath', { rel: 'images/gallery' })
|
||
return { ok: true, path: p }
|
||
} catch (e) { return { ok: false, error: String((e && e.message) || e) } }
|
||
},
|
||
async clipboardReadText () {
|
||
try {
|
||
const T = (typeof window !== 'undefined') ? window.__TAURI__ : null
|
||
if (T && T.core && typeof T.core.invoke === 'function') {
|
||
const t = await T.core.invoke('clipboard_read_text')
|
||
if (t) return { ok: true, text: String(t) }
|
||
}
|
||
} catch (e) { /* 落到 navigator */ }
|
||
try {
|
||
const cb = navigator.clipboard
|
||
if (cb && typeof cb.readText === 'function') return { ok: true, text: String((await cb.readText()) || '') }
|
||
} catch (e) { return { ok: false, error: '浏览器拒绝了剪贴板读取' } }
|
||
return { ok: false, error: '剪贴板不可用' }
|
||
},
|
||
}
|
||
|
||
global.IWBDesktopHelpers = helpers
|
||
global.IWBDesktop = bridge
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|