|
@@ -22,34 +22,37 @@ import { B2_APIupFile } from "@/store/action/B2Goods";
|
|
|
import { fileDomInitialFu } from "@/utils/domShow";
|
|
|
import { baseURL } from "@/utils/http";
|
|
|
|
|
|
+import { forwardRef, useImperativeHandle } from "react";
|
|
|
+
|
|
|
type Props = {
|
|
|
- check: boolean;
|
|
|
- dirCode: string;
|
|
|
- richTxt: string;
|
|
|
- setRichTxt: (val: string) => void;
|
|
|
- isLook: boolean;
|
|
|
+ check: boolean; //表单校验,为fasle表示不校验
|
|
|
+ dirCode: string; //文件的code码
|
|
|
+ isLook: boolean; //是否是查看进来
|
|
|
+ ref: any; //当前自己的ref,给父组件调用
|
|
|
};
|
|
|
|
|
|
-function RichText({ check, dirCode, richTxt, setRichTxt, isLook }: Props) {
|
|
|
+function RichText({ check, dirCode, isLook }: Props, ref: any) {
|
|
|
// 添加 上传 图片的dom
|
|
|
useEffect(() => {
|
|
|
- setTimeout(() => {
|
|
|
- const dom = document.querySelector(".bf-controlbar")!;
|
|
|
- const div = document.createElement("div");
|
|
|
- div.className = isLook ? "upImgBoxNo" : "upImgBox";
|
|
|
- div.title = "上传图片";
|
|
|
- div.innerHTML = "上传图片";
|
|
|
- div.onclick = async () => {
|
|
|
- myInput.current?.click();
|
|
|
- };
|
|
|
- dom.appendChild(div);
|
|
|
- }, 200);
|
|
|
+ if (!isLook) {
|
|
|
+ setTimeout(() => {
|
|
|
+ const dom = document.querySelector(".bf-controlbar")!;
|
|
|
+ const div = document.createElement("div");
|
|
|
+ div.className = "upImgBox";
|
|
|
+ // div.title = "上传图片";
|
|
|
+ div.innerHTML = "上传图片";
|
|
|
+ div.onclick = async () => {
|
|
|
+ myInput.current?.click();
|
|
|
+ };
|
|
|
+ dom.appendChild(div);
|
|
|
+ }, 200);
|
|
|
+ }
|
|
|
}, [isLook]);
|
|
|
|
|
|
// 编辑器文本
|
|
|
const [editorValue, setEditorValue] = useState(
|
|
|
// 初始内容
|
|
|
- BraftEditor.createEditorState(richTxt)
|
|
|
+ BraftEditor.createEditorState("")
|
|
|
);
|
|
|
|
|
|
// 判断 富文本是否为空
|
|
@@ -63,25 +66,10 @@ function RichText({ check, dirCode, richTxt, setRichTxt, isLook }: Props) {
|
|
|
} else return false;
|
|
|
}, [editorValue]);
|
|
|
|
|
|
- // 回显数据
|
|
|
- const timeRef = useRef(false);
|
|
|
-
|
|
|
- useEffect(() => {
|
|
|
- if (timeRef.current) return;
|
|
|
- if (richTxt) {
|
|
|
- setEditorValue(BraftEditor.createEditorState(richTxt));
|
|
|
- timeRef.current = true;
|
|
|
- }
|
|
|
- }, [richTxt]);
|
|
|
-
|
|
|
// 富文本信息改变的回调
|
|
|
- const handleChange = useCallback(
|
|
|
- (e: any) => {
|
|
|
- setEditorValue(e);
|
|
|
- setRichTxt(editorValue.toHTML());
|
|
|
- },
|
|
|
- [editorValue, setRichTxt]
|
|
|
- );
|
|
|
+ const handleChange = useCallback((e: any) => {
|
|
|
+ setEditorValue(e);
|
|
|
+ }, []);
|
|
|
|
|
|
const myInput = useRef<HTMLInputElement>(null);
|
|
|
|
|
@@ -134,6 +122,22 @@ function RichText({ check, dirCode, richTxt, setRichTxt, isLook }: Props) {
|
|
|
[dirCode, editorValue]
|
|
|
);
|
|
|
|
|
|
+ // 让父组件调用的 回显 富文本
|
|
|
+ const ritxtShowFu = useCallback((val: string) => {
|
|
|
+ setEditorValue(BraftEditor.createEditorState(val));
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ // 让父组件调用的返回 富文本信息 和 表单校验
|
|
|
+ const fatherBtnOkFu = useCallback(() => {
|
|
|
+ return { val: editorValue.toHTML(), flag: isTxtFlag };
|
|
|
+ }, [editorValue, isTxtFlag]);
|
|
|
+
|
|
|
+ // 可以让父组件调用子组件的方法
|
|
|
+ useImperativeHandle(ref, () => ({
|
|
|
+ ritxtShowFu,
|
|
|
+ fatherBtnOkFu,
|
|
|
+ }));
|
|
|
+
|
|
|
return (
|
|
|
<div className={styles.RichText}>
|
|
|
<input
|
|
@@ -150,6 +154,7 @@ function RichText({ check, dirCode, richTxt, setRichTxt, isLook }: Props) {
|
|
|
placeholder="请输入内容"
|
|
|
value={editorValue}
|
|
|
onChange={(e) => handleChange(e)}
|
|
|
+ imageControls={["remove"]}
|
|
|
/>
|
|
|
</div>
|
|
|
<div
|
|
@@ -160,18 +165,10 @@ function RichText({ check, dirCode, richTxt, setRichTxt, isLook }: Props) {
|
|
|
>
|
|
|
请输入正文!
|
|
|
</div>
|
|
|
-
|
|
|
- {/* 编辑器内容 */}
|
|
|
- {/* <br />
|
|
|
- <button onClick={btnOk}>点击确定获取文本</button>
|
|
|
- <br /> */}
|
|
|
- {/* 插入图片 */}
|
|
|
- {/* <br />
|
|
|
- <button onClick={insertTextFu}>在光标位置插入内容</button> */}
|
|
|
</div>
|
|
|
);
|
|
|
}
|
|
|
|
|
|
-const MemoRichText = React.memo(RichText);
|
|
|
+// const MemoRichText = React.memo(RichText);
|
|
|
|
|
|
-export default MemoRichText;
|
|
|
+export default forwardRef(RichText);
|