Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: preserve native clipboard formats on paste

Tw93ad088e38dc7c
diff --git a/AGENTS.md b/AGENTS.md
index 37caac96..fcd96ad5 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -98,7 +98,7 @@ Execution rules:
- `--incognito` intentionally trades persistence for clean private sessions; be careful around login, cookies, local storage, and WeChat-style WebView detection.
- `--new-window` and `--multi-window` do not bypass every provider policy. Google OAuth and similar embedded-WebView restrictions may still require a normal browser or native client.
- macOS auth-popup behavior is fragile. Auth/sign-in URLs that trigger WebKit `SOAuthorization` popup creation should stay in the current window when that path can abort the app; changes in `src-tauri/src/inject/event.js` need targeted tests. Apple Sign-In (`appleid.apple.com` / `AppleAuthentication` named windows) is the exception and must keep the native `window.open` popup.
-- Safe clipboard shortcuts (Ctrl+C/X/V/A) on Linux/Windows are bridged by an injected `keydown` handler in `src-tauri/src/inject/event.js` (`handleClipboardShortcut`). It is gated on `isNonMacDesktop()` and `event.isTrusted`, only acts on editable/selected targets, and must never fire on macOS (native shortcuts already work). Changes here need the `tests/unit/event-clipboard-shortcuts.test.js` coverage.
+- Safe clipboard shortcuts (Ctrl+C/X/V/A) on Linux/Windows are bridged in `src-tauri/src/inject/event.js`. Copy/cut/select-all stay in the trusted `handleClipboardShortcut` keydown path; Ctrl+V must leave keydown unhandled so the native WebView paste event preserves images, files, and rich formats, with text-only `navigator.clipboard.readText()` fallback allowed only from a trusted keyup when no native paste event fired. The bridge is gated on `isNonMacDesktop()` and `event.isTrusted`, only acts on editable/selected targets, and must never fire on macOS (native shortcuts already work). Locked by `event-clipboard-shortcuts.test.js` tests `lets native paste preserve non-text clipboard data` and `falls back to clipboard text only when native paste does not fire`.
- Notification flows cross injected JS, Tauri invokes, capabilities, and native notification plugins. Verify the Rust capability and JS caller together.
- WebKit compositing behavior is platform-sensitive on Linux/Wayland. Runtime flag decisions live in `src-tauri/src/lib.rs`; keep the default conservative, cover compositor exceptions with unit tests, and document user-facing fallbacks in `docs/faq*.md`.
- Linux AppImage reports often include harmless GTK, appindicator, or GStreamer warnings. Separate optional runtime warnings from the actual symptom before changing code; input/click failures on pure Wayland compositors are not the same class as blank-window failures.
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js
index 20ee801d..632dc932 100644
--- a/src-tauri/src/inject/event.js
+++ b/src-tauri/src/inject/event.js
@@ -206,19 +206,9 @@ function insertTextIntoEditableElement(element, text) {
return false;
}
-function runBrowserPasteCommand() {
- try {
- return document.execCommand("paste") === true;
- } catch (error) {
- return false;
- }
-}
+let clipboardPasteFallbackTarget;
function pasteClipboardText(activeElement) {
- if (runBrowserPasteCommand()) {
- return;
- }
-
const readText = navigator.clipboard?.readText;
if (typeof readText !== "function") {
return;
@@ -261,9 +251,11 @@ function handleClipboardShortcut(event) {
}
if (key === "v" && canPasteIntoEditableElement(activeElement)) {
- event.preventDefault();
- pasteClipboardText(activeElement);
- return true;
+ // Let the native WebView paste event run first so images, files, and rich
+ // clipboard formats remain intact. If the platform does not emit paste,
+ // keyup applies the existing text-only fallback.
+ clipboardPasteFallbackTarget = activeElement;
+ return false;
}
if (key === "a" && isEditable && selectEditableElement(activeElement)) {
@@ -274,6 +266,42 @@ function handleClipboardShortcut(event) {
return false;
}
+function handleClipboardPasteFallback(event) {
+ if (
+ event.isTrusted !== true ||
+ !isNonMacDesktop() ||
+ event.key?.toLowerCase() !== "v"
+ ) {
+ return false;
+ }
+
+ const activeElement = clipboardPasteFallbackTarget;
+ clipboardPasteFallbackTarget = undefined;
+ if (
+ !activeElement ||
+ document.activeElement !== activeElement ||
+ !canPasteIntoEditableElement(activeElement)
+ ) {
+ return false;
+ }
+
+ pasteClipboardText(activeElement);
+ return true;
+}
+
+function handlePaste(event) {
+ clipboardPasteFallbackTarget = undefined;
+ if (!pasteAsPlainTextPending) return;
+
+ event.preventDefault();
+ event.stopImmediatePropagation();
+
+ const text = event.clipboardData?.getData("text/plain") || "";
+ if (text) {
+ document.execCommand("insertText", false, text);
+ }
+}
+
const DOWNLOADABLE_FILE_EXTENSIONS = {
documents: [
"pdf",
@@ -590,22 +618,8 @@ document.addEventListener("DOMContentLoaded", () => {
}
document.addEventListener("keydown", handleClipboardShortcut, true);
-
- document.addEventListener(
- "paste",
- (event) => {
- if (pasteAsPlainTextPending) {
- event.preventDefault();
- event.stopImmediatePropagation();
-
- const text = event.clipboardData?.getData("text/plain") || "";
- if (text) {
- document.execCommand("insertText", false, text);
- }
- }
- },
- true,
- );
+ document.addEventListener("keyup", handleClipboardPasteFallback, true);
+ document.addEventListener("paste", handlePaste, true);
// Trigger a native browser download via a transient anchor click. The Rust
// on_download handler then writes the file to the Downloads folder. This is
diff --git a/tests/unit/event-clipboard-shortcuts.test.js b/tests/unit/event-clipboard-shortcuts.test.js
index 3187a776..905c571b 100644
--- a/tests/unit/event-clipboard-shortcuts.test.js
+++ b/tests/unit/event-clipboard-shortcuts.test.js
@@ -31,7 +31,6 @@ function loadEventHelpers({
selectionText = "",
clipboardText = "clipboard text",
clipboardReadRejects = false,
- pasteCommandSucceeds = false,
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
} = {}) {
const source = fs.readFileSync(
@@ -140,9 +139,6 @@ function loadEventHelpers({
activeElement,
execCommand: (command, showUI, value) => {
execCommandCalls.push([command, showUI, value]);
- if (command === "paste") {
- return pasteCommandSucceeds;
- }
return true;
},
},
@@ -167,6 +163,18 @@ function getClipboardShortcutHandler(context) {
).handler;
}
+function getClipboardPasteFallbackHandler(context) {
+ return context.eventListeners.keyup.find(
+ ({ handler }) => handler.name === "handleClipboardPasteFallback",
+ ).handler;
+}
+
+function getPasteHandler(context) {
+ return context.eventListeners.paste.find(
+ ({ handler }) => handler.name === "handlePaste",
+ ).handler;
+}
+
function createKeyEvent(key, overrides = {}) {
return {
key,
@@ -210,56 +218,105 @@ describe("event clipboard shortcuts", () => {
expect(selectAllEvent.preventDefault).toHaveBeenCalled();
});
- it("pastes clipboard text into editable elements without Tauri clipboard IPC", async () => {
+ it("lets native paste preserve non-text clipboard data", async () => {
+ const editor = createElement("div");
+ editor.isContentEditable = true;
+ const context = loadEventHelpers({ activeElement: editor });
+ const shortcutHandler = getClipboardShortcutHandler(context);
+ const fallbackHandler = getClipboardPasteFallbackHandler(context);
+ const pasteHandler = getPasteHandler(context);
+ const keydownEvent = createKeyEvent("v");
+ const nativePasteEvent = {
+ clipboardData: {
+ types: ["Files", "image/png"],
+ getData: () => "",
+ },
+ preventDefault: vi.fn(),
+ stopImmediatePropagation: vi.fn(),
+ };
+
+ shortcutHandler(keydownEvent);
+ pasteHandler(nativePasteEvent);
+ fallbackHandler(createKeyEvent("v"));
+ await Promise.resolve();
+
+ expect(keydownEvent.preventDefault).not.toHaveBeenCalled();
+ expect(nativePasteEvent.preventDefault).not.toHaveBeenCalled();
+ expect(context.clipboardReadCalls).toEqual([]);
+ expect(context.execCommandCalls).toEqual([]);
+ });
+
+ it("keeps paste and match style text-only behavior", () => {
+ const input = createElement("input");
+ const context = loadEventHelpers({ activeElement: input });
+ const pasteHandler = getPasteHandler(context);
+ const pasteEvent = {
+ clipboardData: { getData: () => "plain text" },
+ preventDefault: vi.fn(),
+ stopImmediatePropagation: vi.fn(),
+ };
+
+ context.triggerPasteAsPlainText();
+ pasteHandler(pasteEvent);
+
+ expect(pasteEvent.preventDefault).toHaveBeenCalled();
+ expect(pasteEvent.stopImmediatePropagation).toHaveBeenCalled();
+ expect(context.execCommandCalls).toEqual([
+ ["paste", undefined, undefined],
+ ["insertText", false, "plain text"],
+ ]);
+ });
+
+ it("falls back to clipboard text only when native paste does not fire", async () => {
const input = createElement("input");
const context = loadEventHelpers({
activeElement: input,
clipboardText: "pasted text",
});
- const handler = getClipboardShortcutHandler(context);
- const pasteEvent = createKeyEvent("v");
+ const shortcutHandler = getClipboardShortcutHandler(context);
+ const fallbackHandler = getClipboardPasteFallbackHandler(context);
+ const keydownEvent = createKeyEvent("v");
- handler(pasteEvent);
+ shortcutHandler(keydownEvent);
+ expect(keydownEvent.preventDefault).not.toHaveBeenCalled();
+ expect(context.clipboardReadCalls).toEqual([]);
+
+ fallbackHandler(createKeyEvent("v"));
await Promise.resolve();
expect(context.clipboardReadCalls).toEqual([[]]);
expect(context.execCommandCalls).toEqual([
- ["paste", undefined, undefined],
["insertText", false, "pasted text"],
]);
- expect(pasteEvent.preventDefault).toHaveBeenCalled();
expect(context.invokeCalls).toEqual([]);
});
- it("uses the browser paste command first when available", async () => {
+ it("does not read clipboard for synthetic paste shortcuts", async () => {
const input = createElement("input");
- const context = loadEventHelpers({
- activeElement: input,
- pasteCommandSucceeds: true,
- });
+ const context = loadEventHelpers({ activeElement: input });
const handler = getClipboardShortcutHandler(context);
- const pasteEvent = createKeyEvent("v");
+ const pasteEvent = createKeyEvent("v", { isTrusted: false });
handler(pasteEvent);
await Promise.resolve();
expect(context.clipboardReadCalls).toEqual([]);
- expect(context.execCommandCalls).toEqual([["paste", undefined, undefined]]);
- expect(pasteEvent.preventDefault).toHaveBeenCalled();
+ expect(context.execCommandCalls).toEqual([]);
+ expect(pasteEvent.preventDefault).not.toHaveBeenCalled();
});
- it("does not read clipboard for synthetic paste shortcuts", async () => {
+ it("does not run a pending paste fallback from a synthetic keyup", async () => {
const input = createElement("input");
const context = loadEventHelpers({ activeElement: input });
- const handler = getClipboardShortcutHandler(context);
- const pasteEvent = createKeyEvent("v", { isTrusted: false });
+ const shortcutHandler = getClipboardShortcutHandler(context);
+ const fallbackHandler = getClipboardPasteFallbackHandler(context);
- handler(pasteEvent);
+ shortcutHandler(createKeyEvent("v"));
+ fallbackHandler(createKeyEvent("v", { isTrusted: false }));
await Promise.resolve();
expect(context.clipboardReadCalls).toEqual([]);
expect(context.execCommandCalls).toEqual([]);
- expect(pasteEvent.preventDefault).not.toHaveBeenCalled();
});
it("does not read clipboard for non-text input elements", async () => {
@@ -283,16 +340,18 @@ describe("event clipboard shortcuts", () => {
activeElement: input,
clipboardReadRejects: true,
});
- const handler = getClipboardShortcutHandler(context);
- const pasteEvent = createKeyEvent("v");
+ const shortcutHandler = getClipboardShortcutHandler(context);
+ const fallbackHandler = getClipboardPasteFallbackHandler(context);
+ const keydownEvent = createKeyEvent("v");
- handler(pasteEvent);
+ shortcutHandler(keydownEvent);
+ fallbackHandler(createKeyEvent("v"));
await Promise.resolve();
await Promise.resolve();
expect(context.clipboardReadCalls).toEqual([[]]);
- expect(context.execCommandCalls).toEqual([["paste", undefined, undefined]]);
- expect(pasteEvent.preventDefault).toHaveBeenCalled();
+ expect(context.execCommandCalls).toEqual([]);
+ expect(keydownEvent.preventDefault).not.toHaveBeenCalled();
});
it("leaves non-editable paste and macOS shortcuts untouched", () => {