feat(ui): 改造模板库页面
Deploy / Deploy Staging (push) Failing after 197h14m12s
CI/CD Pipeline / Frontend Lint (push) Failing after 197h14m41s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 197h14m51s
Deploy / Production Browser E2E (push) Failing after 1855h48m47s
Deploy / Build Production Runtime Images (push) Failing after 1855h48m52s
Deploy / Deploy Production (push) Failing after 1856h20m10s

- 新增 templates.css V21 设计系统样式
- 重写 TemplateLibrary.tsx 移除所有 Ant Design 依赖
- 页面头部:标题 + 副标题 + 创建模板按钮
- 工具栏:搜索框(max-width 400px)+ 分类胶囊按钮组(全部/口播/种草/产品/品牌)
- 4列模板卡片网格,16:9 缩略图 + 渐变背景 + 代码文本
- 收藏切换功能(本地状态)
- 空状态展示
- 响应式:1200px→3列, 768px→2列, 480px→1列
- 全部使用 CSS 变量 + mock 数据
This commit is contained in:
灵应
2026-07-01 07:39:36 +08:00
parent d8c8fcf858
commit 5805892fdb
2 changed files with 601 additions and 217 deletions
+224 -217
View File
@@ -1,244 +1,251 @@
/**
* 模板库页面
* 展示系统模板,支持预览和筛选
* 模板库页面 — V21 设计系统
* 页面头部 + 工具栏(搜索+分类胶囊按钮组)+ 4列模板卡片网格 + 空状态
* 使用 mock 数据,CSS 变量,V21 组件
*/
import React, { useState } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import {
Card,
Button,
Typography,
Space,
Row,
Col,
Tag,
Empty,
Spin,
Input,
Select,
Modal,
Image,
message,
} from "antd";
import {
PlayCircleOutlined,
StarOutlined,
StarFilled,
SearchOutlined,
} from "@ant-design/icons";
import {
getTemplates,
toggleFavoriteTemplate,
type TemplateItem,
} from "@/api/templates";
import PageHead from "@/components/layout/PageHead";
import { Button } from "@/components/ui";
import "./templates.css";
const { Paragraph } = Typography;
/* ============================================================
* Mock 数据
* ============================================================ */
type TemplateCategory = "口播" | "种草" | "产品" | "品牌";
interface TemplateItem {
id: string;
name: string;
category: TemplateCategory;
usageCount: number;
isFavorite: boolean;
gradient: string;
codeSnippet: string;
}
const categories: Array<"全部" | TemplateCategory> = [
"全部",
"口播",
"种草",
"产品",
"品牌",
];
const mockTemplates: TemplateItem[] = [
{
id: "tpl-1",
name: "商品口播模板",
category: "口播",
usageCount: 128,
isFavorite: true,
gradient: "linear-gradient(135deg, #6366f1, #8b5cf6)",
codeSnippet:
"# 商品口播脚本\n## 开场白\n大家好,今天给大家推荐...\n## 产品介绍\n这款产品的特点是...",
},
{
id: "tpl-2",
name: "种草笔记视频模板",
category: "种草",
usageCount: 96,
isFavorite: false,
gradient: "linear-gradient(135deg, #10b981, #059669)",
codeSnippet:
"# 种草视频\n## 使用场景\n早上出门前的护肤步骤...\n## 产品亮点\n温和不刺激,适合敏感肌...",
},
{
id: "tpl-3",
name: "新品发布展示模板",
category: "产品",
usageCount: 74,
isFavorite: false,
gradient: "linear-gradient(135deg, #0ea5e9, #0284c7)",
codeSnippet:
"# 新品发布\n## 产品概览\n全新升级,性能提升50%...\n## 核心功能\nAI智能识别,一键优化...",
},
{
id: "tpl-4",
name: "品牌故事宣传片",
category: "品牌",
usageCount: 52,
isFavorite: true,
gradient: "linear-gradient(135deg, #f59e0b, #d97706)",
codeSnippet:
"# 品牌故事\n## 品牌起源\n2020年,我们从一个想法开始...\n## 品牌理念\n让创作更简单...",
},
{
id: "tpl-5",
name: "知识分享口播模板",
category: "口播",
usageCount: 215,
isFavorite: false,
gradient: "linear-gradient(135deg, #8b5cf6, #7c3aed)",
codeSnippet:
"# 知识分享\n## 主题引入\n今天我们来聊一个很多人问的问题...\n## 核心内容\n第一点,第二点,第三点...",
},
{
id: "tpl-6",
name: "好物推荐种草模板",
category: "种草",
usageCount: 183,
isFavorite: true,
gradient: "linear-gradient(135deg, #059669, #047857)",
codeSnippet:
"# 好物推荐\n## 痛点引入\n你是不是也有这样的困扰...\n## 解决方案\n直到我发现了这个神器...",
},
{
id: "tpl-7",
name: "产品对比评测模板",
category: "产品",
usageCount: 67,
isFavorite: false,
gradient: "linear-gradient(135deg, #0284c7, #0369a1)",
codeSnippet:
"# 产品对比\n## 对比维度\n外观、性能、价格、体验...\n## 总结推荐\n综合来看,A适合...B适合...",
},
{
id: "tpl-8",
name: "品牌活动预热模板",
category: "品牌",
usageCount: 41,
isFavorite: false,
gradient: "linear-gradient(135deg, #d97706, #b45309)",
codeSnippet:
"# 活动预热\n## 悬念引入\n倒计时3天,惊喜即将揭晓...\n## 活动亮点\n福利一、福利二、福利三...",
},
];
/* ============================================================
* 组件
* ============================================================ */
const TemplateLibrary: React.FC = () => {
const queryClient = useQueryClient();
const [searchText, setSearchText] = useState("");
const [categoryFilter, setCategoryFilter] = useState("");
const [previewTemplate, setPreviewTemplate] = useState<TemplateItem | null>(
null,
const [activeCategory, setActiveCategory] = useState<
"全部" | TemplateCategory
>("全部");
const [favorites, setFavorites] = useState<Set<string>>(
() => new Set(mockTemplates.filter((t) => t.isFavorite).map((t) => t.id))
);
// 获取模板列表
const { data: templates = [], isLoading } = useQuery({
queryKey: ["templates"],
queryFn: getTemplates,
});
/** 切换收藏 */
const toggleFavorite = (id: string, e: React.MouseEvent) => {
e.stopPropagation();
setFavorites((prev) => {
const next = new Set(prev);
if (next.has(id)) {
next.delete(id);
} else {
next.add(id);
}
return next;
});
};
// 收藏/取消收藏
const favMutation = useMutation({
mutationFn: toggleFavoriteTemplate,
onSuccess: (data) => {
message.success(data.is_favorite ? "已收藏" : "已取消收藏");
queryClient.invalidateQueries({ queryKey: ["templates"] });
},
onError: (err: unknown) => {
if (!(err as { __msgShown?: boolean })?.__msgShown)
message.error("操作失败");
},
});
/** 提取所有分类 */
const categories = Array.from(
new Set(templates.map((t) => t.category).filter(Boolean)),
);
/** 过滤后的模板 */
const filteredTemplates = templates.filter((t) => {
/** 过滤模板 */
const filtered = mockTemplates.filter((t) => {
const matchSearch =
!searchText ||
t.name.toLowerCase().includes(searchText.toLowerCase()) ||
(t.description || "").toLowerCase().includes(searchText.toLowerCase());
const matchCategory = !categoryFilter || t.category === categoryFilter;
t.codeSnippet.toLowerCase().includes(searchText.toLowerCase());
const matchCategory =
activeCategory === "全部" || t.category === activeCategory;
return matchSearch && matchCategory;
});
return (
<div style={{ padding: "24px", maxWidth: 1200, margin: "0 auto" }}>
<PageHead title="模板库" />
<div className="xx-templates-page">
{/* ── 页面头部 ──────────────────────────────────────────── */}
<div className="xx-templates-header">
<div className="xx-templates-header-text">
<h2>模板库</h2>
<p>选择模板快速开始创作,支持自定义修改</p>
</div>
<Button buttonType="primary" buttonSize="md">
+ 创建模板
</Button>
</div>
{/* 搜索和筛选 */}
<Row gutter={[16, 16]} style={{ marginBottom: 24 }}>
<Col xs={24} sm={16} md={18}>
<Input
prefix={<SearchOutlined />}
placeholder="搜索模板..."
{/* ── 工具栏:搜索 + 分类按钮组 ─────────────────────────── */}
<div className="xx-templates-toolbar">
<div className="xx-templates-search">
<span className="xx-templates-search-icon">🔍</span>
<input
className="xx-templates-search-input"
type="text"
placeholder="搜索模板名称或内容..."
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
allowClear
/>
</Col>
<Col xs={24} sm={8} md={6}>
<Select
placeholder="按分类筛选"
value={categoryFilter || undefined}
onChange={setCategoryFilter}
allowClear
style={{ width: "100%" }}
options={categories.map((c) => ({ value: c, label: c }))}
/>
</Col>
</Row>
{isLoading ? (
<div style={{ textAlign: "center", padding: 60 }}>
<Spin size="large" />
</div>
) : filteredTemplates.length === 0 ? (
<Empty
description={
searchText || categoryFilter ? "未找到匹配的模板" : "暂无模板"
}
/>
) : (
<Row gutter={[16, 16]}>
{filteredTemplates.map((template) => (
<Col xs={24} sm={12} md={8} lg={6} key={template.id}>
<Card
hoverable
size="small"
cover={
template.thumbnail_url ? (
<Image
src={template.thumbnail_url}
alt={template.name}
style={{ height: 180, objectFit: "cover" }}
fallback="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjE4MCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCB3aWR0aD0iMjAwIiBoZWlnaHQ9IjE4MCIgZmlsbD0iI2YwZjBmMCIvPjx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBkb21pbmFudC1iYXNlbGluZT0ibWlkZGxlIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBmaWxsPSIjOTk5IiBmb250LXNpemU9IjE0Ij7lt6XlooPlvIDova7kuK3lm77niYw8L3RleHQ+PC9zdmc+"
/>
) : (
<div
style={{
height: 180,
display: "flex",
alignItems: "center",
justifyContent: "center",
background: "#f5f5f5",
}}
>
<PlayCircleOutlined
style={{ fontSize: 48, color: "#bbb" }}
/>
</div>
)
}
actions={[
<Button
key="preview"
type="text"
size="small"
icon={<PlayCircleOutlined />}
onClick={() => setPreviewTemplate(template)}
>
预览
</Button>,
<Button
key="fav"
type="text"
size="small"
icon={
template.is_favorite ? (
<StarFilled style={{ color: "#faad14" }} />
) : (
<StarOutlined />
)
}
disabled={favMutation.isPending}
onClick={() => favMutation.mutate(template.id)}
/>,
]}
>
<Card.Meta
title={template.name}
description={
<Space direction="vertical" size={4}>
<Paragraph
ellipsis={{ rows: 2 }}
style={{ marginBottom: 0, fontSize: 12 }}
>
{template.description || "暂无描述"}
</Paragraph>
<Space>
{template.category && (
<Tag color="blue">{template.category}</Tag>
)}
{template.target_duration && (
<Tag>目标时长 {template.target_duration}s</Tag>
)}
</Space>
</Space>
}
/>
</Card>
</Col>
<div className="xx-templates-categories">
{categories.map((cat) => (
<button
key={cat}
className={`xx-templates-cat-btn${activeCategory === cat ? " active" : ""}`}
onClick={() => setActiveCategory(cat)}
>
{cat}
</button>
))}
</Row>
)}
</div>
</div>
{/* 预览弹窗 */}
<Modal
title={previewTemplate?.name}
open={!!previewTemplate}
onCancel={() => setPreviewTemplate(null)}
footer={
<Button
type="primary"
onClick={() => {
// TODO: 跳转到生成页面,携带模板参数
setPreviewTemplate(null);
}}
>
使用此模板
</Button>
}
>
{previewTemplate && (
<Space direction="vertical" style={{ width: "100%" }}>
{previewTemplate.preview_url && (
<video
src={previewTemplate.preview_url}
controls
style={{ width: "100%", maxHeight: 400 }}
/>
)}
<Paragraph>{previewTemplate.description}</Paragraph>
<Space wrap>
{previewTemplate.category && (
<Tag color="blue">{previewTemplate.category}</Tag>
)}
{previewTemplate.target_duration && (
<Tag>目标时长 {previewTemplate.target_duration}s</Tag>
)}
{previewTemplate.clip_count && (
<Tag>片段数 {previewTemplate.clip_count}</Tag>
)}
</Space>
</Space>
)}
</Modal>
{/* ── 模板卡片网格 ──────────────────────────────────────── */}
{filtered.length === 0 ? (
<div className="xx-templates-empty">
<div className="xx-templates-empty-icon">📭</div>
<h3>
{searchText || activeCategory !== "全部"
? "未找到匹配的模板"
: "暂无模板"}
</h3>
<p>
{searchText || activeCategory !== "全部"
? "试试调整搜索条件或切换分类"
: "点击上方「创建模板」开始创作"}
</p>
</div>
) : (
<div className="xx-templates-grid">
{filtered.map((tpl) => (
<div key={tpl.id} className="xx-template-card">
{/* 缩略图 */}
<div className="xx-template-thumb">
<div
className="xx-template-thumb-bg"
style={{ background: tpl.gradient }}
>
{tpl.codeSnippet}
</div>
<div className="xx-template-thumb-overlay" />
<button
className={`xx-template-fav-btn${favorites.has(tpl.id) ? " is-favorite" : ""}`}
onClick={(e) => toggleFavorite(tpl.id, e)}
title={favorites.has(tpl.id) ? "取消收藏" : "收藏"}
>
{favorites.has(tpl.id) ? "★" : "☆"}
</button>
</div>
{/* 信息区 */}
<div className="xx-template-info">
<div className="xx-template-info-top">
<h4 className="xx-template-name">{tpl.name}</h4>
<span
className={`xx-template-category-pill xx-template-category-pill--${tpl.category}`}
>
{tpl.category}
</span>
</div>
<div className="xx-template-meta">
<span className="xx-template-usage">
已使用 {tpl.usageCount} 次
</span>
<button className="xx-template-use-btn">使用此模板</button>
</div>
</div>
</div>
))}
</div>
)}
</div>
);
};
+377
View File
@@ -0,0 +1,377 @@
/**
* 模板库页面 - V21 设计系统样式
* 页面头部 + 工具栏(搜索+分类按钮组)+ 4列模板卡片网格 + 空状态
* 统一使用 CSS 变量,支持深色/浅色主题
*/
@import "../../styles/global.css";
/* ============================================================
页面容器
============================================================ */
.xx-templates-page {
min-height: 100%;
padding: var(--space-xl);
}
/* ============================================================
页面头部
============================================================ */
.xx-templates-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
margin-bottom: var(--space-lg);
}
.xx-templates-header-text h2 {
margin: 0 0 4px;
font-size: var(--font-size-xl);
font-weight: var(--font-weight-bold);
color: var(--text-primary);
}
.xx-templates-header-text p {
margin: 0;
font-size: var(--font-size-sm);
color: var(--text-secondary);
}
/* ============================================================
工具栏:搜索框 + 分类按钮组
============================================================ */
.xx-templates-toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--space-md);
margin-bottom: var(--space-lg);
flex-wrap: wrap;
}
.xx-templates-search {
position: relative;
max-width: 400px;
flex: 1;
min-width: 200px;
}
.xx-templates-search-icon {
position: absolute;
left: 14px;
top: 50%;
transform: translateY(-50%);
font-size: 16px;
color: var(--text-secondary);
pointer-events: none;
}
.xx-templates-search-input {
width: 100%;
height: 40px;
padding: 0 14px 0 40px;
border: 1px solid var(--border-color);
border-radius: var(--radius-md);
background: var(--bg-primary);
color: var(--text-primary);
font-size: var(--font-size-sm);
transition: var(--transition-all);
outline: none;
}
.xx-templates-search-input::placeholder {
color: var(--text-secondary);
}
.xx-templates-search-input:focus {
border-color: var(--primary-color);
box-shadow: 0 0 0 3px var(--primary-soft);
}
/* 分类按钮组 */
.xx-templates-categories {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
.xx-templates-cat-btn {
padding: 6px 18px;
border-radius: 999px;
border: 1px solid var(--border-color);
background: var(--bg-primary);
color: var(--text-secondary);
font-size: var(--font-size-sm);
font-weight: 500;
cursor: pointer;
transition: var(--transition-all);
white-space: nowrap;
}
.xx-templates-cat-btn:hover {
border-color: var(--primary-color);
color: var(--primary-color);
}
.xx-templates-cat-btn.active {
background: var(--primary-soft);
border-color: var(--primary-color);
color: var(--primary-color);
font-weight: 600;
}
/* ============================================================
模板卡片网格
============================================================ */
.xx-templates-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: var(--space-md);
}
/* ============================================================
模板卡片
============================================================ */
.xx-template-card {
background: var(--bg-primary);
border: 1px solid var(--border-color);
border-radius: var(--radius-lg);
overflow: hidden;
transition: var(--transition-all);
cursor: pointer;
}
.xx-template-card:hover {
border-color: var(--primary-color);
box-shadow: var(--shadow-md);
transform: translateY(-3px);
}
/* 缩略图区域 — 16:9 */
.xx-template-thumb {
position: relative;
aspect-ratio: 16 / 9;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
}
.xx-template-thumb-bg {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
font-size: 12px;
color: rgba(255, 255, 255, 0.7);
line-height: 1.6;
padding: 16px;
white-space: pre-wrap;
word-break: break-all;
}
/* 底部渐变遮罩 */
.xx-template-thumb-overlay {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 50%;
background: linear-gradient(0deg, rgba(0, 0, 0, 0.3), transparent);
pointer-events: none;
}
/* 收藏按钮 */
.xx-template-fav-btn {
position: absolute;
top: 10px;
right: 10px;
width: 32px;
height: 32px;
border-radius: 50%;
border: none;
background: rgba(0, 0, 0, 0.35);
backdrop-filter: blur(4px);
color: #fff;
font-size: 16px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: var(--transition-all);
z-index: 2;
}
.xx-template-fav-btn:hover {
background: rgba(0, 0, 0, 0.55);
transform: scale(1.1);
}
.xx-template-fav-btn.is-favorite {
color: #fbbf24;
background: rgba(0, 0, 0, 0.5);
}
/* 卡片信息区 */
.xx-template-info {
padding: 14px 16px;
}
.xx-template-info-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 10px;
}
.xx-template-name {
margin: 0;
font-size: 14px;
font-weight: 600;
color: var(--text-primary);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
flex: 1;
min-width: 0;
}
/* 分类药丸 */
.xx-template-category-pill {
display: inline-flex;
align-items: center;
padding: 2px 10px;
border-radius: 999px;
font-size: 11px;
font-weight: 500;
white-space: nowrap;
flex-shrink: 0;
}
.xx-template-category-pill--口播 {
color: var(--primary-color);
background: var(--primary-soft);
}
.xx-template-category-pill--种草 {
color: var(--success-color);
background: var(--success-soft);
}
.xx-template-category-pill--产品 {
color: var(--info-color, #0ea5e9);
background: var(--info-soft, #e0f2fe);
}
.xx-template-category-pill--品牌 {
color: var(--warning-color);
background: var(--warning-soft);
}
/* 使用次数 */
.xx-template-meta {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.xx-template-usage {
font-size: 12px;
color: var(--text-secondary);
}
.xx-template-use-btn {
padding: 5px 14px;
border-radius: var(--radius-md);
border: none;
background: var(--gradient-primary);
color: #fff;
font-size: 12px;
font-weight: 500;
cursor: pointer;
transition: var(--transition-all);
white-space: nowrap;
}
.xx-template-use-btn:hover {
opacity: 0.9;
box-shadow: var(--shadow-sm);
}
/* ============================================================
空状态
============================================================ */
.xx-templates-empty {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 80px 20px;
text-align: center;
}
.xx-templates-empty-icon {
font-size: 56px;
margin-bottom: 16px;
opacity: 0.5;
}
.xx-templates-empty h3 {
margin: 0 0 8px;
font-size: var(--font-size-base);
font-weight: var(--font-weight-semibold);
color: var(--text-primary);
}
.xx-templates-empty p {
margin: 0;
font-size: var(--font-size-sm);
color: var(--text-secondary);
}
/* ============================================================
响应式
============================================================ */
@media (max-width: 1200px) {
.xx-templates-grid {
grid-template-columns: repeat(3, 1fr);
}
}
@media (max-width: 768px) {
.xx-templates-page {
padding: var(--space-md);
}
.xx-templates-grid {
grid-template-columns: repeat(2, 1fr);
}
.xx-templates-header {
flex-direction: column;
gap: var(--space-sm);
}
.xx-templates-toolbar {
flex-direction: column;
align-items: stretch;
}
.xx-templates-search {
max-width: none;
}
.xx-templates-categories {
justify-content: flex-start;
}
}
@media (max-width: 480px) {
.xx-templates-grid {
grid-template-columns: 1fr;
}
}