支持内容素材选择和轮播高度配置
This commit is contained in:
1 parent
7f4f62702a
commit
26406481e2
3 files changed
+187
-13
No files matched your search
+167
-11
@@ -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<MediaTarget>("COVER");
|
||||
const [mediaItems, setMediaItems] = useState<MediaItem[]>([]);
|
||||
const [mediaLoading, setMediaLoading] = useState(false);
|
||||
const [mediaQuery, setMediaQuery] = useState("");
|
||||
const [selectedMediaCode, setSelectedMediaCode] = useState("");
|
||||
const coverInputRef = useRef<HTMLInputElement>(null);
|
||||
const mediaInputRef = useRef<HTMLInputElement>(null);
|
||||
const editor = useEditor({
|
||||
extensions: [StarterKit, Link.configure({ openOnClick: false }), Image],
|
||||
content: "<p></p>",
|
||||
@@ -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 (
|
||||
<div className="product-editor">
|
||||
@@ -327,6 +386,14 @@ export default function ContentEditor({
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
className="gallery-upload-button"
|
||||
onClick={() => void openMediaLibrary("COVER")}
|
||||
>
|
||||
<ImageIcon size={16} />
|
||||
从素材库选择
|
||||
</button>
|
||||
<p>支持 JPG、PNG、WebP、GIF,单图不超过 10MB</p>
|
||||
{notice && <small className="upload-notice">{notice}</small>}
|
||||
</div>
|
||||
@@ -525,6 +592,95 @@ export default function ContentEditor({
|
||||
</section>
|
||||
</aside>
|
||||
</div>
|
||||
{mediaOpen && (
|
||||
<div
|
||||
className="media-picker-backdrop"
|
||||
onMouseDown={() => setMediaOpen(false)}
|
||||
>
|
||||
<section
|
||||
className="media-picker"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
onMouseDown={(event) => event.stopPropagation()}
|
||||
>
|
||||
<header>
|
||||
<h2>{mediaTarget === "COVER" ? "选择封面" : "插入正文图片"}</h2>
|
||||
<button type="button" onClick={() => setMediaOpen(false)}>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</header>
|
||||
<div className="media-picker-toolbar">
|
||||
<label>
|
||||
<Search size={16} />
|
||||
<input
|
||||
placeholder="搜索图片名称"
|
||||
value={mediaQuery}
|
||||
onChange={(event) => setMediaQuery(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<label className="media-local-upload">
|
||||
<Plus size={16} />
|
||||
上传本地图片
|
||||
<input
|
||||
ref={mediaInputRef}
|
||||
disabled={uploading}
|
||||
type="file"
|
||||
accept=".jpg,.jpeg,.png,.webp,.gif,image/jpeg,image/png,image/webp,image/gif"
|
||||
onChange={(event) =>
|
||||
void upload(event.currentTarget.files?.[0], mediaTarget)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div className="media-picker-grid">
|
||||
{mediaLoading ? (
|
||||
<div className="media-picker-empty">加载中...</div>
|
||||
) : (
|
||||
mediaItems
|
||||
.filter(
|
||||
(item) =>
|
||||
!mediaQuery ||
|
||||
item.originalFileName
|
||||
.toLowerCase()
|
||||
.includes(mediaQuery.toLowerCase()),
|
||||
)
|
||||
.map((item) => (
|
||||
<button
|
||||
type="button"
|
||||
className={`media-picker-item${selectedMediaCode === item.mediaCode ? " selected" : ""}`}
|
||||
key={item.mediaCode}
|
||||
onClick={() => setSelectedMediaCode(item.mediaCode)}
|
||||
>
|
||||
<img
|
||||
src={assetUrl(item.url)}
|
||||
alt={item.altText || item.originalFileName}
|
||||
/>
|
||||
<span>
|
||||
{selectedMediaCode === item.mediaCode && (
|
||||
<Check size={15} />
|
||||
)}
|
||||
{item.originalFileName}
|
||||
</span>
|
||||
</button>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
<footer>
|
||||
<button type="button" onClick={() => setMediaOpen(false)}>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="primary"
|
||||
disabled={!selectedMediaCode}
|
||||
onClick={confirmMediaSelection}
|
||||
>
|
||||
确定
|
||||
</button>
|
||||
</footer>
|
||||
</section>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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://..."
|
||||
/>
|
||||
</Field>
|
||||
<Field label="轮播高度">
|
||||
<select
|
||||
value={item.heroHeight || "80"}
|
||||
onChange={(e) =>
|
||||
onChange({ heroHeight: e.target.value as HeroHeight })
|
||||
}
|
||||
>
|
||||
<option value="30">屏幕高度 30%</option>
|
||||
<option value="50">屏幕高度 50%</option>
|
||||
<option value="80">屏幕高度 80%</option>
|
||||
<option value="100">一屏高度 100%</option>
|
||||
</select>
|
||||
</Field>
|
||||
<Field label="按钮文案">
|
||||
<input
|
||||
value={item.buttonText}
|
||||
|
||||
@@ -302,7 +302,7 @@ function CustomCodeRuntime() {
|
||||
useEffect(() => { 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 <section className="live-hero" style={{...style,backgroundImage:image?`linear-gradient(90deg,rgba(0,0,0,.72),rgba(0,0,0,.12)),url(${image})`:undefined}}><button className="hero-arrow hero-arrow-prev" type="button" onClick={() => setIndex((index - 1 + images.length) % images.length)} aria-label="Previous slide" disabled={images.length < 2}>‹</button><div><p>ENGINEERED FOR INDUSTRY</p><h1>{section.title}</h1><span>{section.subtitle}</span>{section.buttonText&&<a href={section.buttonUrl||"#"}>{section.buttonText}<ArrowRight size={17}/></a>}</div><button className="hero-arrow hero-arrow-next" type="button" onClick={() => setIndex((index + 1) % images.length)} aria-label="Next slide" disabled={images.length < 2}>›</button>{images.length > 1 && <div className="hero-dots">{images.map((_, dot) => <button type="button" key={dot} className={dot === index ? "active" : ""} onClick={() => setIndex(dot)} aria-label={`Go to slide ${dot + 1}`} />)}</div>}</section>;
|
||||
return <section className="live-hero" style={{...style,minHeight:`${section.heroHeight||"80"}vh`,backgroundImage:image?`linear-gradient(90deg,rgba(0,0,0,.72),rgba(0,0,0,.12)),url(${image})`:undefined}}><button className="hero-arrow hero-arrow-prev" type="button" onClick={() => setIndex((index - 1 + images.length) % images.length)} aria-label="Previous slide" disabled={images.length < 2}>‹</button><div><p>ENGINEERED FOR INDUSTRY</p><h1>{section.title}</h1><span>{section.subtitle}</span>{section.buttonText&&<a href={section.buttonUrl||"#"}>{section.buttonText}<ArrowRight size={17}/></a>}</div><button className="hero-arrow hero-arrow-next" type="button" onClick={() => setIndex((index + 1) % images.length)} aria-label="Next slide" disabled={images.length < 2}>›</button>{images.length > 1 && <div className="hero-dots">{images.map((_, dot) => <button type="button" key={dot} className={dot === index ? "active" : ""} onClick={() => setIndex(dot)} aria-label={`Go to slide ${dot + 1}`} />)}</div>}</section>;
|
||||
}
|
||||
/** 装修模板中的产品详情组件,按当前访问路径加载真实商品。 */
|
||||
function RuntimeProductDetail({ configuration }: { configuration: ProductDetailConfiguration }) {
|
||||
|
||||
Reference in new issue
Block a user