/* clipboard-native.js —— 桌面端原生剪贴板(2026-11-18) * * 为什么单独一个模块: * WKWebView 的 paste 事件拿不到「从 Finder/预览复制的图片」, * navigator.clipboard.write([ClipboardItem]) 在部分 macOS 版本也不可用。 * 图片读写统一走 Rust 侧 arboard(命令 clipboard_read_image / clipboard_write_image)。 * * 为什么 IPC 传 PNG 而不是裸 RGBA: * 12MP 照片的 RGBA 是 46MB,base64 后 62MB,穿过 IPC 会卡住前端好几秒; * PNG 通常只有几 MB。读方向由 Rust 编码 PNG,写方向直接把 Blob 的 PNG 字节发过去。 */ ;(function (global) { 'use strict' function tauri() { const T = global && global.__TAURI__ return (T && T.core && typeof T.core.invoke === 'function') ? T : null } function b64FromU8(u8) { let s = '' const CH = 0x8000 for (let i = 0; i < u8.length; i += CH) s += String.fromCharCode.apply(null, u8.subarray(i, i + CH)) return global.btoa(s) } function u8FromB64(b64) { const s = global.atob(b64) const u8 = new Uint8Array(s.length) for (let i = 0; i < s.length; i++) u8[i] = s.charCodeAt(i) return u8 } /** Blob/File → PNG Blob(其它格式统一转一次,避免写进剪贴板后别人贴出来是 HEIC) */ function toPngBlob(blobOrFile) { if (blobOrFile && blobOrFile.type === 'image/png') return Promise.resolve(blobOrFile) const C = global.IWBImageBlob if (C && typeof C.toPng === 'function') return Promise.resolve(C.toPng(blobOrFile)) return Promise.resolve(blobOrFile) } /** 读系统剪贴板里的图片 → PNG File(读不到返回 null) */ async function readImage() { const T = tauri() if (!T) return null try { const r = await T.core.invoke('clipboard_read_image') const b64 = r && (r.pngBase64 || r.png_base64) if (!b64) return null const u8 = u8FromB64(b64) const blob = new global.Blob([u8], { type: 'image/png' }) const name = 'clipboard-' + Date.now() + '.png' try { return new global.File([blob], name, { type: 'image/png' }) } catch (e) { const named = /** @type {any} */ (blob) named.name = name return named } } catch (e) { return null } } /** 把图片写进系统剪贴板(Blob/File 均可) */ async function writeImage(blobOrFile) { const T = tauri() if (!T || !blobOrFile) return false try { const png = await toPngBlob(blobOrFile) const buf = new Uint8Array(await png.arrayBuffer()) await T.core.invoke('clipboard_write_image', { pngBase64: b64FromU8(buf) }) return true } catch (e) { return false } } global.IWBNativeClipboard = Object.freeze({ readImage, writeImage, available: () => !!tauri() }) })(typeof window !== 'undefined' ? window : globalThis)