feat(website): 对齐官网文章配置与展示顺序

This commit is contained in:
zhanjunhao
2026-11-17 00:38:40 +08:00
parent 34a7eca5c7
commit d563fe7c76
9 changed files with 291 additions and 245 deletions
+108 -125
View File
@@ -30,8 +30,8 @@ import {
TextArea,
Typography,
} from '@/components/ui';
import { articleErrors } from './insightsModel';
import { articleContentInfo, sanitizeArticleHtml } from './insightsContent';
import { articleErrors, RELATED_LIMIT } from './insightsModel';
import { articleHeadings, sanitizeArticleHtml } from './insightsContent';
import ArticleRichEditor from './ArticleRichEditor';
export default function ArticleEditor({ initial, insights, onApply, onClose }) {
@@ -53,8 +53,7 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
setErrors((previous) => ({ ...previous, [name]: '' }));
};
const close = () => {
if (JSON.stringify(draft) === JSON.stringify(initial) && !processingImage)
onClose();
if (!hasDraftChanges && !processingImage) onClose();
else
Modal.confirm({
title: '放弃本次编辑?',
@@ -67,7 +66,7 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
const apply = () => {
if (processingImage) return;
const article = { ...draft, body: sanitizeArticleHtml(draft.body) };
for (const key of ['title', 'summary', 'subtitle', 'tag'])
for (const key of ['title', 'summary', 'tag'])
article[key] = article[key].trim();
const nextErrors = articleErrors(article, insights);
setErrors(nextErrors);
@@ -83,7 +82,10 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
{children}
</Form.Slot>
);
const info = articleContentInfo(draft.body);
const categoryName = (id) =>
insights.categories.find((c) => c.id === id)?.name ?? '';
const headings = articleHeadings(draft.body);
const candidates = insights.articles.filter((a) => a.id !== draft.id);
return (
<SideSheet
visible
@@ -110,6 +112,59 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
>
基础信息
</Typography.Title>
<div className='grid grid-cols-1 gap-x-6 md:grid-cols-2'>
{field(
'category',
'所属分类',
<Select
aria-label='所属分类'
validateStatus={errors.category ? 'error' : 'default'}
aria-invalid={Boolean(errors.category)}
value={
insights.categories.some((c) => c.id === draft.category)
? draft.category
: undefined
}
placeholder='请选择文章分类'
style={{ width: '100%' }}
optionList={insights.categories.map((c) => ({
label: c.name,
value: c.id,
}))}
onChange={(value) => change('category', value)}
/>,
true,
)}
{field(
'date',
'发布日期',
<DatePicker
aria-label='发布日期'
status={errors.date ? 'error' : undefined}
aria-invalid={Boolean(errors.date)}
type='date'
format='YYYY-MM-DD'
placeholder='请选择发布日期'
style={{ width: '100%' }}
value={draft.date || undefined}
onChange={(value) => change('date', value)}
/>,
true,
)}
{field(
'tag',
'角标标签',
<Input
aria-label='角标标签'
validateStatus={errors.tag ? 'error' : 'default'}
aria-invalid={Boolean(errors.tag)}
value={draft.tag}
maxLength={12}
placeholder='例如 NEW、推荐,留空不展示'
onChange={(value) => change('tag', value)}
/>,
)}
</div>
{field(
'title',
'文章标题',
@@ -126,86 +181,21 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
/>,
true,
)}
<div className='grid grid-cols-1 gap-x-6 md:grid-cols-2'>
{field(
'category',
'所属分类',
<Select
aria-label='所属分类'
validateStatus={errors.category ? 'error' : 'default'}
aria-invalid={Boolean(errors.category)}
value={draft.category || undefined}
placeholder='请选择文章分类'
style={{ width: '100%' }}
optionList={[
{ label: '全部内容', value: 'all' },
...insights.categories.map((c) => ({
label: c.name,
value: c.id,
})),
]}
onChange={(value) => change('category', value)}
/>,
true,
)}
{field(
'date',
'发布日期',
<DatePicker
aria-label='发布日期'
status={errors.date ? 'error' : undefined}
aria-invalid={Boolean(errors.date)}
type='dateTime'
format='YYYY-MM-DD HH:mm:ss'
placeholder='请选择发布日期'
style={{ width: '100%' }}
value={draft.date || undefined}
onChange={(value) => change('date', value)}
/>,
true,
)}
</div>
{field(
'subtitle',
'详情副标题',
<Input
aria-label='详情副标题'
validateStatus={errors.subtitle ? 'error' : 'default'}
aria-invalid={Boolean(errors.subtitle)}
value={draft.subtitle}
maxLength={160}
placeholder='留空时使用卡片摘要作为副标题'
onChange={(value) => change('subtitle', value)}
/>,
)}
{field(
'summary',
'卡片摘要',
'文章描述',
<TextArea
aria-label='卡片摘要'
aria-label='文章描述'
validateStatus={errors.summary ? 'error' : 'default'}
aria-invalid={Boolean(errors.summary)}
value={draft.summary}
placeholder='请输入卡片摘要'
placeholder='请输入文章描述,用于列表卡片摘要与详情页标题下方'
maxCount={200}
rows={3}
onChange={(value) => change('summary', value)}
/>,
true,
)}
{field(
'tag',
'角标标签',
<Input
aria-label='角标标签'
validateStatus={errors.tag ? 'error' : 'default'}
aria-invalid={Boolean(errors.tag)}
value={draft.tag}
maxLength={12}
placeholder='例如 NEW、推荐'
onChange={(value) => change('tag', value)}
/>,
)}
<Typography.Title
heading={5}
className='website-insights-section-title website-insights-detail-title'
@@ -226,58 +216,46 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
</div>
<aside className='website-insights-aside'>
<Card title='文章目录'>
<Checkbox
checked={draft.toc}
onChange={(e) => change('toc', e.target.checked)}
>
根据正文标题自动生成
</Checkbox>
<div className='website-insights-toc'>
{!draft.toc ? (
<Typography.Text type='tertiary'>
已关闭目录展示
</Typography.Text>
) : info.headings.length ? (
info.headings.map((h, i) => (
<div
key={i}
style={{ paddingLeft: h.level === 'H3' ? 12 : 0 }}
>
{h.text}
</div>
))
) : (
<Typography.Text type='tertiary'>
添加 H2 / H3 标题后自动生成目录
</Typography.Text>
)}
</div>
<Typography.Text type='tertiary'>
根据正文 H2 标题自动生成,官网点击目录定位到对应标题。
</Typography.Text>
<ol className='website-insights-toc'>
{headings.map((text, i) => (
<li key={i}>{text}</li>
))}
</ol>
{!headings.length && (
<Typography.Text type='tertiary'>
正文暂无 H2 标题,官网将不展示目录项
</Typography.Text>
)}
</Card>
<Card title={`相关推荐(${draft.related.length} / 3)`}>
<Card
title={`相关推荐(${draft.related.length} / ${RELATED_LIMIT})`}
>
<div className='website-insights-related'>
{insights.articles
.filter((a) => a.id !== draft.id)
.map((a) => (
<Checkbox
key={a.id}
checked={draft.related.includes(a.id)}
disabled={
!draft.related.includes(a.id) && draft.related.length >= 3
}
onChange={(e) =>
change(
'related',
e.target.checked
? [...draft.related, a.id]
: draft.related.filter((id) => id !== a.id),
)
}
>
{a.title}
</Checkbox>
))}
{candidates.map((a) => (
<Checkbox
key={a.id}
checked={draft.related.includes(a.id)}
disabled={
!draft.related.includes(a.id) &&
draft.related.length >= RELATED_LIMIT
}
onChange={(e) =>
change(
'related',
e.target.checked
? [...draft.related, a.id]
: draft.related.filter((id) => id !== a.id),
)
}
>
{`${categoryName(a.category)} · ${a.title}`}
</Checkbox>
))}
</div>
{!insights.articles.some((a) => a.id !== draft.id) && (
{!candidates.length && (
<Typography.Text type='tertiary'>
暂无可推荐的其他文章
</Typography.Text>
@@ -288,10 +266,15 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
</Card>
<Card title='展示说明'>
<Typography.Paragraph type='tertiary'>
列表页:标题、摘要、分类、日期、角标。
列表页:分类、发布日期、角标、标题、文章描述,按列表顺序展示。
</Typography.Paragraph>
<Typography.Paragraph type='tertiary'>
详情页:分类、标题、文章描述、正文、目录(正文
H2),不展示发布日期。
</Typography.Paragraph>
<Typography.Text type='tertiary'>
详情页:副标题(留空时使用摘要)、正文、目录和推荐。
相关推荐按勾选顺序展示,最多 {RELATED_LIMIT}{' '}
篇;不勾选则官网不展示相关推荐。
</Typography.Text>
</Card>
</aside>
+76 -32
View File
@@ -40,6 +40,7 @@ import {
categoryError,
createContentId,
filterArticles,
moveArticle,
newArticle,
removeArticle,
} from './insightsModel';
@@ -56,6 +57,9 @@ export default function InsightsPanel({ value, onChange, disabled, error }) {
? category
: 'all';
const rows = filterArticles(value, keyword, activeCategory);
// Reordering only makes sense on the full list, which is the official site order.
const sortable = activeCategory === 'all' && !keyword.trim();
const hasCategories = value.categories.length > 0;
const count = (id) =>
id === 'all'
? value.articles.length
@@ -91,7 +95,7 @@ export default function InsightsPanel({ value, onChange, disabled, error }) {
};
const deleteCategory = (item) => {
if (count(item.id)) {
showError('该分类下仍有文章,请先调整文章所属分类,再删除分类。');
showError('该分类下仍有文章,请先将文章调整到其他分类,再删除分类。');
return;
}
Modal.confirm({
@@ -158,37 +162,68 @@ export default function InsightsPanel({ value, onChange, disabled, error }) {
title: '所属分类',
dataIndex: 'category',
width: 140,
render: (id) =>
id === 'all'
? '全部内容'
: value.categories.find((c) => c.id === id)?.name || '未分类',
render: (id) => value.categories.find((c) => c.id === id)?.name,
},
{ title: '发布日期', dataIndex: 'date', width: 180 },
{
title: '角标',
dataIndex: 'tag',
width: 100,
render: (tag) => tag || '-',
},
{ title: '发布日期', dataIndex: 'date', width: 140 },
{
title: '操作',
key: 'actions',
width: 160,
render: (_, item) => (
<div className='website-insights-row-actions'>
<Button
type='link'
size='small'
disabled={disabled}
onClick={() => setArticle({ ...item, related: [...item.related] })}
>
编辑
</Button>
<Button
type='link'
size='small'
danger
disabled={disabled}
onClick={() => deleteArticle(item)}
>
删除
</Button>
</div>
),
width: 220,
render: (_, item) => {
const index = value.articles.findIndex((a) => a.id === item.id);
const move = (offset) => onChange(moveArticle(value, item.id, offset));
return (
<div className='website-insights-row-actions'>
<Button
type='link'
size='small'
disabled={disabled}
onClick={() =>
setArticle({ ...item, related: [...item.related] })
}
>
编辑
</Button>
<Button
type='link'
size='small'
aria-label={`上移${item.title}`}
title={sortable ? undefined : '请在全部分类且无搜索时调整顺序'}
disabled={disabled || !sortable || index <= 0}
onClick={() => move(-1)}
>
上移
</Button>
<Button
type='link'
size='small'
aria-label={`下移${item.title}`}
title={sortable ? undefined : '请在全部分类且无搜索时调整顺序'}
disabled={
disabled || !sortable || index >= value.articles.length - 1
}
onClick={() => move(1)}
>
下移
</Button>
<Button
type='link'
size='small'
danger
disabled={disabled}
onClick={() => deleteArticle(item)}
>
删除
</Button>
</div>
);
},
},
];
return (
@@ -304,7 +339,8 @@ export default function InsightsPanel({ value, onChange, disabled, error }) {
<Button
icon={<IconPlus />}
theme='solid'
disabled={disabled}
disabled={disabled || !hasCategories}
title={hasCategories ? undefined : '请先新增内容分类'}
onClick={() => {
setArticle(newArticle(activeCategory));
}}
@@ -313,11 +349,19 @@ export default function InsightsPanel({ value, onChange, disabled, error }) {
</Button>
</div>
</TableToolbar>
{!hasCategories && (
<Typography.Paragraph type='tertiary'>
请先新增内容分类,文章需归属一个分类后才能在官网展示。
</Typography.Paragraph>
)}
<Typography.Paragraph type='tertiary'>
列表顺序即官网展示顺序,新增文章默认排在最前,可在“全部分类”下上移或下移调整。
</Typography.Paragraph>
<CardTable
rowKey='id'
columns={columns}
dataSource={rows}
scroll={{ x: 860 }}
scroll={{ x: 1040 }}
empty={<TableEmpty />}
hidePagination
/>
@@ -337,12 +381,12 @@ export default function InsightsPanel({ value, onChange, disabled, error }) {
}
>
<Typography.Paragraph type='tertiary'>
“全部内容”为默认分类,也汇总展示全部文章;已有文章的自定义分类需先迁移文章再删除。
“全部内容”为官网默认筛选项,汇总展示全部文章,不能作为文章分类;已有文章的分类需先将文章调整到其他分类再删除。
</Typography.Paragraph>
<div className='website-insights-manage-row'>
<span>全部内容</span>
<Typography.Text type='tertiary'>
默认分类(不可删除)
默认筛选项(不可删除)
</Typography.Text>
</div>
{value.categories.map((c) => (
+21 -14
View File
@@ -64,30 +64,37 @@ ArticleEditor.jsx 承载文章新增/编辑抽屉,ArticleRichEditor.jsx 复用
工具栏采用独立的中性样式,避免项目 primary 按钮全局渐变影响编辑工具和选中状态。
默认保留公司动态、产品更新、行业洞察三个分类,文章为空,不向官网注入原型示例文章。
单篇文章结构:
字段对齐官网 weelink-open 的 `/insights` 列表页与 `/insight-detail?id=` 详情页,单篇文章结构:
```json
{
"id": "由前端生成的 UUID,后续可替换为后端 ID",
"id": "由前端生成的 UUID,官网详情页 ?id= 使用",
"category": "分类 id,必须是 categories 中的分类",
"date": "2026-11-16",
"title": "文章标题",
"category": "all",
"date": "2026-10-31",
"subtitle": "详情副标题,空值时由官网使用 summary",
"summary": "卡片摘要",
"summary": "文章描述:列表卡片摘要与详情页标题下方描述共用",
"tag": "NEW",
"body": "<h2>章节标题</h2><p>文章正文</p>",
"toc": true,
"related": []
}
```
必填:title(最多 80 字)、category、date、summary(最多 200 字)、body。
subtitle 最多 160 字,tag 最多 12 字,推荐最多 3 篇且不能选择自己。
正文 H2/H3 自动生成目录预览;toc 为是否展示目录,官网可从 body 提取标题。
“全部内容”是内置默认归属(category 为 all),也汇总展示所有分类的文章,不存入自定义 categories 数组。
新增文章默认归属当前分类,从全部内容新增时直接归属 all;自定义分类为空时也可新增文章。
分类名称最多 12 字,禁止空白、重名和系统保留名称;已有文章的自定义分类不能直接删除,可先迁移到全部内容。
删除文章同步清理其他文章的 related 引用。列表按发布日期倒序排列。
| 官网位置 | 使用字段 |
| -------------- | -------------------------------------------------------------------------------------------- |
| 列表页分类页签 | “全部内容” + `categories[].name`(按数组顺序) |
| 列表卡片 | 分类名称、`date`(官网格式化为 `YYYY.MM.DD`)、`tag`(为空不展示)、`title`、`summary`、`id` |
| 详情页头部 | 分类名称(标签与面包屑)、`title`、`summary` |
| 详情页正文 | `body` 富文本,官网渲染前需再次白名单清理 |
| 详情页目录 | 正文中的非空 H2,按出现顺序生成锚点,点击定位到对应标题 |
| 相关推荐 | `related` 按勾选顺序,最多 3 篇;为空时官网不展示该区块 |
必填:title(最多 80 字)、category、date、summary(最多 200 字)、body。tag 最多 12 字,推荐最多 3 篇且不能选择自己。
发布日期格式 `YYYY-MM-DD`,只用于列表卡片,详情页不展示;新增文章默认当天,不影响排序。
读取配置时只保留上述字段并去除首尾空白,不兼容其他历史字段。
“全部内容”仅是官网与后台的筛选项,不能作为文章分类。
没有任何分类时不能新增文章;已有文章的分类不能直接删除,需先将文章调整到其他分类。
文章数组顺序即官网展示顺序:新增文章排在最前,可在“全部分类”且无搜索时上移、下移。删除文章同步清理其他文章的 related 引用。
允许保存空文章列表,官网列表页显示“内容筹备中”空状态。
文章抽屉“保存”、分类编辑与文章删除只更新当前内存配置,不请求后端保存接口。
页面底部“保存”统一校验四个页签后,将整个配置序列化为 JSON 字符串调用 saveWebsiteSettings。
@@ -86,15 +86,13 @@
.website-insights-category,
.website-insights-toolbar,
.website-insights-manage-row,
.website-insights-editor-footer,
.website-rich-tools {
.website-insights-editor-footer {
display: flex;
align-items: center;
gap: 8px;
}
.website-insights-category-list,
.website-rich-tools,
.website-insights-editor-footer {
flex-wrap: wrap;
}
@@ -232,9 +230,13 @@
}
.website-insights-toc {
margin-top: 12px;
margin: 12px 0 0;
padding-left: 20px;
max-height: 240px;
overflow-y: auto;
line-height: 1.8;
overflow-wrap: anywhere;
list-style: decimal;
}
.website-insights-editor-footer {
+5 -8
View File
@@ -17,16 +17,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { sanitizeArticleHtml } from '../../components/common/ui/richTextContent.js';
import { countRichText } from '../../components/common/ui/richTextLength.js';
export { sanitizeArticleHtml };
export function articleContentInfo(html) {
// The website TOC is one level: every non-empty H2 in the body, in document order.
export function articleHeadings(html) {
const template = document.createElement('template');
template.innerHTML = sanitizeArticleHtml(html);
return {
count: countRichText(template.content.textContent),
headings: [...template.content.querySelectorAll('h2,h3')]
.map((h) => ({ level: h.tagName, text: h.textContent.trim() }))
.filter((h) => h.text),
};
return [...template.content.querySelectorAll('h2')]
.map((h) => h.textContent.trim())
.filter(Boolean);
}
+45 -46
View File
@@ -53,41 +53,35 @@ export function categoryError(name, categories, id) {
return '';
}
export const RELATED_LIMIT = 3;
const pad = (value) => String(value).padStart(2, '0');
// Publish time is stored as local "YYYY-MM-DD HH:mm:ss"; legacy "YYYY-MM-DD" stays valid.
// Publish date is a local calendar day "YYYY-MM-DD" shown on the website list cards.
export function isValidPublishDate(value) {
const match = /^(\d{4})-(\d{2})-(\d{2})(?: (\d{2}):(\d{2}):(\d{2}))?$/.exec(
value,
);
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
if (!match) return false;
const [year, month, day, hour = 0, minute = 0, second = 0] = match
.slice(1)
.map((part) => (part === undefined ? undefined : Number(part)));
const date = new Date(year, month - 1, day, hour, minute, second);
const [year, month, day] = match.slice(1).map(Number);
const date = new Date(year, month - 1, day);
return (
date.getFullYear() === year &&
date.getMonth() === month - 1 &&
date.getDate() === day &&
date.getHours() === hour &&
date.getMinutes() === minute &&
date.getSeconds() === second
date.getDate() === day
);
}
export function newArticle(category = 'all') {
// Website list card: category / date / tag / title / summary.
// Website detail page: category / title / summary / rich body (H2 table of contents) / related.
export function newArticle(category = '') {
const now = new Date();
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
return {
id: '',
title: '',
category,
date,
subtitle: '',
category: category === 'all' ? '' : category,
date: `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`,
summary: '',
tag: '',
body: '',
toc: true,
related: [],
};
}
@@ -96,8 +90,7 @@ export function articleErrors(article, insights) {
const errors = {};
for (const [key, label, limit, required] of [
['title', '文章标题', 80, true],
['summary', '卡片摘要', 200, true],
['subtitle', '详情副标题', 160, false],
['summary', '文章描述', 200, true],
['tag', '角标标签', 12, false],
]) {
const text = String(article[key] ?? '').trim();
@@ -105,10 +98,8 @@ export function articleErrors(article, insights) {
else if (text.length > limit)
errors[key] = `${label}不能超过 ${limit} 个字符`;
}
if (
article.category !== 'all' &&
!insights.categories.some((c) => c.id === article.category)
)
// Every article needs a real category: the site renders it as the tag, breadcrumb and list filter.
if (!insights.categories.some((c) => c.id === article.category))
errors.category = '请选择所属分类';
const date = String(article.date ?? '');
if (!isValidPublishDate(date))
@@ -132,40 +123,40 @@ export function articleErrors(article, insights) {
errors.body = '正文包含不支持的内容,请清除格式后重试';
if (
!Array.isArray(article.related) ||
article.related.length > 3 ||
article.related.length > RELATED_LIMIT ||
new Set(article.related).size !== article.related.length ||
article.related.some(
(id) => id === article.id || !insights.articles.some((a) => a.id === id),
)
)
errors.related = '最多选择 3 篇有效的其他文章';
errors.related = `最多选择 ${RELATED_LIMIT} 篇有效的其他文章`;
return errors;
}
const ARTICLE_TEXT_FIELDS = [
'id',
'title',
'category',
'date',
'summary',
'tag',
'body',
];
// Articles keep only the website schema with trimmed text; other insights keys are preserved
// per the page-config contract (unknown fields survive a save).
export function normalizeInsights(value) {
const data = value ?? emptyInsights();
return {
...data,
categories: (data.categories || []).map((c) => ({
...c,
id: String(c.id),
name: String(c.name ?? '').trim(),
})),
articles: (data.articles || []).map((a) => ({
...a,
...Object.fromEntries(
[
'id',
'title',
'category',
'date',
'subtitle',
'summary',
'tag',
'body',
].map((key) => [key, String(a[key] ?? '').trim()]),
ARTICLE_TEXT_FIELDS.map((key) => [key, String(a[key] ?? '').trim()]),
),
toc: a.toc !== false,
related: [...(a.related || [])],
})),
};
@@ -207,13 +198,21 @@ export function removeArticle(insights, id) {
};
}
// Array order is the display order on the official site.
export function filterArticles(insights, keyword, category) {
const query = keyword.trim().toLocaleLowerCase();
return insights.articles
.filter(
(a) =>
(category === 'all' || a.category === category) &&
`${a.title} ${a.summary}`.toLocaleLowerCase().includes(query),
)
.sort((a, b) => b.date.localeCompare(a.date));
return insights.articles.filter(
(a) =>
(category === 'all' || a.category === category) &&
`${a.title} ${a.summary}`.toLocaleLowerCase().includes(query),
);
}
export function moveArticle(insights, id, offset) {
const from = insights.articles.findIndex((a) => a.id === id);
const to = from + offset;
if (from < 0 || to < 0 || to >= insights.articles.length) return insights;
const articles = [...insights.articles];
[articles[from], articles[to]] = [articles[to], articles[from]];
return { ...insights, articles };
}
+2 -6
View File
@@ -168,15 +168,11 @@ export function validateField(key, rawValue) {
return '';
}
// An empty article list is valid: the official list page shows its "内容筹备中" empty state.
export function validateSettings(values) {
return Object.fromEntries(
[...new Set(TABS.flatMap((tab) => tab.fields))]
.map((key) => [
key,
key === 'insights' && !values.insights?.articles?.length
? '请完善动态与洞察的配置'
: validateField(key, values[key]),
])
.map((key) => [key, validateField(key, values[key])])
.filter(([, error]) => error),
);
}