import { Box, TextField, Typography, Paper } from "@mui/material"; import { Create } from "@refinedev/mui"; import { useForm } from "@refinedev/react-hook-form"; import { Controller, FieldValues } from "react-hook-form"; import React, { useState, useEffect } from "react"; import ReactMarkdown from "react-markdown"; import { MarkdownEditor } from "../../components/MarkdownEditor"; import "easymde/dist/easymde.min.css"; import { LanguageSelector } from "@ui"; import { observer } from "mobx-react-lite"; import { EVERY_LANGUAGE, Languages, languageStore, META_LANGUAGE } from "@stores"; const MemoizedSimpleMDE = React.memo(MarkdownEditor); export const ArticleCreate = observer(() => { const { language, setLanguageAction } = languageStore; const [articleData, setArticleData] = useState({ heading: EVERY_LANGUAGE(""), body: EVERY_LANGUAGE("") }); const { saveButtonProps, refineCore: { formLoading, onFinish }, register, control, watch, formState: { errors }, setValue, handleSubmit, } = useForm({ refineCoreProps: { resource: "article", ...META_LANGUAGE(language) } }); // Следим за изменениями в полях body и heading const bodyContent = watch("body"); const headingContent = watch("heading"); function updateTranslations(update: boolean = true) { const newArticleData = { ...articleData, heading: { ...articleData.heading, [language]: watch("heading") ?? "", }, body: { ...articleData.body, [language]: watch("body") ?? "", } } if(update) setArticleData(newArticleData); return newArticleData; } const handleFormSubmit = handleSubmit((values) => { const newTranslations = updateTranslations(false); return onFinish({ translations: newTranslations }); }); useEffect(() => { setValue("heading", articleData.heading[language] ?? ""); setValue("body", articleData.body[language] ?? ""); setPreview(articleData.body[language] ?? ""); setHeadingPreview(articleData.heading[language] ?? ""); }, [language, articleData, setValue]); const handleLanguageChange = (lang: Languages) => { updateTranslations(); setLanguageAction(lang); }; const [preview, setPreview] = useState(""); const [headingPreview, setHeadingPreview] = useState(""); useEffect(() => { setPreview(bodyContent ?? ""); }, [bodyContent]); useEffect(() => { setHeadingPreview(headingContent ?? ""); }, [headingContent]); const simpleMDEOptions = React.useMemo(() => ({ placeholder: "Введите контент в формате Markdown...", spellChecker: false, }), []); return ( {/* Форма создания */} ( )} /> {/* Блок предпросмотра */} theme.palette.mode === "dark" ? "background.paper" : "#fff", }} > Предпросмотр {/* Заголовок статьи */} theme.palette.mode === "dark" ? "grey.300" : "grey.800", mb: 3, }} > {headingPreview} {/* Markdown контент */} theme.palette.mode === "dark" ? "grey.300" : "grey.800", }, "& a": { color: "primary.main", textDecoration: "none", "&:hover": { textDecoration: "underline", }, }, "& blockquote": { borderLeft: "4px solid", borderColor: "primary.main", pl: 2, my: 2, color: "text.secondary", }, "& code": { bgcolor: (theme) => theme.palette.mode === "dark" ? "grey.900" : "grey.100", p: 0.5, borderRadius: 0.5, color: "primary.main", }, }} > {preview} ); });