дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
fix(clipboard): read the macOS clipboard natively for menu paste, without WebKit's Paste callout (#6061)
On macOS, WebKit asks the user to confirm execCommand("paste") and navigator.clipboard reads outside a paste event with a "Paste" callout. readClipboardPlainText fell back to navigator.clipboard.readText() whenever the native read found no text, and the terminal's menu Paste called execCommand("paste") and navigator.clipboard.read() before reading natively. On macOS a null from clipboard_text now means empty text (a refused command still falls back), and in the desktop app the terminal menu Paste reads files, text and images through the backend commands.
Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
1 джерело
b0d7bfef24
коміт
3c3681ef2b
4 files changed
+126
-4
No files matched your search
@@ -13,6 +13,7 @@ beforeEach(() => {
|
||||
notify.mockClear();
|
||||
});
|
||||
afterEach(() => {
|
||||
usePlatform.setState({ os: "macos" });
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
@@ -181,6 +182,7 @@ function remoteTarget(current = () => true): TerminalPasteTarget {
|
||||
}
|
||||
|
||||
it.each(["event", "menu"])("rejects oversized %s images before reading or uploading", async (route) => {
|
||||
usePlatform.setState({ os: "windows" }); // the WebView menu path; macOS reads natively
|
||||
const read = vi.fn(async () => new ArrayBuffer(0));
|
||||
const blob = image(read, 20 * 1024 * 1024 + 1);
|
||||
const store = vi.spyOn(commands, "termStoreImage");
|
||||
@@ -246,6 +248,7 @@ it("claims remote image events before xterm and pastes a quoted path only after
|
||||
});
|
||||
|
||||
it("reads an image from the explicit paste menu after text and file recovery", async () => {
|
||||
usePlatform.setState({ os: "windows" }); // the WebView menu path; macOS reads natively
|
||||
vi.spyOn(commands, "clipboardFileList").mockResolvedValue({ status: "ok", data: ["/local/stale.png"] });
|
||||
const store = vi.spyOn(commands, "termStoreImage").mockResolvedValue({ status: "ok", data: "/remote/image.png" });
|
||||
const getType = vi.fn(async () => image());
|
||||
@@ -261,6 +264,7 @@ it("reads an image from the explicit paste menu after text and file recovery", a
|
||||
});
|
||||
|
||||
it("recovers remote menu file paths when image clipboard reading is unavailable", async () => {
|
||||
usePlatform.setState({ os: "windows" }); // the WebView menu path; macOS reads natively
|
||||
vi.spyOn(commands, "clipboardFileList").mockResolvedValue({ status: "ok", data: ["/local/file.txt"] });
|
||||
const store = vi.spyOn(commands, "termStoreImage");
|
||||
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { readText: async () => "" } });
|
||||
@@ -271,6 +275,7 @@ it("recovers remote menu file paths when image clipboard reading is unavailable"
|
||||
});
|
||||
|
||||
it("uploads a menu image even if reading clipboard text is rejected", async () => {
|
||||
usePlatform.setState({ os: "windows" }); // the WebView menu path; macOS reads natively
|
||||
const store = vi.spyOn(commands, "termStoreImage").mockResolvedValue({ status: "ok", data: "/remote/image.png" });
|
||||
Object.defineProperty(navigator, "clipboard", {
|
||||
configurable: true,
|
||||
@@ -304,6 +309,7 @@ it("uploads image events instead of recovering a local image path", async () =>
|
||||
});
|
||||
|
||||
it("does not upload a menu image when the target changes while reading", async () => {
|
||||
usePlatform.setState({ os: "windows" }); // the WebView menu path; macOS reads natively
|
||||
let valid = true;
|
||||
let finish!: (value: Blob) => void;
|
||||
const getType = vi.fn(
|
||||
@@ -446,6 +452,7 @@ function localTarget(shell = "sh", current = () => true): TerminalPasteTarget {
|
||||
}
|
||||
|
||||
it.each(["event", "menu"])("stores raw local clipboard media through %s paste", async (route) => {
|
||||
usePlatform.setState({ os: "windows" }); // the WebView menu path; macOS reads natively
|
||||
const store = vi.spyOn(commands, "termStoreImage").mockResolvedValue({ status: "ok", data: "/tmp/a'b c.png" });
|
||||
const host = document.createElement("div");
|
||||
const paste = vi.fn();
|
||||
@@ -632,7 +639,7 @@ it.each([
|
||||
{ os: "linux", native: "ls -la", expected: ["ls -la"], execCommand: 0 },
|
||||
// An image-only clipboard keeps the WebView paths.
|
||||
{ os: "linux", native: "", expected: [], execCommand: 1 },
|
||||
{ os: "macos", native: "ls -la", expected: ["ls -la"], execCommand: 1 },
|
||||
{ os: "macos", native: "ls -la", expected: ["ls -la"], execCommand: 0 },
|
||||
] as const)("menu paste on $os with native text '$native'", async ({ os, native, expected, execCommand }) => {
|
||||
usePlatform.setState({ os });
|
||||
vi.spyOn(commands, "clipboardText").mockResolvedValue({ status: "ok", data: native });
|
||||
@@ -651,3 +658,68 @@ it.each([
|
||||
expect(exec).toHaveBeenCalledTimes(execCommand);
|
||||
expect(paste.mock.calls).toEqual(expected.map((text) => [text]));
|
||||
});
|
||||
|
||||
// WebKit on macOS shows a "Paste" callout for execCommand("paste") and navigator.clipboard reads
|
||||
// outside a paste event: a menu paste reads text, files and images through the backend.
|
||||
it.each([
|
||||
{ name: "text", text: "ls -la", files: [], png: null, target: localTarget, expected: "ls -la" },
|
||||
{
|
||||
name: "Finder file",
|
||||
text: "a b.txt",
|
||||
files: ["/x/a b.txt"],
|
||||
png: null,
|
||||
target: localTarget,
|
||||
expected: "'/x/a b.txt'",
|
||||
},
|
||||
{ name: "screenshot", text: null, files: [], png: "AQI=", target: localTarget, expected: "'/tmp/shot.png'" },
|
||||
{ name: "remote screenshot", text: null, files: [], png: "AQI=", target: remoteTarget, expected: "'/tmp/shot.png'" },
|
||||
{ name: "empty clipboard", text: null, files: [], png: null, target: localTarget, expected: null },
|
||||
{
|
||||
name: "remote file list",
|
||||
text: "",
|
||||
files: ["/x/a b.txt"],
|
||||
png: null,
|
||||
target: remoteTarget,
|
||||
expected: "/x/a b.txt",
|
||||
},
|
||||
])("macOS menu paste of $name never asks WebKit", async ({ text, files, png, target, expected }) => {
|
||||
usePlatform.setState({ os: "macos" });
|
||||
vi.spyOn(commands, "clipboardText").mockResolvedValue({ status: "ok", data: text });
|
||||
vi.spyOn(commands, "clipboardFileList").mockResolvedValue({ status: "ok", data: files });
|
||||
vi.spyOn(commands, "clipboardImage").mockResolvedValue({
|
||||
status: "ok",
|
||||
data: png && `data:image/png;base64,${png}`,
|
||||
});
|
||||
const store = vi.spyOn(commands, "termStoreImage").mockResolvedValue({ status: "ok", data: "/tmp/shot.png" });
|
||||
const web = { readText: vi.fn(async () => "web"), read: vi.fn(async () => []) };
|
||||
Object.defineProperty(navigator, "clipboard", { configurable: true, value: web });
|
||||
const exec = vi.fn(() => false);
|
||||
Object.defineProperty(document, "queryCommandSupported", { configurable: true, value: () => true });
|
||||
Object.defineProperty(document, "execCommand", { configurable: true, value: exec });
|
||||
const paste = vi.fn();
|
||||
try {
|
||||
await pasteTerminalClipboard(paste, () => target());
|
||||
} finally {
|
||||
Reflect.deleteProperty(document, "queryCommandSupported");
|
||||
Reflect.deleteProperty(document, "execCommand");
|
||||
}
|
||||
expect(exec).not.toHaveBeenCalled();
|
||||
expect(web.readText).not.toHaveBeenCalled();
|
||||
expect(web.read).not.toHaveBeenCalled();
|
||||
expect(paste.mock.calls).toEqual(expected ? [[expected]] : []);
|
||||
if (png) expect(store).toHaveBeenCalledWith(42, png, "image/png");
|
||||
expect(notify).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps the WebView menu paste for a browser on macOS", async () => {
|
||||
vi.unstubAllGlobals();
|
||||
const store = vi.spyOn(commands, "termStoreImage").mockResolvedValue({ status: "ok", data: "/remote/image.png" });
|
||||
Object.defineProperty(navigator, "clipboard", {
|
||||
configurable: true,
|
||||
value: { readText: async () => "", read: async () => [{ types: ["image/png"], getType: async () => image() }] },
|
||||
});
|
||||
const paste = vi.fn();
|
||||
await pasteTerminalClipboard(paste, () => remoteTarget());
|
||||
expect(store).toHaveBeenCalledWith(42, "AQI=", "image/png");
|
||||
expect(paste).toHaveBeenCalledWith("'/remote/image.png'");
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { pasteClipboardFiles, readClipboardPlainText, readClipboardText } from "@/platform/clipboard";
|
||||
import { pasteClipboardFiles, readClipboardPlainText, readClipboardPng, readClipboardText } from "@/platform/clipboard";
|
||||
import { toBase64 } from "@/platform/base64";
|
||||
import { call, commands, type TerminalId } from "@/platform/ipc";
|
||||
import { call, commands, isTauri, type TerminalId } from "@/platform/ipc";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import { usePlatform } from "@/platform/platformInfo";
|
||||
import { notifications } from "@/workbench/contributions";
|
||||
@@ -139,6 +139,28 @@ export async function pasteTerminalClipboard(paste: (text: string) => void, getT
|
||||
return;
|
||||
}
|
||||
}
|
||||
// WebKit on macOS asks the user to confirm execCommand("paste") and Clipboard.read() from a
|
||||
// menu with a "Paste" callout; the backend reads files, text and images without asking.
|
||||
if (target && isTauri() && usePlatform.getState().os === "macos") {
|
||||
const paths: string[] = await call(commands.clipboardFileList()).catch(() => []);
|
||||
const text =
|
||||
paths.length && !target.remote ? pathsText(paths, target) : await readClipboardPlainText().catch(() => "");
|
||||
if (!target.current()) return;
|
||||
if (text) {
|
||||
paste(text);
|
||||
return;
|
||||
}
|
||||
const png = await readClipboardPng();
|
||||
if (!target.current()) return;
|
||||
if (png)
|
||||
await pasteMedia(
|
||||
new Blob([Uint8Array.from(atob(png), (c) => c.charCodeAt(0))], { type: "image/png" }),
|
||||
target,
|
||||
paste,
|
||||
);
|
||||
else if (paths.length) paste(paths.join("\n"));
|
||||
return;
|
||||
}
|
||||
// Native paste preserves clipboard blobs where Clipboard.read() is unavailable.
|
||||
if (target && document.queryCommandSupported?.("paste") && document.execCommand("paste")) return;
|
||||
const images =
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { commands } from "./ipc";
|
||||
import { usePlatform } from "./platformInfo";
|
||||
import {
|
||||
pasteClipboardFiles,
|
||||
readClipboardPlainText,
|
||||
readClipboardPng,
|
||||
readClipboardText,
|
||||
writeClipboardPng,
|
||||
@@ -44,6 +46,29 @@ describe("clipboard text", () => {
|
||||
expect(list).toHaveBeenCalledTimes(calls);
|
||||
});
|
||||
|
||||
// WebKit on macOS asks the user to confirm navigator.clipboard reads outside a paste event.
|
||||
it("treats no native text on macOS as empty, but falls back when the backend refuses", async () => {
|
||||
usePlatform.setState({ os: "macos" });
|
||||
const readText = vi.fn(async () => "web");
|
||||
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { readText } });
|
||||
const native = vi.spyOn(commands, "clipboardText").mockResolvedValue({ status: "ok", data: null });
|
||||
expect(await readClipboardPlainText()).toBe("");
|
||||
expect(readText).not.toHaveBeenCalled();
|
||||
native.mockRejectedValue(new Error("restricted"));
|
||||
expect(await readClipboardPlainText()).toBe("web");
|
||||
});
|
||||
|
||||
it("falls back to the WebView where the backend has no text reader", async () => {
|
||||
usePlatform.setState({ os: "windows" });
|
||||
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { readText: async () => "web" } });
|
||||
vi.spyOn(commands, "clipboardText").mockResolvedValue({ status: "ok", data: null });
|
||||
try {
|
||||
expect(await readClipboardPlainText()).toBe("web");
|
||||
} finally {
|
||||
usePlatform.setState({ os: "macos" });
|
||||
}
|
||||
});
|
||||
|
||||
it("does not consult files for an empty text-only event or a browser", async () => {
|
||||
const list = vi.spyOn(commands, "clipboardFileList");
|
||||
expect(await readClipboardText(transfer("", false))).toBe("");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { call, commands, isTauri } from "./ipc";
|
||||
import { ipcPath } from "./paths";
|
||||
import { toBase64 } from "./base64";
|
||||
import { usePlatform } from "./platformInfo";
|
||||
|
||||
/** Native menu actions and async work can outlive WebKit's user activation. */
|
||||
export async function writeClipboardText(text: string): Promise<void> {
|
||||
@@ -28,8 +29,10 @@ export async function writeClipboardPng(png: Promise<Blob>): Promise<void> {
|
||||
*/
|
||||
export async function readClipboardPlainText(): Promise<string> {
|
||||
if (isTauri()) {
|
||||
const text = await call(commands.clipboardText()).catch(() => null);
|
||||
const text = await call(commands.clipboardText()).catch(() => undefined);
|
||||
if (text != null) return text;
|
||||
// WebKit on macOS makes the user confirm the read with a "Paste" callout; the backend found no text.
|
||||
if (text === null && usePlatform.getState().os === "macos") return "";
|
||||
}
|
||||
return (await navigator.clipboard?.readText()) ?? "";
|
||||
}
|
||||
|
||||
Reference in new issue
Заблокувати користувача