集成wangEditor5 富文本

This commit is contained in:
1575321367@qq.com 2022-08-08 17:50:04 +08:00
parent 8fe07a7e6d
commit 17fc837663
3 changed files with 206 additions and 2 deletions

View File

@ -36,6 +36,7 @@
}, },
"dependencies": { "dependencies": {
"@riophae/vue-treeselect": "0.4.0", "@riophae/vue-treeselect": "0.4.0",
"@wangeditor/editor-for-vue": "^1.0.2",
"axios": "0.24.0", "axios": "0.24.0",
"clipboard": "2.0.8", "clipboard": "2.0.8",
"core-js": "3.19.1", "core-js": "3.19.1",
@ -51,7 +52,7 @@
"quill": "1.3.7", "quill": "1.3.7",
"screenfull": "5.0.2", "screenfull": "5.0.2",
"sortablejs": "1.10.2", "sortablejs": "1.10.2",
"vue": "2.6.12", "vue": "2.6.14",
"vue-count-to": "1.0.13", "vue-count-to": "1.0.13",
"vue-cropper": "0.5.5", "vue-cropper": "0.5.5",
"vue-meta": "2.4.0", "vue-meta": "2.4.0",
@ -76,7 +77,7 @@
"sass-loader": "10.1.1", "sass-loader": "10.1.1",
"script-ext-html-webpack-plugin": "2.1.5", "script-ext-html-webpack-plugin": "2.1.5",
"svg-sprite-loader": "5.1.1", "svg-sprite-loader": "5.1.1",
"vue-template-compiler": "2.6.12" "vue-template-compiler": "2.6.14"
}, },
"engines": { "engines": {
"node": ">=8.9", "node": ">=8.9",

View File

@ -0,0 +1,202 @@
<template>
<div>
<div style="border: 1px solid #ccc; margin-top: 10px;">
<!-- 工具栏 -->
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editor"
:defaultConfig="toolbarConfig"
/>
<!-- 编辑器 -->
<Editor
style="height: 400px; overflow-y: hidden;"
:defaultConfig="editorConfig"
v-model="currentValue"
@onChange="onChange"
@onCreated="onCreated"
/>
</div>
</div>
</template>
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { getToken } from "@/utils/auth";
export default {
name: "WEditor",
components: { Editor, Toolbar },
props: {
/* 编辑器的内容 */
value: {
type: String,
default: "",
},
/* 高度 */
height: {
type: Number,
default: null,
},
/* 最小高度 */
minHeight: {
type: Number,
default: null,
},
/* 只读 */
readOnly: {
type: Boolean,
default: false,
},
// (MB)
fileSize: {
type: Number,
default: 5,
},
/* 类型base64格式、url格式 */
type: {
type: String,
default: "url",
}
},
data() {
return {
editor: null,
toolbarConfig: {
// toolbarKeys: [ /* */ ],
// excludeKeys: [ /* */ ],
},
editorConfig: {
placeholder: '请输入内容...',
// autoFocus: false,
// MENU_CONF
MENU_CONF: {
uploadImage: {
server: process.env.VUE_APP_BASE_API + "/system/oss/upload",
// http header
headers: {
Authorization: "Bearer " + getToken()
},
// ['image/*'] []
allowedFileTypes: ['image/*'],
// 100
maxNumberOfFiles: 10,
// 5M
maxFileSize: 5 * 1024 * 1024, // 5M
// form-data fieldName 'wangeditor-uploaded-image'
fieldName: 'file',
//
customInsert(res, insertFn) {
// res
// res url alt href
const url = res.data.url
const alt = res.data.fileName
const href = null
insertFn(url, alt, href)
},
//
onSuccess(file, res) { // JS
console.log(`${file.name} 上传成功`, res)
},
//
onFailed(file, res) { // JS
console.error(`${file.name} 上传失败`, res)
},
// timeout
onError(file, err, res) { // JS
console.error(`${file.name} 上传出错`, err, res)
},
},
uploadVideo: {
server: process.env.VUE_APP_BASE_API + "/system/oss/upload",
// http header
headers: {
Authorization: "Bearer " + getToken()
},
// ['image/*'] []
allowedFileTypes: ['video/*'],
// 5
maxNumberOfFiles: 1,
// 5M
maxFileSize: 20 * 1024 * 1024, // 5M
// form-data fieldName 'wangeditor-uploaded-image'
fieldName: 'file',
// 30
timeout: 15 * 1000, // 15
//
customInsert(res, insertFn) {
const url = res.data.url
const poster = res.data.poster
// res
// res url poster
insertFn(url, poster)
},
//
onSuccess(file, res) { // JS
console.log(`${file.name} 上传成功`, res)
},
//
onFailed(file, res) { // JS
console.log(`${file.name} 上传失败`, res)
},
// timeout
onError(file, err, res) { // JS
console.log(`${file.name} 上传出错`, err, res)
},
},
},
readOnly: this.readOnly,
codeSelectLang:{
codeLangs: [
{ text: 'CSS', value: 'css' },
{ text: 'HTML', value: 'html' },
{ text: 'XML', value: 'xml' },
//
]
}
},
currentValue: "",
};
},
computed: {
styles() {
let style = {};
if (this.minHeight) {
style.minHeight = `${this.minHeight}px`;
}
if (this.height) {
style.height = `${this.height}px`;
}
return style;
},
},
watch: {
value: {
handler(val) {
if (val !== this.currentValue) {
this.currentValue = val === null ? "" : val;
if (this.editor) {
this.editor.setContent(this.currentValue);
}
}
},
immediate: true,
},
},
beforeDestroy() {
const editor = this.editor
if (editor == null) return
editor.destroy() // editor
},
methods: {
onCreated(editor) {
this.editor = Object.seal(editor) // Object.seal()
},
onChange(editor) {
this.$emit("input", editor.getHtml());
},
},
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

View File

@ -25,6 +25,7 @@ import Pagination from "@/components/Pagination";
import RightToolbar from "@/components/RightToolbar" import RightToolbar from "@/components/RightToolbar"
// 富文本组件 // 富文本组件
import Editor from "@/components/Editor" import Editor from "@/components/Editor"
import Editor from "@/components/WEditor"
// 文件上传组件 // 文件上传组件
import FileUpload from "@/components/FileUpload" import FileUpload from "@/components/FileUpload"
// 图片上传组件 // 图片上传组件