fix: insert uploaded file into vditor

This commit is contained in:
Tim
2025-08-01 00:41:52 +08:00
parent c6a133978b
commit 69b5e96695
+17 -6
View File
@@ -37,7 +37,8 @@ export function createVditor(editorId, options = {}) {
return searchUsers(value) return searchUsers(value)
} }
return new Vditor(editorId, { let vditor
vditor = new Vditor(editorId, {
placeholder, placeholder,
height: 'auto', height: 'auto',
theme: getEditorTheme(), theme: getEditorTheme(),
@@ -87,11 +88,20 @@ export function createVditor(editorId, options = {}) {
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) return '上传失败'
return JSON.stringify({
code: 0, const ext = file.name.split('.').pop().toLowerCase()
msg: '', const imageExts = ['apng','bmp','gif','ico','cur','jpg','jpeg','jfif','pjp','pjpeg','png','svg','webp']
data: { errFiles: [], succMap: { [file.name]: info.fileUrl } } const audioExts = ['wav','mp3','ogg']
}) let md
if (imageExts.includes(ext)) {
md = `![${file.name}](${info.fileUrl})`
} else if (audioExts.includes(ext)) {
md = `<audio controls="controls" src="${info.fileUrl}"></audio>`
} else {
md = `[${file.name}](${info.fileUrl})`
}
vditor.insertValue(md + '\n')
return null
} }
}, },
// upload: { // upload: {
@@ -125,4 +135,5 @@ export function createVditor(editorId, options = {}) {
input, input,
after after
}) })
return vditor
} }