long
2022-02-28 b350757df151c04f22d040eac001cb479d4a4776
提交 | 用户 | 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)
958f5a 43     this.editor.config.focus = false
L 44     this.editor.config.onchange = (html) => {
2a61f6 45       this.editorContent = html
L 46       this.catchdata(this.editorContent) // 把这个html通过catchData的方法传入父组件
47     }
48     // 去掉插入网络图片
958f5a 49     this.editor.config.showLinkImg = false
2a61f6 50     // 粘贴来的内容过滤图片
958f5a 51     this.editor.config.pasteIgnoreImg = true
L 52     this.editor.config.menus = [ // 菜单配置
2a61f6 53       'head', // 标题
L 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', // 插入图片
958f5a 68       'video', // 插入视频
2a61f6 69       // 'table',  // 表格
958f5a 70       'code', // 插入代码
L 71       'video',
2a61f6 72       'undo', // 撤销
L 73       'redo' // 重复
74     ]
75     // 下面是最重要的的方法
958f5a 76     // this.editor.config.withCredentials = true
2a61f6 77     // 将图片大小限制为 5M
958f5a 78     this.editor.config.uploadImgMaxSize = 5 * 1024 * 1024
2a61f6 79     // 限制一次最多上传 1 张图片
958f5a 80     this.editor.config.uploadImgMaxLength = 1
2a61f6 81     // 不忽略粘贴内容中的图片
958f5a 82     this.editor.config.pasteIgnoreImg = false
L 83     this.editor.config.customUploadImg = function(files, insert) {
2a61f6 84       // files 是 input 中选中的文件列表
L 85       // insert 是获取图片 url 后,插入到编辑器的方法
86       // 因为file是个FormData格式的对象所以可以直接通过接口开始上传,不需要做多余操作
87       // 虽然文档说是FormData格式,但是我实际使用需要以下操作
88       const formData = new FormData()
89       formData.append('file', files[0])
958f5a 90       console.log(formData, 'formDataformDataformDataformDataformData')
2a61f6 91       self.postFN(
L 92         {
d8cbe9 93           url: 'admin/image/upload?folderCode=IMG',
2a61f6 94           header: { 'Content-Type': 'multipart/form-data' },
L 95           params: formData,
96           mockData: {
97             code: 100,
98             msg: '',
99             data: {
100               imgUrl: 'xxxx'
101             }
102           }
103         },
104         inf => {
105           insert(inf.imgUrl)
106         }
107       )
108     }
958f5a 109
L 110     // 配置富文本视频上传
111     this.editor.config.customUploadVideo = function(files, insert) {
112     // files 是 input 中选中的文件列表
113     // insert 是获取视频 url 后,插入到编辑器的方法
114       const formDataVideo = new FormData()
115       formDataVideo.append('file', files[0])
116       console.log(formDataVideo, 'formDataVideoformDataVideoformDataVideoformDataVideo')
117       self.postFN(
118         {
119           url: 'admin/image/upload?folderCode=VIDEO',
120           header: { 'Content-Type': 'multipart/form-data' },
121           params: formDataVideo,
122           mockData: {
123             code: 100,
124             msg: '',
125             data: {
126               imgUrl: 'xxxx'
127             }
128           }
129         },
130         inf => {
131           // 上传视频,返回结果,将视频地址插入到编辑器中
132           insert(inf.imgUrl)
133           console.log(inf.imgUrl, '99999999999999999999999999')
134         }
135       )
136     }
137
2a61f6 138     // 自定义错误提示
958f5a 139     this.editor.config.customAlert = function(info) {
2a61f6 140       // info 是需要提示的内容
762ab7 141       self.$messageWarn(info)
2a61f6 142     }
b58e8a 143     // 过滤word复制
958f5a 144     this.editor.config.pasteTextHandle = function(content) {
b58e8a 145       var html = content
C 146       html = html.replace(/<\/?SPANYES[^>]*>/gi, '')//  移除span
147       html = html.replace(/<\!\-\-(.|[\r\n])*?\-\->/gi, '')//  移除注释
148       html = html.replace(/<(\w[^>]*) {2}lang=([^|>]*)([^>]*)/gi, '<$1$3')// 移除lang属性
149       html = html.replace(/<\\?\?xml[^>]*>/gi, '')//  移除xml和相关描述
150       html = html.replace(/<\/?\w+:[^>]*>/gi, '')// 移除xml命名空间的标签
151       html = html.replace(/&nbsp;/gi, '')//  移除空格
152       html = html.replace(/^\s+|\s+$/g, '')
153       html = html.replace(/<o:p> <\/o:p>/g, '')// 移除标签
154       html = html.replace(/<br\/>/g, '')// 移除br标签
155       return html
156     }
2a61f6 157     this.editor.create() // 创建富文本实例
L 158     // if (!this.content) {
159     //     // this.editor.txt.html('请编辑内容1')
160     // }
161   }
162 }
163 </script>
164 <style lang="scss" scoped>
165 #wangeditor {
166     position: sticky;
167     /*设置富文本框高度*/
168     .w-e-text-container{
169         height: 500px !important;
170     }
171 }
172 </style>
173