From 26406481e24b75a2122e7098cb9f7f6046947608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=94=90?= Date: Thu, 26 Nov 2026 08:15:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=86=85=E5=AE=B9=E7=B4=A0?= =?UTF-8?q?=E6=9D=90=E9=80=89=E6=8B=A9=E5=92=8C=E8=BD=AE=E6=92=AD=E9=AB=98?= =?UTF-8?q?=E5=BA=A6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-admin/src/ContentEditor.tsx | 178 +++++++++++++++++++++++++++-- web-admin/src/DecorationEditor.tsx | 18 +++ web-site/src/main.tsx | 4 +- 3 files changed, 187 insertions(+), 13 deletions(-) diff --git a/web-admin/src/ContentEditor.tsx b/web-admin/src/ContentEditor.tsx index e839542..e4dd41b 100644 --- a/web-admin/src/ContentEditor.tsx +++ b/web-admin/src/ContentEditor.tsx @@ -6,6 +6,7 @@ import Image from "@tiptap/extension-image"; import { ArrowLeft, Bold, + Check, Heading2, Heading3, Image as ImageIcon, @@ -15,14 +16,24 @@ import { Plus, Redo2, Save, + Search, Trash2, Undo2, + X, } 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 MediaItem = { + mediaCode: string; + url: string; + originalFileName: string; + mediaType: string; + altText?: string; +}; +type MediaTarget = "COVER" | "CONTENT"; type Props = { contentType: ContentType; contentCode?: string; @@ -84,7 +95,14 @@ export default function ContentEditor({ const [saving, setSaving] = useState(false); const [uploading, setUploading] = useState(false); const [notice, setNotice] = useState(""); + const [mediaOpen, setMediaOpen] = useState(false); + const [mediaTarget, setMediaTarget] = useState("COVER"); + const [mediaItems, setMediaItems] = useState([]); + const [mediaLoading, setMediaLoading] = useState(false); + const [mediaQuery, setMediaQuery] = useState(""); + const [selectedMediaCode, setSelectedMediaCode] = useState(""); const coverInputRef = useRef(null); + const mediaInputRef = useRef(null); const editor = useEditor({ extensions: [StarterKit, Link.configure({ openOnClick: false }), Image], content: "

", @@ -121,7 +139,8 @@ export default function ContentEditor({ .catch(() => undefined); }, [contentCode, contentType, editor, meta.seoType]); - const upload = async (file?: File) => { + /** 上传图片并根据使用位置设置封面或插入正文。 */ + const upload = async (file?: File, target: MediaTarget = "COVER") => { if (!file) return; if (file.size > 10 * 1024 * 1024) { setNotice("图片不能超过 10MB"); @@ -147,19 +166,62 @@ export default function ContentEditor({ (await response.json().catch(() => ({}))).message || "上传失败", ); const data = await response.json(); - setForm((current) => ({ - ...current, - coverMediaCode: data.mediaCode, - coverMediaUrl: data.url, - })); - setNotice(`${meta.label}封面上传成功`); + if (target === "COVER") { + setForm((current) => ({ + ...current, + coverMediaCode: data.mediaCode, + coverMediaUrl: data.url, + })); + } else { + editor?.chain().focus().setImage({ src: data.url }).run(); + } + setNotice( + target === "COVER" ? `${meta.label}封面上传成功` : "正文图片上传成功", + ); + setMediaOpen(false); } catch (error) { setNotice(error instanceof Error ? error.message : "上传失败"); } finally { setUploading(false); if (coverInputRef.current) coverInputRef.current.value = ""; + if (mediaInputRef.current) mediaInputRef.current.value = ""; } }; + /** 打开素材库并加载当前站点已经上传的图片。 */ + const openMediaLibrary = async (target: MediaTarget) => { + setMediaTarget(target); + setSelectedMediaCode(""); + setMediaOpen(true); + setMediaLoading(true); + try { + const data = await post("/media/list", { + siteCode: SITE, + mediaType: "IMAGE", + }); + setMediaItems(data.items || []); + } catch (error) { + setNotice(error instanceof Error ? error.message : "素材库加载失败"); + } finally { + setMediaLoading(false); + } + }; + /** 将素材库中选中的图片应用到封面或正文。 */ + const confirmMediaSelection = () => { + const selected = mediaItems.find( + (item) => item.mediaCode === selectedMediaCode, + ); + if (!selected) return; + if (mediaTarget === "COVER") { + setForm((current) => ({ + ...current, + coverMediaCode: selected.mediaCode, + coverMediaUrl: selected.url, + })); + } else { + editor?.chain().focus().setImage({ src: selected.url }).run(); + } + setMediaOpen(false); + }; const save = async () => { if (!editor) return; try { @@ -210,10 +272,7 @@ export default function ContentEditor({ 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 addImage = () => void openMediaLibrary("CONTENT"); return (
@@ -327,6 +386,14 @@ export default function ContentEditor({ } /> +

支持 JPG、PNG、WebP、GIF,单图不超过 10MB

{notice && {notice}}
@@ -525,6 +592,95 @@ export default function ContentEditor({ + {mediaOpen && ( +
setMediaOpen(false)} + > +
event.stopPropagation()} + > +
+

{mediaTarget === "COVER" ? "选择封面" : "插入正文图片"}

+ +
+
+ + +
+
+ {mediaLoading ? ( +
加载中...
+ ) : ( + mediaItems + .filter( + (item) => + !mediaQuery || + item.originalFileName + .toLowerCase() + .includes(mediaQuery.toLowerCase()), + ) + .map((item) => ( + + )) + )} +
+
+ + +
+
+
+ )} ); } diff --git a/web-admin/src/DecorationEditor.tsx b/web-admin/src/DecorationEditor.tsx index d8baa98..982ba74 100644 --- a/web-admin/src/DecorationEditor.tsx +++ b/web-admin/src/DecorationEditor.tsx @@ -23,6 +23,7 @@ import { statusLabel } from "./statusLabels"; const SITE = "SIT0000000000001"; type Viewport = "desktop" | "tablet" | "mobile"; +type HeroHeight = "30" | "50" | "80" | "100"; type SectionType = | "ANNOUNCEMENT" | "HEADER" @@ -40,6 +41,8 @@ type Section = { subtitle: string; imageUrl: string; imageUrls: string[]; + /** 首页轮播区域占浏览器可视高度的百分比。 */ + heroHeight: HeroHeight; buttonText: string; buttonUrl: string; sourceType: string; @@ -130,6 +133,7 @@ function createSection(type: SectionType): Section { : "", imageUrl: "", imageUrls: [], + heroHeight: "80", buttonText: type === "HERO" ? "Explore Products" : "", buttonUrl: type === "HERO" ? "/products" : "", sourceType: type === "CONTENT" ? "CASE" : "", @@ -542,6 +546,7 @@ function PreviewSection({ className="pv-hero" style={{ ...style, + minHeight: `${item.heroHeight || "80"}vh`, 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})` @@ -803,6 +808,19 @@ function SectionSettings({ placeholder="https://..." /> + + + { let nodes: HTMLElement[] = []; post("/custom-codes/list", { siteCode: "SIT0000000000001" }).then(data => { (data.items || []).forEach((item: { content: string; position: string }) => { const container = item.position === "HEAD" ? document.head : document.body; const wrapper = document.createElement("div"); wrapper.innerHTML = item.content; Array.from(wrapper.children).forEach(child => { let node: HTMLElement = child as HTMLElement; if (child.tagName === "SCRIPT") { const script = document.createElement("script"); Array.from(child.attributes).forEach(attribute => script.setAttribute(attribute.name, attribute.value)); script.text = child.textContent || ""; node = script; } container.insertBefore(node, item.position === "BODY_START" ? container.firstChild : null); nodes.push(node); }); }); }).catch(() => undefined); return () => nodes.forEach(node => node.remove()); }, []); return null; } -type DecorationSection = { id:string; type:string; title:string; subtitle:string; imageUrl:string; imageUrls?:string[]; buttonText:string; buttonUrl:string; sourceType:string; limit:number; visible:boolean; background:string; textColor:string; showProductInformation?:boolean; productInformationTitle?:string; productInformationLimit?:number }; +type DecorationSection = { id:string; type:string; title:string; subtitle:string; imageUrl:string; imageUrls?:string[]; heroHeight?:"30"|"50"|"80"|"100"; buttonText:string; buttonUrl:string; sourceType:string; limit:number; visible:boolean; background:string; textColor:string; showProductInformation?:boolean; productInformationTitle?:string; productInformationLimit?:number }; type ProductDetailConfiguration = { /** 是否在商品摘要区域展示产品信息。 */ showProductInformation?: boolean; @@ -337,7 +337,7 @@ function LiveHero({ section, style }: { section: DecorationSection; style: React const [index, setIndex] = useState(0); useEffect(() => { if (images.length < 2) return; const timer = window.setInterval(() => setIndex((current) => (current + 1) % images.length), 5000); return () => window.clearInterval(timer); }, [images.length]); const image = images[index] || ""; - return

ENGINEERED FOR INDUSTRY

{section.title}

{section.subtitle}{section.buttonText&&{section.buttonText}}
{images.length > 1 &&
{images.map((_, dot) =>
}
; + return

ENGINEERED FOR INDUSTRY

{section.title}

{section.subtitle}{section.buttonText&&{section.buttonText}}
{images.length > 1 &&
{images.map((_, dot) =>
}
; } /** 装修模板中的产品详情组件,按当前访问路径加载真实商品。 */ function RuntimeProductDetail({ configuration }: { configuration: ProductDetailConfiguration }) {