feat: 支持批量拖拽/粘贴系统图片

This commit is contained in:
pipipi-pikachu 2025-02-24 21:27:50 +08:00
parent faa198a8d0
commit f0127686a2
3 changed files with 21 additions and 8 deletions

View File

@ -141,6 +141,7 @@ export const HOTKEY_DOC: HotkeyItem[] = [
{ label: '添加图片 - 将本地图片拖拽到画布中' },
{ label: '添加图片 - 在画布中粘贴SVG代码' },
{ label: '添加图片 - 粘贴来自 pexels 的图片链接' },
{ label: '添加文本 - 粘贴来自系统剪贴板的文字' },
{ label: '添加文本 - 将外部选中文字拖拽到画布中' },
{ label: '文本编辑 - 支持 markdown 语法创建列表和引用' },
],

View File

@ -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') {

View File

@ -13,17 +13,26 @@ export default (elementRef: Ref<HTMLElement | undefined>) => {
// 拖拽元素到画布中
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({