当前位置:   article > 正文

wangeditor富文本组件-复制可用_富文本控件 支持 excel拷贝

富文本控件 支持 excel拷贝

功能介绍:

本文记录了wangeditor最基本的用法,其他更多用法可去官网查找。

整体思路:

Typescript 开发的 Web 富文本编辑器, 轻量、简洁、易用、开源免费(摘自官网)。

具体实现:

一、安装:

npm i wangeditor --save
  • 1

二、引入并使用:

<template>
	<div>
		<div ref="richText" style="height: 100%;"></div>
	</div>
</template>
  • 1
  • 2
  • 3
  • 4
  • 5
<script>
// 导入富文本编辑器
import wangeditor from 'wangeditor';
export default {
    name: "richtext",
	data() {
        return {
        	richTextEditor: {},
        };
    },
    mounted() {
    	// 调用
    	this.initRichText();
  	},
    methods: {
    	// 富文本框初始化
	    initRichText() {
	    	this.richTextEditor = new WangEditor(self.$refs.richText);
	    	// 配置菜单栏,删减菜单,调整顺序
	    	this.richTextEditor.config.menus = [
				'head', // 标题
				'bold', // 加粗
				'fontSize', // 字号
				'fontName', // 字体
				'italic', // 斜体
				'underline', // 下划线
				'strikeThrough', // 删除线
				'indent', // 缩进
				'lineHeight', // 行高
				'foreColor', // 文字颜色
				'backColor', // 背景色
				'link', // 链接
				'list', // 序列
				'todo', // 待办事项
				'justify', // 对齐
				'quote', // 引用
				// 'emoticon',
				// 'image', // 图片
				// 'video',
				'table', // 表格
				// 'code', // 代码
				'splitLine', // 分割线
				'undo', // 撤销
				'redo', // 恢复
        	];
        	// 使用base64保存图片
        	this.richTextEditor.config.uploadImgShowBase64 = true;
	        // 隐藏网络图片
	        this.richTextEditor.config.showLinkImg = false;
	        // 配置全屏功能按钮不展示
	        // this.richTextEditor.config.showFullScreen = true;
	        // 设置编辑器内容区域的高度
	        this.richTextEditor.config.height = 150;
	        // 设置富文本的z-index
	        this.richTextEditor.config.zIndex = 105;
	        // 禁止自动选中
	        this.richTextEditor.config.focus = false;
	        // 修改 placeholder 的提示文字
	        this.richTextEditor.config.placeholder = '';
	        // 菜单栏提示为下标
	        // this.richTextEditor.config.menuTooltipPosition = 'bottom';
	        // 去除复制过来文本的默认样式
	        this.richTextEditor.config.pasteFilterStyle = true;
        	this.richTextEditor.create();
	        this.richTextEditor.config.onchange = this.richTextChange;
	        // 赋值
	        const myHtml = '<p>123</p>';
	        this.richTextEditor.txt.html(myHtml);
	    },
	    // 富文本框内容发生改变,取值
	    richTextChange(newHtml) {
	    	console.log(newHtml);
	    	// 获取 html
	    	console.log(this.richTextEditor.txt.html());
	    	// 获取 text 文本
	    	console.log(this.richTextEditor.txt.text());
	    },
    }
};
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

三、使用(表单验证,结合element-ui使用):

非空验证,设置一个隐藏input组件,负责使用验证提示。

<template>
	<el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-position="top"
        >
          <el-form-item label="内容" prop="noticeContent" >
            <el-input v-if="false" v-model="form.content" readonly />
            <div ref="richText" class="richText" style="height: 100%;"/>
          </el-form-item>
        </el-form>
</template>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
<script>
export default {
  data() {
  	// 自定义验证
    const contentLength = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('内容不可为空');
      } else {
        if (this.richTxt.length > 100) {
          callback(new Error('字数不可超过100字');
        }
        callback();
      }
    };
    return {
      form: {
        content: ''
      },
      // 表单验证
      rules: {
        // 公告内容
        content: [
          { validator: contentLength, trigger: 'change' }
        ]
      },
      richTxt: '',
      // 富文本编辑组件
      richTextEditor: {},
    };
  },
  watch: {
    // 公告内容校验监听
    'form.content': {
      handler() {
      	// 调用表单验证
        this.$refs.form.validateField('content');
      }
    }
  },
  mounted() {
    this.initRichText();
  },
  methods: {
    // 富文本框初始化
    initRichText() {
      // 省略上面初始化的代码
      // 绑定change事件
      this.richTextEditor.config.onchange = this.richTextChange;
    },
    // 富文本框内容复制
    richTextChange(newHtml) {
      this.richTxt = this.richTextEditor.txt.text();
      this.form.content= newHtml;
    }
  }
};
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号