数据结构优化
This commit is contained in:
@@ -29,7 +29,10 @@
|
||||
<form @submit.prevent="handleSubmit" class="flex-1 flex overflow-hidden gap-6 relative">
|
||||
<!-- Left Column: Main Editor -->
|
||||
<div class="flex-1 flex flex-col min-w-0 transition-all duration-300 ease-in-out h-full">
|
||||
<div class="admin-card flex-1 flex flex-col overflow-hidden border border-white/10 focus-within:border-art-accent/50 transition-colors">
|
||||
<div
|
||||
class="admin-card flex-1 flex flex-col overflow-hidden border border-white/10 focus-within:border-art-accent/50 transition-colors"
|
||||
@paste="handlePaste"
|
||||
>
|
||||
<MdEditor
|
||||
v-model="form.content"
|
||||
theme="dark"
|
||||
@@ -308,6 +311,71 @@ const handleCancel = () => {
|
||||
router.push('/admin/posts')
|
||||
}
|
||||
|
||||
// Handle paste event for image upload
|
||||
const handlePaste = async (e: ClipboardEvent) => {
|
||||
const items = e.clipboardData?.items
|
||||
if (!items) return
|
||||
|
||||
// Check if any item is an image
|
||||
let hasImage = false
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
if (items[i].type.startsWith('image/')) {
|
||||
hasImage = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasImage) return
|
||||
|
||||
// Prevent default paste behavior for images
|
||||
e.preventDefault()
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const item = items[i]
|
||||
if (item.type.startsWith('image/')) {
|
||||
const file = item.getAsFile()
|
||||
if (!file) continue
|
||||
|
||||
try {
|
||||
// Upload image
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
formData.append('categoryId', '1') // 默认上传到分类1
|
||||
formData.append('storageType', 'local')
|
||||
|
||||
const token = localStorage.getItem('token')
|
||||
const response = await fetch('http://localhost:8081/api/admin/attachments/upload', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {})
|
||||
},
|
||||
body: formData
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
throw new Error(errorData.message || '上传失败')
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
const imageUrl = data.result.fileUrl
|
||||
|
||||
// Insert markdown image syntax
|
||||
const imageMarkdown = ``
|
||||
const currentContent = form.content
|
||||
// Insert at the end of content (md-editor-v3 will handle cursor position)
|
||||
form.content = currentContent + (currentContent ? '\n\n' : '') + imageMarkdown + '\n'
|
||||
|
||||
toast.showToast('图片上传成功', 'success')
|
||||
} catch (error: any) {
|
||||
console.error('上传图片失败:', error)
|
||||
toast.showToast(error.message || '上传图片失败', 'error')
|
||||
}
|
||||
break // Only handle first image
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load initial data
|
||||
const loadData = async () => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user