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('content**', value)
34       // console.log("没有光标则执行,有光标不执行,回显--解决输入时光标老跳到最底部", this.content, this.rangenum)
35       if (this.rangenum == null) {
36         // this.content.setRange(this.rangenum)
37         this.editor.txt.html(value)
38       }
39     }
40   },
41   mounted() {
42     self = this
43     this.editor = new E(this.$refs.editorElem)
44     this.editor.config.focus = false
45     this.editor.config.onchange = (html) => {
46       this.editorContent = html
47       this.catchdata(this.editorContent) // 把这个html通过catchData的方法传入父组件
48     }
49     // 去掉插入网络图片
50     this.editor.config.showLinkImg = false
51     // 粘贴来的内容过滤图片
52     this.editor.config.pasteIgnoreImg = true
53     this.editor.config.menus = [ // 菜单配置
54       'head', // 标题
55       'bold', // 粗体
56       'fontSize', // 字号
57       'fontName', // 字体
58       'italic', // 斜体
59       'underline', // 下划线
60       'strikeThrough', // 删除线
61       'foreColor', // 文字颜色
62       'backColor', // 背景颜色
63       'link', // 插入链接
64       'list', // 列表
65       'justify', // 对齐方式
66       'quote', // 引用
67       // 'emoticon',  // 表情
68       'image', // 插入图片
69       // 'table',  // 表格
70       // 'code',  // 插入代码
71       'undo', // 撤销
72       'redo' // 重复
73     ]
74     // 下面是最重要的的方法
75     // this.editor.config.withCredentials = true
76     // 将图片大小限制为 5M
77     this.editor.config.uploadImgMaxSize = 5 * 1024 * 1024
78     // 限制一次最多上传 1 张图片
79     this.editor.config.uploadImgMaxLength = 1
80     // 不忽略粘贴内容中的图片
81     this.editor.config.pasteIgnoreImg = false
82     this.editor.config.customUploadImg = function(files, insert) {
83       // files 是 input 中选中的文件列表
84       // insert 是获取图片 url 后,插入到编辑器的方法
85       // 因为file是个FormData格式的对象所以可以直接通过接口开始上传,不需要做多余操作
86       // 虽然文档说是FormData格式,但是我实际使用需要以下操作
87       const formData = new FormData()
88       formData.append('file', files[0])
89       self.postFN(
90         {
91           url: 'admin/image/upload',
92           header: { 'Content-Type': 'multipart/form-data' },
93           params: formData,
94           mockData: {
95             code: 100,
96             msg: '',
97             data: {
98               imgUrl: 'xxxx'
99             }
100           }
101         },
102         inf => {
103           insert(inf.imgUrl)
104         }
105       )
106     }
107     // 自定义错误提示
108     this.editor.config.customAlert = function(info) {
109       // info 是需要提示的内容
110       self.$messagewarn(info)
111     }
112     this.editor.create() // 创建富文本实例
113     // if (!this.content) {
114     //     // this.editor.txt.html('请编辑内容1')
115     // }
116   }
117 }
118 </script>
119 <style lang="scss" scoped>
120 #wangeditor {
121     position: sticky;
122     /*设置富文本框高度*/
123     .w-e-text-container{
124         height: 500px !important;
125     }
126
127 }
128 </style>
129