An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Convert Android clipboard images to PNG before native copy https://github.com/siyuan-note/siyuan/issues/20433
Danielbd9fe81a3304
diff --git a/app/src/menus/copyPNG.test.ts b/app/src/menus/copyPNG.test.tsindex ebc78ccdb0..86403541e6 100644--- a/app/src/menus/copyPNG.test.ts+++ b/app/src/menus/copyPNG.test.ts@@ -57,3 +57,45 @@ for (const scenario of [}});}++test("Android converts SVG before handing a temporary PNG to the native clipboard", async () => {+ const code = transpileModule(source.substring(source.indexOf("export const writePNGBlob =")), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+ }).outputText;+ const png = new Blob(["png bytes"], {type: "image/png"});+ const nativeImages: string[] = [];+ const uploads: Array<{file: File; type: string}> = [];+ const errors: string[] = [];+ let failUpload = false;+ const exports = {} as {copyPNGByLink: (link: string) => void};+ runInNewContext(code, {+ exports, File,+ isInAndroid: () => true,+ genUUID: () => "unique-id",+ ContractFormData: class {constructor(public fields: unknown) {}},+ fetchSyncPost: async (url: string, form: {fields: typeof uploads[number]}) => {+ assert.equal(url, "/api/export/exportAsFile");+ uploads.push(form.fields);+ return failUpload ? {code: -1} : {code: 0, data: {file: "/export/file-clipboard-unique-id.png"}};+ },+ showMessage: (message: string) => errors.push(message),+ window: {devicePixelRatio: 1, siyuan: {languages: {copied: "copied", imageCopyFailed: "failed"}},+ JSAndroid: {writeImageClipboard: (link: string) => nativeImages.push(link)}},+ fetch: async () => ({ok: true, blob: async () => new Blob(["svg"], {type: "image/svg+xml"})}),+ URL: {createObjectURL: () => "blob:svg", revokeObjectURL() {}},+ document: {createElement: (tag: string) => tag === "canvas" ? {+ getContext: () => ({drawImage() {}}), toBlob: (callback: (blob: Blob) => void) => callback(png),+ } : {naturalWidth: 100, naturalHeight: 50, onload() {}, set src(_value: string) {this.onload();}}},+ });+ exports.copyPNGByLink("assets/vector.svg");+ await new Promise(setImmediate);+ assert.equal(uploads[0].type, "image/png");+ assert.equal(await uploads[0].file.text(), "png bytes");+ assert.deepEqual(nativeImages, ["/export/file-clipboard-unique-id.png"]);+ assert.deepEqual(errors, ["copied"]);+ failUpload = true;+ exports.copyPNGByLink("assets/vector.svg");+ await new Promise(setImmediate);+ assert.equal(nativeImages.length, 1);+ assert.deepEqual(errors, ["copied", "failed"]);+});diff --git a/app/src/menus/util.ts b/app/src/menus/util.tsindex a1461fb8c1..afaa6fa9de 100644--- a/app/src/menus/util.ts+++ b/app/src/menus/util.ts@@ -2,7 +2,9 @@import {ipcRenderer} from "electron";import * as path from "path";/// #endif-import {fetchPost} from "../util/fetch";+import {fetchPost, fetchSyncPost} from "../util/fetch";+import {ContractFormData} from "../util/contractFormData";+import {genUUID} from "../util/genID";import {getAssetExtension, getAssetName, useShell} from "../util/pathName";import {openFileById} from "../editor/util";import {Constants} from "../constants";@@ -221,6 +223,23 @@ export const openEditorTab = (app: App, ids: string[], notebookId?: string, path};export const writePNGBlob = async (blob: Blob): Promise<boolean> => {+ if (isInAndroid()) {+ try {+ // 通过已有临时导出通道向原生剪贴板传递 PNG,支持 WebView 渲染的矢量图片。+ const response = await fetchSyncPost("/api/export/exportAsFile", new ContractFormData({+ file: new File([blob], `clipboard-${genUUID()}.png`, {type: "image/png"}),+ type: "image/png",+ }));+ if (response.code !== 0 || !response.data?.file) {+ throw new Error("could not prepare clipboard image");+ }+ window.JSAndroid.writeImageClipboard(response.data.file);+ return true;+ } catch (e) {+ showMessage(window.siyuan.languages.imageCopyFailed, 6000, "error");+ return false;+ }+ }try {if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined") {throw new Error("image clipboard is unavailable");@@ -239,10 +258,6 @@ export const writePNGBlob = async (blob: Blob): Promise<boolean> => {};export const copyPNGByLink = (link: string) => {- if (isInAndroid()) {- window.JSAndroid.writeImageClipboard(link);- return;- }const showCopyError = () => showMessage(window.siyuan.languages.imageCopyFailed, 6000, "error");const copyBlob = async (blob: Blob) => {if (await writePNGBlob(blob)) {