一、新建js文件
该js文件表示新增的菜单内容,例newMenu.js
class NewMenu {
constructor() {
this.title = "插入新菜单";
this.iconSvg = '<svg><path d="..."></path></svg>';
this.tag = "button";
}
isActive(editor) {
return false;
}
getValue(editor) {
return "11111";
}
isDisabled(editor) {
return false;
}
exec(editor, value) {
if (this.isDisabled(editor)) return;
editor.insertText(value)
}
}
const menuConfig = {
key: "insertNewMenu",
factory() {
return new NewMenu();
},
};
export default menuConfig;
二、wangEditor富文本vue文件引用该js文件
将该新菜单注册到wangEditor里并加入到toolbar里,例RichTextEdit.vue
<template>
<div ref="editor">
<Toolbar
class="toolbar-container"
:editor="editor"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
<Editor
:style="{ height: height }"
class="editor-container"
v-model="html"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="onCreated"
@onChange="onChange"
@onDestroyed="onDestroyed"
/>
</div>
</template>
<script>
import { EventBus } from '@/assets/utils/eventBus';
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { Boot, DomEditor } from "@wangeditor/editor";
import newMenu from "@/components/RichTextEdit/newMenu";
Boot.registerMenu(newMenu);
export default {
name: "EditorComponent",
props: {
initHtml: {
type: String,
default: "",
},
placeholder: {
type: String,
default: "请输入内容",
},
editorHeight: {
type: String,
default: "110px",
},
mode: {
type: String,
default: "simple",
},
},
components: { Editor, Toolbar },
data() {
return {
editor: null,
toolbarConfig: {},
editorConfig: {
autoFocus: false,
placeholder: this.placeholder,
readOnly: false,
MENU_CONF: {
uploadImage: {
maxFileSize: 2 * 1024 * 1024,
maxNumberOfFiles: 4,
allowedFileTypes: [
"image/jpeg",
"image/jpg",
"image/png",
"image/svg",
"image/gif",
"image/webp",
],
timeout: 6 * 1000,
customUpload: (file, insertFn) => {
this.uploadImage(file, insertFn);
},
},
uploadVideo: {
maxNumberOfFiles: 1,
maxFileSize: 100 * 1024 * 1024,
allowedFileTypes: ["video/mp4", "video/3gp", "video/m3u8"],
timeout: 6 * 1000,
customUpload: (file, insertFn) => {
this.uploadVideo(file, insertFn);
},
},
},
},
simpleExcludeConfig: [
"blockquote",
"header3",
"underline",
"italic",
"through",
"color",
"bgColor",
"clearStyle",
"todo",
"justifyLeft",
"justifyRight",
"justifyCenter",
"insertLink",
"group-image",
"insertVideo",
"codeBlock",
],
simpleInsertConfig: {
index: 20,
keys: [
"newMenu",
],
},
html: "",
};
},
created() {
this.toolbarConfig.insertKeys = this.simpleInsertConfig;
this.toolbarConfig.excludeKeys = this.simpleExcludeConfig;
this.html = this.initHtml;
},
watch: {
initHtml() {
this.html = this.initHtml;
},
},
computed: {
height() {
return parseInt(this.editorHeight) < 110 ? "110px" : this.editorHeight;
},
},
mounted() {},
methods: {
onCreated(editor) {
this.editor = Object.seal(editor);
},
onChange(editor) {},
onDestroyed(editor) {
console.log("onDestroyed", editor);
},
uploadImage(file, insertFn) {...},
uploadVideo(file, insertFn) {...},
},
beforeDestroy() {
const editor = this.editor;
if (editor == null) return;
editor.destroy();
},
};
</script>
<style scoped lang="scss">
@import "@wangeditor/editor/dist/css/style.css";
</style>
三、结果显示
