wangEditor 富文本编辑器使用

news/2024/10/23 7:17:42/

wangEditor 富文本编辑器使用

框架: react hooks
链接: 官方文档参考

富文本编辑器组件

import '@wangeditor/editor/dist/css/style.css'; // 引入 css
import { useState, useEffect } from 'react';
import { Editor, Toolbar } from '@wangeditor/editor-for-react';
import type { IDomEditor, IEditorConfig, IToolbarConfig } from '@wangeditor/editor';
import { upload } from '@/services/myEditor';
import { message } from 'antd';/*** 富文本编辑器* @param props* @constructor*/
function MyEditor(props: any) {const { data, onChange } = props;/*** editor 实例*/const [editor, setEditor] = useState<IDomEditor | null>(null);/*** 编辑器内容*/const [html, setHtml] = useState('');/*** 初始化*/useEffect(() => {setHtml(data);}, []);/*** 工具栏配置*/const toolbarConfig: Partial<IToolbarConfig> = {};/*** 上传文件* @param file* @param insertFn*/const update = (file: any, insertFn: any) => {const imgData = new FormData();imgData.append('file', file);//调接口,上传图片upload(imgData).then((res) => {if (res.code == 0) {//接口调用成功,将上传文件插入到富文本中去insertFn(res.data.url, res.data.name);} else {message.error('上传失败');}});};/*** 编辑器配置*/const editorConfig: Partial<IEditorConfig> = {placeholder: '请输入内容...',// 菜单操作设置MENU_CONF: {uploadImage: {// 上传图片配置customUpload: update,server: '/web/v1/files', // 这里是接口},uploadVideo: {// 上传视频配置customUpload: update,server: '/web/v1/files', // 这里是接口},},};/*** 及时销毁 editor ,重要!*/useEffect(() => {return () => {if (editor == null) return;editor.destroy();setEditor(null);};}, [editor]);const handleChange = (el: any) => {if (onChange) onChange(el.getHtml());};return (<><div style={{ border: '1px solid #ccc', zIndex: 100 }}><Toolbareditor={editor}defaultConfig={toolbarConfig}mode="default"style={{ borderBottom: '1px solid #ccc' }}/><EditordefaultConfig={editorConfig}value={html}onCreated={setEditor}onChange={handleChange}mode="default"style={{ height: '300px', overflowY: 'hidden' }}/></div></>);
}export default MyEditor;

使用

<Form.Item{...formItemLayout}name="content"label="富文本内容"rules={[{ required: false, message: '请输入富文本内容' }]}initialValue={params?.content ? params?.content : ''}
><MyEditor data={params?.content ? params?.content : ''} />
</Form.Item>

http://www.ppmy.cn/news/651680.html

相关文章

vue 中使用 wangeditor

项目中用到了富文本&#xff0c;选来选去选择了wangeditor,先写了demo&#xff0c;用起来还算比较简单 用法 安装 npm install wangeditor/editor --save npm install wangeditor/editor-for-vue --save 空白编辑器 <template><div id"app"><div…

wangEditor的使用及上传图片(一)

由于业务需要&#xff0c;最近新入手了一款富文本编辑器wangEditor&#xff0c;这是一款轻量级的富文本编辑器&#xff0c;比起百度的ueditor&#xff0c;这款编辑器的界面更加简单&#xff0c;文档也很详细。对于需求不是很高的功能来说&#xff0c;这款编辑器实在是不二之选。…

wangEditor的完整使用(一)

最近在做一个类似博客之类的页面&#xff0c;所以就打算借助wangEditor这一富文本编辑器来实现项目的需求。主要使用SpringBootJSPLayUi来完成。一.导入wangEditor.js 我自己使用的是wangEditor.min.js,通过手工导入的方式放在项目的静态文件下来引用。 下载地址&#xff1a;…

对wangeditor进行扩展---- 源代码

看到有人对我做的WangEditor比较感兴趣&#xff0c;问了一些问题。但由于我并不常来&#xff0c;所以就没能及时答复&#xff0c;抱歉了。 未避免以后类似问题发生&#xff0c;我将我修改的wangeditor.js直接发在这里&#xff0c;有兴趣的可以下载后自己分析。希望能帮到需要的…

vue 使用 wangeditor 富文本编辑器

wangeditor 是一个轻量级 web 富文本编辑器&#xff0c;配置方便&#xff0c;使用简单。 1&#xff09;安装 wangeditor 终端安装 wangeditor 库&#xff1a; yarn add wangeditor/editor # 或者 npm install wangeditor/editor --save2&#xff09;页面绑定 创建一个 xxx.…

【wangEditor 5】在线解析本地word,将内容展现在wangEditor中

最近有个问题&#xff0c;在word中复制的多张图片&#xff0c;或者同时复制了图片和文字&#xff0c;图片粘贴到编辑器中不显示。 原因很简单&#xff1a;浏览器无法直接访问本地资源下的文件&#xff0c;切记这一点&#xff0c;不要试图用浏览器直接访问你本地文件。因为从技…

wangEditor富文本编辑器获取html内容

该示例为官方示例&#xff0c;代码仅供参考。 功能&#xff1a;获取html内容&#xff0c;目录结构如下&#xff1a; get-html.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"X-UA…

vue 使用wangEditor

1、npm install wangeditor/core wangeditor/editor wangeditor/editor-for-vue 2、封装组件 MyEditor.vue, &#xff08;这里是通过props content 将展示内容传入&#xff09; <template><div><div style"border: 1px solid #ccc; margin-top: 10px&quo…