Toolbar,mode=”simple”
取消了选中文字的悬浮菜单
@wangeditor/editor@5
"@wangeditor/editor": "^5.1.23","@wangeditor/editor-for-react": "^1.0.6",
�
WangEditor编辑器简洁版预览
1190px宽度,编辑器UI效果
865px宽度,编辑器UI效果
WangEditor 编辑器
import {useState,useEffect,useMemo} from 'react';import {string,number,object,array,func,oneOf,oneOfType} from "prop-types";import { merge, noop, isEqual } from "lodash-es";import cls from 'classnames';import { Editor, Toolbar } from '@wangeditor/editor-for-react';import {IDomEditor,IEditorConfig,IToolbarConfig} from '@wangeditor/editor';WangEditor.propTypes = {value: oneOfType([array, string]),onChange: func,height: number,placeholder: string,mode: oneOf(["simple", "default"]),className: oneOfType([string, object]),style: object,};WangEditor.defaultProps = {value: "",onChange: noop,height: 200,placeholder: "请输入内容...",mode: "simple"};function WangEditor(props) {const {value,onChange,height,placeholder,mode,className,style} = props;// editor 编辑器实例const [editorInst, setEditorInst] = useState<IDomEditor | null>(null);// 编辑器内容const [content, setContent] = useState(value);// useEffect 是在render结束之后才执行的useEffect(() => {// 及时销毁 editorInst 重要!return () => {if (!editorInst) return;editorInst.destroy();setEditorInst(null);};}, [editorInst]);// value 改变,设置 contentuseEffect(() => {if (isEqual(value, content)) return;setContent(value);}, [value]);// 工具栏配置const toolbarConfig: Partial<IToolbarConfig> = useMemo(() => ({}), []);// 编辑器配置const editorConfig: Partial<IEditorConfig> = useMemo(() => ({placeholder,// scroll: false, // 禁止编辑器滚动// maxLength: 1000,// onMaxLength(editor) {// console.log('Trigger maxlength callback')// },MENU_CONF: {// onChange(editor: any) {// console.log(editor.getHtml());// },uploadImage: {// form-data fieldName ,默认值 'wangeditor-uploaded-image'fieldName: 'your-custom-name',// 单个文件的最大体积限制,默认为 2MmaxFileSize: 2 * 1024 * 1024, // 1M// 最多可上传几个文件,默认为 100maxNumberOfFiles: 10,// 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []allowedFileTypes: ['image/*'],/*** 小于该值就插入 base64 格式(而不上传),默认为 0* 1MB 兆 = 1024KB 千字节 * 1024B 比特 = 1048576 kb 字节* 1MB = 1024KB* 1KB = 1024B* 10 * 1024 * 1024, // 10M 以下插入 base64*/// 10 * 1024 * 1024 // 10M 以下base64LimitSize: 5 * 1024, // 5kb// 跨域是否传递 cookie ,默认为 falsewithCredentials: true,// 超时时间,默认为 10 秒timeout: 5 * 1000, // 5 秒// 自定义增加 http headerheaders: {Accept: 'text/x-json',otherKey: 'xxx',},// 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。meta: {token: 'xxx',otherKey: 'yyy',},},// fontSize: { // 覆盖默认的配置// fontSizeList: [// { name: '26px', value: '26px' },// ],// }}}), [placeholder]);function handleChange(editor: IDomEditor) {// const html = editor.getHtml(); // 获取 HTML// const text = editor.getText(); // 获取纯文本,默认带换行符// const text = editor.getText().replace(/\n|\r/mg, '');const json = editor.children; // 获取 JSON []// editor.getSelectionText() // 选中的文字setContent(json);onChange(json, editor);}const EditorStyle = useMemo(() => {return merge({ height, overflow: "hidden auto" }, style);}, [style, height]);return (<div className={cls('z-100', className)}>{/*工具栏组件*/}<Toolbareditor={editorInst}defaultConfig={toolbarConfig}mode={mode}/>{/*编辑器组件*/}<EditordefaultConfig={editorConfig}content={content}onCreated={setEditorInst}// 编辑器内容改变onChange={handleChange}style={EditorStyle}mode={mode}/></div>);}export default WangEditor;
value
value,默认只支持 html标签 string字符串格式
html字符串细节
- html字符串存储是没有样式的,需要自己维护 css样式
- html必须是 WangEditor生成的 html字符串,不能是自定义的 html,自定义的可能解析失败
- 自定义的 html无法全部兼容,例如行内的样式,就会解析失败
content
json格式,需要设置 content,而不是 value
@wangeditor.css
global.less引入 编辑器全局样式
@import “./assets/css/@wangeditor.css”;
样式自定义参考 import “@wangeditor/editor/dist/css/style.css”;
.w-e-text-container {position: relative;height: 100%;border: 1px solid var(--w-e-toolbar-border-color);border-top: none;background-color: var(--w-e-textarea-bg-color);color: var(--w-e-textarea-color);}.w-e-toolbar {flex-wrap: wrap;position: relative;border-radius: 8px 8px 0 0;background-color: var(--w-e-toolbar-border-color);}
