long
2021-10-26 e9f8083097a8d4b247e41c629e4822400136e803
提交 | 用户 | age
2a61f6 1 <template>
L 2   <div id="wangeditor">
3     <div ref="editorElem" style="text-align:left;" />
4   </div>
5 </template>
6 <script>
7 import E from 'wangeditor'
8 let self
9 export default {
10   name: 'EditorElem',
11   props: {
12     catchdata: {
13       type: Function,
14       default() {}
15     },
16     content: {
17       type: String,
18       default: ''
19     },
20     rangenum: {
21       type: Range,
22       default: null
23     }
24   },
25   data() {
26     return {
27       editor: null,
28       editorContent: ''
29     }
30   }, // 接收父组件的方法
31   watch: {
32     content(value) {
33       // console.log("没有光标则执行,有光标不执行,回显--解决输入时光标老跳到最底部", this.content, this.rangenum)
34       if (this.rangenum == null) {
35         // this.content.setRange(this.rangenum)
36         this.editor.txt.html(value)
37       }
38     }
39   },
40   mounted() {
41     self = this
42     this.editor = new E(this.$refs.editorElem)
43     this.editor.customConfig.focus = false
44     this.editor.customConfig.onchange = (html) => {
45       this.editorContent = html
46       this.catchdata(this.editorContent) // 把这个html通过catchData的方法传入父组件
47     }
48     // 去掉插入网络图片
49     this.editor.customConfig.showLinkImg = false
50     // 粘贴来的内容过滤图片
51     this.editor.customConfig.pasteIgnoreImg = true
52     this.editor.customConfig.menus = [ // 菜单配置
53       'head', // 标题
54       'bold', // 粗体
55       'fontSize', // 字号
56       'fontName', // 字体
57       'italic', // 斜体
58       'underline', // 下划线
59       'strikeThrough', // 删除线
60       'foreColor', // 文字颜色
61       'backColor', // 背景颜色
62       'link', // 插入链接
63       'list', // 列表
64       'justify', // 对齐方式
65       'quote', // 引用
66       // 'emoticon',  // 表情
67       'image', // 插入图片
68       // 'table',  // 表格
69       // 'code',  // 插入代码
70       'undo', // 撤销
71       'redo' // 重复
72     ]
73     // 下面是最重要的的方法
74     // this.editor.customConfig.withCredentials = true
75     // 将图片大小限制为 5M
76     this.editor.customConfig.uploadImgMaxSize = 5 * 1024 * 1024
77     // 限制一次最多上传 1 张图片
78     this.editor.customConfig.uploadImgMaxLength = 1
79     // 不忽略粘贴内容中的图片
80     this.editor.customConfig.pasteIgnoreImg = false
81     this.editor.customConfig.customUploadImg = function(files, insert) {
82       // files 是 input 中选中的文件列表
83       // insert 是获取图片 url 后,插入到编辑器的方法
84       // 因为file是个FormData格式的对象所以可以直接通过接口开始上传,不需要做多余操作
85       // 虽然文档说是FormData格式,但是我实际使用需要以下操作
86       const formData = new FormData()
87       formData.append('file', files[0])
88       self.postFN(
89         {
d8cbe9 90           url: 'admin/image/upload?folderCode=IMG',
2a61f6 91           header: { 'Content-Type': 'multipart/form-data' },
L 92           params: formData,
93           mockData: {
94             code: 100,
95             msg: '',
96             data: {
97               imgUrl: 'xxxx'
98             }
99           }
100         },
101         inf => {
102           insert(inf.imgUrl)
103         }
104       )
105     }
106     // 自定义错误提示
107     this.editor.customConfig.customAlert = function(info) {
108       // info 是需要提示的内容
109       self.$messagewarn(info)
110     }
b58e8a 111     // 过滤word复制
C 112     this.editor.customConfig.pasteTextHandle = function(content) {
113       var html = content
114       html = html.replace(/<\/?SPANYES[^>]*>/gi, '')//  移除span
115       html = html.replace(/<\!\-\-(.|[\r\n])*?\-\->/gi, '')//  移除注释
116       html = html.replace(/<(\w[^>]*) {2}lang=([^|>]*)([^>]*)/gi, '<$1$3')// 移除lang属性
117       html = html.replace(/<\\?\?xml[^>]*>/gi, '')//  移除xml和相关描述
118       html = html.replace(/<\/?\w+:[^>]*>/gi, '')// 移除xml命名空间的标签
119       html = html.replace(/&nbsp;/gi, '')//  移除空格
120       html = html.replace(/^\s+|\s+$/g, '')
121       html = html.replace(/<o:p> <\/o:p>/g, '')// 移除标签
122       html = html.replace(/<br\/>/g, '')// 移除br标签
123       return html
124     }
2a61f6 125     this.editor.create() // 创建富文本实例
L 126     // if (!this.content) {
127     //     // this.editor.txt.html('请编辑内容1')
128     // }
129   }
130 }
131 </script>
132 <style lang="scss" scoped>
133 #wangeditor {
134     position: sticky;
135     /*设置富文本框高度*/
136     .w-e-text-container{
137         height: 500px !important;
138     }
139 }
140 </style>
141