Editor.vue
4.02 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
<script setup lang="ts">
import { onBeforeUnmount, computed, PropType, unref, nextTick, ref, watch, shallowRef, onMounted } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { ElMessage } from 'element-plus'
import { IDomEditor, IEditorConfig, DomEditor } from '@wangeditor/editor'
// const currentLocale = computed(() => localeStore.getCurrentLocale)
// i18nChangeLanguage(unref(currentLocale).lang)
const props = defineProps({
editorId: {
type: String,
default: 'wangeEditor-1'
},
height: {
type: String,
default: '300px'
},
editorConfig: {
type: Object as PropType<IEditorConfig>,
default: () => undefined
},
modelValue: {
type: String,
default: ''
}
})
const emit = defineEmits(['change', 'updateValue'])
// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef<IDomEditor>()
const valueHtml = ref('')
watch(
() => props.modelValue,
(val: string) => {
if (val === unref(valueHtml)) return
valueHtml.value = val
}
)
// 监听
watch(
() => valueHtml.value,
(val: string) => {
emit('updateValue', val)
}
)
const handleCreated = (editor: IDomEditor) => {
editorRef.value = editor;
editorRef.value.on('fullScreen', () => {
const fullscreenBtn = <HTMLElement>document.querySelector('[data-menu-key="fullScreen"]');
if (fullscreenBtn) {
fullscreenBtn.setAttribute('data-tooltip', '退出全屏');
}
})
editorRef.value.on('unFullScreen', () => {
const fullscreenBtn = <HTMLElement>document.querySelector('[data-menu-key="fullScreen"]');
if (fullscreenBtn) {
fullscreenBtn.setAttribute('data-tooltip', '全屏');
}
});
valueHtml.value = props.modelValue
}
const toolbarConfig = ref({
excludeKeys: ['group-video', 'emotion', 'insertImage', 'editImage','viewImageLink', 'deleteImage', ]
});
// 编辑器配置
const editorConfig = computed((): IEditorConfig => {
return Object.assign(
{
readOnly: false,
customAlert: (s: string, t: string) => {
switch (t) {
case 'success':
ElMessage.success(s)
break
case 'info':
ElMessage.info(s)
break
case 'warning':
ElMessage.warning(s)
break
case 'error':
ElMessage.error(s)
break
default:
ElMessage.info(s)
break
}
},
autoFocus: false,
scroll: true,
MENU_CONF: {
uploadImage: {
base64LimitSize: 20 * 1024 * 1024 ,
uploadImgShowBase64: true,
showBase64: true, // 设置为true表示上传图片将以Base64形式显示
},
fullScreen: {
onClick: () => {
//点击全屏
console.log(';dianji');
},
}
},
onFullscreen: (isFullscreen) => {
if (isFullscreen) {
console.log('去');
} else {
console.log('去');
}
},
uploadImgShowBase64: true,
},
props.editorConfig || {}
)
})
const editorStyle = computed(() => {
return {
height: '300px'
}
})
// 回调函数
const handleChange = (editor: IDomEditor) => {
emit('change', editor)
}
// 组件销毁时,及时销毁编辑器
onBeforeUnmount(() => {
const editor = unref(editorRef.value)
// 销毁,并移除 editor
editor?.destroy()
})
const getEditorRef = async (): Promise<IDomEditor> => {
await nextTick()
return unref(editorRef.value) as IDomEditor
}
defineExpose({
getEditorRef
})
</script>
<template>
<div class="border" id="wangeEditor-1" fullscreen>
<!-- 工具栏 -->
<Toolbar
:editor="editorRef"
:editorId="editorId"
:defaultConfig="toolbarConfig"
class="border-bottom"
/>
<!-- 编辑器 -->
<Editor
v-model="valueHtml"
:editorId="editorId"
:defaultConfig="editorConfig"
:style="editorStyle"
@on-change="handleChange"
@on-created="handleCreated"
/>
</div>
</template>
<style src="@wangeditor/editor/dist/css/style.css">
</style>