1<template>
2 <div ref="editor" class="editor_ref"></div>
3</template>
4
5<script>
6 import { onMounted, onBeforeUnmount, watch, getCurrentInstance, ref } from "vue";
7 import WEditor from "wangeditor";
8
9 export default {
10 props: {
11 defaultText: { type: String, default: "" }
12 },
13 setup(props, context) {
14 const { proxy } = getCurrentInstance();
15 const editor = ref();
16
17 let instance;
18 onMounted(() => {
19 initEditor();
20 });
21
22 onBeforeUnmount(() => {
23 instance.destroy();
24 instance = null;
25 });
26
27 watch(
28 () => props.defaultText,
29 nv => {
30 instance.txt.html(nv);
31 !!nv && context.emit("richHtml", nv);
32 }
33 );
34
35 const initEditor = () => {
36 instance = new WEditor(editor.value);
37 // 配置富文本
38 Object.assign(instance.config, {
39 zIndex: 100,
40 // placeholder: "" /* 提示文字 */,
41 showFullScreen: true /* 显示全屏按钮 */,
42 showLinkImg: true /* 显示插入网络图片 */,
43 showLinkVideo: true /* 显示插入网络视频 */,
44 onchangeTimeout: 400 /* 触发 onchange 的时间频率,默认 200ms */,
45 uploadImgMaxLength: 1 /* 单次上传图片数量限制 */,
46 uploadImgMaxSize: 5 * 1024 * 1024 /* 上传图片大小限制 */,
47 uploadVideoAccept: ["mp4", "mov"] /* 上传视频格式限制 */,
48 uploadVideoMaxSize: 1024 * 1024 * 1024 /* 上传视频大小限制1024m */,
49 excludeMenus: ["strikeThrough", "todo", "code"] /* 移除系统菜单 */,
50 customAlert(msg, type) {
51 type == "success" ? proxy.$message.success(msg) : proxy.$message.error(msg);
52 },
53 customUploadImg(resultFiles, insertImgFn) {
54 /**
55 * @param {Object} file - 文件对象
56 * @param {String} rootPath - 文件根路径(默认为空、例:“filepath/”)
57 * @param {Array} fileType - 文件类型限制(默认 [] 不限制,例:['.png','.jpeg'])
58 * @param {Number} size - 文件大小限制(单位:兆、默认 0 不限制、例:1)
59 **/
60 proxy.$oss(resultFiles[0]).then(imgUrl => !!imgUrl && insertImgFn(imgUrl));
61 },
62
63 customUploadVideo(resultFiles, insertVideoFn) {
64 proxy.$oss(resultFiles[0]).then(videoUrl => !!videoUrl && insertVideoFn(videoUrl)); /* 参数同上 */
65 },
66 onchange(nv) {
67 context.emit("richHtml", nv);
68 }
69 });
70 instance.create();
71 };
72
73 return { editor };
74 }
75 };
76</script>
77
78<style scoped>
79 div.editor_ref :deep(iframe) {
80 max-width: 100%;
81 max-height: auto;
82 width: 360px;
83 height: 180px;
84 }
85</style>