From 386f5252aae95d1dfcaeef1ae67a2ffe1b342807 Mon Sep 17 00:00:00 2001 From: margrop <1731827+margrop@users.noreply.github.com> Date: Sun, 18 Oct 2026 19:15:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E4=BC=A0=E9=A1=B5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=8E=E5=89=AA=E8=B4=B4=E6=9D=BF=E7=B2=98=E8=B4=B4?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/upload-zone.tsx | 99 +++++++++++++++++++++++++++++++++- src/lib/translations.ts | 12 +++++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/components/upload-zone.tsx b/src/components/upload-zone.tsx index d8110aa0..32a56c10 100644 --- a/src/components/upload-zone.tsx +++ b/src/components/upload-zone.tsx @@ -4,8 +4,9 @@ import { useCallback, useState, useEffect } from "react"; import { useDropzone } from "react-dropzone"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; -import { UploadCloud, Loader2, Monitor } from "lucide-react"; +import { UploadCloud, Loader2, Monitor, ClipboardPaste } from "lucide-react"; import { useLanguage } from "@/contexts/LanguageContext"; +import { frontendLogger } from "@/lib/frontend-logger"; // 添加 CaptureController 类型声明 declare global { @@ -56,6 +57,81 @@ export function UploadZone({ onImageSelect, isAnalyzing }: UploadZoneProps) { maxFiles: 1, disabled: isAnalyzing, }); + // 将剪贴板中的图片 Blob 转为 File 并交给父组件 + const selectClipboardImage = useCallback( + (blob: Blob) => { + const type = blob.type || "image/png"; + const ext = type.split("/")[1] || "png"; + const file = blob instanceof File && blob.name + ? blob + : new File([blob], `clipboard-${Date.now()}.${ext}`, { type }); + onImageSelect(file); + }, + [onImageSelect] + ); + + // 监听 Ctrl/Cmd+V 粘贴图片 + useEffect(() => { + const handlePaste = (event: ClipboardEvent) => { + if (isAnalyzing) return; + + // 在输入框中粘贴时不拦截 + const target = event.target as HTMLElement | null; + if (target && (target.isContentEditable || ["INPUT", "TEXTAREA"].includes(target.tagName))) { + return; + } + + const items = Array.from(event.clipboardData?.items ?? []); + const imageItem = items.find((item) => item.kind === "file" && item.type.startsWith("image/")); + const file = imageItem?.getAsFile(); + if (!file) return; + + event.preventDefault(); + selectClipboardImage(file); + }; + + window.addEventListener("paste", handlePaste); + return () => window.removeEventListener("paste", handlePaste); + }, [isAnalyzing, selectClipboardImage]); + + // 检查是否支持通过按钮读取剪贴板(需要安全上下文) + const isClipboardReadSupported = () => { + return isClient && + typeof navigator !== 'undefined' && + !!navigator.clipboard && + typeof navigator.clipboard.read === 'function'; + }; + + // 点击按钮读取剪贴板图片 + const handleClipboardRead = async () => { + if (!isClipboardReadSupported()) { + alert(t.upload.pasteNotSupported); + return; + } + + try { + const clipboardItems = await navigator.clipboard.read(); + for (const item of clipboardItems) { + const imageType = item.types.find((type) => type.startsWith("image/")); + if (imageType) { + const blob = await item.getType(imageType); + selectClipboardImage(blob); + return; + } + } + alert(t.upload.pasteNoImage); + } catch (error) { + frontendLogger.warn('[UploadZone]', 'Clipboard read failed', { + error: error instanceof Error ? error.message : String(error), + }); + if (error instanceof Error && error.name === 'NotAllowedError') { + alert(t.upload.pastePermissionDenied); + } else { + alert(t.upload.pasteFailed); + } + } + }; + // 检查是否支持屏幕截图 const isScreenshotSupported = () => { return isClient && @@ -206,6 +282,24 @@ export function UploadZone({ onImageSelect, isAnalyzing }: UploadZoneProps) { +
+ {t.upload.pasteDesc} +
+