67969a041d
补齐页面缺译,统一动态文案的整句插值方式。
291 lines
10 KiB
React
291 lines
10 KiB
React
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import { useTranslation } from 'react-i18next';
|
||
import React, { useEffect, useState } from 'react';
|
||
import {
|
||
Button,
|
||
Card,
|
||
Checkbox,
|
||
DatePicker,
|
||
Form,
|
||
Input,
|
||
Modal,
|
||
Select,
|
||
SideSheet,
|
||
TextArea,
|
||
Typography,
|
||
} from '@/components/ui';
|
||
import { articleErrors, RELATED_LIMIT } from './insightsModel';
|
||
import { articleHeadings, sanitizeArticleHtml } from './insightsContent';
|
||
import ArticleRichEditor from './ArticleRichEditor';
|
||
|
||
export default function ArticleEditor({ initial, insights, onApply, onClose }) {
|
||
const { t } = useTranslation();
|
||
|
||
const [draft, setDraft] = useState(initial);
|
||
const [errors, setErrors] = useState({});
|
||
const [processingImage, setProcessingImage] = useState(false);
|
||
const hasDraftChanges = JSON.stringify(draft) !== JSON.stringify(initial);
|
||
useEffect(() => {
|
||
if (!hasDraftChanges && !processingImage) return;
|
||
const warn = (event) => {
|
||
event.preventDefault();
|
||
event.returnValue = '';
|
||
};
|
||
window.addEventListener('beforeunload', warn);
|
||
return () => window.removeEventListener('beforeunload', warn);
|
||
}, [hasDraftChanges, processingImage]);
|
||
const change = (name, value) => {
|
||
setDraft((previous) => ({ ...previous, [name]: value }));
|
||
setErrors((previous) => ({ ...previous, [name]: '' }));
|
||
};
|
||
const close = () => {
|
||
if (!hasDraftChanges && !processingImage) onClose();
|
||
else
|
||
Modal.confirm({
|
||
title: '放弃本次编辑?',
|
||
content: '当前文章的修改尚未应用,关闭后将丢失本次编辑内容。',
|
||
okText: '放弃修改',
|
||
cancelText: '继续编辑',
|
||
onOk: onClose,
|
||
});
|
||
};
|
||
const apply = () => {
|
||
if (processingImage) return;
|
||
const article = { ...draft, body: sanitizeArticleHtml(draft.body) };
|
||
for (const key of ['title', 'summary', 'tag'])
|
||
article[key] = article[key].trim();
|
||
const nextErrors = articleErrors(article, insights);
|
||
setErrors(nextErrors);
|
||
if (Object.keys(nextErrors).length) return;
|
||
onApply(article);
|
||
};
|
||
const field = (name, label, children, required = false) => (
|
||
<Form.Slot
|
||
className='website-insights-editor-field'
|
||
label={{ text: label, required }}
|
||
error={{ error: errors[name] || undefined }}
|
||
>
|
||
{children}
|
||
</Form.Slot>
|
||
);
|
||
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
|
||
title={initial.id ? '编辑文章' : '新增文章'}
|
||
width='min(1100px, 100vw)'
|
||
onCancel={close}
|
||
maskClosable
|
||
footer={
|
||
<div className='website-insights-editor-footer'>
|
||
<div className='flex gap-2'>
|
||
<Button onClick={close}>{t('取消')}</Button>
|
||
<Button theme='solid' disabled={processingImage} onClick={apply}>
|
||
{processingImage ? '图片处理中' : '保存'}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
}
|
||
>
|
||
<div className='website-insights-editor-layout'>
|
||
<div className='website-insights-editor-main'>
|
||
<Typography.Title
|
||
heading={5}
|
||
className='website-insights-section-title'
|
||
>
|
||
基础信息
|
||
</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',
|
||
'文章标题',
|
||
<Input
|
||
aria-label='文章标题'
|
||
validateStatus={errors.title ? 'error' : 'default'}
|
||
aria-invalid={Boolean(errors.title)}
|
||
value={draft.title}
|
||
placeholder='请输入文章标题'
|
||
maxLength={80}
|
||
showClear
|
||
suffix={`${draft.title.length} / 80`}
|
||
onChange={(value) => change('title', value)}
|
||
/>,
|
||
true,
|
||
)}
|
||
{field(
|
||
'summary',
|
||
'文章描述',
|
||
<TextArea
|
||
aria-label={t('文章描述')}
|
||
validateStatus={errors.summary ? 'error' : 'default'}
|
||
aria-invalid={Boolean(errors.summary)}
|
||
value={draft.summary}
|
||
placeholder={t(
|
||
'请输入文章描述,用于列表卡片摘要与详情页标题下方',
|
||
)}
|
||
maxCount={200}
|
||
rows={3}
|
||
onChange={(value) => change('summary', value)}
|
||
/>,
|
||
true,
|
||
)}
|
||
<Typography.Title
|
||
heading={5}
|
||
className='website-insights-section-title website-insights-detail-title'
|
||
>
|
||
详情内容
|
||
</Typography.Title>
|
||
{field(
|
||
'body',
|
||
'文章正文',
|
||
<ArticleRichEditor
|
||
value={draft.body}
|
||
invalid={Boolean(errors.body)}
|
||
onChange={(value) => change('body', value)}
|
||
onBusyChange={setProcessingImage}
|
||
/>,
|
||
true,
|
||
)}
|
||
</div>
|
||
<aside className='website-insights-aside'>
|
||
<Card title='文章目录'>
|
||
<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} / ${RELATED_LIMIT})`}
|
||
>
|
||
<div className='website-insights-related'>
|
||
{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>
|
||
{!candidates.length && (
|
||
<Typography.Text type='tertiary'>
|
||
暂无可推荐的其他文章
|
||
</Typography.Text>
|
||
)}
|
||
{errors.related && (
|
||
<Typography.Text type='danger'>{errors.related}</Typography.Text>
|
||
)}
|
||
</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>
|
||
</div>
|
||
</SideSheet>
|
||
);
|
||
}
|