fix: keep formatting when pasting from the context menu on WebKitGTK

For content copied from another app, WebKitGTK's navigator.clipboard.read() often
returns nothing, or only text/plain, while Ctrl+V still receives the HTML. The
context menu then pasted plain text and lost bold, links and other formatting.

Whatever the web clipboard leaves out is now filled in from the native clipboard.
The new read_clipboard_contents command (replacing read_clipboard_text) returns
both text and HTML through arboard. The result is replayed through the same paste
handlers as Ctrl+V. That includes the WebKitGTK image fallback for an <img>-only
clipboard. The direct native image read only runs when there is no text or HTML.
This commit is contained in:
Shawn Olson committed 2026-10-09 12:34:54 -07:00
1 parent ec479ced8e
commit 32b1e091fd
6 files changed
+92 -29

No files matched your search

+16 -8
View File
@@ -2029,19 +2029,27 @@ pub fn copy_text_to_clipboard(_text: String) -> Result<(), String> {
Err("Text clipboard copy is only supported on desktop".to_string())
}
/// Read text from the system clipboard through the native backend.
/// Text and HTML on the system clipboard, read through the native backend.
#[derive(Debug, Default, Clone, serde::Serialize)]
pub struct ClipboardContents {
pub text: Option<String>,
pub html: Option<String>,
}
/// Read text and HTML from the system clipboard through the native backend.
/// Works around the WebKitGTK clipboard bug where the async `navigator.clipboard`
/// API returns empty for text copied from external applications.
/// API returns nothing, or only plain text, for content copied from external
/// applications. Each format is optional; a missing one is not an error.
#[cfg(desktop)]
#[tauri::command]
pub async fn read_clipboard_text() -> Result<String, String> {
pub async fn read_clipboard_contents() -> Result<ClipboardContents, String> {
// Reading the clipboard can block while the owning application answers, like the image read below.
tauri::async_runtime::spawn_blocking(move || {
let mut clipboard =
arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?;
clipboard
.get_text()
.map_err(|e| format!("Failed to read text from clipboard: {}", e))
let text = clipboard.get_text().ok().filter(|text| !text.is_empty());
let html = clipboard.get().html().ok().filter(|html| !html.is_empty());
Ok(ClipboardContents { text, html })
})
.await
.map_err(|error| error.to_string())?
@@ -2049,8 +2057,8 @@ pub async fn read_clipboard_text() -> Result<String, String> {
#[cfg(mobile)]
#[tauri::command]
pub async fn read_clipboard_text() -> Result<String, String> {
Err("Text clipboard read is only supported on desktop".to_string())
pub async fn read_clipboard_contents() -> Result<ClipboardContents, String> {
Err("Native clipboard read is only supported on desktop".to_string())
}
/// Read image from system clipboard (bypasses WebKitGTK clipboard bug).
+1 -1
View File
@@ -212,7 +212,7 @@ pub fn run() {
commands::load_vault_state,
commands::save_vault_state,
commands::copy_text_to_clipboard,
commands::read_clipboard_text,
commands::read_clipboard_contents,
commands::read_clipboard_image,
commands::copy_image_to_clipboard,
commands::save_image,