2021-10-17 12:32:28 +08:00
|
|
|
|
<template>
|
|
|
|
|
<div class="editor-container">
|
2022-11-16 15:34:23 +08:00
|
|
|
|
<Toolbar :editor="editorRef" :mode="mode" />
|
|
|
|
|
<Editor :mode="mode" :defaultConfig="editorConfig" :style="{ height }" v-model="editorVal" @onCreated="handleCreated" @onChange="handleChange" />
|
2021-10-17 12:32:28 +08:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-11-16 15:34:23 +08:00
|
|
|
|
// https://www.wangeditor.com/v5/for-frame.html#vue3
|
2022-04-28 19:39:44 +08:00
|
|
|
|
import '@wangeditor/editor/dist/css/style.css';
|
2022-11-16 15:34:23 +08:00
|
|
|
|
import { defineComponent, reactive, toRefs, shallowRef, watch, onBeforeUnmount } from 'vue';
|
|
|
|
|
import { Toolbar, Editor } from '@wangeditor/editor-for-vue';
|
2022-02-21 23:52:59 +08:00
|
|
|
|
|
|
|
|
|
export default defineComponent({
|
2021-10-17 12:32:28 +08:00
|
|
|
|
name: 'wngEditor',
|
2022-11-16 15:34:23 +08:00
|
|
|
|
components: { Toolbar, Editor },
|
2021-10-17 12:32:28 +08:00
|
|
|
|
props: {
|
|
|
|
|
// 是否禁用
|
2022-11-16 15:34:23 +08:00
|
|
|
|
disable: {
|
2021-10-17 12:32:28 +08:00
|
|
|
|
type: Boolean,
|
2022-11-16 15:34:23 +08:00
|
|
|
|
default: () => true,
|
2021-10-17 12:32:28 +08:00
|
|
|
|
},
|
2021-12-12 21:27:33 +08:00
|
|
|
|
// 内容框默认 placeholder
|
|
|
|
|
placeholder: {
|
|
|
|
|
type: String,
|
2022-11-16 15:34:23 +08:00
|
|
|
|
default: () => '请输入内容...',
|
2021-12-12 21:27:33 +08:00
|
|
|
|
},
|
2022-04-28 19:39:44 +08:00
|
|
|
|
// 双向绑定:双向绑定值,字段名为固定,改了之后将不生效
|
2021-10-17 12:32:28 +08:00
|
|
|
|
// 参考:https://v3.cn.vuejs.org/guide/migration/v-model.html#%E8%BF%81%E7%A7%BB%E7%AD%96%E7%95%A5
|
|
|
|
|
modelValue: String,
|
2022-04-28 19:39:44 +08:00
|
|
|
|
// https://www.wangeditor.com/v5/getting-started.html#mode-%E6%A8%A1%E5%BC%8F
|
|
|
|
|
// 模式,可选 <default|simple>,默认 default
|
|
|
|
|
mode: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: () => 'default',
|
|
|
|
|
},
|
|
|
|
|
// 高度
|
|
|
|
|
height: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: () => '310px',
|
|
|
|
|
},
|
2021-10-17 12:32:28 +08:00
|
|
|
|
},
|
|
|
|
|
setup(props, { emit }) {
|
2022-11-16 15:34:23 +08:00
|
|
|
|
const editorRef = shallowRef();
|
|
|
|
|
const state = reactive({
|
|
|
|
|
editorConfig: {
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
},
|
|
|
|
|
editorVal: props.modelValue,
|
2021-10-17 12:32:28 +08:00
|
|
|
|
});
|
2022-11-16 15:34:23 +08:00
|
|
|
|
// 编辑器回调函数
|
|
|
|
|
const handleCreated = (editor: any) => {
|
|
|
|
|
editorRef.value = editor;
|
2022-04-28 19:39:44 +08:00
|
|
|
|
};
|
2022-11-16 15:34:23 +08:00
|
|
|
|
// 编辑器内容改变时
|
|
|
|
|
const handleChange = (editor: any) => {
|
|
|
|
|
// console.log(editor.getText());
|
|
|
|
|
// console.log(editor.getHtml());
|
|
|
|
|
emit('update:modelValue', editor.getHtml());
|
2022-04-28 19:39:44 +08:00
|
|
|
|
};
|
2022-11-16 15:34:23 +08:00
|
|
|
|
// 监听是否禁用改变
|
2021-12-12 21:27:33 +08:00
|
|
|
|
// https://gitee.com/lyt-top/vue-next-admin/issues/I4LM7I
|
|
|
|
|
watch(
|
2022-11-16 15:34:23 +08:00
|
|
|
|
() => props.disable,
|
|
|
|
|
(bool) => {
|
|
|
|
|
const editor = editorRef.value;
|
|
|
|
|
if (editor == null) return;
|
|
|
|
|
bool ? editor.disable() : editor.enable();
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
deep: true,
|
2021-12-12 21:27:33 +08:00
|
|
|
|
}
|
|
|
|
|
);
|
2022-11-16 15:34:23 +08:00
|
|
|
|
// 页面销毁时
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
|
const editor = editorRef.value;
|
|
|
|
|
if (editor == null) return;
|
|
|
|
|
editor.destroy();
|
|
|
|
|
});
|
2021-10-17 12:32:28 +08:00
|
|
|
|
return {
|
2022-11-16 15:34:23 +08:00
|
|
|
|
editorRef,
|
|
|
|
|
handleCreated,
|
|
|
|
|
handleChange,
|
2021-10-17 12:32:28 +08:00
|
|
|
|
...toRefs(state),
|
|
|
|
|
};
|
|
|
|
|
},
|
2022-02-21 23:52:59 +08:00
|
|
|
|
});
|
2021-10-17 12:32:28 +08:00
|
|
|
|
</script>
|