前端vue-实现富文本组件

打印 上一主题 下一主题

主题 870|帖子 870|积分 2610

1.使用wangeditor富文本编辑器
工具网站:https://www.wangeditor.com/v4/
下载安装下令:npm i wangeditor --save
制品如下图:

组件实现代码
  1. <template>
  2.   <div>
  3.     <!-- 富文本编辑器 -->
  4.     <div id="wangeditor">
  5.     </div>
  6.   </div>
  7. </template>
  8. <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  9. <script>
  10. import { uploadImage } from '@/api/api'; // 导入图片上传api
  11. import E from 'wangeditor';
  12. export default {
  13.   name: 'richText',
  14.   components: {
  15.   },
  16.   props: {
  17.     defaultDetails: {
  18.       default: '请填写内容',
  19.       type: String,
  20.     },
  21.   },
  22.   watch: {
  23.     htmlContent(val) {
  24.       this.$emit('change', val); // 将改变同步到父组件
  25.       if (this.validateEvent) {
  26.         this.dispatch('ElFormItem', 'el.form.change', [val]);
  27.       }
  28.     }
  29.   },
  30.   data() {
  31.     return {
  32.       editor: null,
  33.       htmlContent: '<p>hello</p>',
  34.       firtherMethod: 'loadingCompleted', // 回调父组件,通知editor已经创建完成
  35.     };
  36.   },
  37.   methods: {
  38.     // 获取text文本
  39.     getText() {
  40.       const text = this.editor.txt.text();
  41.       console.log('text = ', text);
  42.       return text;
  43.     },
  44.     // 获取html
  45.     getHtml() {
  46.       const html = this.editor.txt.html();
  47.       console.log('thml = ', html);
  48.       return html;
  49.     },
  50.     // 图片上传自定义实现
  51.     async uploadImage(files) {
  52.       const file = files[0];
  53.       console.log('Fuedit2-uploadImage file = ', file);
  54.       const res = await uploadImage(obj);
  55.       const path = SOCKET + (res.path || {});
  56.       console.log('完整path = ', path);
  57.       return path;
  58.     },
  59.     // 设置内容
  60.     setHtml(html) {
  61.       this.editor.txt.html(html);// 重新设置编辑器内容
  62.     },
  63.     // 追加内容
  64.     appentHtml(html) {
  65.       this.editor.txt.append(html);// 继续追加内容。
  66.     },
  67.     // 销毁编辑器
  68.     beforeDestroy() {
  69.       // 销毁编辑器
  70.       console.log('销毁前');
  71.       this.editor.destroy()
  72.       console.log('销毁后');
  73.       this.editor = null
  74.     },
  75.     // 清空编辑器内容
  76.     clearText() {
  77.       this.editor.txt.clear();
  78.     },
  79.     createEditor() {
  80.       if(this.editor !== null) {
  81.         return;
  82.       }
  83.       this.editor = new E('#wangeditor');
  84.       // 或者 const editor = new E( document.getElementById('div1') )
  85.       this.editor.config.height = 200; // 设置高度
  86.       // 内容发生改变时回调
  87.       // this.editor.config.onchange = function (html) {
  88.         // this.htmlContent = html;
  89.       // }
  90.       this.editor.config.placeholder = this.defaultDetails; // 自定义初始文字提示
  91.       this.editor.config.focus = false;// 取消初始化时自动聚焦
  92.       this.editor.config.menus = [ // 定义显示哪些菜单和菜单的顺序。
  93.         'head', // 标题
  94.         'bold', // 粗体
  95.         'fontSize', // 字号
  96.         'fontName', // 字体
  97.         'italic', // 斜体
  98.         'underline', // 下划线
  99.         // 'strikeThrough', // 删除线
  100.         // 'indent',
  101.         'lineHeight',
  102.         'foreColor', // 文字颜色
  103.         'backColor', // 背景颜色
  104.         'link', // 插入链接
  105.         'list', // 列表
  106.         // 'todo',
  107.         // 'justify', // 对齐方式
  108.         // 'quote', // 引用
  109.         // 'emoticon', // 表情
  110.         'image', // 插入图片
  111.         // 'table', // 表格
  112.         // 'video', // 插入视频
  113.         // 'code', // 插入代码
  114.         'splitLine',
  115.         'undo', // 撤销
  116.         'redo', // 重复
  117.       ];
  118.       // this.editor.config.uploadImgServer = '/upload-img'; // 配置上传server 接口地址
  119.       this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024; // 图片上传max
  120.       this.editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']; // 图片上传类型
  121.       this.editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片
  122.       this.editor.config.customUploadImg = async function (resultFiles, insertImgFn) { // 自定义图片上传实现
  123.       // resultFiles 是 input 中选中的文件列表;insertImgFn 是获取图片 url 后,插入到编辑器的方法
  124.         const file = resultFiles[0];
  125.         const path = await uploadImage(file);//返回图片地址
  126.         console.log('完整path = ', path);
  127.       // 上传图片,返回结果,将图片插入到编辑器中
  128.         insertImgFn(path);
  129.       }
  130.       // 使用base64格式保存本地图片,不可与uploadImgServer同时使用
  131.       // this.editor.config.uploadImgShowBase64 = true;
  132.       this.editor.create();
  133.       // this.editor.txt.html('<p>用 JS 设置的内容</p>');// 重新设置编辑器内容
  134.       // 第一步,初始化 textarea 的值
  135.       // text1.val(this.editor.txt.html())
  136.       console.log('this.editor = ', this.editor);
  137.       // this.editor.txt.append('<p>追加的内容</p>');// 继续追加内容。
  138.       // 创建完成,回调父组件
  139.       try {
  140.         this.$emit(this.firtherMethod, null);
  141.       } catch (error) {
  142.         console.log('editor 完成,回调父组件失败 error = ', error);
  143.       }
  144.     },
  145.   },
  146.   mounted() {
  147.     this.createEditor();
  148.   },
  149. };
  150. </script>
  151. <style lang="css"  src="">
  152. /* @import '../css/Cnel.css';
  153. /* 使用style属性src引入外部css,仅在当前s组件有效 */
  154. </style>
复制代码
组件使用方式
  1. RichText: () => import('@/components/RichText.vue'),
  2. <rich-text v-model="details" ref="fueditModule" @loadingCompleted="loadingCompleted"></rich-text>
  3. export default Vue.extend({
  4.   name: 'UpdateText',
  5.   components: {
  6.     RichText: () => import('@/components/RichText.vue'),
  7.   },
  8.   methods: {
  9.           // 富文本组件加载完成回调
  10.     loadingCompleted() {
  11.       try {
  12.         console.log('editor加载完成,回调父组件');
  13.         // this.details = this.$refs.fueditModule.setHtml('<p><b>招商会详情!!</b></p>');
  14.       } catch (error) {
  15.         console.log('打开弹窗 err =', error);
  16.       }
  17.     },
  18.         // 调用子组件获取富文本内容
  19.         this.details = this.$refs.fueditModule.getHtml();
  20.         // 调用子组件设置富文本内容
  21.         this.$refs.fueditModule.setHtml('<p><b>设置详情!!</b></p>');
  22.         // 调用子组件销毁富文本编辑框
  23.         this.$refs.fueditModule.beforeDestroy();
  24.   }
  25. })
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

光之使者

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表