支持内容素材选择和轮播高度配置
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 {
|
import {
|
||||||
ArrowLeft,
|
ArrowLeft,
|
||||||
Bold,
|
Bold,
|
||||||
|
Check,
|
||||||
Heading2,
|
Heading2,
|
||||||
Heading3,
|
Heading3,
|
||||||
Image as ImageIcon,
|
Image as ImageIcon,
|
||||||
@@ -15,14 +16,24 @@ import {
|
|||||||
Plus,
|
Plus,
|
||||||
Redo2,
|
Redo2,
|
||||||
Save,
|
Save,
|
||||||
|
Search,
|
||||||
Trash2,
|
Trash2,
|
||||||
Undo2,
|
Undo2,
|
||||||
|
X,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { API, assetUrl } from "./api";
|
import { API, assetUrl } from "./api";
|
||||||
|
|
||||||
const SITE = "SIT0000000000001";
|
const SITE = "SIT0000000000001";
|
||||||
type ContentType = "NEWS" | "CASE" | "HELP";
|
type ContentType = "NEWS" | "CASE" | "HELP";
|
||||||
type Heading = { level: number; text: string; sortNo: number };
|
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 = {
|
type Props = {
|
||||||
contentType: ContentType;
|
contentType: ContentType;
|
||||||
contentCode?: string;
|
contentCode?: string;
|
||||||
@@ -84,7 +95,14 @@ export default function ContentEditor({
|
|||||||
const [saving, setSaving] = useState(false);
|
const [saving, setSaving] = useState(false);
|
||||||
const [uploading, setUploading] = useState(false);
|
const [uploading, setUploading] = useState(false);
|
||||||
const [notice, setNotice] = useState("");
|
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 coverInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const mediaInputRef = useRef<HTMLInputElement>(null);
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
extensions: [StarterKit, Link.configure({ openOnClick: false }), Image],
|
extensions: [StarterKit, Link.configure({ openOnClick: false }), Image],
|
||||||
content: "<p></p>",
|
content: "<p></p>",
|
||||||
@@ -121,7 +139,8 @@ export default function ContentEditor({
|
|||||||
.catch(() => undefined);
|
.catch(() => undefined);
|
||||||
}, [contentCode, contentType, editor, meta.seoType]);
|
}, [contentCode, contentType, editor, meta.seoType]);
|
||||||
|
|
||||||
const upload = async (file?: File) => {
|
/** 上传图片并根据使用位置设置封面或插入正文。 */
|
||||||
|
const upload = async (file?: File, target: MediaTarget = "COVER") => {
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
if (file.size > 10 * 1024 * 1024) {
|
if (file.size > 10 * 1024 * 1024) {
|
||||||
setNotice("图片不能超过 10MB");
|
setNotice("图片不能超过 10MB");
|
||||||
@@ -147,19 +166,62 @@ export default function ContentEditor({
|
|||||||
(await response.json().catch(() => ({}))).message || "上传失败",
|
(await response.json().catch(() => ({}))).message || "上传失败",
|
||||||
);
|
);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setForm((current) => ({
|
if (target === "COVER") {
|
||||||
...current,
|
setForm((current) => ({
|
||||||
coverMediaCode: data.mediaCode,
|
...current,
|
||||||
coverMediaUrl: data.url,
|
coverMediaCode: data.mediaCode,
|
||||||
}));
|
coverMediaUrl: data.url,
|
||||||
setNotice(`${meta.label}封面上传成功`);
|
}));
|
||||||
|
} else {
|
||||||
|
editor?.chain().focus().setImage({ src: data.url }).run();
|
||||||
|
}
|
||||||
|
setNotice(
|
||||||
|
target === "COVER" ? `${meta.label}封面上传成功` : "正文图片上传成功",
|
||||||
|
);
|
||||||
|
setMediaOpen(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setNotice(error instanceof Error ? error.message : "上传失败");
|
setNotice(error instanceof Error ? error.message : "上传失败");
|
||||||
} finally {
|
} finally {
|
||||||
setUploading(false);
|
setUploading(false);
|
||||||
if (coverInputRef.current) coverInputRef.current.value = "";
|
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 () => {
|
const save = async () => {
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
try {
|
try {
|
||||||
@@ -210,10 +272,7 @@ export default function ContentEditor({
|
|||||||
const href = window.prompt("请输入链接地址");
|
const href = window.prompt("请输入链接地址");
|
||||||
if (href) editor?.chain().focus().setLink({ href }).run();
|
if (href) editor?.chain().focus().setLink({ href }).run();
|
||||||
};
|
};
|
||||||
const addImage = () => {
|
const addImage = () => void openMediaLibrary("CONTENT");
|
||||||
const src = window.prompt("请输入图片地址");
|
|
||||||
if (src) editor?.chain().focus().setImage({ src }).run();
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="product-editor">
|
<div className="product-editor">
|
||||||
@@ -327,6 +386,14 @@ export default function ContentEditor({
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="gallery-upload-button"
|
||||||
|
onClick={() => void openMediaLibrary("COVER")}
|
||||||
|
>
|
||||||
|
<ImageIcon size={16} />
|
||||||
|
从素材库选择
|
||||||
|
</button>
|
||||||
<p>支持 JPG、PNG、WebP、GIF,单图不超过 10MB</p>
|
<p>支持 JPG、PNG、WebP、GIF,单图不超过 10MB</p>
|
||||||
{notice && <small className="upload-notice">{notice}</small>}
|
{notice && <small className="upload-notice">{notice}</small>}
|
||||||
</div>
|
</div>
|
||||||
@@ -525,6 +592,95 @@ export default function ContentEditor({
|
|||||||
</section>
|
</section>
|
||||||
</aside>
|
</aside>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import { statusLabel } from "./statusLabels";
|
|||||||
|
|
||||||
const SITE = "SIT0000000000001";
|
const SITE = "SIT0000000000001";
|
||||||
type Viewport = "desktop" | "tablet" | "mobile";
|
type Viewport = "desktop" | "tablet" | "mobile";
|
||||||
|
type HeroHeight = "30" | "50" | "80" | "100";
|
||||||
type SectionType =
|
type SectionType =
|
||||||
| "ANNOUNCEMENT"
|
| "ANNOUNCEMENT"
|
||||||
| "HEADER"
|
| "HEADER"
|
||||||
@@ -40,6 +41,8 @@ type Section = {
|
|||||||
subtitle: string;
|
subtitle: string;
|
||||||
imageUrl: string;
|
imageUrl: string;
|
||||||
imageUrls: string[];
|
imageUrls: string[];
|
||||||
|
/** 首页轮播区域占浏览器可视高度的百分比。 */
|
||||||
|
heroHeight: HeroHeight;
|
||||||
buttonText: string;
|
buttonText: string;
|
||||||
buttonUrl: string;
|
buttonUrl: string;
|
||||||
sourceType: string;
|
sourceType: string;
|
||||||
@@ -130,6 +133,7 @@ function createSection(type: SectionType): Section {
|
|||||||
: "",
|
: "",
|
||||||
imageUrl: "",
|
imageUrl: "",
|
||||||
imageUrls: [],
|
imageUrls: [],
|
||||||
|
heroHeight: "80",
|
||||||
buttonText: type === "HERO" ? "Explore Products" : "",
|
buttonText: type === "HERO" ? "Explore Products" : "",
|
||||||
buttonUrl: type === "HERO" ? "/products" : "",
|
buttonUrl: type === "HERO" ? "/products" : "",
|
||||||
sourceType: type === "CONTENT" ? "CASE" : "",
|
sourceType: type === "CONTENT" ? "CASE" : "",
|
||||||
@@ -542,6 +546,7 @@ function PreviewSection({
|
|||||||
className="pv-hero"
|
className="pv-hero"
|
||||||
style={{
|
style={{
|
||||||
...style,
|
...style,
|
||||||
|
minHeight: `${item.heroHeight || "80"}vh`,
|
||||||
backgroundImage:
|
backgroundImage:
|
||||||
item.imageUrls?.[0] || item.imageUrl
|
item.imageUrls?.[0] || item.imageUrl
|
||||||
? `linear-gradient(90deg,rgba(0,0,0,.65),rgba(0,0,0,.1)),url(${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://..."
|
placeholder="https://..."
|
||||||
/>
|
/>
|
||||||
</Field>
|
</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="按钮文案">
|
<Field label="按钮文案">
|
||||||
<input
|
<input
|
||||||
value={item.buttonText}
|
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()); }, []);
|
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;
|
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 = {
|
type ProductDetailConfiguration = {
|
||||||
/** 是否在商品摘要区域展示产品信息。 */
|
/** 是否在商品摘要区域展示产品信息。 */
|
||||||
showProductInformation?: boolean;
|
showProductInformation?: boolean;
|
||||||
@@ -337,7 +337,7 @@ function LiveHero({ section, style }: { section: DecorationSection; style: React
|
|||||||
const [index, setIndex] = useState(0);
|
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]);
|
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] || "";
|
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 }) {
|
function RuntimeProductDetail({ configuration }: { configuration: ProductDetailConfiguration }) {
|
||||||
|
|||||||
Reference in new issue
Block a user