Fix paste upload handler

This commit is contained in:
Tim
2025-08-01 02:09:50 +08:00
parent 22c2b41ac5
commit 97118e7bc8

View File

@@ -162,27 +162,32 @@ export function createVditor(editorId, options = {}) {
input, input,
after after
}) })
const container = document.getElementById(editorId) const pasteHandler = async (e) => {
if (container) { const items = e.clipboardData && e.clipboardData.items
container.addEventListener('paste', async (e) => { if (!items) return
const items = e.clipboardData && e.clipboardData.items const files = []
if (!items) return for (let i = 0; i < items.length; i++) {
const files = [] const item = items[i]
for (let i = 0; i < items.length; i++) { if (item.kind === 'file') {
const item = items[i] const file = item.getAsFile()
if (item.kind === 'file') { if (file) files.push(file)
const file = item.getAsFile()
if (file) files.push(file)
}
} }
if (files.length > 0) { }
e.preventDefault() if (files.length > 0) {
const err = await vditor.options.upload.handler(files) e.preventDefault()
if (typeof err === 'string') { const err = await vditor.options.upload.handler(files)
vditor.tip(err) if (typeof err === 'string') {
} vditor.tip(err)
} }
}) }
} }
const elements = []
if (vditor.wysiwyg) elements.push(vditor.wysiwyg.element)
if (vditor.ir) elements.push(vditor.ir.element)
if (vditor.sv) elements.push(vditor.sv.element)
elements.forEach(el => {
el.addEventListener('paste', pasteHandler)
})
return vditor return vditor
} }