import React, { useCallback, useEffect, useMemo, useRef, useState, } from "react"; import styles from "./index.module.scss"; // 引入编辑器组件 // 安装---npm install braft-editor --save --force // npm install braft-utils --save --force import { ContentUtils } from "braft-utils"; import BraftEditor from "braft-editor"; // 引入编辑器样式 import "braft-editor/dist/index.css"; import classNames from "classnames"; import { MessageFu } from "@/utils/message"; import { fileDomInitialFu } from "@/utils/domShow"; import { baseURL } from "@/utils/http"; import { forwardRef, useImperativeHandle } from "react"; import { API_upFile } from "@/store/action/layout"; type Props = { check: boolean; //表单校验,为fasle表示不校验 dirCode: string; //文件的code码 isLook: boolean; //是否是查看进来 ref: any; //当前自己的ref,给父组件调用 myUrl: string; //上传的api地址 full?: boolean; }; function ZRichText({ check, dirCode, isLook, myUrl, full }: Props, ref: any) { // 添加 上传 图片的dom useEffect(() => { 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); }, 20); // 监听 富文本 的 class 变化,在全屏的时候会 富文本会添加上 fullscreen 的类 // 修复顶部样式冲突问题 const editorDom = document.querySelector(".bf-container") as HTMLDivElement; const observer = new MutationObserver(() => { // console.log("change"); const dom = document.querySelector(".layoutRightTop") as HTMLDivElement; if (editorDom.className.includes("fullscreen")) dom.style.zIndex = "-1"; else dom.style.zIndex = "100"; }); observer.observe(editorDom, { attributes: true, }); // 销毁监听 return () => { observer.disconnect(); }; }, []); useEffect(() => { const controlbarDom = document.querySelectorAll(".txtBox .bf-controlbar "); const contentDom = document.querySelectorAll(".txtBox .bf-content "); if (controlbarDom) { controlbarDom.forEach((v: any) => { v.style.display = isLook ? "none" : "block"; }); contentDom.forEach((v: any) => { v.style.height = isLook ? "100%" : ""; }); } }, [isLook]); // 编辑器文本 const [editorValue, setEditorValue] = useState( // 初始内容 BraftEditor.createEditorState("") ); // 判断 富文本是否为空 const isTxtFlag = useMemo(() => { const txt: string = editorValue.toHTML(); if ( txt.replaceAll("
", "").replaceAll("
", "").replaceAll(" ", "") === "" ) { return true; } else return false; }, [editorValue]); const myInput = useRef