董国庆
7 天以前 069ef85293bb4baa74dd9251e8b3fd8cc4355410
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<template>
  <div class="ai-editor-container">
    <div ref="editorRef" :style="{ height }" />
  </div>
</template>
 
<script>
import { AiEditor } from "aieditor";
import 'aieditor/dist/style.css'
 
export default {
  name: 'AiEditor',
  props: {
    value: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: '500px'
    },
    placeholder: {
      type: String,
      default: '请输入内容...'
    },
    readOnly: {
      type: Boolean,
      default: false
    },
    toolbar: {
      type: Array,
      default: () => [
        'bold',
        'italic',
        'underline',
        'strikethrough',
        'heading',
        'quote',
        'code',
        'list',
        'ordered-list',
        'unordered-list',
        'link',
        'image',
        'table'
      ]
    }
  },
  data() {
    return {
      editor: null
    }
  },
  watch: {
    value: {
      handler(newVal) {
        if (this.editor && newVal !== this.editor.getHtml()) {
          this.editor.setContent(newVal)
        }
      },
      immediate: true
    },
    readOnly: {
      handler(newVal) {
        console.log('readOnly', newVal)
        if (this.editor) {
          this.editor.setReadOnly(newVal)
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.initEditor()
  },
  beforeDestroy() {
    this.destroyEditor()
  },
  methods: {
    initEditor() {
      const options = {
        element: this.$refs.editorRef,
        placeholder: this.placeholder,
        content: this.value,
        editable: !this.readOnly,
        toolbar: this.toolbar,
        toolbarExcludeKeys: ["ai"],
        draggable: false,
        onChange: (content) => {
          this.$emit('input', content)
          this.$emit('change', content)
        },
        onFocus: () => {
          this.$emit('focus')
        },
        onBlur: () => {
          this.$emit('blur')
        },
        // 禁用 AI 功能
        ai: false,
        // 禁用调整大小功能
        resize: false,
        // 添加唯一标识
        id: `editor-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
      }
 
      try {
        this.editor = new AiEditor(options)
      } catch (error) {
        console.error('Failed to initialize AiEditor:', error)
      }
    },
    destroyEditor() {
      if (this.editor) {
        this.editor.destroy()
        this.editor = null
      }
    },
    // 获取编辑器内容
    getContent() {
      return this.editor ? this.editor.getHtml() : ''
    },
    // 设置编辑器内容
    setContent(content) {
      if (this.editor) {
        this.editor.setContent(content)
      }
    },
    // 清空编辑器内容
    clear() {
      if (this.editor) {
        this.editor.setContent('')
      }
    },
    // 获取编辑器实例
    getEditor() {
      return this.editor
    }
  }
}
</script>
 
<style scoped>
:deep(aie-footer) {
  display: none !important;
}
 
.ai-editor-container {
  width: 100%;
  border-radius: 4px;
  background: #fff;
}
 
#editor {
  width: 100%;
  min-height: 300px;
}
 
:deep(.ai-editor) {
  border: none !important;
}
 
:deep(.ai-editor-toolbar) {
  border-bottom: 1px solid #dcdfe6 !important;
}
 
:deep(.ai-editor-content) {
  border: none !important;
}
 
/* 隐藏右下角拖动图标 */
:deep(.ai-editor-resize) {
  display: none !important;
}
 
/* 隐藏 AI 相关按钮 */
:deep(.ai-editor-ai-button) {
  display: none !important;
}
</style>