/* 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) } /** 最后一次读失败的原因(给上层拼提示文案用;成功时清空) */ let lastReason = '' function makeFile(u8, mime, name) { const blob = new global.Blob([u8], { type: mime }) try { return new global.File([blob], name, { type: mime }) } catch (e) { const named = /** @type {any} */ (blob) named.name = name return named } } /** 读系统剪贴板里的图片 → File(读不到返回 null) * * 两种来源(Rust 侧 clipboard_read_image 已经分流): * · pngBase64 —— 位图:截图、⌘C 复制的图片内容 * · bytesBase64 —— 文件:在访达里选中图片文件按 ⌘C(剪贴板里只有路径,没有位图) * 文件分支**保留原始格式**(jpg/heic 不在这里转),交给画布导入链路自己解: * 那边有 canvas 解码与「转成什么格式」的准入判据,比在 Rust 里瞎转更准。 */ async function readImage() { const T = tauri() if (!T) return null lastReason = '' try { const r = await T.core.invoke('clipboard_read_image') if (!r) return null const png = r.pngBase64 || r.png_base64 if (png) return makeFile(u8FromB64(png), 'image/png', 'clipboard-' + Date.now() + '.png') const raw = r.bytesBase64 || r.bytes_base64 if (raw) { const p2 = String(r.name || '') const dot = p2.lastIndexOf('.') const stem = dot > 0 ? p2.slice(0, dot) : p2 return makeFile(u8FromB64(raw), String(r.mime || 'application/octet-stream'), (stem || 'clipboard-' + Date.now()) + (dot > 0 ? p2.slice(dot) : '.png')) } return null } catch (e) { // 2026-11-25:原来的 catch 直接 return null,权限/编码失败/超时全被压成「剪贴板里没有图」, // 用户与我们都无法定位。这里留一条可查的日志 + 把原因交给上层拼文案。 lastReason = String((e && e.message) || e || '') try { console.warn('[iwb] 读取剪贴板图片失败:' + lastReason) } catch (err) { /* ignore */ } 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(), lastReason: () => lastReason, }) })(typeof window !== 'undefined' ? window : globalThis)