import React, { useMemo } from "react"; import { FormattingToolbar, FormattingToolbarController, useCreateBlockNote, getFormattingToolbarItems, SuggestionMenuController } from "@blocknote/react"; import { BlockNoteView } from "@blocknote/mantine"; import { Button, message, Tooltip } from "antd"; import { UploadOutlined } from "@ant-design/icons"; import { useLocale } from "../../locale/useLocale"; import { richTextFileUpload } from "./constant"; import { ProgramConfig } from "../../index"; import HcserviceV3 from "./../../tmpl/hcservice-v3"; import UploadFileButton from "./components/upload-file-button"; import { VEControlProps } from "../../tmpl/interface"; import { schema } from "./components/schema"; import { filterSuggestionItems, getCustomSlashMenuItems } from "./components/custom-menu"; // @ts-expect-error import { zh } from "@blocknote/core/locales"; import "@blocknote/core/fonts/inter.css"; import "@blocknote/mantine/style.css"; import "./index.less"; const { sourceId, serverKey } = richTextFileUpload; const CustomFormattingToolbar = ({ size }: { size: number }) => ( {getFormattingToolbarItems()} ); // 本地文件上传,处理url export const handleFileUrl = (basePath: string) => { if (basePath.startsWith("http")) { return basePath; } const url = encodeURI(ProgramConfig.joinPath( ProgramConfig.joinPath(ProgramConfig.api(), "/v3/files"), basePath )) + `?@token=${ProgramConfig.hydrocarbonToken()}&@programToken=${ProgramConfig.programCode(serverKey)}&@applicationToken=${ProgramConfig.applicationCode(serverKey)}`; return url; } interface Props extends VEControlProps { size?: number; theme?: "light" | "dark"; }; // 富文本编辑器-由于数据处理,因此单独封装 const RichEditor = (props: Props) => { const { value, size, onChange, viewOrEdit, theme = "light" } = props; const { translate } = useLocale(); // 文件上传校验 const beforeUpload = async (file: File) => { if (size && file.size > size * 1024 * 1024) { message.warning(translate("${文件大小不能超过}") + `${size}M`); return false; } return true; } // 上传文件 const handleUploadFile = async (file: File) => { const isUpload = await beforeUpload(file); if (!isUpload) return; const fd = new FormData(); fd.append("file", file); const res = await HcserviceV3.postFile(fd); if (res.status !== "success") { message.error(translate("${文件上传失败}")); return; } const fileKey = res.fileKey; const fileName = file.name; const data = { valid: "new", fileKey, fileName }; const result = { 使用说明: fileName, 文件: JSON.stringify(data) }; const { code } = await HcserviceV3.postMstrucDtmplData(serverKey, sourceId, result, message); const imgData = await HcserviceV3.requestMstrucDtmplData(serverKey, sourceId, code, null); const basePath = JSON.parse(imgData.fieldMap["文件"] || "{}").base?.path; const fileUrl = handleFileUrl(basePath); return { props: { url: fileUrl, basePath } }; } const editor = useCreateBlockNote({ schema, dictionary: zh, // 中文 UI initialContent: value?.length > 0 ? value : [{ type: "paragraph", content: [{ type: "text", text: "" }] }], // 图片/视频/音频/文件上传统一走这里 uploadFile: handleUploadFile }); const onChangeValue = () => { const val = editor.document; const temVal = ProgramConfig.compressString(JSON.stringify(val)); onChange(temVal); } const downloadBlob = (blob: Blob, filename: string) => { const link = document.createElement("a"); link.href = URL.createObjectURL(blob); link.download = filename; document.body.appendChild(link); link.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, view: window })); link.remove(); URL.revokeObjectURL(link.href); } // 导出HTML const exportHtml = async (editor) => { const bodyHtml = await editor.blocksToFullHTML(editor.document); const html = `${bodyHtml}`; downloadBlob(new Blob([html], { type: "text/html;charset=utf-8" }), "doc.html"); } return (
{viewOrEdit === 'view' && } CustomFormattingToolbar({ size })} /> filterSuggestionItems(getCustomSlashMenuItems({ editor, size, translate }), query)} />
); }; // 主文件 const Index = (props: Props) => { const { value, theme } = props; const version = localStorage.getItem("version"); // 处理数据格式 const temValue = useMemo(() => { const temVal = value ? ProgramConfig.decompressString(value) : "[]"; const temArr = JSON.parse(temVal).map(item => { const { props, type } = item; if (["image", "video", "audio", "file"].includes(type)) { // 文件处理路径 return { ...item, props: { ...props, url: handleFileUrl(props.basePath || "") || "" } }; } return item; }); return temArr; }, [value]); const temTheme = useMemo(() => { if (theme) return theme; return ["v2", "v5"].includes(version || "") ? "light" : "dark"; }, [theme, version]); return } export default Index;