import Link from "next/link"; import { useRouter } from "next/router"; import { useEffect, useRef, useState } from "react"; import { ArrowUpRight, Braces, CheckCircle2, FileText, Globe2, Link2, ListChecks, Plus, ShieldCheck, Trash2, } from "lucide-react"; import GuidanceShell, { useGuidanceAccount } from "@/components/GuidanceShell"; import { EmptyState, KindBadge, Notice, PageHeading, kindNames, } from "@/components/GuidanceUI"; import { guidanceXml, type Guidance } from "@/content/guides/catalog"; import s from "@/styles/guidance.module.css"; const blank: Guidance = { id: "", revision: 1, kind: "guide", title: "", summary: "", applies: "", body: "", steps: [], checks: [], links: [], tags: [], }; type Preview = { document: Guidance; xml: string; links: { label: string; when: string; resolved: boolean }[]; }; function fieldName(path: string) { const labels: Record = { id: "文档短名", revision: "版本", kind: "文档类型", title: "标题", summary: "一句话摘要", applies: "适用条件", body: "正文", steps: "建议步骤", checks: "完成检查", tags: "标签", target: "目标文档 ID", relation: "关系类型", when: "读取条件", label: "链接标题", }; const [group, index, field] = path.split("."); if (group === "links") return ( "关联文档" + (index === undefined ? "" : " " + (Number(index) + 1)) + (field ? " · " + (labels[field] || "内容") : "") ); return ( (labels[group] || "文档内容") + (index !== undefined ? " " + (Number(index) + 1) : "") ); } async function responseBody(response: Response) { const body = await response .json() .catch(() => ({ error: "服务返回异常,请稍后重试" })); if (!response.ok) throw Error( (body.error || "请求失败") + (body.fields?.length ? ":" + body.fields.map(fieldName).join("、") : ""), ); return body; } export default function Write() { return ( ); } function Editor() { const account = useGuidanceAccount(), router = useRouter(), dialog = useRef(null); const [doc, setDoc] = useState(blank), [xml, setXml] = useState(""), [mode, setMode] = useState<"form" | "xml">("form"), [busy, setBusy] = useState(false), [loading, setLoading] = useState(false), [error, setError] = useState(""), [preview, setPreview] = useState(null), [published, setPublished] = useState<{ id: string; revision: number; } | null>(null), [dirty, setDirty] = useState(true); const prefix = account.user ? "user-" + account.user.id + "-" : "", editId = typeof router.query.id === "string" ? router.query.id : ""; useEffect(() => { if (!router.isReady || !account.user) return; let active = true; setError(""); setPublished(null); setPreview(null); setMode("form"); setDirty(true); if (!editId) { setDoc({ ...blank, id: "user-" + account.user.id + "-document-" + Math.random().toString(16).slice(2, 8), }); setLoading(false); return; } if (!editId.startsWith("user-" + account.user.id + "-")) { setError("只能编辑你自己发布的文档。"); setDoc(blank); return; } setLoading(true); fetch("/api/documents/" + encodeURIComponent(editId)) .then(responseBody) .then((body) => { if (active) setDoc({ ...body.document, revision: body.document.revision + 1 }); }) .catch((e) => { if (active) { setError(e.message); setDoc(blank); } }) .finally(() => { if (active) setLoading(false); }); return () => { active = false; }; }, [account.user?.id, editId, router.isReady]); function change(key: K, value: Guidance[K]) { setDoc((current) => ({ ...current, [key]: value })); setPreview(null); setDirty(true); setPublished(null); } async function request(action: "preview" | "publish" | "convert") { setBusy(true); setError(""); setPreview(null); try { const body = await responseBody( await fetch( "/api/documents" + (action === "publish" ? "" : "/preview"), { method: "POST", headers: { "Content-Type": mode === "xml" ? "application/xml" : "application/json", }, body: mode === "xml" ? xml : JSON.stringify(normalized()), }, ), ); if (action === "convert") { setDoc(body.document); setMode("form"); } else if (action === "preview") { setPreview(body); dialog.current?.showModal(); } else { setDoc({ ...body.document, revision: body.document.revision + 1 }); setXml( guidanceXml({ ...body.document, revision: body.document.revision + 1, }), ); setPublished({ id: body.document.id, revision: body.document.revision, }); setDirty(false); } } catch (e) { setError(e instanceof Error ? e.message : "请求失败,请稍后重试"); } finally { setBusy(false); } } function normalized() { return { ...doc, steps: doc.steps.filter((step) => step.trim()), checks: doc.checks.filter((check) => check.trim()), tags: doc.tags.filter((tag) => tag.trim()), }; } function selectXml() { setXml(guidanceXml(normalized())); setMode("xml"); setPreview(null); } const authorized = account.user?.verified_at; return ( <> {account.loading ? (
正在读取账户…
) : !authorized ? (
{account.user ? "前往验证邮箱" : "登录 / 创建账户"}
) : ( <> {error && {error}} {published && ( 版本 v{published.revision} 已公开发布。
查看我的文档
)} {loading ? (
正在加载最新版本…
) : ( doc.id && (
{mode === "xml" ? (

协议源文档

可直接粘贴完整 XML。切回可视化编辑时,会先校验并解析当前内容;校验失败时保留输入。