import { useRef, useState } from "react"; import { useIntl } from "react-intl"; import { ProForm, ProFormInstance, ProFormSwitch, ProFormText, ProFormTextArea, } from "@ant-design/pro-components"; import { Alert, Button, Form, message, Result } from "antd"; import { get, put } from "../../request"; import { IArticleDataRequest, IArticleResponse, } from "../../components/api/Article"; import LangSelect from "../../components/general/LangSelect"; import PublicitySelect from "../../components/studio/PublicitySelect"; import MDEditor from "@uiw/react-md-editor"; import ArticlePrevDrawer from "../../components/article/ArticlePrevDrawer"; import { IStudio } from "../auth/Studio"; 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; onReady?: Function; onLoad?: Function; onChange?: Function; } const ArticleEditWidget = ({ studioName, articleId, anthologyId, onReady, onLoad, onChange, }: 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(); return unauthorized ? ( } /> ) : ( <> {readonly ? ( 详情 } /> ) : undefined} formRef={formRef} 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.log("save", url, request); put(url, request) .then((res) => { console.log("save response", res); if (res.ok) { if (typeof onChange !== "undefined") { onChange(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); } 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, }; }} > {intl.formatMessage({ id: "forms.fields.content.label", })} {articleId ? ( 预览} articleId={articleId} content={content} /> ) : undefined} } > setContent(value)} height={450} minHeight={200} style={{ width: "100%" }} /> ); }; export default ArticleEditWidget;