Blame view

src/components/editor/RichTextEditor.vue 3.68 KB
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
1
2
3
4
  <!-- src/components/RichTextEditor.vue -->
  <template>
    <div ref="editor" class="rich-text-editor" style="text-align: left;"></div>
  </template>
2e81c59f   wuxw   完成办公功能测试
5
  
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
  <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
2e81c59f   wuxw   完成办公功能测试
60
        this.editor.config.uploadImgServer = '/callComponent/upload/uploadFile/uploadImage'
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
61
62
63
64
65
        let headers = getHeader()
        this.editor.config.uploadImgHeaders = headers
        this.editor.config.uploadFileName = 'uploadFile'
        this.editor.config.uploadImgMaxSize = 10 * 1024 * 1024 // 10M
  
2e81c59f   wuxw   完成办公功能测试
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        this.editor.config.customUploadImg = (resultFiles, insertImgFn) => {
          // 创建 FormData
          const formData = new FormData()
          formData.append('uploadFile', resultFiles[0])
          formData.append('communityId', '-1') // 添加 communityId 参数
  
          // 发送请求
          fetch('/callComponent/upload/uploadFile/uploadImage', {
            method: 'POST',
            headers: headers,
            body: formData
          })
            .then(response => response.json())
            .then(result => {
              console.log(result)
                const url = result.url 
                insertImgFn(url)
          
            })
            .catch(err => {
              this.$message.error(this.$t('upload.error'))
              console.error('Image upload error:', err)
            })
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
89
90
        }
  
2e81c59f   wuxw   完成办公功能测试
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        // 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)
        //   }
        // }
  
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
        // 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>
2e81c59f   wuxw   完成办公功能测试
140
  
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  <style lang="scss" scoped>
  .rich-text-editor {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
  
    &:hover {
      border-color: #c0c4cc;
    }
  
    &:focus {
      border-color: #409eff;
    }
  }
  </style>