/*
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 React, { useEffect, useState } from 'react';
import {
Button,
Card,
Checkbox,
Form,
Input,
Modal,
Select,
SideSheet,
TextArea,
Typography,
} from '@/components/ui';
import { articleErrors } from './insightsModel';
import { articleContentInfo, sanitizeArticleHtml } from './insightsContent';
import ArticleRichEditor from './ArticleRichEditor';
export default function ArticleEditor({ initial, insights, onApply, onClose }) {
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 (JSON.stringify(draft) === JSON.stringify(initial) && !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', 'subtitle', '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 info = articleContentInfo(draft.body);
return (