-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathMyEditorWithMention.vue
96 lines (91 loc) · 3.06 KB
/
MyEditorWithMention.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
<template>
<div>
<p>wangEditor mention demo</p>
<div style="border: 1px solid #ccc;">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editor"
:defaultConfig="toolbarConfig"
/>
<Editor
style="height: 400px"
v-model="html"
:defaultConfig="editorConfig"
@onChange="onChange"
@onCreated="onCreated"
/>
<mention-modal
v-if="isShowModal"
@hideMentionModal="hideMentionModal"
@insertMention="insertMention"
></mention-modal>
</div>
<div style="margin-top: 10px;">
<textarea v-model="html" style="width: 100%; height: 500px;"></textarea>
</div>
</div>
</template>
<script>
import { Boot } from '@wangeditor/editor'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import mentionModule from '@wangeditor/plugin-mention'
import MentionModal from './MentionModal'
// 注册插件
Boot.registerModule(mentionModule)
export default {
name: 'MyEditorWithMention',
components: { Editor, Toolbar, MentionModal },
data() {
return {
editor: null,
html: '<p>你好<span data-w-e-type="mention" data-w-e-is-void data-w-e-is-inline data-value="A张三" data-info="%7B%22id%22%3A%22a%22%7D">@A张三</span></p>',
toolbarConfig: {},
editorConfig: {
placeholder: '请输入内容...',
EXTEND_CONF: {
mentionConfig: {
showModal: this.showMentionModal,
hideModal: this.hideMentionModal,
},
},
},
isShowModal: false
}
},
methods: {
onCreated(editor) {
this.editor = Object.seal(editor) // 【注意】一定要用 Object.seal() 否则会报错
},
onChange(editor) {
this.curHtml = editor.getHtml()
},
showMentionModal() {
this.isShowModal = true
},
hideMentionModal() {
this.isShowModal = false
},
insertMention(id, name) {
const mentionNode = {
type: 'mention', // 必须是 'mention'
value: name,
info: { id },
children: [{ text: '' }], // 必须有一个空 text 作为 children
}
const editor = this.editor
if (editor) {
editor.restoreSelection() // 恢复选区
editor.deleteBackward('character') // 删除 '@'
editor.insertNode(mentionNode) // 插入 mention
editor.move(1) // 移动光标
}
}
},
beforeDestroy() {
const editor = this.editor
if (editor == null) return
editor.destroy() // 组件销毁时,及时销毁 editor ,重要!!!
},
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>