Files
nl-blogs/client/src/pages/admin/PostForm.vue

403 lines
13 KiB
Vue
Raw Normal View History

2026-01-15 13:51:44 +08:00
<template>
2026-01-16 11:16:32 +08:00
<div class="w-full animate-reveal h-[calc(100vh-8rem)] flex flex-col">
<!-- Top Bar -->
<div class="flex items-center justify-between mb-4 shrink-0">
<div class="flex items-center gap-4">
<h1 class="text-2xl font-serif italic text-white">{{ isEditing ? '编辑文章' : '新建文章' }}</h1>
<button
type="button"
@click="toggleSidebar"
class="p-2 rounded-md hover:bg-white/5 text-art-muted hover:text-white transition-colors border border-transparent hover:border-white/10"
:title="isSidebarOpen ? '收起侧边栏' : '展开侧边栏'"
>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="transition-transform duration-300" :class="{ 'rotate-180': !isSidebarOpen }">
<rect width="18" height="18" x="3" y="3" rx="2" ry="2"></rect>
<line x1="15" x2="15" y1="3" y2="21"></line>
</svg>
</button>
</div>
2026-01-16 11:06:59 +08:00
<div class="flex gap-3">
<button type="button" class="admin-btn-secondary" @click="handleCancel">
取消
</button>
<button type="button" class="admin-btn-primary" @click="handleSubmit" :disabled="isSubmitting">
{{ isSubmitting ? '提交中...' : (isEditing ? '更新文章' : '发布文章') }}
</button>
</div>
</div>
2026-01-15 16:37:58 +08:00
2026-01-16 11:16:32 +08:00
<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">
<MdEditor
v-model="form.content"
theme="dark"
:preview="false"
placeholder="开始创作..."
class="custom-md-editor flex-1"
/>
2026-01-15 13:51:44 +08:00
</div>
2026-01-16 11:06:59 +08:00
</div>
2026-01-16 11:16:32 +08:00
<!-- Right Column: Meta & Settings (Collapsible) -->
<div
class="shrink-0 transition-all duration-300 ease-[cubic-bezier(0.25,0.8,0.25,1)] overflow-hidden flex flex-col h-full"
:class="[isSidebarOpen ? 'w-80 opacity-100 translate-x-0' : 'w-0 opacity-0 translate-x-10']"
>
<div class="w-80 space-y-6 overflow-y-auto pr-1 h-full custom-scrollbar pb-6">
<div class="admin-card p-6 space-y-6">
<h3 class="text-sm font-medium text-white border-b border-white/5 pb-4 mb-4 flex items-center gap-2">
<span class="text-art-accent">📝</span> 文章属性
</h3>
<!-- Title Field -->
<div class="space-y-2">
<label for="title" class="block text-xs font-medium text-art-muted uppercase tracking-wider">文章标题</label>
<input
type="text"
id="title"
v-model="form.title"
class="admin-input font-medium"
placeholder="请输入标题"
required
/>
<div class="text-art-error text-xs mt-1" v-if="errors.title">
{{ errors.title }}
</div>
2026-01-16 11:06:59 +08:00
</div>
2026-01-16 11:16:32 +08:00
<!-- Excerpt Field -->
<div class="space-y-2">
<label for="excerpt" class="block text-xs font-medium text-art-muted uppercase tracking-wider">摘要</label>
<textarea
id="excerpt"
v-model="form.excerpt"
rows="4"
class="admin-input resize-none text-sm leading-relaxed"
placeholder="简短的介绍..."
></textarea>
<div class="text-art-error text-xs mt-1" v-if="errors.excerpt">
{{ errors.excerpt }}
</div>
2026-01-16 11:06:59 +08:00
</div>
2026-01-16 11:16:32 +08:00
<!-- Category Field -->
<div class="space-y-2">
<label for="category" class="block text-xs font-medium text-art-muted uppercase tracking-wider">分类</label>
2026-01-16 17:03:34 +08:00
<CustomSelect
v-model="form.categoryId"
:options="categories"
placeholder="选择文章分类"
2026-01-16 11:16:32 +08:00
/>
2026-01-16 17:03:34 +08:00
<div class="text-art-error text-xs mt-1" v-if="errors.categoryId">
{{ errors.categoryId }}
</div>
</div>
2026-01-19 16:14:08 +08:00
<!-- Column Field -->
<div class="space-y-2">
<label for="column" class="block text-xs font-medium text-art-muted uppercase tracking-wider">专栏 <span class="text-art-muted text-xs">(可选)</span></label>
<CustomSelect
v-model="form.columnId"
:options="columns"
placeholder="选择专栏(可选)"
/>
</div>
2026-01-16 17:03:34 +08:00
<!-- Tags Field -->
<div class="space-y-2">
<label class="block text-xs font-medium text-art-muted uppercase tracking-wider">标签</label>
<div class="flex flex-wrap gap-2">
<button
type="button"
v-for="tag in availableTags"
:key="tag.id"
@click="toggleTag(tag.id)"
class="px-2 py-1 text-xs rounded border transition-colors"
:class="form.tagIds.includes(tag.id) ? 'bg-art-accent/20 border-art-accent text-art-accent' : 'bg-white/5 border-white/10 text-art-muted hover:border-white/30'"
>
{{ tag.name }}
</button>
<div v-if="availableTags.length === 0" class="text-xs text-art-muted italic">
暂无可用标签
</div>
2026-01-16 11:16:32 +08:00
</div>
</div>
<!-- Date Field -->
<div class="space-y-2">
<label for="date" class="block text-xs font-medium text-art-muted uppercase tracking-wider">发布日期</label>
<input
type="date"
id="date"
v-model="form.date"
class="admin-input"
required
/>
<div class="text-art-error text-xs mt-1" v-if="errors.date">
{{ errors.date }}
</div>
</div>
<!-- Is Published Field -->
<div class="space-y-2">
<label for="isPublished" class="block text-xs font-medium text-art-muted uppercase tracking-wider">状态</label>
<CustomSelect
v-model="form.isPublished"
:options="publishStatusOptions"
placeholder="请选择发布状态"
/>
<div class="text-art-error text-xs mt-1" v-if="errors.isPublished">
{{ errors.isPublished }}
</div>
2026-01-16 11:06:59 +08:00
</div>
2026-01-15 13:51:44 +08:00
</div>
</div>
2026-01-16 11:06:59 +08:00
</div>
</form>
2026-01-15 13:51:44 +08:00
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
2026-01-15 16:37:58 +08:00
// 引入 md-editor-v3
import { MdEditor } from 'md-editor-v3'
import 'md-editor-v3/lib/style.css'
2026-01-15 13:51:44 +08:00
import { useToast } from '../../composables/useToast'
2026-01-19 16:14:08 +08:00
import { createPost, updatePost, fetchPost, fetchCategories, fetchColumns, fetchTags, Category, Tag } from '../../services/api'
2026-01-15 13:51:44 +08:00
import CustomSelect from '../../components/CustomSelect.vue'
const router = useRouter()
const route = useRoute()
const toast = useToast()
2026-01-16 11:16:32 +08:00
// Sidebar toggle state
const isSidebarOpen = ref(true)
const toggleSidebar = () => {
isSidebarOpen.value = !isSidebarOpen.value
}
2026-01-15 13:51:44 +08:00
// Form state
const isSubmitting = ref(false)
const isEditing = computed(() => !!route.params.id)
const errors = reactive<Record<string, string>>({})
2026-01-16 17:03:34 +08:00
// Data sources
const categories = ref<{value: number, label: string}[]>([])
2026-01-19 16:14:08 +08:00
const columns = ref<{value: number, label: string}[]>([])
2026-01-16 17:03:34 +08:00
const availableTags = ref<Tag[]>([])
2026-01-15 13:51:44 +08:00
// Form data
const form = reactive({
title: '',
2026-01-16 17:03:34 +08:00
categoryId: 0,
2026-01-19 16:14:08 +08:00
columnId: 0,
2026-01-15 13:51:44 +08:00
date: new Date().toISOString().split('T')[0],
excerpt: '',
content: '',
2026-01-16 17:03:34 +08:00
isPublished: 1,
tagIds: [] as number[]
2026-01-15 13:51:44 +08:00
})
// Select options
const publishStatusOptions = [
{ value: 1, label: '已发布' },
{ value: 0, label: '草稿' }
]
// Validation function
const validateForm = (): boolean => {
// Reset errors
Object.keys(errors).forEach(key => delete errors[key])
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
let isValid = true
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
// Validate title
if (!form.title.trim()) {
errors.title = '文章标题不能为空'
isValid = false
2026-01-16 11:16:32 +08:00
// Auto open sidebar if there are errors in it
isSidebarOpen.value = true
2026-01-15 13:51:44 +08:00
}
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
// Validate category
2026-01-16 17:03:34 +08:00
if (!form.categoryId) {
errors.categoryId = '请选择文章分类'
2026-01-15 13:51:44 +08:00
isValid = false
2026-01-16 11:16:32 +08:00
isSidebarOpen.value = true
2026-01-15 13:51:44 +08:00
}
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
// Validate date
if (!form.date) {
errors.date = '发布日期不能为空'
isValid = false
2026-01-16 11:16:32 +08:00
isSidebarOpen.value = true
2026-01-15 13:51:44 +08:00
}
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
// Validate content
if (!form.content.trim()) {
errors.content = '文章内容不能为空'
isValid = false
}
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
return isValid
}
2026-01-16 17:03:34 +08:00
// Toggle tag selection
const toggleTag = (tagId: number) => {
const index = form.tagIds.indexOf(tagId)
if (index === -1) {
form.tagIds.push(tagId)
} else {
form.tagIds.splice(index, 1)
}
}
2026-01-15 13:51:44 +08:00
// Submit handler
const handleSubmit = async () => {
if (!validateForm()) {
return
}
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
isSubmitting.value = true
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
try {
2026-01-16 17:03:34 +08:00
// Construct payload
2026-01-19 16:14:08 +08:00
const payload: any = {
2026-01-16 17:03:34 +08:00
title: form.title,
categoryId: form.categoryId,
date: form.date,
excerpt: form.excerpt,
content: form.content,
isPublished: form.isPublished,
tags: form.tagIds.map(id => ({ id } as any)) // Backend expects objects with ID
}
2026-01-19 16:14:08 +08:00
// Add columnId if selected
if (form.columnId > 0) {
payload.columnId = form.columnId
} else {
payload.columnId = null
}
2026-01-15 13:51:44 +08:00
if (isEditing.value) {
// Update existing post
2026-01-16 17:03:34 +08:00
await updatePost(route.params.id as string, payload)
2026-01-16 11:06:59 +08:00
toast.showToast('文章更新成功', 'success')
2026-01-15 13:51:44 +08:00
} else {
// Create new post
2026-01-16 17:03:34 +08:00
await createPost(payload)
2026-01-16 11:06:59 +08:00
toast.showToast('文章创建成功', 'success')
2026-01-15 13:51:44 +08:00
}
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
// Redirect to posts list
router.push('/admin/posts')
} catch (error: any) {
console.error('Error submitting form:', error)
2026-01-16 11:06:59 +08:00
toast.showToast(error.message || (isEditing.value ? '更新文章失败' : '创建文章失败'), 'error')
2026-01-15 13:51:44 +08:00
} finally {
isSubmitting.value = false
}
}
// Cancel handler
const handleCancel = () => {
router.push('/admin/posts')
}
2026-01-16 17:03:34 +08:00
// Load initial data
const loadData = async () => {
try {
2026-01-19 16:14:08 +08:00
// Fetch categories, columns and tags in parallel
const [cats, cols, tagList] = await Promise.all([
2026-01-16 17:03:34 +08:00
fetchCategories(),
2026-01-19 16:14:08 +08:00
fetchColumns(),
2026-01-16 17:03:34 +08:00
fetchTags()
])
categories.value = cats.map(c => ({
value: c.id,
label: c.name
}))
2026-01-19 16:14:08 +08:00
columns.value = cols.map(c => ({
value: c.id,
label: c.name
}))
availableTags.value = tagList
2026-01-16 17:03:34 +08:00
// If editing, load post data
if (isEditing.value) {
2026-01-15 13:51:44 +08:00
const postId = route.params.id as string
const post = await fetchPost(postId)
2026-01-15 16:37:58 +08:00
2026-01-15 13:51:44 +08:00
// Populate form with post data
form.title = post.title
2026-01-16 17:03:34 +08:00
form.categoryId = post.categoryId
2026-01-19 16:14:08 +08:00
form.columnId = post.columnId || 0
2026-01-15 13:51:44 +08:00
form.date = post.date
form.excerpt = post.excerpt || ''
form.content = post.content || ''
form.isPublished = post.isPublished === 1 ? 1 : 0
2026-01-16 17:03:34 +08:00
// Map tags to tagIds
if (post.tags && post.tags.length > 0) {
form.tagIds = post.tags.map(t => t.id)
}
2026-01-15 13:51:44 +08:00
}
2026-01-16 17:03:34 +08:00
} catch (error: any) {
console.error('Failed to load data:', error)
toast.showToast('加载数据失败: ' + (error.message || '未知错误'), 'error')
2026-01-15 13:51:44 +08:00
}
2026-01-16 17:03:34 +08:00
}
// Lifecycle
onMounted(() => {
loadData()
2026-01-15 13:51:44 +08:00
})
</script>
<style scoped>
2026-01-15 16:37:58 +08:00
:deep(.custom-md-editor) {
2026-01-16 11:06:59 +08:00
background-color: transparent !important;
2026-01-15 16:37:58 +08:00
--md-bk-color: transparent !important;
--md-color: white !important;
--md-border-color: transparent !important;
2026-01-16 11:16:32 +08:00
height: 100% !important;
2026-01-15 16:37:58 +08:00
}
:deep(.md-editor-toolbar-wrapper) {
2026-01-16 11:06:59 +08:00
background-color: rgba(255, 255, 255, 0.02) !important;
border-bottom: 1px solid rgba(255, 255, 255, 0.05) !important;
2026-01-16 11:16:32 +08:00
padding: 4px 8px;
2026-01-15 16:37:58 +08:00
}
:deep(.md-editor-content) {
background-color: transparent !important;
}
:deep(.md-editor-footer) {
2026-01-16 11:06:59 +08:00
background-color: rgba(255, 255, 255, 0.02) !important;
border-top: 1px solid rgba(255, 255, 255, 0.05) !important;
2026-01-15 13:51:44 +08:00
}
2026-01-16 11:16:32 +08:00
/* Custom Scrollbar for sidebar */
.custom-scrollbar::-webkit-scrollbar {
width: 4px;
}
.custom-scrollbar::-webkit-scrollbar-track {
background: transparent;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.1);
border-radius: 2px;
}
.custom-scrollbar:hover::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.2);
}
2026-01-16 11:06:59 +08:00
</style>