index.vue
5.28 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<template>
<div class="editor-ctnr">
<!-- <div class="extend-bar" v-if="operateList && operateList.length">
<el-button v-for="item in operateList" :key="item.key" class="el-icon-circle-plus extend-icon" @click="insertNodeFn(item)">{{ item.name }}</el-button>
</div> -->
<label v-if="label" for="" :class="['content-label', { 'required-field': required }]">{{ label }}</label>
<div>
<div class="editor" style="border: 1px solid #ccc;">
<Toolbar v-if="hasTool" style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" />
<Editor style="min-height: 52px; overflow-y: hidden;" @onChange="onChange" @customPaste="customPaste" v-model="value" :defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" />
</div>
<div class="extend-bar" v-if="operateList && operateList.length">
<el-button v-for="item in operateList" :key="item.key" class="el-icon-circle-plus extend-icon" @click="insertNodeFn(item)">{{ item.name }}</el-button>
</div>
</div>
</div>
</template>
<script>
import Vue from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { Boot, DomEditor } from '@wangeditor/editor'
import { withBreakAndDelete } from './withimage'
Boot.registerPlugin(withBreakAndDelete)
import { attachmentToHtml } from './attachmentToHtml'
const elemToHtmlConf = {
type: 'text', // 新元素的 type ,重要!!!
elemToHtml: attachmentToHtml
}
Boot.registerElemToHtml(elemToHtmlConf)
import { renderAttachment } from './renderAttachment'
const renderElemConf = {
type: 'text', // 新元素 type ,重要!!!
renderElem: renderAttachment
}
Boot.registerRenderElem(renderElemConf)
import { parseAttachmentHtml } from './parseAttachmentHtml'
const parseHtmlConf = {
selector: 'span[data-w-e-type="text"]', // CSS 选择器,匹配特定的 HTML 标签
parseElemHtml: parseAttachmentHtml
}
Boot.registerParseElemHtml(parseHtmlConf)
export default Vue.extend({
components: { Editor, Toolbar },
model: {
prop: 'value',
event: 'change'
},
props: {
required: {
type: Boolean,
default: false
},
hasTool: {
type: Boolean,
default: true
},
value: {
type: Object
},
label: {
type: String,
default: ''
},
operateList: {
type: Array,
default: () => {
return []
}
},
disabled: {
type: Boolean,
default: false
}
},
data() {
return {
editor: null,
html: '',
toolbarConfig: {
toolbarKeys: ['fontSize', 'lineHeight', 'bold', 'justifyLeft', 'justifyRight', 'justifyCenter', 'underline', 'through', 'color', 'bgColor', 'insertTable', 'undo', 'redo', 'clearStyle']
// toolbarKeys: ['fontSize', 'lineHeight', "justifyLeft","justifyRight", "justifyCenter","justifyJustify", 'underline', 'through', 'color', 'bgColor', 'insertTable', 'undo', 'redo', 'clearStyle']
},
editorConfig: {
placeholder: '请输入内容...'
},
mode: 'simple' // 设置simple,选中文字不会出现hoverBar
}
},
methods: {
onChange(editer) {
this.$emit('change', this.editor.getHtml())
},
// 复制粘贴内容时,自动清除格式
customPaste(editor, event) {
editor.insertText(event.clipboardData.getData('text/plain'))
event.preventDefault()
return false
},
onCreated(editor) {
this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
},
insertNodeFn(item) {
// event.preventDefault()
const resume = {
// JS 语法
type: 'text',
name: item.name,
key: item.key,
children: [{ text: '' }]
}
this.editor.insertNode(resume)
setTimeout(() => {
this.editor.focus()
this.editor.move(1)
}, 300)
}
},
mounted() {},
beforeDestroy() {
const editor = this.editor
if (editor == null) return
editor.destroy() // 组件销毁时,及时销毁编辑器
}
})
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style scoped lang="scss">
.editor-ctnr {
& > div {
width: 100%;
}
width: 100%;
height: 100%;
display: flex;
.editor {
background: #fff;
// /deep/ .w-e-text-container {
// .w-e-hover-bar {
// display: none;
// }
// }
}
.content-label {
flex-shrink: 0;
width: 154px;
display: inline-block;
vertical-align: top;
height: 20px;
font-weight: 400;
font-size: 14px;
color: #333333;
text-align: right;
line-height: 20px;
}
/deep/ .w-e-text-container {
padding: 10px 5px;
p {
margin: 0;
}
.w-e-text-placeholder {
top: 5px;
left: 14px;
height: 32px;
line-height: 32px;
}
}
}
.extend-bar {
width: 937px;
padding: 0 20px;
line-height: 40px;
background: #fafbfe;
border: 1px solid #ccc;
box-sizing: border-box;
border-radius: 0 0 4px 4px;
border-top: none;
/deep/ .el-button {
border: none;
padding: 0;
margin: 0 16px;
background-color: transparent;
&:hover {
background-color: unset;
}
}
.extend-icon {
cursor: pointer;
&::before {
// font-size: 16px;
margin-right: 4px;
}
font-size: 14px;
color: #3f3b3b;
}
}
</style>