import "vditor/dist/index.css"; import Vditor from "vditor"; import { useEffect, useRef } from "react"; import "./index.less"; import { message } from "antd"; interface IProps { onChange?: Function; styles?: React.CSSProperties; id: string; defaultValue?: string; } const emoji = { "+1": "πŸ‘", "-1": "πŸ‘Ž", confused: "πŸ˜•", eyes: "πŸ‘€οΈ", heart: "❀️", rocket: "πŸš€οΈ", smile: "πŸ˜„", tada: "πŸŽ‰οΈ", }; const toolbar = [ "emoji", "headings", "bold", "italic", "strike", "link", "|", "list", "ordered-list", "check", "outdent", "indent", "|", "quote", "line", "code", "inline-code", "insert-before", "insert-after", "|", "upload", // "record", "table", "|", "undo", "redo", "|", "fullscreen", "edit-mode", { name: "more", toolbar: [ "both", "code-theme", "content-theme", "export", "outline", "preview", "devtools", "info", "help", ], }, ]; export default function Guide(props: IProps) { const vditorRef = useRef(); const submitTool = { name: "submit", tipPosition: "s", tip: "提亀", className: "right", icon: '', click() { const value = vditorRef.current?.getValue(); const html = vditorRef.current?.getHTML(); if (props.onChange) props.onChange({ value, html }); }, }; useEffect(() => { if (props.defaultValue) vditorRef.current = new Vditor("vditor", { height: 600, cache: { id: props.id, enable: false, }, toolbar: [...toolbar, "|", submitTool], value: props.defaultValue ?? "", hint: { delay: 200, emoji }, counter: { enable: true }, preview: { actions: ["desktop", "mobile"] }, after: () => console.log("[info] vditor init success..."), upload: { accept: "image/*", url: "/api/vod/oss/image/upload", multiple: false, success(_, res) { const { code, data, msg } = JSON.parse(res); if (code === 10000) { message.success("上传成功"); const { name, url } = data; vditorRef.current?.insertValue(`![${name}](${url})`); } else { message.error(msg); } }, }, }); }, [props.defaultValue]); return (
); }