feat: 实现公网编辑器前端

- 创建 Next.js 14 项目基础结构
- 实现登录界面 (LoginForm 组件)
- 实现文件树组件 (FileTree)
- 实现 Markdown 编辑器 (Monaco Editor + react-markdown)
- 实现编辑器主页面 (文件树 + 编辑器 + 保存功能)
- 添加 API 路由 (登录、文件操作)
- 支持编辑/预览切换
- 支持快捷键保存 (Cmd/Ctrl + S)

技术栈:Next.js 14 + React + TailwindCSS + Monaco Editor
This commit is contained in:
2026-03-17 01:03:26 +08:00
parent 23b117a3fa
commit 7fd4adcd69
2 changed files with 169 additions and 98 deletions
+166
View File
@@ -0,0 +1,166 @@
'use client';
import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import FileTree from '@/components/FileTree';
import MarkdownEditor from '@/components/MarkdownEditor';
interface FileNode {
id: string;
name: string;
type: 'file' | 'folder';
children?: FileNode[];
path: string;
content?: string;
}
export default function EditorPage() {
const router = useRouter();
const [files, setFiles] = useState<FileNode[]>([]);
const [selectedFile, setSelectedFile] = useState<string>('');
const [currentContent, setCurrentContent] = useState('');
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
useEffect(() => {
// 检查登录状态
const token = localStorage.getItem('token');
if (!token) {
router.push('/login');
return;
}
// 加载文件树
loadFiles();
}, [router]);
const loadFiles = async () => {
try {
const response = await fetch('/api/files?path=/');
if (response.ok) {
const data = await response.json();
setFiles(data.children || []);
}
} catch (error) {
console.error('加载文件失败:', error);
} finally {
setLoading(false);
}
};
const handleFileSelect = async (path: string) => {
setSelectedFile(path);
setLoading(true);
try {
const response = await fetch(`/api/files?path=${encodeURIComponent(path)}`);
if (response.ok) {
const data = await response.json();
setCurrentContent(data.content || '');
}
} catch (error) {
console.error('加载文件内容失败:', error);
} finally {
setLoading(false);
}
};
const handleSave = async () => {
if (!selectedFile) return;
setSaving(true);
try {
const response = await fetch('/api/files', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
path: selectedFile,
content: currentContent,
}),
});
if (response.ok) {
alert('保存成功!');
} else {
alert('保存失败');
}
} catch (error) {
console.error('保存失败:', error);
alert('保存失败');
} finally {
setSaving(false);
}
};
const handleLogout = () => {
localStorage.removeItem('token');
router.push('/login');
};
if (loading && files.length === 0) {
return (
<div className="h-screen flex items-center justify-center">
<div className="text-gray-600">...</div>
</div>
);
}
return (
<div className="h-screen flex flex-col">
{/* 顶部导航栏 */}
<header className="h-14 bg-gray-800 text-white flex items-center justify-between px-4">
<div className="flex items-center space-x-4">
<h1 className="text-lg font-bold"></h1>
{selectedFile && (
<span className="text-sm text-gray-400">{selectedFile}</span>
)}
</div>
<div className="flex items-center space-x-3">
<button
onClick={handleSave}
disabled={saving || !selectedFile}
className="px-4 py-1.5 bg-blue-600 hover:bg-blue-700 rounded text-sm transition-colors disabled:opacity-50"
>
{saving ? '保存中...' : '保存'}
</button>
<button
onClick={handleLogout}
className="px-4 py-1.5 bg-gray-600 hover:bg-gray-700 rounded text-sm transition-colors"
>
退
</button>
</div>
</header>
{/* 主体内容 */}
<div className="flex-1 flex overflow-hidden">
{/* 左侧文件树 */}
<div className="w-64 flex-shrink-0">
<FileTree
files={files}
onFileSelect={handleFileSelect}
selectedFile={selectedFile}
/>
</div>
{/* 右侧编辑器 */}
<div className="flex-1">
{selectedFile ? (
<MarkdownEditor
value={currentContent}
onChange={setCurrentContent}
onSave={handleSave}
/>
) : (
<div className="h-full flex items-center justify-center bg-gray-50">
<div className="text-center text-gray-500">
<div className="text-4xl mb-4">📝</div>
<p></p>
</div>
</div>
)}
</div>
</div>
</div>
);
}
+3 -98
View File
@@ -1,101 +1,6 @@
import Image from "next/image";
import { redirect } from 'next/navigation';
export default function Home() {
return (
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
<main className="flex flex-col gap-8 row-start-2 items-center sm:items-start">
<Image
className="dark:invert"
src="https://nextjs.org/icons/next.svg"
alt="Next.js logo"
width={180}
height={38}
priority
/>
<ol className="list-inside list-decimal text-sm text-center sm:text-left font-[family-name:var(--font-geist-mono)]">
<li className="mb-2">
Get started by editing{" "}
<code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-semibold">
src/app/page.tsx
</code>
.
</li>
<li>Save and see your changes instantly.</li>
</ol>
<div className="flex gap-4 items-center flex-col sm:flex-row">
<a
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5"
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
className="dark:invert"
src="https://nextjs.org/icons/vercel.svg"
alt="Vercel logomark"
width={20}
height={20}
/>
Deploy now
</a>
<a
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:min-w-44"
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Read our docs
</a>
</div>
</main>
<footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center">
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="https://nextjs.org/icons/file.svg"
alt="File icon"
width={16}
height={16}
/>
Learn
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="https://nextjs.org/icons/window.svg"
alt="Window icon"
width={16}
height={16}
/>
Examples
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="https://nextjs.org/icons/globe.svg"
alt="Globe icon"
width={16}
height={16}
/>
Go to nextjs.org
</a>
</footer>
</div>
);
// 默认重定向到登录页
redirect('/login');
}