import { useRef, useState } from "react"; import { useIntl } from "react-intl"; import { ProForm, type ProFormInstance, ProFormSwitch, ProFormText, ProFormTextArea, } from "@ant-design/pro-components"; import { Alert, Button, Form, message, Result } from "antd"; import { get, put } from "../../request"; import type { IArticleDataRequest, IArticleDataResponse, IArticleResponse, } from "../../api/Article"; import LangSelect from "../general/LangSelect"; import PublicitySelect from "../studio/PublicitySelect"; import MDEditor from "@uiw/react-md-editor"; import ArticlePrevDrawer from "./ArticlePrevDrawer"; import type { IStudio } from "../auth/Studio"; import ArticleEditTools from "./ArticleEditTools"; import { useAppSelector } from "../../hooks"; import { currentUser } from "../../reducers/current-user"; import "./article.css"; interface IFormData { uid: string; title: string; subtitle: string; summary?: string | null; content?: string; content_type?: string; status: number; lang: string; to_tpl?: boolean; } interface IWidget { studioName?: string; articleId?: string; anthologyId?: string; resetButton?: "reset" | "cancel"; onReady?: ( title: string, readonly: boolean, studioName?: string, parentId?: string ) => void; onChange?: (data: IArticleDataResponse) => void; onCancel?: () => void; onSubmit?: (data: IArticleDataResponse) => void; } const ArticleEditWidget = ({ studioName, articleId, anthologyId, resetButton = "reset", onReady, onChange, onCancel, onSubmit, }: IWidget) => { const intl = useIntl(); const [unauthorized, setUnauthorized] = useState(false); const [readonly, setReadonly] = useState(false); const [content, setContent] = useState(); const [owner, setOwner] = useState(); const formRef = useRef(undefined); const [title, setTitle] = useState(); const user = useAppSelector(currentUser); return unauthorized ? ( } /> ) : ( <> {readonly ? ( 详情 } /> ) : undefined}
formRef={formRef} submitter={{ // 完全自定义整个区域 render: (props) => { console.log(props); return [ , , ]; }, }} onFinish={async (values: IFormData) => { const request: IArticleDataRequest = { uid: articleId ? articleId : "", title: values.title, subtitle: values.subtitle, summary: values.summary, content: values.content, content_type: "markdown", status: values.status, lang: values.lang, to_tpl: values.to_tpl, anthology_id: anthologyId, }; const url = `/v2/article/${articleId}`; console.info("save url", url, request); put(url, request) .then((res) => { console.debug("save response", res); if (res.ok) { if (typeof onChange !== "undefined") { onChange(res.data); } if (typeof onSubmit !== "undefined") { onSubmit(res.data); } formRef.current?.setFieldValue("content", res.data.content); message.success(intl.formatMessage({ id: "flashes.success" })); } else { message.error(res.message); } }) .catch((e: IArticleResponse) => { message.error(e.message); }); }} request={async () => { const url = `/v2/article/${articleId}`; console.info("url", url); const res = await get(url); console.log("article", res); let mTitle: string, mReadonly = false; if (res.ok) { setOwner(res.data.studio); mReadonly = res.data.role === "editor" ? false : true; setReadonly(mReadonly); mTitle = res.data.title; setContent(res.data.content); setTitle(res.data.title); } else { setUnauthorized(true); mTitle = "无权访问"; } if (typeof onReady !== "undefined") { onReady( mTitle, mReadonly, res.data.studio?.realName, res.data.parent_uid ); } return { uid: res.data.uid, title: res.data.title, subtitle: res.data.subtitle, summary: res.data.summary, content: res.data.content, content_type: res.data.content_type, lang: res.data.lang, status: res.data.status, studio: res.data.studio, }; }} > {intl.formatMessage({ id: "forms.fields.content.label", })} {articleId ? ( 预览} articleId={articleId} content={content} /> ) : undefined} } > { if (typeof value === "string") { setContent(value); } }} height={450} minHeight={200} style={{ width: "100%" }} /> ); }; export default ArticleEditWidget;