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({