123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <template>
- <div style="border: 1px solid #ccc; z-index: 999999">
- <Toolbar
- style="border-bottom: 1px solid #ccc"
- :editor="editor"
- :defaultConfig="toolbarConfig"
- :mode="mode"
- />
- <Editor
- style="height: 500px; overflow-y: hidden"
- v-model="currentValue"
- :defaultConfig="editorConfig"
- :mode="mode"
- @onCreated="onCreated"
- @onChange="onChange"
- @onDestroyed="onDestroyed"
- @customAlert="customAlert"
- />
- </div>
- </template>
- <script>
- import { getToken } from "@/utils/auth";
- import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
- export default {
- components: { Editor, Toolbar },
- data() {
- return {
- editor: null,
- currentValue: '',
- toolbarConfig: {
- insertKeys:{
- index: 0,
- keys:['uploadAttachment']
- }
- },
- editorConfig: {
- hoverbarKeys: {
- attachment: {
- menuKeys: ['downloadAttachment'], // “下载附件”菜单
- },
- },
- placeholder: '请输入内容...',
- MENU_CONF:
- {
- uploadImage:{
- server: BASE_URL + "/v1/common/upload",
- fieldName: 'file',
- maxFileSize: this.fileSize * 1024 * 1024,
- headers:{
- Authorization: "Bearer " + getToken()
- },
- onBeforeUpload(file){
- return file
- },
- onProgress(progress){
- console.log('progress', progress)
- },
- onSuccess(file, res) {
- console.log(file.name + "上传成功")
- },
- onFailed(file, res) {
- console.log(file.name + "上传失败")
- },
- onError(file, err, res){
- console.log(file.name + "上传失败")
- },
- customInsert(res, insertFn) {
- insertFn(BASE_URL + res.url, res.fileName, '#')
- },
- },
- uploadVideo:{
- server: BASE_URL + "/v1/common/upload",
- fieldName: 'file',
- maxFileSize: this.fileSize * 1024 * 1024,
- headers:{
- Authorization: "Bearer " + getToken()
- },
- onBeforeUpload(file){
- return file
- },
- onProgress(progress){
- console.log('progress', progress)
- },
- onSuccess(file, res) {
- console.log(file.name + "上传成功")
- },
- onFailed(file, res) {
- console.log(file.name + "上传失败")
- },
- onError(file, err, res){
- console.log(file.name + "上传失败")
- },
- customInsert(res, insertFn) {
- insertFn(BASE_URL + res.url, res.fileName)
- },
- },
- uploadAttachment:{
- server: BASE_URL + "/v1/common/upload",
- fieldName: 'file',
- maxFileSize: this.fileSize * 1024 * 1024,
- headers:{
- Authorization: "Bearer " + getToken()
- },
- onBeforeUpload(file){
- return file
- },
- onProgress(progress){
- console.log('progress', progress)
- },
- onSuccess(file, res) {
- console.log(file.name + "上传成功")
- },
- onFailed(file, res) {
- console.log(file.name + "上传失败")
- },
- onError(file, err, res){
- console.log(file.name + "上传失败")
- },
- customInsert(res, file, insertFn) {
- insertFn(res.fileName, BASE_URL + res.url)
- },
- }
- }
- },
- mode: 'default', // or 'simple'
- }
- },
- // 接收父组件的方法
- props: {
- value: {
- type: String,
- default: ''
- },
- // 上传文件大小限制(MB)
- fileSize: {
- type: Number,
- default: 2048,
- },
- },
- watch: {
- value: {
- handler(val) {
- if (val !== this.currentValue) {
- this.currentValue = val === null ? "" : val;
- }
- },
- immediate: true,
- }
- },
- methods: {
- onCreated(editor) {
- this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
- },
- onChange(editor) {
- this.$emit("input", this.editor.getHtml());
- },
- onDestroyed(editor) {},
- customAlert(s, t){ //自定义弹框
- switch (t){
- case 'success':
- this.$message.success(s)
- break
- case 'info':
- this.$message.info(s)
- break
- case 'warning':
- this.$message.warning(s)
- break
- case 'error':
- this.$message.error(s)
- break
- default:
- this.$message.info(s)
- break
- }
- },
- },
- mounted() {
- },
- beforeDestroy() {
- const editor = this.editor
- if (editor == null) return
- editor.destroy() // 组件销毁时,及时销毁编辑器
- }
- }
- </script>
- <style src="@wangeditor/editor/dist/css/style.css"></style>
|