tj
2025-03-20 a35f4b4d0c555493cc464bfd36d037230547f1aa
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
<template>
  <div class="editor-container" :style="{ width: width }">
    <div ref="editor"></div>
  </div>
</template>
 
<script>
import E from 'wangeditor'
 
export default {
  name: 'Editor',
  props: {
    value: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: Number,
      default: 300
    }
  },
  data() {
    return {
      editor: null,
      content: ''
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val !== this.content && this.editor) {
          this.content = val || ''
          this.editor.txt.html(this.content)
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.initEditor()
  },
  beforeDestroy() {
    if (this.editor) {
      this.editor.destroy()
      this.editor = null
    }
  },
  methods: {
    initEditor() {
      this.editor = new E(this.$refs.editor)
 
      // 配置菜单栏
      this.editor.config.menus = [
        'head',
        'bold',
        'fontSize',
        'fontName',
        'italic',
        'underline',
        'strikeThrough',
        'foreColor',
        'backColor',
        'link',
        'list',
        'justify',
        'quote',
        'emoticon',
        'image',
        'table',
        'code',
        'undo',
        'redo'
      ]
 
      // 配置编辑器高度
      this.editor.config.height = this.height
 
      // 配置图片上传
      this.editor.config.uploadImgServer = '/jshERP-boot/common/upload'
      this.editor.config.uploadFileName = 'file'
      this.editor.config.uploadImgHooks = {
        customInsert: (insertImgFn, result) => {
          if (result.code === 200) {
            insertImgFn(result.data.url)
          } else {
            this.$message.error('图片上传失败')
          }
        }
      }
 
      // 配置 onchange 回调函数
      this.editor.config.onchange = (html) => {
        this.content = html
        this.$emit('input', html)
        this.$emit('change', html)
      }
 
      // 创建编辑器
      this.editor.create()
 
      // 设置初始内容
      if (this.value) {
        this.editor.setHtml(this.value)
      }
    }
  }
}
</script>
 
<style scoped>
.editor-container {
  border: 1px solid #ccc;
  z-index: 100;
}
</style>