0%

富文本编辑器


推荐一个好用的富文本编辑器,之前也有用过其他版本的,但是都太简易了

安装 vue-editor-wrap

npm install vue-editor-wrap

下载大神已经处理后的UEditor,下载 地址链接: https://pan.baidu.com/s/1ds-HyhXD5EFYCcp7uen2XA 提取码: s7ry

解压, 重命名文件夹为UEditor, 放入public文件夹下(如果是旧项目对应static文件夹)

在 main.js中引入

1
2
import VueUeditorWrap from 'vue-ueditor-wrap'
Vue.component('vue-ueditor-wrap', VueUeditorWrap)

然后再.vue文件中就可以使用啦

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<template>
<div class="ueditor-page">
<div class="ueditor-box">
<vue-ueditor-wrap v-model="msg" :config="myConfig" @before-init="addCustomButtom"></vue-ueditor-wrap>
</div>
<div class="Preview" v-html="msg"></div>
</div>
</template>

<script>
export default {
data() {
return {
msg: '',
myConfig: {
// 编辑器不自动被内容撑高
autoHeightEnabled: false,
// 底部元素路径
elementPathEnabled : false,
//是否开启字数统计
wordCount: true,
// 初始容器高度
initialFrameHeight: '100%',
// 初始容器宽度
initialFrameWidth: '100%',
// 上传文件接口
serverUrl: 'http://10.100.2.127:8080/webapi.php',
toolbars: [
[
'anchor', //锚点
'undo', //撤销
'redo', //重做
'bold', //加粗
'indent', //首行缩进
// 'snapscreen', //截图
'italic', //斜体
'underline', //下划线
'strikethrough', //删除线
'subscript', //下标
'fontborder', //字符边框
'superscript', //上标
'formatmatch', //格式刷
// 'source', //源代码
'blockquote', //引用
'pasteplain', //纯文本粘贴模式
'selectall', //全选
'print', //打印
// 'preview', //预览
'horizontal', //分隔线
'removeformat', //清除格式
'time', //时间
'date', //日期
'unlink', //取消链接
'insertrow', //前插入行
'insertcol', //前插入列
'mergeright', //右合并单元格
'mergedown', //下合并单元格
'deleterow', //删除行
'deletecol', //删除列
'splittorows', //拆分成行
'splittocols', //拆分成列
'splittocells', //完全拆分单元格
'deletecaption', //删除表格标题
'inserttitle', //插入标题
'mergecells', //合并多个单元格
'deletetable', //删除表格
'cleardoc', //清空文档
'insertparagraphbeforetable', //"表格前插入行"
// 'insertcode', //代码语言
'fontfamily', //字体
'fontsize', //字号
'paragraph', //段落格式
// 'simpleupload', //单图上传
// 'insertimage', //多图上传
// 'edittable', //表格属性
// 'edittd', //单元格属性
'link', //超链接
'emotion', //表情
'spechars', //特殊字符
'searchreplace', //查询替换
'map', //Baidu地图
// 'gmap', //Google地图
'insertvideo', //视频
'help', //帮助
'justifyleft', //居左对齐
'justifyright', //居右对齐
'justifycenter', //居中对齐
'justifyjustify', //两端对齐
'forecolor', //字体颜色
'backcolor', //背景色
'insertorderedlist', //有序列表
'insertunorderedlist', //无序列表
'fullscreen', //全屏
'directionalityltr', //从左向右输入
'directionalityrtl', //从右向左输入
'rowspacingtop', //段前距
'rowspacingbottom', //段后距
// 'pagebreak', //分页
'insertframe', //插入Iframe
'imagenone', //默认
'imageleft', //左浮动
'imageright', //右浮动
// 'attachment', //附件
'imagecenter', //居中
'wordimage', //图片转存
'lineheight', //行间距
'edittip ', //编辑提示
// 'customstyle', //自定义标题
'autotypeset', //自动排版
// 'webapp', //百度应用
'touppercase', //字母大写
'tolowercase', //字母小写
// 'background', //背景
// 'template', //模板
// 'scrawl', //涂鸦
// 'music', //音乐
'inserttable', //插入表格
// 'drafts', // 从草稿箱加载
// 'charts', // 图表
]
]
}
}
},
methods: {
addCustomButtom (editorId) {
let _this = this
let img = ''
window.UE.registerUI('test-button', function (editor, uiName) {
// 注册按钮执行时的 command 命令,使用命令默认就会带有回退操作
editor.registerCommand(uiName, {
execCommand: function () {
editor.execCommand('inserthtml', `<img src="${img}" alt="" />`)
}
})

// 创建一个 button
var btn = new window.UE.ui.Button({
// 按钮的名字
name: uiName,
// 提示
title: '单图上传',
// 需要添加的额外样式,可指定 icon 图标,图标路径参考常见问题 2
cssRules: "background-image: url('/static/icons.png')!important;background-position: -380px 0px;",
// 点击时执行的命令
onclick: function () {
// 这里可以不用执行命令,做你自己的操作也可
// editor.execCommand(uiName)
var input = document.createElement('input')
input.type = "file"
input.style.display = 'none'
document.body.appendChild(input)
input.click()
input.addEventListener('change',(e)=>{
// 利用 axios 上传,上传成功之后销毁 DOM
console.log(e.target.files)
let sendMsg = {
action: 'material.MaterialSalesman.getImgUrl',
files: e.target.files
}
// return;
_this.$store.dispatch('Post', sendMsg).then((res) => {
img = res.data[0].data
editor.execCommand(uiName)
})
})
}
})

// 当点到编辑内容上时,按钮要做的状态反射
editor.addListener('selectionchange', function () {
var state = editor.queryCommandState(uiName)
if (state === -1) {
btn.setDisabled(true)
btn.setChecked(false)
} else {
btn.setDisabled(false)
btn.setChecked(state)
}
})

// 因为你是添加 button,所以需要返回这个 button
return btn
}, 0 /* 指定添加到工具栏上的哪个位置,默认时追加到最后 */, editorId /* 指定这个 UI 是哪个编辑器实例上的,默认是页面上所有的编辑器都会添加这个按钮 */)
}
},
created() {

},
computed: {

},
components: {

}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="css">
.ueditor-page {
width: 100%;
height: 100vh;
display: flex;
overflow: hidden;
}
.ueditor-box {
width: 50%;
height: 100%;
border: 1px solid black;
}
.Preview {
width: 50%;
height: 100%;
border: 1px solid black;
margin-left: 20px;
padding: 10px;
overflow: auto;
}

</style>

我这边还使用到了自定义按钮的功能,原理注释都在代码里面啦

以上就是我对富文本插件的一些理解,如果文章由于我学识浅薄,导致您发现有严重谬误的地方,请一定在评论中指出,我会在第一时间修正我的博文,以避免误人子弟。

-------------本文结束感谢您的阅读-------------
没办法,总要恰饭的嘛~~