index.vue
3.26 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
<template>
<div class="page" style="border: 1px solid #ccc;">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editor"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
<Editor
style="height: 500px; overflow-y: hidden;"
v-model="html"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="onCreated"
@customPaste="customPaste"
/>
</div>
</template><script>
import Vue from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
export default Vue.extend({
props: ["value", "subPath"],
components: { Editor, Toolbar },
data() {
return {
editor: null,
html: "<p><br></p>",
toolbarConfig: {
excludeKeys: [
"insertLink",
"todo",
"fullScreen",
"fontFamily",
"lineHeight",
"insertTable",
"codeBlock"
]
},
editorConfig: {
placeholder: "请输入内容...",
// autoFocus: false,
// 所有的菜单配置,都要在 MENU_CONF 属性下
MENU_CONF: {
uploadImage: {
fieldName: "file",
server:
"https://api.k.wxpai.cn/bizproxy/tianbaoServiceApi/" +
app.api.upload,
meta: {
subPath: this.subPath
},
customInsert(res, insertFn) {
console.log(res);
// res 即服务端的返回结果
// 从 res 中找到 url alt href ,然后插图图片
insertFn(res.content, "", "");
}
},
uploadVideo: {
fieldName: "file",
server:
"https://api.k.wxpai.cn/bizproxy/tianbaoServiceApi/" +
app.api.upload,
meta: {
subPath: this.subPath
},
customInsert(res, insertFn) {
console.log(res);
// res 即服务端的返回结果
// 从 res 中找到 url alt href ,然后插图图片
insertFn(res.content, "", "");
}
}
}
},
mode: "default" // or 'simple'
};
},
watch: {
html(newVal, oldVal) {
this.$emit("input", newVal);
}
},
methods: {
onCreated(editor) {
this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否则会报错
editor.on("modalOrPanelShow", modalOrPanel => {});
this.editor.on("modalOrPanelHide", () => {
// 隐藏蒙层
});
},
onOk() {
this.editor.restoreSelection();
SlateTransforms.setNodes(
this.editor,
{ alt: this.alt, href: this.href },
{ match: n => SlateElement.isElement(n) }
);
},
onChange(editor) {
console.log("onChange", editor.getHtml()); // onChange 时获取编辑器最新内容
this.html = this.pasteTextHandle("aa");
},
customPaste(editor, event) {
},
},
mounted() {
setTimeout(() => {
this.html = this.value;
}, 500);
},
beforeDestroy() {
const editor = this.editor;
if (editor == null) return;
editor.destroy(); // 组件销毁时,及时销毁编辑器
}
});
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="scss" scoped>
</style>