From f0127686a2b3ed57c2ff9768f8f135f4d74a91ea Mon Sep 17 00:00:00 2001 From: pipipi-pikachu Date: Mon, 24 Feb 2025 21:27:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=8B=96=E6=8B=BD/=E7=B2=98=E8=B4=B4=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/configs/hotkey.ts | 1 + src/hooks/usePasteEvent.ts | 5 +++- .../Editor/Canvas/hooks/useDropImageOrText.ts | 23 +++++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/configs/hotkey.ts b/src/configs/hotkey.ts index 42089788..3494098e 100644 --- a/src/configs/hotkey.ts +++ b/src/configs/hotkey.ts @@ -141,6 +141,7 @@ export const HOTKEY_DOC: HotkeyItem[] = [ { label: '添加图片 - 将本地图片拖拽到画布中' }, { label: '添加图片 - 在画布中粘贴SVG代码' }, { label: '添加图片 - 粘贴来自 pexels 的图片链接' }, + { label: '添加文本 - 粘贴来自系统剪贴板的文字' }, { label: '添加文本 - 将外部选中文字拖拽到画布中' }, { label: '文本编辑 - 支持 markdown 语法创建列表和引用' }, ], diff --git a/src/hooks/usePasteEvent.ts b/src/hooks/usePasteEvent.ts index 48f06183..21c81fc2 100644 --- a/src/hooks/usePasteEvent.ts +++ b/src/hooks/usePasteEvent.ts @@ -32,13 +32,16 @@ export default () => { if (!clipboardDataFirstItem) return // 如果剪贴板内有图片,优先尝试读取图片 + let isImage = false for (const item of clipboardDataItems) { if (item.kind === 'file' && item.type.indexOf('image') !== -1) { const imageFile = item.getAsFile() if (imageFile) pasteImageFile(imageFile) - return + isImage = true } } + + if (isImage) return // 如果剪贴板内没有图片,但有文字内容,尝试解析文字内容 if (clipboardDataFirstItem.kind === 'string' && clipboardDataFirstItem.type === 'text/plain') { diff --git a/src/views/Editor/Canvas/hooks/useDropImageOrText.ts b/src/views/Editor/Canvas/hooks/useDropImageOrText.ts index 0847376d..e481fa6f 100644 --- a/src/views/Editor/Canvas/hooks/useDropImageOrText.ts +++ b/src/views/Editor/Canvas/hooks/useDropImageOrText.ts @@ -13,17 +13,26 @@ export default (elementRef: Ref) => { // 拖拽元素到画布中 const handleDrop = (e: DragEvent) => { if (!e.dataTransfer || e.dataTransfer.items.length === 0) return - const dataTransferItem = e.dataTransfer.items[0] + + const dataItems = e.dataTransfer.items + const dataTransferFirstItem = dataItems[0] // 检查事件对象中是否存在图片,存在则插入图片,否则继续检查是否存在文字,存在则插入文字 - if (dataTransferItem.kind === 'file' && dataTransferItem.type.indexOf('image') !== -1) { - const imageFile = dataTransferItem.getAsFile() - if (imageFile) { - getImageDataURL(imageFile).then(dataURL => createImageElement(dataURL)) + let isImage = false + for (const item of dataItems) { + if (item.kind === 'file' && item.type.indexOf('image') !== -1) { + const imageFile = item.getAsFile() + if (imageFile) { + getImageDataURL(imageFile).then(dataURL => createImageElement(dataURL)) + } + isImage = true } } - else if (dataTransferItem.kind === 'string' && dataTransferItem.type === 'text/plain') { - dataTransferItem.getAsString(text => { + + if (isImage) return + + if (dataTransferFirstItem.kind === 'string' && dataTransferFirstItem.type === 'text/plain') { + dataTransferFirstItem.getAsString(text => { if (disableHotkeys.value) return const string = parseText2Paragraphs(text) createTextElement({