-
Notifications
You must be signed in to change notification settings - Fork 362
fix(chat): support image paste from clipboard on Linux/Tauri #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1294,14 +1294,46 @@ export function MessageInput({ | |
| ) | ||
|
|
||
| const handlePaste = useCallback( | ||
| (event: React.ClipboardEvent<HTMLTextAreaElement>) => { | ||
| async (event: React.ClipboardEvent<HTMLTextAreaElement>) => { | ||
| if (disabled) return | ||
| const files = filesFromClipboard(event.clipboardData) | ||
| if (files.length === 0) return | ||
| event.preventDefault() | ||
| void appendFilesFromInput(files).catch((error) => { | ||
| console.error("[MessageInput] paste files failed:", error) | ||
| }) | ||
| if (files.length > 0) { | ||
| event.preventDefault() | ||
| void appendFilesFromInput(files).catch((error) => { | ||
| console.error("[MessageInput] paste files failed:", error) | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| // Fallback for Linux/Tauri webview where DataTransfer API doesn't | ||
| // expose screenshot images (e.g. WeChat screenshot). The async | ||
| // Clipboard API can read raw image blobs when the sync paste event | ||
| // yields nothing. | ||
| if (navigator.clipboard?.read) { | ||
| try { | ||
| const items = await navigator.clipboard.read() | ||
| const imageFiles: File[] = [] | ||
| for (const item of items) { | ||
| for (const type of item.types) { | ||
| if (type.startsWith("image/")) { | ||
| const blob = await item.getType(type) | ||
| const ext = type.split("/")[1] || "png" | ||
| imageFiles.push( | ||
| new File([blob], `clipboard-image.${ext}`, { type }) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| if (imageFiles.length > 0) { | ||
| event.preventDefault() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 这里的 |
||
| void appendFilesFromInput(imageFiles).catch((error) => { | ||
| console.error("[MessageInput] paste files failed:", error) | ||
| }) | ||
| } | ||
| } catch { | ||
| // Clipboard API may be blocked by permissions; silently fall through. | ||
| } | ||
| } | ||
| }, | ||
| [appendFilesFromInput, disabled] | ||
| ) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 降级被过度触发 + 破坏「文本优先」设计
filesFromClipboard()对纯文本也返回[](见 :230,检测到text/plain即return []),所以每一次普通文本粘贴都会进到这里执行navigator.clipboard.read()——一次完整、权限/隐私敏感的全剪贴板读取。MessageInput 在桌面与浏览器/服务器模式共用,浏览器模式下这是无谓开销、部分浏览器还会弹权限;而本 bug 只存在于 Tauri WebKitGTK webview。此外这段忽略了
filesFromClipboard()(:226-235)刻意的「文本优先」设计:复制电子表格单元格这类 text+image 混合内容时原本返回[]是为了让文本正常粘贴,这里会在所有平台把它劫持成图片附件;又因为是await之后才preventDefault(),文本其实已经粘进输入框,最终是「文本 + 图片」双重动作。建议把降级限定到 Tauri,并在同步剪贴板已有文本时直接返回(下方 suggestion,需额外
import { detectEnvironment } from "@/lib/transport/detect";若确认 web 端 Linux 也受影响可再放宽该判断):