| 1 | import { api, native, nativeAvailable } from "./api"; |
| 2 | |
| 3 | export const imageLimit = 20 * 1024 * 1024; |
| 4 | const imageTypes = new Set([ |
| 5 | "image/png", |
| 6 | "image/jpeg", |
| 7 | "image/webp", |
| 8 | "image/gif", |
| 9 | ]); |
| 10 | |
| 11 | export function transferFiles(data) { |
| 12 | const files = Array.from(data?.files || []); |
| 13 | if (files.length) return files; |
| 14 | return Array.from(data?.items || []) |
| 15 | .filter((item) => item.kind === "file") |
| 16 | .map((item) => item.getAsFile()) |
| 17 | .filter(Boolean); |
| 18 | } |
| 19 | |
| 20 | export function hasFiles(data) { |
| 21 | return ( |
| 22 | Array.from(data?.types || []).includes("Files") || |
| 23 | transferFiles(data).length > 0 |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | export async function imagePayload(file) { |
| 28 | if (!imageTypes.has(file.type)) |
| 29 | throw new Error("请选择 PNG、JPEG、WebP 或 GIF 图片"); |
| 30 | if (!file.size || file.size > imageLimit) |
| 31 | throw new Error("每张图片不能超过 20 MB"); |
| 32 | const url = URL.createObjectURL(file); |
| 33 | try { |
| 34 | const image = new Image(); |
| 35 | await new Promise((resolve, reject) => { |
| 36 | image.onload = resolve; |
| 37 | image.onerror = () => |
| 38 | reject(new Error("图片无法读取,请重新复制或选择图片")); |
| 39 | image.src = url; |
| 40 | }); |
| 41 | if (image.naturalWidth * image.naturalHeight > 40_000_000) |
| 42 | throw new Error("图片不能超过 4000 万像素"); |
| 43 | |
| 44 | const canvas = document.createElement("canvas"); |
| 45 | canvas.width = image.naturalWidth; |
| 46 | canvas.height = image.naturalHeight; |
| 47 | canvas.getContext("2d").drawImage(image, 0, 0); |
| 48 | const png = await new Promise((resolve) => |
| 49 | canvas.toBlob(resolve, "image/png"), |
| 50 | ); |
| 51 | if (!png || png.size > imageLimit) |
| 52 | throw new Error("转换后的图片超过 20 MB,请缩小图片后重试"); |
| 53 | return await new Promise((resolve, reject) => { |
| 54 | const reader = new FileReader(); |
| 55 | reader.onload = () => resolve({ png: reader.result.split(",")[1] }); |
| 56 | reader.onerror = () => reject(new Error("图片读取失败")); |
| 57 | reader.readAsDataURL(png); |
| 58 | }); |
| 59 | } finally { |
| 60 | URL.revokeObjectURL(url); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | export async function readClipboardContent() { |
| 65 | if (nativeAvailable()) { |
| 66 | const value = await native("clipboardReadContent"); |
| 67 | if ( |
| 68 | !value || |
| 69 | (typeof value.png !== "string" && typeof value.text !== "string" && typeof value.fileSelection !== "string") |
| 70 | ) |
| 71 | throw new Error("剪贴板读取失败,请重新打开新版 Cage 后重试"); |
| 72 | return value; |
| 73 | } |
| 74 | if (navigator.clipboard.read) { |
| 75 | const items = await navigator.clipboard.read(); |
| 76 | for (const item of items) { |
| 77 | const type = item.types.find((type) => imageTypes.has(type)); |
| 78 | if (type) return imagePayload(await item.getType(type)); |
| 79 | } |
| 80 | for (const item of items) { |
| 81 | if (item.types.includes("text/plain")) |
| 82 | return { text: await (await item.getType("text/plain")).text() }; |
| 83 | } |
| 84 | throw new Error("剪贴板中没有可粘贴的图片或文本"); |
| 85 | } |
| 86 | return { text: await navigator.clipboard.readText() }; |
| 87 | } |
| 88 | |
| 89 | export function prepareClipboardPaste(target, id, { windowTarget, valid = () => true } = {}) { |
| 90 | let disposed = false, sent = false; |
| 91 | const destination = { target, id, pid: windowTarget?.pid, window: windowTarget?.window }; |
| 92 | const check = () => { |
| 93 | if (disposed || !valid()) throw new Error("粘贴接收位置已切换,请重新粘贴"); |
| 94 | }; |
| 95 | const prepared = api("/input/paste/start", destination).then(result => { |
| 96 | if (result?.available !== true || typeof result.transfer !== "string" && typeof result.context !== "string") |
| 97 | throw new Error("客机尚未确认粘贴接收位置,请同步环境组件后重试"); |
| 98 | return { transfer: result.transfer, context: result.context }; |
| 99 | }); |
| 100 | |
| 101 | |
| 102 | void prepared.catch(() => {}); |
| 103 | const dispose = () => { |
| 104 | if (disposed) return; |
| 105 | disposed = true; |
| 106 | void prepared.then(binding => api("/input/paste/cancel", { ...destination, ...binding })).catch(() => {}); |
| 107 | }; |
| 108 | return { |
| 109 | async send(payload) { |
| 110 | check(); |
| 111 | const binding = await prepared; |
| 112 | check(); |
| 113 | if (sent) throw new Error("本次粘贴已经提交,请核对接收结果"); |
| 114 | sent = true; |
| 115 | try { return await api(typeof payload.text === "string" ? "/input/text" : "/input/image", { ...payload, ...destination, ...binding }); } |
| 116 | finally { dispose(); } |
| 117 | }, |
| 118 | dispose, |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | export const sendImage = (payload, target, id, options = {}) => |
| 123 | (options.prepared || prepareClipboardPaste(target, id, options)).send(payload); |