fix: enable paste upload with progress

This commit is contained in:
Tim
2025-08-01 00:51:11 +08:00
parent 296b0a737f
commit 913ffa8a5a

View File

@@ -81,17 +81,42 @@ export function createVditor(editorId, options = {}) {
multiple: false, multiple: false,
handler: async (files) => { handler: async (files) => {
const file = files[0] const file = files[0]
const res = await fetch(`${API_BASE_URL}/api/upload/presign?filename=${encodeURIComponent(file.name)}`, { vditor.tip.show('图片上传中', 0)
headers: { Authorization: `Bearer ${getToken()}` } vditor.disabled()
}) const res = await fetch(
if (!res.ok) return '获取上传地址失败' `${API_BASE_URL}/api/upload/presign?filename=${encodeURIComponent(file.name)}`,
{ headers: { Authorization: `Bearer ${getToken()}` } }
)
if (!res.ok) {
vditor.enable()
vditor.tip.hide()
return '获取上传地址失败'
}
const info = await res.json() const info = await res.json()
const put = await fetch(info.uploadUrl, { method: 'PUT', body: file }) const put = await fetch(info.uploadUrl, { method: 'PUT', body: file })
if (!put.ok) return '上传失败' if (!put.ok) {
vditor.enable()
vditor.tip.hide()
return '上传失败'
}
const ext = file.name.split('.').pop().toLowerCase() const ext = file.name.split('.').pop().toLowerCase()
const imageExts = ['apng','bmp','gif','ico','cur','jpg','jpeg','jfif','pjp','pjpeg','png','svg','webp'] const imageExts = [
const audioExts = ['wav','mp3','ogg'] 'apng',
'bmp',
'gif',
'ico',
'cur',
'jpg',
'jpeg',
'jfif',
'pjp',
'pjpeg',
'png',
'svg',
'webp'
]
const audioExts = ['wav', 'mp3', 'ogg']
let md let md
if (imageExts.includes(ext)) { if (imageExts.includes(ext)) {
md = `![${file.name}](${info.fileUrl})` md = `![${file.name}](${info.fileUrl})`
@@ -101,6 +126,8 @@ export function createVditor(editorId, options = {}) {
md = `[${file.name}](${info.fileUrl})` md = `[${file.name}](${info.fileUrl})`
} }
vditor.insertValue(md + '\n') vditor.insertValue(md + '\n')
vditor.enable()
vditor.tip.hide()
return null return null
} }
}, },