import { useEffect, useMemo, useState } from "react"; import { ChevronDown, ChevronUp, Copy, Eye, EyeOff, GripVertical, Monitor, Plus, Redo2, Save, Smartphone, Tablet, Trash2, Undo2, X, ExternalLink, } from "lucide-react"; import { API } from "./api"; import { statusLabel } from "./statusLabels"; const SITE = "SIT0000000000001"; type Viewport = "desktop" | "tablet" | "mobile"; type SectionType = | "ANNOUNCEMENT" | "HEADER" | "HERO" | "RICH_TEXT" | "PRODUCTS" | "CONTENT" | "INQUIRY" | "FOOTER"; type Section = { id: string; type: SectionType; title: string; subtitle: string; imageUrl: string; buttonText: string; buttonUrl: string; sourceType: string; limit: number; visible: boolean; background: string; textColor: string; }; type Schema = { global: { primaryColor: string; backgroundColor: string; textColor: string; fontFamily: string; contentWidth: number; }; sections: Section[]; }; const labels: Record = { ANNOUNCEMENT: "公告栏", HEADER: "导航栏", HERO: "轮播横幅", RICH_TEXT: "富文本", PRODUCTS: "产品展示", CONTENT: "内容展示", INQUIRY: "询盘表单", FOOTER: "页脚", }; const initialSchema: Schema = { global: { primaryColor: "#176b5d", backgroundColor: "#ffffff", textColor: "#17211f", fontFamily: "Inter", contentWidth: 1200, }, sections: [], }; const pageOptions = [ { value: "HOME", label: "首页", path: "/" }, { value: "PRODUCT_CATEGORY", label: "产品分类页", path: "/products" }, { value: "PRODUCT_DETAIL", label: "产品详情页", path: "/products" }, { value: "NEWS_CATEGORY", label: "新闻分类页", path: "/news" }, { value: "NEWS_DETAIL", label: "新闻详情页", path: "/news" }, { value: "CASE_CATEGORY", label: "案例分类页", path: "/cases" }, { value: "CASE_DETAIL", label: "案例详情页", path: "/cases" }, { value: "HELP_CATEGORY", label: "帮助分类页", path: "/help" }, { value: "HELP_DETAIL", label: "帮助详情页", path: "/help" }, { value: "ABOUT_US", label: "关于我们", path: "/about-us" }, { value: "CUSTOM_PAGE", label: "自定义页面", path: "/custom-page" }, ]; 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 || "请求失败"); return response.json(); } function createSection(type: SectionType): Section { return { id: `${type.toLowerCase()}-${Date.now()}`, type, title: type === "HERO" ? "Reliable equipment for demanding operations." : labels[type], subtitle: type === "HERO" ? "Industrial products and project-proven solutions for global B2B customers." : "", imageUrl: "", buttonText: type === "HERO" ? "Explore Products" : "", buttonUrl: type === "HERO" ? "/products" : "", sourceType: type === "CONTENT" ? "CASE" : "", limit: 3, visible: true, background: type === "HEADER" || type === "FOOTER" ? "#101a25" : "#ffffff", textColor: type === "HEADER" || type === "FOOTER" ? "#ffffff" : "#17211f", }; } export default function DecorationEditor({ onBack }: { onBack: () => void }) { const [pageType, setPageType] = useState("HOME"); const [schema, setSchema] = useState(initialSchema); const [history, setHistory] = useState([]); const [future, setFuture] = useState([]); const [selected, setSelected] = useState(null); const [globalMode, setGlobalMode] = useState(false); const [viewport, setViewport] = useState("desktop"); const [notice, setNotice] = useState(""); const [status, setStatus] = useState("DRAFT"); const [version, setVersion] = useState(0); useEffect(() => { setSelected(null); setGlobalMode(false); setHistory([]); setFuture([]); post("/decorations/detail", { siteCode: SITE, pageType }) .then((data) => { setSchema(JSON.parse(data.schema)); setStatus(data.status); setVersion(data.publishedVersion); }) .catch((error) => setNotice(error.message)); }, [pageType]); const commit = (next: Schema) => { setHistory((items) => [...items.slice(-19), schema]); setFuture([]); setSchema(next); }; const updateSection = (id: string, patch: Partial
) => commit({ ...schema, sections: schema.sections.map((item) => item.id === id ? { ...item, ...patch } : item, ), }); const active = schema.sections.find((item) => item.id === selected); const add = (type: SectionType) => { const item = createSection(type); commit({ ...schema, sections: [...schema.sections, item] }); setSelected(item.id); setGlobalMode(false); }; const move = (index: number, offset: number) => { const target = index + offset; if (target < 0 || target >= schema.sections.length) return; const sections = [...schema.sections]; [sections[index], sections[target]] = [sections[target], sections[index]]; commit({ ...schema, sections }); }; const undo = () => { const previous = history.at(-1); if (!previous) return; setFuture((items) => [schema, ...items]); setSchema(previous); setHistory((items) => items.slice(0, -1)); }; const redo = () => { const next = future[0]; if (!next) return; setHistory((items) => [...items, schema]); setSchema(next); setFuture((items) => items.slice(1)); }; const save = async () => { await post("/decorations/save", { siteCode: SITE, pageType, schema: JSON.stringify(schema), }); setNotice("装修草稿已保存"); }; const publish = async () => { await save(); await post("/decorations/publish", { siteCode: SITE, pageType }); setStatus("PUBLISHED"); setVersion((value) => value + 1); setNotice("已发布到英文前台站"); }; const preview = () => { const target = pageOptions.find((item) => item.value === pageType) || pageOptions[0]; window.open( `http://localhost:5174${target.path}?previewPage=${pageType}`, "_blank", "noopener,noreferrer", ); }; const previewWidth = useMemo( () => viewport === "desktop" ? "100%" : viewport === "tablet" ? "820px" : "390px", [viewport], ); return (
{pageOptions.find((item) => item.value === pageType)?.label || "页面装修"} {statusLabel(status)} {status === "PUBLISHED" ? ` V${version}` : ""}
{notice &&
{notice}
}
{schema.sections.filter((item) => item.visible).length ? ( schema.sections .filter((item) => item.visible) .map((item) => ( { setSelected(item.id); setGlobalMode(false); }} /> )) ) : (

开始装修首页

从左侧添加轮播横幅、产品、案例或询盘表单等组件。

)}
); } function Panels() { return (
); } function PreviewSection({ item, primary, selected, onSelect, }: { item: Section; primary: string; selected: boolean; onSelect: () => void; }) { const style = { background: item.background, color: item.textColor, outline: selected ? `2px solid ${primary}` : undefined, }; if (item.type === "ANNOUNCEMENT") return (
{item.title || "Free consultation for your next industrial project"}
); if (item.type === "HEADER") return (
SHOPLIX
); if (item.type === "HERO") return (
ENGINEERED FOR INDUSTRY

{item.title}

{item.subtitle}

{item.buttonText && ( )}
); if (item.type === "RICH_TEXT") return (

{item.title}

{item.subtitle || "Add your English introduction, advantages or service description here."}

); if (item.type === "PRODUCTS" || item.type === "CONTENT") return (

FEATURED

{item.title}

{Array.from({ length: Math.min(item.limit, 4) }, (_, i) => (
{item.type === "PRODUCTS" ? "PRODUCT" : item.sourceType || "CONTENT"} {item.type === "PRODUCTS" ? `Industrial product ${i + 1}` : `Project story ${i + 1}`}
))}
); if (item.type === "INQUIRY") return (

{item.title}

{item.subtitle}