index.vue
4.3 KB
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
<template>
<div style="border: 1px solid #ccc">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editor"
:class="showToolbar == true?'':'tooll'"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
<Editor
:style="`height:${height}px;overflow-y: hidden;`"
v-model="html"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="onCreated"
@onChange="onChange"
/>
</div>
</template>
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {
imgUp
} from '../../api/imgFlieUp'
export default {
components: { Editor, Toolbar },
props: {
value:{
type: String,
default: ''
},
read: {
type: Boolean,
default: false
},
height:{
type: Number,
default: 300
}
},
data() {
return {
// read:false,
editor: null,
showToolbar: true, // 默认显示编辑栏
html: "",
toolbarConfig: {
// 不要的功能按钮
excludeKeys: ['insertVideo','uploadVideo','insertImage'],
},
editorConfig: {
// maxLength:254,
readOnly:false,
placeholder: "请输入内容...",
MENU_CONF: {
uploadImage: {
customUpload: this.uploadImg, // 自定义上传方法
},
},
},
mode: "default", // or 'simple'
};
},
watch: {
read: {
handler(newVal, oldVal) {
// this.showToolbar = !newVal;
// this.editorConfig.readOnly = newVal;
this.$nextTick(() => {
this.showToolbar = !newVal;
this.editorConfig.readOnly = newVal;
});
},
immediate: true, // 添加此选项以立即执行回调
deep:true
},
value(newVule,old){
if(newVule !='<p><br></p>'){
this.html = this.value;
console.log(newVule, old);
}
},
},
// computed: {
// computedEditorConfig() {
// return {
// readOnly: this.read,
// placeholder: "请输入内容...",
// MENU_CONF: {
// uploadImage: {
// customUpload: this.uploadImg, // 自定义上传方法
// },
// },
// };
// }
// },
mounted() {
// 异步渲染编辑器
this.$nextTick(() => {
this.html = this.value;
});
},
methods: {
// 上传图片
uploadImg(file, insertFn) {
const { type } = file;
if (type.indexOf("image") < 0) {
this.$message.warning("请选择上传图片");
return false;
}
const formData = new FormData();
formData.append("file", file);
imgUp(formData)
.then((res) => {
console.log(res)
if (res.code == ''||res.code == '200') {
// 从 res 中找到 url alt href ,然后插入图片
insertFn(this.$baseURL+res.data);
} else {
this.$message.error(res.statusText || "上传失败");
}
})
.catch(() => {
this.$message.error("上传失败");
});
},
// 上传视频
// uploadVideo(file, insertFn) {
// const { type } = file;
// if (type.indexOf("video") < 0) {
// this.$message.warning("请选择上传视频");
// return false;
// }
// const formData = new FormData();
// formData.append("file", file);
// this.$api
// .uploadFile(formData)
// .then((res) => {
// if (res.status === 200) {
// // 从 res 中找到 url poster(封面图) ,然后插入视频
// insertFn(res.data.url);
// } else {
// this.$message.error(res.statusText || "上传失败");
// }
// })
// .catch(() => {
// this.$message.error("上传失败");
// });
// },
onCreated(editor) {
this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
},
onChange() {
this.$emit("input", this.html);
},
},
beforeDestroy() {
const editor = this.editor;
if (editor == null) return;
editor.destroy(); // 组件销毁时,及时销毁编辑器
},
};
</script>
<style scoped lang="scss">
@import url("./index.css");
::v-deep .tooll{
.w-e-toolbar{
display: none;
}
}
::v-deep .title{
margin: 0 !important;
}
</style>