From b2669b647a134c2cb35c6f1dcc31191e21908d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E9=98=B3?= <3311118881@qq.com> Date: Tue, 13 Aug 2024 20:24:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BF=AE=E6=94=B9=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Create/components/PublishForm/index.tsx | 21 +++++++++----- .../Create/components/VditorMD/index.tsx | 29 +++++++++---------- src/pages/Create/index.tsx | 21 ++++---------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/pages/Create/components/PublishForm/index.tsx b/src/pages/Create/components/PublishForm/index.tsx index d8128b8..b4cd5aa 100644 --- a/src/pages/Create/components/PublishForm/index.tsx +++ b/src/pages/Create/components/PublishForm/index.tsx @@ -10,6 +10,7 @@ import { Tag } from "@/types/tag"; import dayjs from 'dayjs'; import "./index.scss" import { Article } from "@/types/article"; +import { useNavigate } from "react-router-dom"; interface FieldType { title: string, @@ -20,16 +21,17 @@ interface FieldType { description: string; } -const PublishForm = ({ data }: { data: Article }) => { +const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => void }) => { const [form] = Form.useForm() + const navigate = useNavigate() + const [cateList, setCateList] = useState([]) const [tagList, setTagList] = useState([]) useEffect(() => { if (!data.id) return - const cateIds = transCateArray(data.cateList) - console.log(cateIds); + const cateIds = transCateArray(data.cateList) const tagIds = data.tagList.map(item => item.id) form.setFieldsValue({ ...data, @@ -37,7 +39,6 @@ const PublishForm = ({ data }: { data: Article }) => { tagIds, createTime: dayjs(+data.createTime!) }) - console.log(data, 9999); }, [data]) const getCateList = async () => { @@ -64,15 +65,21 @@ const PublishForm = ({ data }: { data: Article }) => { values.createTime = values.createTime.valueOf() values.cateIds = (values.cateIds as number[]).flat().join(',') values.tagIds = values.tagIds ? (values.tagIds as number[]).join(',') : "" - console.log(values); if (data.id) { - await editArticleDataAPI({ ...values, content: data.content } as Article) + await editArticleDataAPI({ id: data.id, ...values, content: data.content } as Article) message.success("🎉 编辑成功") } else { - await addArticleDataAPI({ ...values, content: data.content } as Article) + await addArticleDataAPI({ id: data.id, ...values, content: data.content } as Article) message.success("🎉 发布成功") } + + // 关闭弹框 + closeModel() + // 跳转到文章页 + navigate("/article") + // 初始化表单 + form.resetFields() } return ( diff --git a/src/pages/Create/components/VditorMD/index.tsx b/src/pages/Create/components/VditorMD/index.tsx index a71bbf7..a1f3e60 100644 --- a/src/pages/Create/components/VditorMD/index.tsx +++ b/src/pages/Create/components/VditorMD/index.tsx @@ -11,14 +11,6 @@ interface VditorProps { const VditorEditor = ({ value, getValue }: VditorProps) => { const [vd, setVd] = useState(); - // useEffect(() => { - // if (vd) { - // vd.setValue(value || '') - // } - // console.log(value,444); - - // }, [value]) - useEffect(() => { const vditor = new Vditor("vditor", { minHeight: 550, @@ -27,32 +19,37 @@ const VditorEditor = ({ value, getValue }: VditorProps) => { // enable: true, // position: 'left' // }, + // 禁止缓存数据 cache: { enable: false }, + preview: { + // 限制防抖时间 + delay: 500 + }, input: (value) => { // 把数据传给父组件 getValue(value) }, after: () => { - console.log(value?.trim(),!!value?.trim().length); - - // value?.trim().length && vditor.setValue(value) - - // vditor.setValue("`Vditor` 最小代码示例"); - setVd(vditor); } }) - // Clear the effect return () => { vd?.destroy(); setVd(undefined); }; }, []); + // 监听 value 变化并更新编辑器内容 + useEffect(() => { + if (vd && value) { + vd.setValue(value); + } + }, [value, vd]); + return
; }; -export default VditorEditor; +export default VditorEditor; \ No newline at end of file diff --git a/src/pages/Create/index.tsx b/src/pages/Create/index.tsx index 00f737b..a29648d 100644 --- a/src/pages/Create/index.tsx +++ b/src/pages/Create/index.tsx @@ -20,8 +20,6 @@ const CreatePage = () => { // 获取编辑器的内容 const getVditorData = (value: string) => { - console.log(value, 333); - setContent(value) } @@ -34,23 +32,16 @@ const CreatePage = () => { const getArticleData = async () => { const { data } = await getArticleDataAPI(id) setData(data) - console.log(222,data); + setContent(data.content) } useEffect(() => { - if (id) { - console.log(111); - - getArticleData() - } + if (id) getArticleData() }, [id]) useEffect(() => { - if (data.id) { - setContent(data.content) - console.log(333,data); - } - }, [data]) + setData({ ...data, content }) + }, [content]) return ( <> @@ -63,7 +54,7 @@ const CreatePage = () => {
- + { onClose={() => setPublishOpen(false)} open={publishOpen} > - + setPublishOpen(false)} />