Blame view

src/components/editor/RichTextEditor.vue 2.83 KB
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
  <!-- src/components/RichTextEditor.vue -->
  <template>
    <div ref="editor" class="rich-text-editor" style="text-align: left;"></div>
  </template>
    
  <script>
  import E from 'wangeditor'
  import { getHeader } from '@/utils/header'
  
  export default {
    name: 'RichTextEditor',
    props: {
      // Initial content for the editor
      content: {
        type: String,
        default: ''
      },
    },
    data() {
      return {
        editor: null
      }
    },
    mounted() {
      this.initEditor()
    },
    beforeDestroy() {
      // Destroy the editor to prevent memory leaks
      if (this.editor) {
        this.editor.destroy()
        this.editor = null
      }
    },
    methods: {
      initEditor() {
        this.editor = new E(this.$refs.editor)
        this.editor.config.zIndex = 100
  
        // Configure editor menus
        this.editor.config.menus = [
          'head',
          'bold',
          'italic',
          'underline',
          'strikeThrough',
          'foreColor',
          'backColor',
          'link',
          'list',
          'justify',
          'quote',
          'image',
          'table',
          'code',
          'undo',
          'redo'
        ]
  
        // Image upload configuration
        this.editor.config.uploadImgServer = '/upload/uploadFile'
        let headers = getHeader()
        this.editor.config.uploadImgHeaders = headers
        this.editor.config.uploadFileName = 'uploadFile'
        this.editor.config.uploadImgMaxSize = 10 * 1024 * 1024 // 10M
  
        // Custom image upload callback
        this.editor.config.uploadImgHooks = {
          customInsert: (insertImgFn, result) => {
            if (result.code === 0) {
              const url = result.data.url || result.data.path
              insertImgFn(url)
            } else {
              this.$message.error(result.msg || this.$t('upload.error'))
            }
          },
          error: (err) => {
            this.$message.error(this.$t('upload.error'))
            console.error('Image upload error:', err)
          }
        }
  
        // Create the editor
        this.editor.create()
  
        // Initialize content
        if (this.content) {
          this.editor.txt.html(this.content)
        }
  
        // Emit content changes
        this.editor.config.onchange = (html) => {
          this.$emit('input', html)
          console.log('Editor content changed:', html)
          //this.$emit('update:content', html)
        }
      },
      // Method to clear the editor content
      clear() {
        if (this.editor) {
          this.editor.txt.clear()
        }
      },
      setContent(html) {
        if (this.editor) {
          this.editor.txt.html(html)
        }
      },
      // Method to get the current HTML content
      getContent() {
        return this.editor ? this.editor.txt.html() : ''
      }
    }
  }
  </script>
    
  <style lang="scss" scoped>
  .rich-text-editor {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
  
    &:hover {
      border-color: #c0c4cc;
    }
  
    &:focus {
      border-color: #409eff;
    }
  }
  </style>