bug修复
This commit is contained in:
1 parent
288f8bb851
commit
7f4f62702a
2 files changed
+655
-90
No files matched your search
+523
-39
@@ -3,83 +3,567 @@ import { EditorContent, useEditor } from "@tiptap/react";
|
||||
import StarterKit from "@tiptap/starter-kit";
|
||||
import Link from "@tiptap/extension-link";
|
||||
import Image from "@tiptap/extension-image";
|
||||
import { ArrowLeft, Bold, Heading2, Heading3, Image as ImageIcon, Italic, Link as LinkIcon, List, Plus, Redo2, Save, Trash2, Undo2 } from "lucide-react";
|
||||
import {
|
||||
ArrowLeft,
|
||||
Bold,
|
||||
Heading2,
|
||||
Heading3,
|
||||
Image as ImageIcon,
|
||||
Italic,
|
||||
Link as LinkIcon,
|
||||
List,
|
||||
Plus,
|
||||
Redo2,
|
||||
Save,
|
||||
Trash2,
|
||||
Undo2,
|
||||
} from "lucide-react";
|
||||
import { API, assetUrl } from "./api";
|
||||
|
||||
const SITE = "SIT0000000000001";
|
||||
type ContentType = "NEWS" | "CASE" | "HELP";
|
||||
type Heading = { level: number; text: string; sortNo: number };
|
||||
type Props = { contentType: ContentType; contentCode?: string; onBack: () => void };
|
||||
const META: Record<ContentType, { label: string; route: string; seoType: string }> = {
|
||||
type Props = {
|
||||
contentType: ContentType;
|
||||
contentCode?: string;
|
||||
onBack: () => void;
|
||||
};
|
||||
const META: Record<
|
||||
ContentType,
|
||||
{ label: string; route: string; seoType: string }
|
||||
> = {
|
||||
NEWS: { label: "新闻", route: "news", seoType: "NEWS_DETAIL" },
|
||||
CASE: { label: "案例", route: "cases", seoType: "CASE_DETAIL" },
|
||||
HELP: { label: "帮助", route: "help", seoType: "HELP_DETAIL" },
|
||||
};
|
||||
|
||||
async function post(path: string, body: unknown) {
|
||||
const response = await fetch(API + path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
||||
if (!response.ok) throw new Error((await response.json()).message || "请求失败");
|
||||
const response = await fetch(API + path, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!response.ok)
|
||||
throw new Error((await response.json()).message || "请求失败");
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/** 新闻、案例和帮助共用的独立内容编辑页。 */
|
||||
export default function ContentEditor({ contentType, contentCode, onBack }: Props) {
|
||||
export default function ContentEditor({
|
||||
contentType,
|
||||
contentCode,
|
||||
onBack,
|
||||
}: Props) {
|
||||
const meta = META[contentType];
|
||||
const [form, setForm] = useState({ title: "", slug: "", summary: "", primaryCategoryCode: "", coverMediaCode: "", coverMediaUrl: "", authorName: "", sourceName: "", featured: false, sortNo: 30, status: "DRAFT" });
|
||||
const [seo, setSeo] = useState({ title: "", description: "", keywords: "", h1: "", canonicalUrl: "", robots: "index,follow", ogTitle: "", ogDescription: "", ogImageUrl: "", status: "DRAFT" });
|
||||
const [form, setForm] = useState({
|
||||
title: "",
|
||||
slug: "",
|
||||
summary: "",
|
||||
primaryCategoryCode: "",
|
||||
coverMediaCode: "",
|
||||
coverMediaUrl: "",
|
||||
authorName: "",
|
||||
sourceName: "",
|
||||
featured: false,
|
||||
sortNo: 30,
|
||||
status: "DRAFT",
|
||||
});
|
||||
const [seo, setSeo] = useState({
|
||||
title: "",
|
||||
description: "",
|
||||
keywords: "",
|
||||
h1: "",
|
||||
canonicalUrl: "",
|
||||
robots: "index,follow",
|
||||
ogTitle: "",
|
||||
ogDescription: "",
|
||||
ogImageUrl: "",
|
||||
status: "DRAFT",
|
||||
});
|
||||
const [headings, setHeadings] = useState<Heading[]>([]);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [notice, setNotice] = useState("");
|
||||
const coverInputRef = useRef<HTMLInputElement>(null);
|
||||
const editor = useEditor({ extensions: [StarterKit, Link.configure({ openOnClick: false }), Image], content: "<p></p>" });
|
||||
const editor = useEditor({
|
||||
extensions: [StarterKit, Link.configure({ openOnClick: false }), Image],
|
||||
content: "<p></p>",
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!contentCode) return;
|
||||
post("/contents/detail", { siteCode: SITE, contentCode, contentType }).then((data) => {
|
||||
setForm({ ...data, coverMediaUrl: data.coverMediaUrl || "" });
|
||||
editor?.commands.setContent(data.contentBody || "<p></p>");
|
||||
}).catch((error) => setNotice(error.message));
|
||||
post("/seo/detail", { siteCode: SITE, pageType: meta.seoType, targetCode: contentCode }).then((data) => {
|
||||
setSeo({ title: data.title, description: data.description, keywords: data.keywords || "", h1: data.h1, canonicalUrl: data.canonicalUrl || "", robots: data.robots, ogTitle: data.ogTitle || "", ogDescription: data.ogDescription || "", ogImageUrl: data.ogImageUrl || "", status: data.status });
|
||||
setHeadings(data.headings || []);
|
||||
}).catch(() => undefined);
|
||||
post("/contents/detail", { siteCode: SITE, contentCode, contentType })
|
||||
.then((data) => {
|
||||
setForm({ ...data, coverMediaUrl: data.coverMediaUrl || "" });
|
||||
editor?.commands.setContent(data.contentBody || "<p></p>");
|
||||
})
|
||||
.catch((error) => setNotice(error.message));
|
||||
post("/seo/detail", {
|
||||
siteCode: SITE,
|
||||
pageType: meta.seoType,
|
||||
targetCode: contentCode,
|
||||
})
|
||||
.then((data) => {
|
||||
setSeo({
|
||||
title: data.title,
|
||||
description: data.description,
|
||||
keywords: data.keywords || "",
|
||||
h1: data.h1,
|
||||
canonicalUrl: data.canonicalUrl || "",
|
||||
robots: data.robots,
|
||||
ogTitle: data.ogTitle || "",
|
||||
ogDescription: data.ogDescription || "",
|
||||
ogImageUrl: data.ogImageUrl || "",
|
||||
status: data.status,
|
||||
});
|
||||
setHeadings(data.headings || []);
|
||||
})
|
||||
.catch(() => undefined);
|
||||
}, [contentCode, contentType, editor, meta.seoType]);
|
||||
|
||||
const upload = async (file?: File) => {
|
||||
if (!file) return;
|
||||
if (file.size > 10 * 1024 * 1024) { setNotice("图片不能超过 10MB"); return; }
|
||||
if (file.size > 10 * 1024 * 1024) {
|
||||
setNotice("图片不能超过 10MB");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setUploading(true); setNotice("");
|
||||
const body = new FormData(); body.append("siteCode", SITE); body.append("altText", form.title); body.append("file", file);
|
||||
const response = await fetch(API + "/media/upload-image", { method: "POST", body });
|
||||
if (response.status === 413) throw new Error("图片超过服务器上传限制,请联系管理员调整 Nginx 上传上限");
|
||||
if (!response.ok) throw new Error((await response.json().catch(() => ({}))).message || "上传失败");
|
||||
setUploading(true);
|
||||
setNotice("");
|
||||
const body = new FormData();
|
||||
body.append("siteCode", SITE);
|
||||
body.append("altText", form.title);
|
||||
body.append("file", file);
|
||||
const response = await fetch(API + "/media/upload-image", {
|
||||
method: "POST",
|
||||
body,
|
||||
});
|
||||
if (response.status === 413)
|
||||
throw new Error(
|
||||
"图片超过服务器上传限制,请联系管理员调整 Nginx 上传上限",
|
||||
);
|
||||
if (!response.ok)
|
||||
throw new Error(
|
||||
(await response.json().catch(() => ({}))).message || "上传失败",
|
||||
);
|
||||
const data = await response.json();
|
||||
setForm((current) => ({ ...current, coverMediaCode: data.mediaCode, coverMediaUrl: data.url }));
|
||||
setForm((current) => ({
|
||||
...current,
|
||||
coverMediaCode: data.mediaCode,
|
||||
coverMediaUrl: data.url,
|
||||
}));
|
||||
setNotice(`${meta.label}封面上传成功`);
|
||||
} catch (error) { setNotice(error instanceof Error ? error.message : "上传失败"); }
|
||||
finally { setUploading(false); if (coverInputRef.current) coverInputRef.current.value = ""; }
|
||||
} catch (error) {
|
||||
setNotice(error instanceof Error ? error.message : "上传失败");
|
||||
} finally {
|
||||
setUploading(false);
|
||||
if (coverInputRef.current) coverInputRef.current.value = "";
|
||||
}
|
||||
};
|
||||
const save = async () => {
|
||||
if (!editor) return;
|
||||
try {
|
||||
setSaving(true); setNotice("");
|
||||
const result = await post("/contents/save", { contentCode, siteCode: SITE, contentType, primaryCategoryCode: form.primaryCategoryCode, title: form.title, slug: form.slug, summary: form.summary, contentBody: editor.getHTML(), coverMediaCode: form.coverMediaCode, authorName: form.authorName, sourceName: form.sourceName, featured: form.featured, sortNo: form.sortNo, status: form.status });
|
||||
await post("/seo/save", { siteCode: SITE, pageType: meta.seoType, targetCode: result.contentCode, title: seo.title || form.title, description: seo.description || form.summary, keywords: seo.keywords, h1: seo.h1 || form.title, canonicalUrl: seo.canonicalUrl || `/${meta.route}/${form.slug}`, robots: seo.robots, ogTitle: seo.ogTitle, ogDescription: seo.ogDescription, ogImageUrl: seo.ogImageUrl || form.coverMediaUrl, structuredData: null, status: form.status, headings });
|
||||
setSaving(true);
|
||||
setNotice("");
|
||||
const result = await post("/contents/save", {
|
||||
contentCode,
|
||||
siteCode: SITE,
|
||||
contentType,
|
||||
primaryCategoryCode: form.primaryCategoryCode,
|
||||
title: form.title,
|
||||
slug: form.slug,
|
||||
summary: form.summary,
|
||||
contentBody: editor.getHTML(),
|
||||
coverMediaCode: form.coverMediaCode,
|
||||
authorName: form.authorName,
|
||||
sourceName: form.sourceName,
|
||||
featured: form.featured,
|
||||
sortNo: form.sortNo,
|
||||
status: form.status,
|
||||
});
|
||||
await post("/seo/save", {
|
||||
siteCode: SITE,
|
||||
pageType: meta.seoType,
|
||||
targetCode: result.contentCode,
|
||||
title: seo.title || form.title,
|
||||
description: seo.description || form.summary,
|
||||
keywords: seo.keywords,
|
||||
h1: seo.h1 || form.title,
|
||||
canonicalUrl: seo.canonicalUrl || `/${meta.route}/${form.slug}`,
|
||||
robots: seo.robots,
|
||||
ogTitle: seo.ogTitle,
|
||||
ogDescription: seo.ogDescription,
|
||||
ogImageUrl: seo.ogImageUrl || form.coverMediaUrl,
|
||||
structuredData: null,
|
||||
status: form.status,
|
||||
headings,
|
||||
});
|
||||
setNotice(`${meta.label}与 SEO 已保存`);
|
||||
if (!contentCode) setTimeout(onBack, 500);
|
||||
} catch (error) { setNotice(error instanceof Error ? error.message : "保存失败"); } finally { setSaving(false); }
|
||||
} catch (error) {
|
||||
setNotice(error instanceof Error ? error.message : "保存失败");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
const addLink = () => {
|
||||
const href = window.prompt("请输入链接地址");
|
||||
if (href) editor?.chain().focus().setLink({ href }).run();
|
||||
};
|
||||
const addImage = () => {
|
||||
const src = window.prompt("请输入图片地址");
|
||||
if (src) editor?.chain().focus().setImage({ src }).run();
|
||||
};
|
||||
const addLink = () => { const href = window.prompt("请输入链接地址"); if (href) editor?.chain().focus().setLink({ href }).run(); };
|
||||
const addImage = () => { const src = window.prompt("请输入图片地址"); if (src) editor?.chain().focus().setImage({ src }).run(); };
|
||||
|
||||
return <div className="product-editor"><div className="editor-top"><div className="breadcrumb-title"><button className="back" onClick={onBack}><ArrowLeft size={17}/>{meta.label}管理</button><span>/</span><h1>{contentCode ? `编辑${meta.label}` : `新增${meta.label}`}</h1>{notice&&<em>{notice}</em>}</div><div className="editor-actions"><button onClick={onBack}>取消</button><button className="primary" disabled={saving || !form.title || !form.slug} onClick={save}><Save size={16}/>{saving ? "保存中" : "保存"}</button></div></div><div className="editor-layout"><div className="editor-main">
|
||||
<section className="edit-section"><h2>基础信息</h2><div className="edit-grid"><Field label={`${meta.label}标题 *`}><input value={form.title} onChange={(event) => setForm({ ...form, title: event.target.value })}/></Field><Field label="英文路径 *"><input value={form.slug} onChange={(event) => setForm({ ...form, slug: event.target.value.toLowerCase().replace(/[^a-z0-9-]/g, "-") })}/></Field><Field label="作者"><input value={form.authorName || ""} onChange={(event) => setForm({ ...form, authorName: event.target.value })}/></Field><Field label="来源"><input value={form.sourceName || ""} onChange={(event) => setForm({ ...form, sourceName: event.target.value })}/></Field><Field label="排序"><input type="number" value={form.sortNo} onChange={(event) => setForm({ ...form, sortNo: Number(event.target.value) })}/></Field><Field wide label={`${meta.label}简介 *`}><textarea rows={4} value={form.summary || ""} onChange={(event) => setForm({ ...form, summary: event.target.value })}/></Field></div></section>
|
||||
<section className="edit-section"><h2>{meta.label}封面</h2><div className="cover-upload">{form.coverMediaUrl ? <img src={assetUrl(form.coverMediaUrl)} alt={`${meta.label}封面预览`}/> : <ImageIcon size={42}/>}<label className={uploading ? "uploading" : ""}><span>{uploading ? "上传中..." : "选择图片"}</span><input ref={coverInputRef} disabled={uploading} type="file" accept=".jpg,.jpeg,.png,.webp,.gif,image/jpeg,image/png,image/webp,image/gif" onClick={(event) => { event.currentTarget.value = ""; }} onChange={(event) => void upload(event.currentTarget.files?.[0])}/></label><p>支持 JPG、PNG、WebP、GIF,单图不超过 10MB</p>{notice && <small className="upload-notice">{notice}</small>}</div></section>
|
||||
<section className="edit-section"><h2>{meta.label}详情 *</h2><div className="rich-editor"><div className="rich-toolbar"><Tool title="撤销" onClick={() => editor?.chain().focus().undo().run()}><Undo2/></Tool><Tool title="重做" onClick={() => editor?.chain().focus().redo().run()}><Redo2/></Tool><Tool title="粗体" active={editor?.isActive("bold")} onClick={() => editor?.chain().focus().toggleBold().run()}><Bold/></Tool><Tool title="斜体" active={editor?.isActive("italic")} onClick={() => editor?.chain().focus().toggleItalic().run()}><Italic/></Tool><Tool title="二级标题" active={editor?.isActive("heading", { level: 2 })} onClick={() => editor?.chain().focus().toggleHeading({ level: 2 }).run()}><Heading2/></Tool><Tool title="三级标题" active={editor?.isActive("heading", { level: 3 })} onClick={() => editor?.chain().focus().toggleHeading({ level: 3 }).run()}><Heading3/></Tool><Tool title="项目列表" onClick={() => editor?.chain().focus().toggleBulletList().run()}><List/></Tool><Tool title="链接" onClick={addLink}><LinkIcon/></Tool><Tool title="插入图片" onClick={addImage}><ImageIcon/></Tool></div><EditorContent editor={editor}/></div></section>
|
||||
</div><aside className="editor-side"><section className="edit-section"><h2>发布</h2><Field label="状态"><select value={form.status} onChange={(event) => setForm({ ...form, status: event.target.value })}><option value="DRAFT">草稿</option><option value="PUBLISHED">上架</option><option value="OFFLINE">下线</option></select></Field><label className="check"><input type="checkbox" checked={form.featured} onChange={(event) => setForm({ ...form, featured: event.target.checked })}/>推荐内容</label></section><section className="edit-section"><h2>搜索引擎优化</h2><Field label="SEO 标题"><input maxLength={70} value={seo.title} onChange={(event) => setSeo({ ...seo, title: event.target.value })}/></Field><Field label="SEO 描述"><textarea maxLength={160} rows={4} value={seo.description} onChange={(event) => setSeo({ ...seo, description: event.target.value })}/></Field><Field label="SEO 关键词"><input value={seo.keywords} onChange={(event) => setSeo({ ...seo, keywords: event.target.value })}/></Field><Field label="H1"><input maxLength={120} value={seo.h1} onChange={(event) => setSeo({ ...seo, h1: event.target.value })}/></Field><Field label="Canonical"><input value={seo.canonicalUrl} placeholder={`/${meta.route}/${form.slug}`} onChange={(event) => setSeo({ ...seo, canonicalUrl: event.target.value })}/></Field><div className="section-head"><h3>H2 / H3 标签</h3><button onClick={() => setHeadings([...headings, { level: 2, text: "", sortNo: headings.length }])}><Plus size={15}/></button></div>{headings.map((heading, index) => <div className="heading-row" key={index}><select value={heading.level} onChange={(event) => setHeadings(headings.map((item, position) => position === index ? { ...item, level: Number(event.target.value) } : item))}><option value={2}>H2</option><option value={3}>H3</option></select><input value={heading.text} onChange={(event) => setHeadings(headings.map((item, position) => position === index ? { ...item, text: event.target.value } : item))}/><button onClick={() => setHeadings(headings.filter((_, position) => position !== index))}><Trash2 size={15}/></button></div>)}</section></aside></div></div>;
|
||||
return (
|
||||
<div className="product-editor">
|
||||
<div className="editor-top">
|
||||
<div className="breadcrumb-title">
|
||||
<button className="back" onClick={onBack}>
|
||||
<ArrowLeft size={17} />
|
||||
{meta.label}管理
|
||||
</button>
|
||||
<span>/</span>
|
||||
<h1>{contentCode ? `编辑${meta.label}` : `新增${meta.label}`}</h1>
|
||||
{notice && <em>{notice}</em>}
|
||||
</div>
|
||||
<div className="editor-actions">
|
||||
<button onClick={onBack}>取消</button>
|
||||
<button
|
||||
className="primary"
|
||||
disabled={saving || !form.title || !form.slug}
|
||||
onClick={save}
|
||||
>
|
||||
<Save size={16} />
|
||||
{saving ? "保存中" : "保存"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="editor-layout">
|
||||
<div className="editor-main">
|
||||
<section className="edit-section">
|
||||
<h2>基础信息</h2>
|
||||
<div className="edit-grid">
|
||||
<Field label={`${meta.label}标题 *`}>
|
||||
<input
|
||||
value={form.title}
|
||||
onChange={(event) =>
|
||||
setForm({ ...form, title: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="英文路径 *">
|
||||
<input
|
||||
value={form.slug}
|
||||
onChange={(event) =>
|
||||
setForm({
|
||||
...form,
|
||||
slug: event.target.value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9-]/g, "-"),
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="作者">
|
||||
<input
|
||||
value={form.authorName || ""}
|
||||
onChange={(event) =>
|
||||
setForm({ ...form, authorName: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="来源">
|
||||
<input
|
||||
value={form.sourceName || ""}
|
||||
onChange={(event) =>
|
||||
setForm({ ...form, sourceName: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="排序">
|
||||
<input
|
||||
type="number"
|
||||
value={form.sortNo}
|
||||
onChange={(event) =>
|
||||
setForm({ ...form, sortNo: Number(event.target.value) })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field wide label={`${meta.label}简介 *`}>
|
||||
<textarea
|
||||
rows={4}
|
||||
value={form.summary || ""}
|
||||
onChange={(event) =>
|
||||
setForm({ ...form, summary: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
</section>
|
||||
<section className="edit-section">
|
||||
<h2>{meta.label}封面</h2>
|
||||
<div className="cover-upload">
|
||||
{form.coverMediaUrl ? (
|
||||
<img
|
||||
src={assetUrl(form.coverMediaUrl)}
|
||||
alt={`${meta.label}封面预览`}
|
||||
/>
|
||||
) : (
|
||||
<ImageIcon size={42} />
|
||||
)}
|
||||
<label className={uploading ? "uploading" : ""}>
|
||||
<span>{uploading ? "上传中..." : "选择图片"}</span>
|
||||
<input
|
||||
ref={coverInputRef}
|
||||
disabled={uploading}
|
||||
type="file"
|
||||
accept=".jpg,.jpeg,.png,.webp,.gif,image/jpeg,image/png,image/webp,image/gif"
|
||||
onClick={(event) => {
|
||||
event.currentTarget.value = "";
|
||||
}}
|
||||
onChange={(event) =>
|
||||
void upload(event.currentTarget.files?.[0])
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<p>支持 JPG、PNG、WebP、GIF,单图不超过 10MB</p>
|
||||
{notice && <small className="upload-notice">{notice}</small>}
|
||||
</div>
|
||||
</section>
|
||||
<section className="edit-section">
|
||||
<h2>{meta.label}详情 *</h2>
|
||||
<div className="rich-editor">
|
||||
<div className="rich-toolbar">
|
||||
<Tool
|
||||
title="撤销"
|
||||
onClick={() => editor?.chain().focus().undo().run()}
|
||||
>
|
||||
<Undo2 />
|
||||
</Tool>
|
||||
<Tool
|
||||
title="重做"
|
||||
onClick={() => editor?.chain().focus().redo().run()}
|
||||
>
|
||||
<Redo2 />
|
||||
</Tool>
|
||||
<Tool
|
||||
title="粗体"
|
||||
active={editor?.isActive("bold")}
|
||||
onClick={() => editor?.chain().focus().toggleBold().run()}
|
||||
>
|
||||
<Bold />
|
||||
</Tool>
|
||||
<Tool
|
||||
title="斜体"
|
||||
active={editor?.isActive("italic")}
|
||||
onClick={() => editor?.chain().focus().toggleItalic().run()}
|
||||
>
|
||||
<Italic />
|
||||
</Tool>
|
||||
<Tool
|
||||
title="二级标题"
|
||||
active={editor?.isActive("heading", { level: 2 })}
|
||||
onClick={() =>
|
||||
editor?.chain().focus().toggleHeading({ level: 2 }).run()
|
||||
}
|
||||
>
|
||||
<Heading2 />
|
||||
</Tool>
|
||||
<Tool
|
||||
title="三级标题"
|
||||
active={editor?.isActive("heading", { level: 3 })}
|
||||
onClick={() =>
|
||||
editor?.chain().focus().toggleHeading({ level: 3 }).run()
|
||||
}
|
||||
>
|
||||
<Heading3 />
|
||||
</Tool>
|
||||
<Tool
|
||||
title="项目列表"
|
||||
onClick={() =>
|
||||
editor?.chain().focus().toggleBulletList().run()
|
||||
}
|
||||
>
|
||||
<List />
|
||||
</Tool>
|
||||
<Tool title="链接" onClick={addLink}>
|
||||
<LinkIcon />
|
||||
</Tool>
|
||||
<Tool title="插入图片" onClick={addImage}>
|
||||
<ImageIcon />
|
||||
</Tool>
|
||||
</div>
|
||||
<EditorContent editor={editor} />
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
<aside className="editor-side">
|
||||
<section className="edit-section">
|
||||
<h2>发布</h2>
|
||||
<Field label="状态">
|
||||
<select
|
||||
value={form.status}
|
||||
onChange={(event) =>
|
||||
setForm({ ...form, status: event.target.value })
|
||||
}
|
||||
>
|
||||
<option value="DRAFT">草稿</option>
|
||||
<option value="PUBLISHED">上架</option>
|
||||
<option value="OFFLINE">下线</option>
|
||||
</select>
|
||||
</Field>
|
||||
<label className="check">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={form.featured}
|
||||
onChange={(event) =>
|
||||
setForm({ ...form, featured: event.target.checked })
|
||||
}
|
||||
/>
|
||||
推荐内容
|
||||
</label>
|
||||
</section>
|
||||
<section className="edit-section">
|
||||
<h2>搜索引擎优化</h2>
|
||||
<Field label="SEO 标题">
|
||||
<input
|
||||
maxLength={70}
|
||||
value={seo.title}
|
||||
onChange={(event) =>
|
||||
setSeo({ ...seo, title: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="SEO 描述">
|
||||
<textarea
|
||||
maxLength={160}
|
||||
rows={4}
|
||||
value={seo.description}
|
||||
onChange={(event) =>
|
||||
setSeo({ ...seo, description: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="SEO 关键词">
|
||||
<input
|
||||
value={seo.keywords}
|
||||
onChange={(event) =>
|
||||
setSeo({ ...seo, keywords: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="H1">
|
||||
<input
|
||||
maxLength={120}
|
||||
value={seo.h1}
|
||||
onChange={(event) => setSeo({ ...seo, h1: event.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Canonical">
|
||||
<input
|
||||
value={seo.canonicalUrl}
|
||||
placeholder={`/${meta.route}/${form.slug}`}
|
||||
onChange={(event) =>
|
||||
setSeo({ ...seo, canonicalUrl: event.target.value })
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<div className="section-head">
|
||||
<h3>H2 / H3 标签</h3>
|
||||
<button
|
||||
onClick={() =>
|
||||
setHeadings([
|
||||
...headings,
|
||||
{ level: 2, text: "", sortNo: headings.length },
|
||||
])
|
||||
}
|
||||
>
|
||||
<Plus size={15} />
|
||||
</button>
|
||||
</div>
|
||||
{headings.map((heading, index) => (
|
||||
<div className="heading-row" key={index}>
|
||||
<select
|
||||
value={heading.level}
|
||||
onChange={(event) =>
|
||||
setHeadings(
|
||||
headings.map((item, position) =>
|
||||
position === index
|
||||
? { ...item, level: Number(event.target.value) }
|
||||
: item,
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
<option value={2}>H2</option>
|
||||
<option value={3}>H3</option>
|
||||
</select>
|
||||
<input
|
||||
value={heading.text}
|
||||
onChange={(event) =>
|
||||
setHeadings(
|
||||
headings.map((item, position) =>
|
||||
position === index
|
||||
? { ...item, text: event.target.value }
|
||||
: item,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
<button
|
||||
onClick={() =>
|
||||
setHeadings(
|
||||
headings.filter((_, position) => position !== index),
|
||||
)
|
||||
}
|
||||
>
|
||||
<Trash2 size={15} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</section>
|
||||
</aside>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Field({ label, wide, children }: { label: string; wide?: boolean; children: React.ReactNode }) { return <label className={wide ? "wide" : ""}><span>{label}</span>{children}</label>; }
|
||||
function Tool({ title, active, onClick, children }: { title: string; active?: boolean; onClick: () => void; children: React.ReactElement }) { return <button type="button" title={title} className={active ? "active" : ""} onClick={onClick}>{React.cloneElement(children, { size: 18 } as never)}</button>; }
|
||||
function Field({
|
||||
label,
|
||||
wide,
|
||||
children,
|
||||
}: {
|
||||
label: string;
|
||||
wide?: boolean;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<label className={wide ? "wide" : ""}>
|
||||
<span>{label}</span>
|
||||
{children}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
function Tool({
|
||||
title,
|
||||
active,
|
||||
onClick,
|
||||
children,
|
||||
}: {
|
||||
title: string;
|
||||
active?: boolean;
|
||||
onClick: () => void;
|
||||
children: React.ReactElement;
|
||||
}) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
title={title}
|
||||
className={active ? "active" : ""}
|
||||
onClick={onClick}
|
||||
>
|
||||
{React.cloneElement(children, { size: 18 } as never)}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -64,7 +64,14 @@ type Schema = {
|
||||
};
|
||||
sections: Section[];
|
||||
};
|
||||
type PreviewProduct = {productCode:string;productName:string;model?:string;summary?:string;coverMediaUrl?:string;attributes?:Array<{name:string;value:string;unit?:string}>};
|
||||
type PreviewProduct = {
|
||||
productCode: string;
|
||||
productName: string;
|
||||
model?: string;
|
||||
summary?: string;
|
||||
coverMediaUrl?: string;
|
||||
attributes?: Array<{ name: string; value: string; unit?: string }>;
|
||||
};
|
||||
const labels: Record<SectionType, string> = {
|
||||
ANNOUNCEMENT: "公告栏",
|
||||
HEADER: "导航栏",
|
||||
@@ -136,8 +143,15 @@ function createSection(type: SectionType): Section {
|
||||
};
|
||||
}
|
||||
/** 为产品详情页提供可直接发布的基础组件结构。 */
|
||||
function productDetailSchema(schema:Schema):Schema{
|
||||
return {...schema,sections:[createSection("HEADER"),createSection("PRODUCT_DETAIL"),createSection("FOOTER")]};
|
||||
function productDetailSchema(schema: Schema): Schema {
|
||||
return {
|
||||
...schema,
|
||||
sections: [
|
||||
createSection("HEADER"),
|
||||
createSection("PRODUCT_DETAIL"),
|
||||
createSection("FOOTER"),
|
||||
],
|
||||
};
|
||||
}
|
||||
export default function DecorationEditor({ onBack }: { onBack: () => void }) {
|
||||
const [pageType, setPageType] = useState("HOME");
|
||||
@@ -150,7 +164,7 @@ export default function DecorationEditor({ onBack }: { onBack: () => void }) {
|
||||
const [notice, setNotice] = useState("");
|
||||
const [status, setStatus] = useState("DRAFT");
|
||||
const [version, setVersion] = useState(0);
|
||||
const [previewProduct,setPreviewProduct]=useState<PreviewProduct>();
|
||||
const [previewProduct, setPreviewProduct] = useState<PreviewProduct>();
|
||||
useEffect(() => {
|
||||
setSelected(null);
|
||||
setGlobalMode(false);
|
||||
@@ -158,14 +172,33 @@ export default function DecorationEditor({ onBack }: { onBack: () => void }) {
|
||||
setFuture([]);
|
||||
post("/decorations/detail", { siteCode: SITE, pageType })
|
||||
.then((data) => {
|
||||
const loaded=JSON.parse(data.schema) as Schema;
|
||||
setSchema(pageType==="PRODUCT_DETAIL"&&!loaded.sections.length?productDetailSchema(loaded):loaded);
|
||||
const loaded = JSON.parse(data.schema) as Schema;
|
||||
setSchema(
|
||||
pageType === "PRODUCT_DETAIL" && !loaded.sections.length
|
||||
? productDetailSchema(loaded)
|
||||
: loaded,
|
||||
);
|
||||
setStatus(data.status);
|
||||
setVersion(data.publishedVersion);
|
||||
})
|
||||
.catch((error) => setNotice(error.message));
|
||||
}, [pageType]);
|
||||
useEffect(()=>{if(pageType!=="PRODUCT_DETAIL")return;post("/products/list",{siteCode:SITE}).then(data=>data.items?.[0]?.productCode?post("/products/detail",{siteCode:SITE,productCode:data.items[0].productCode}):undefined).then(data=>{if(data)setPreviewProduct(data)}).catch(()=>setPreviewProduct(undefined))},[pageType]);
|
||||
useEffect(() => {
|
||||
if (pageType !== "PRODUCT_DETAIL") return;
|
||||
post("/products/list", { siteCode: SITE })
|
||||
.then((data) =>
|
||||
data.items?.[0]?.productCode
|
||||
? post("/products/detail", {
|
||||
siteCode: SITE,
|
||||
productCode: data.items[0].productCode,
|
||||
})
|
||||
: undefined,
|
||||
)
|
||||
.then((data) => {
|
||||
if (data) setPreviewProduct(data);
|
||||
})
|
||||
.catch(() => setPreviewProduct(undefined));
|
||||
}, [pageType]);
|
||||
const commit = (next: Schema) => {
|
||||
setHistory((items) => [...items.slice(-19), schema]);
|
||||
setFuture([]);
|
||||
@@ -482,7 +515,7 @@ function PreviewSection({
|
||||
primary: string;
|
||||
selected: boolean;
|
||||
onSelect: () => void;
|
||||
product?:PreviewProduct;
|
||||
product?: PreviewProduct;
|
||||
}) {
|
||||
const style = {
|
||||
background: item.background,
|
||||
@@ -509,9 +542,10 @@ function PreviewSection({
|
||||
className="pv-hero"
|
||||
style={{
|
||||
...style,
|
||||
backgroundImage: (item.imageUrls?.[0] || item.imageUrl)
|
||||
? `linear-gradient(90deg,rgba(0,0,0,.65),rgba(0,0,0,.1)),url(${item.imageUrls?.[0] || item.imageUrl})`
|
||||
: undefined,
|
||||
backgroundImage:
|
||||
item.imageUrls?.[0] || item.imageUrl
|
||||
? `linear-gradient(90deg,rgba(0,0,0,.65),rgba(0,0,0,.1)),url(${item.imageUrls?.[0] || item.imageUrl})`
|
||||
: undefined,
|
||||
}}
|
||||
onClick={onSelect}
|
||||
>
|
||||
@@ -558,25 +592,53 @@ function PreviewSection({
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
if(item.type==="PRODUCT_DETAIL"){
|
||||
const attributes=product?.attributes?.slice(0,item.productInformationLimit||8)||[];
|
||||
const showInformation=item.showProductInformation!==false&&(Boolean(product?.model)||attributes.length>0);
|
||||
return <section className="pv-product-detail" style={style} onClick={onSelect}>
|
||||
<div className="pv-product-image">{product?.coverMediaUrl?<img src={product.coverMediaUrl} alt={product.productName}/>:<ImageIcon size={44}/>}</div>
|
||||
<div className="pv-product-summary">
|
||||
<span>PRODUCT</span>
|
||||
<h1>{product?.productName||"Product title"}</h1>
|
||||
<p>{product?.summary||"Product summary and key selling points will be loaded from the selected product."}</p>
|
||||
{showInformation&&<div className="pv-product-information">
|
||||
<h2>{item.productInformationTitle||"Product information"}</h2>
|
||||
<dl>
|
||||
{product?.model&&<div><dt>Model</dt><dd>{product.model}</dd></div>}
|
||||
{attributes.map(attribute=><div key={attribute.name}><dt>{attribute.name}</dt><dd>{attribute.value} {attribute.unit}</dd></div>)}
|
||||
</dl>
|
||||
</div>}
|
||||
<button style={{background:primary}}>Request a quote</button>
|
||||
</div>
|
||||
</section>;
|
||||
if (item.type === "PRODUCT_DETAIL") {
|
||||
const attributes =
|
||||
product?.attributes?.slice(0, item.productInformationLimit || 8) || [];
|
||||
const showInformation =
|
||||
item.showProductInformation !== false &&
|
||||
(Boolean(product?.model) || attributes.length > 0);
|
||||
return (
|
||||
<section className="pv-product-detail" style={style} onClick={onSelect}>
|
||||
<div className="pv-product-image">
|
||||
{product?.coverMediaUrl ? (
|
||||
<img src={product.coverMediaUrl} alt={product.productName} />
|
||||
) : (
|
||||
<ImageIcon size={44} />
|
||||
)}
|
||||
</div>
|
||||
<div className="pv-product-summary">
|
||||
<span>PRODUCT</span>
|
||||
<h1>{product?.productName || "Product title"}</h1>
|
||||
<p>
|
||||
{product?.summary ||
|
||||
"Product summary and key selling points will be loaded from the selected product."}
|
||||
</p>
|
||||
{showInformation && (
|
||||
<div className="pv-product-information">
|
||||
<h2>{item.productInformationTitle || "Product information"}</h2>
|
||||
<dl>
|
||||
{product?.model && (
|
||||
<div>
|
||||
<dt>Model</dt>
|
||||
<dd>{product.model}</dd>
|
||||
</div>
|
||||
)}
|
||||
{attributes.map((attribute) => (
|
||||
<div key={attribute.name}>
|
||||
<dt>{attribute.name}</dt>
|
||||
<dd>
|
||||
{attribute.value} {attribute.unit}
|
||||
</dd>
|
||||
</div>
|
||||
))}
|
||||
</dl>
|
||||
</div>
|
||||
)}
|
||||
<button style={{ background: primary }}>Request a quote</button>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
if (item.type === "INQUIRY")
|
||||
return (
|
||||
@@ -612,9 +674,7 @@ function PreviewSection({
|
||||
<br />
|
||||
Privacy Policy
|
||||
</div>
|
||||
<small>
|
||||
{item.subtitle || "Copyright © 2026. All rights reserved."}
|
||||
</small>
|
||||
<small>{item.subtitle || "Copyright © 2026. All rights reserved."}</small>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -703,20 +763,23 @@ function SectionSettings({
|
||||
return (
|
||||
<div>
|
||||
<h3>{labels[item.type]}设置</h3>
|
||||
{item.type!=="PRODUCT_DETAIL"&&<><Field label="英文标题">
|
||||
<input
|
||||
value={item.title}
|
||||
onChange={(e) => onChange({ title: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="英文副标题/内容">
|
||||
<textarea
|
||||
rows={4}
|
||||
value={item.subtitle}
|
||||
onChange={(e) => onChange({ subtitle: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
</>}
|
||||
{item.type !== "PRODUCT_DETAIL" && (
|
||||
<>
|
||||
<Field label="英文标题">
|
||||
<input
|
||||
value={item.title}
|
||||
onChange={(e) => onChange({ title: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="英文副标题/内容">
|
||||
<textarea
|
||||
rows={4}
|
||||
value={item.subtitle}
|
||||
onChange={(e) => onChange({ subtitle: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
</>
|
||||
)}
|
||||
{item.type === "HERO" && (
|
||||
<>
|
||||
<Field label="背景图片 URL">
|
||||
@@ -726,7 +789,19 @@ function SectionSettings({
|
||||
/>
|
||||
</Field>
|
||||
<Field label="轮播图片 URL(每行一张)">
|
||||
<textarea rows={4} value={(item.imageUrls || []).join("\n")} onChange={(e) => onChange({ imageUrls: e.target.value.split(/\r?\n/).map((value) => value.trim()).filter(Boolean) })} placeholder="https://..." />
|
||||
<textarea
|
||||
rows={4}
|
||||
value={(item.imageUrls || []).join("\n")}
|
||||
onChange={(e) =>
|
||||
onChange({
|
||||
imageUrls: e.target.value
|
||||
.split(/\r?\n/)
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean),
|
||||
})
|
||||
}
|
||||
placeholder="https://..."
|
||||
/>
|
||||
</Field>
|
||||
<Field label="按钮文案">
|
||||
<input
|
||||
@@ -760,14 +835,18 @@ function SectionSettings({
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={item.showProductInformation !== false}
|
||||
onChange={(e) => onChange({ showProductInformation: e.target.checked })}
|
||||
onChange={(e) =>
|
||||
onChange({ showProductInformation: e.target.checked })
|
||||
}
|
||||
/>
|
||||
展示产品信息
|
||||
</label>
|
||||
<Field label="产品信息英文标题">
|
||||
<input
|
||||
value={item.productInformationTitle || "Product information"}
|
||||
onChange={(e) => onChange({ productInformationTitle: e.target.value })}
|
||||
onChange={(e) =>
|
||||
onChange({ productInformationTitle: e.target.value })
|
||||
}
|
||||
disabled={item.showProductInformation === false}
|
||||
/>
|
||||
</Field>
|
||||
@@ -777,7 +856,9 @@ function SectionSettings({
|
||||
min="1"
|
||||
max="20"
|
||||
value={item.productInformationLimit || 8}
|
||||
onChange={(e) => onChange({ productInformationLimit: Number(e.target.value) })}
|
||||
onChange={(e) =>
|
||||
onChange({ productInformationLimit: Number(e.target.value) })
|
||||
}
|
||||
disabled={item.showProductInformation === false}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
Reference in new issue
Block a user