Skip to content

Commit b371bc8

Browse files
committed
feat: 添加文章编辑和新建页面,集成Markdown编辑器及文章管理功能
1 parent c2b9b76 commit b371bc8

5 files changed

Lines changed: 390 additions & 229 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use client';
2+
3+
import { useState, useEffect } from 'react';
4+
import { useParams } from 'next/navigation';
5+
import Link from 'next/link';
6+
import http from '@/lib/axios';
7+
import MarkdownEditor from '@/components/MarkdownEditor';
8+
import type { PostWithLabels } from '@/types/api';
9+
10+
export default function EditPostPage() {
11+
const params = useParams();
12+
const postId = params.id as string;
13+
14+
const [post, setPost] = useState<PostWithLabels | null>(null);
15+
const [isLoading, setIsLoading] = useState(true);
16+
const [error, setError] = useState<string | null>(null);
17+
18+
useEffect(() => {
19+
const fetchPost = async () => {
20+
try {
21+
const data = await http.get<PostWithLabels>(`/posts/${postId}`);
22+
if (data) {
23+
setPost(data);
24+
} else {
25+
setError('文章不存在');
26+
}
27+
} catch (err) {
28+
console.error('获取文章失败:', err);
29+
setError('获取文章失败');
30+
} finally {
31+
setIsLoading(false);
32+
}
33+
};
34+
35+
if (postId) {
36+
fetchPost();
37+
}
38+
}, [postId]);
39+
40+
if (isLoading) {
41+
return (
42+
<div className="flex items-center justify-center h-64">
43+
<span className="loading loading-spinner loading-lg text-primary"></span>
44+
</div>
45+
);
46+
}
47+
48+
if (error || !post) {
49+
return (
50+
<div className="space-y-4">
51+
<Link href="/dashboard/posts" className="btn btn-ghost btn-sm">
52+
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
53+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" />
54+
</svg>
55+
返回
56+
</Link>
57+
<div className="alert alert-error">
58+
<svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
59+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
60+
</svg>
61+
<span>{error || '文章不存在'}</span>
62+
</div>
63+
</div>
64+
);
65+
}
66+
67+
return (
68+
<div className="space-y-4">
69+
<div className="flex items-center justify-between">
70+
<div className="flex items-center gap-4">
71+
<Link href="/dashboard/posts" className="btn btn-ghost btn-sm">
72+
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
73+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" />
74+
</svg>
75+
返回
76+
</Link>
77+
<h2 className="text-2xl font-bold">编辑文章</h2>
78+
<span className="text-sm text-base-content/50">ID: {postId}</span>
79+
</div>
80+
</div>
81+
82+
<div className="card bg-base-100 shadow-md">
83+
<div className="card-body p-0">
84+
<MarkdownEditor
85+
blogMode
86+
initialData={{
87+
id: post.id,
88+
title: post.title,
89+
slug: post.slug,
90+
content: post.content,
91+
excerpt: post.excerpt || '',
92+
coverImage: post.featured_image || '',
93+
tags: post.labels || [],
94+
published: post.published,
95+
}}
96+
/>
97+
</div>
98+
</div>
99+
</div>
100+
);
101+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use client';
2+
3+
import Link from 'next/link';
4+
import MarkdownEditor from '@/components/MarkdownEditor';
5+
6+
export default function NewPostPage() {
7+
return (
8+
<div className="space-y-4">
9+
<div className="flex items-center justify-between">
10+
<div className="flex items-center gap-4">
11+
<Link href="/dashboard/posts" className="btn btn-ghost btn-sm">
12+
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
13+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" />
14+
</svg>
15+
返回
16+
</Link>
17+
<h2 className="text-2xl font-bold">新建文章</h2>
18+
</div>
19+
</div>
20+
21+
<div className="card bg-base-100 shadow-md">
22+
<div className="card-body p-0">
23+
<MarkdownEditor blogMode />
24+
</div>
25+
</div>
26+
</div>
27+
);
28+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function robots(): MetadataRoute.Robots {
55
rules: {
66
userAgent: '*',
77
allow: '/',
8-
disallow: ['/api/', '/auth/'],
8+
disallow: ['/api/', '/auth/', '/dashboard/'],
99
},
1010
sitemap: 'https://blog.exquisitecore.xyz/sitemap.xml',
1111
};

0 commit comments

Comments
 (0)