wwf
16 小时以前 e1b028d486713eaf55aaf35fbf334aa568059c0d
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
<template>
    <div style="border: 1px solid #ccc">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden;"
        v-model="contentValue"
        :defaultConfig="editorConfig"
        :mode="mode"
        :disabled="disabled"
        @onCreated="handleCreated"
      />
    </div>
</template>
<script>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { uploadFileRequest, getUploadImagePath } from '@/utils/tool.js'
export default {
  components: { Editor, Toolbar },
  setup(props) {
    // 编辑器实例,必须用 shallowRef
    const editorRef = shallowRef()
 
    const toolbarConfig = {}
    toolbarConfig.excludeKeys = [ // 排除菜单组,写菜单组 key 的值即可
      'insertVideo',
      'uploadVideo',
      'editVideoSize',
      'insertImage'
    ]
    const editorConfig = { 
      placeholder: '请输入内容...',
      MENU_CONF: {},
    }
    editorConfig.MENU_CONF['uploadImage'] = {
      async customUpload(file, insertFn) {
        try {
          const url = await uploadFileRequest(file)
          if (!url) {
            this.$message.error('上传失败')
            return
          }
          insertFn(getUploadImagePath(url), '', '')
        } catch(error) {
          console.log(error)
        }
      },
    }
 
    onBeforeUnmount(() => {
      const editor = editorRef.value
      if (editor == null) return
      editor.destroy()
    })
 
    const handleCreated = (editor) => {
      editorRef.value = editor // 记录 editor 实例,重要!
      if (props.disabled) {
        editorRef.value.disable()
      }
    }
 
    return {
      editorRef,
      mode: 'default', // 或 'simple'
      toolbarConfig,
      editorConfig,
      handleCreated,
    }
  },
  data() {
    return {
      contentValue: '',
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    modelValue: {
      type: String,
      default: ''
    }
  },
  watch: {
    modelValue(val) {
      this.contentValue = val
    },
    contentValue(val) {
      this.$emit('update:modelValue', val)
    }
 
  }
}
</script>
 
<style>
.w-e-full-screen-container {
  z-index: 1000;
}
 
</style>