/* 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 . 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) => ( {children} ); 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 (
} >
基础信息
{field( 'category', '所属分类', change('tag', value)} />, )}
{field( 'title', '文章标题', change('title', value)} />, true, )} {field( 'summary', '文章描述',