diff --git a/src/pages/Container/Notice/Add/index.js b/src/pages/Container/Notice/Add/index.js index 49adae7..c74d518 100644 --- a/src/pages/Container/Notice/Add/index.js +++ b/src/pages/Container/Notice/Add/index.js @@ -1,6 +1,6 @@ import { Connect } from "@cqsjjb/jjb-dva-runtime"; import { Button, Form, message } from "antd"; -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import Editor from "zy-react-library/components/Editor"; import FormBuilder from "zy-react-library/components/FormBuilder"; import Page from "zy-react-library/components/Page"; @@ -27,6 +27,8 @@ const getEditorTextLength = (html = "") => { function Add(props) { const queryParams = useGetUrlQuery(); const [form] = Form.useForm(); + const editorRef = useRef(null); + const lastValidContentRef = useRef(""); const [corpData, setCorpData] = useState([]); const [submitting, setSubmitting] = useState(false); const contentValue = Form.useWatch("content", form); @@ -35,6 +37,37 @@ function Add(props) { // 判断是否为只读模式 const isReadOnly = queryParams["readonly"] === "true"; + useEffect(() => { + const content = contentValue || ""; + + if (getEditorTextLength(content) <= NOTICE_CONTENT_MAX_LENGTH) { + lastValidContentRef.current = content; + } + }, [contentValue]); + + useEffect(() => { + let timer = null; + + const setEditorMaxLength = () => { + const editor = editorRef.current?.getEditorInstance?.(); + + if (editor) { + editor.getConfig().maxLength = NOTICE_CONTENT_MAX_LENGTH; + return; + } + + timer = window.setTimeout(setEditorMaxLength, 100); + }; + + setEditorMaxLength(); + + return () => { + if (timer) { + window.clearTimeout(timer); + } + }; + }, []); + // 当取消勾选分子公司时,清空发布部门ID useEffect(() => { if (publishScope && !publishScope.includes("2")) { @@ -145,6 +178,25 @@ function Add(props) { } }; + const handleContentChange = (value) => { + if (isReadOnly) { + return; + } + + const content = value || ""; + const nextTextLength = getEditorTextLength(content); + + if (nextTextLength <= NOTICE_CONTENT_MAX_LENGTH) { + lastValidContentRef.current = content; + form.setFieldValue("content", content); + return; + } + + const lastValidContent = lastValidContentRef.current; + editorRef.current?.setHtml?.(lastValidContent); + form.setFieldValue("content", lastValidContent); + }; + return ( { - if (!isReadOnly) { - form.setFieldValue("content", value); - } - }} + onChange={handleContentChange} disabled={isReadOnly} />